{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.IoTSiteWise.Types.Metric
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.IoTSiteWise.Types.Metric where

import qualified Amazonka.Core as Core
import Amazonka.IoTSiteWise.Types.ExpressionVariable
import Amazonka.IoTSiteWise.Types.MetricProcessingConfig
import Amazonka.IoTSiteWise.Types.MetricWindow
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Contains an asset metric property. With metrics, you can calculate
-- aggregate functions, such as an average, maximum, or minimum, as
-- specified through an expression. A metric maps several values to a
-- single value (such as a sum).
--
-- The maximum number of dependent\/cascading variables used in any one
-- metric calculation is 10. Therefore, a /root/ metric can have up to 10
-- cascading metrics in its computational dependency tree. Additionally, a
-- metric can only have a data type of @DOUBLE@ and consume properties with
-- data types of @INTEGER@ or @DOUBLE@.
--
-- For more information, see
-- <https://docs.aws.amazon.com/iot-sitewise/latest/userguide/asset-properties.html#metrics Metrics>
-- in the /IoT SiteWise User Guide/.
--
-- /See:/ 'newMetric' smart constructor.
data Metric = Metric'
  { -- | The processing configuration for the given metric property. You can
    -- configure metrics to be computed at the edge or in the Amazon Web
    -- Services Cloud. By default, metrics are forwarded to the cloud.
    Metric -> Maybe MetricProcessingConfig
processingConfig :: Prelude.Maybe MetricProcessingConfig,
    -- | The mathematical expression that defines the metric aggregation
    -- function. You can specify up to 10 variables per expression. You can
    -- specify up to 10 functions per expression.
    --
    -- For more information, see
    -- <https://docs.aws.amazon.com/iot-sitewise/latest/userguide/quotas.html Quotas>
    -- in the /IoT SiteWise User Guide/.
    Metric -> Text
expression :: Prelude.Text,
    -- | The list of variables used in the expression.
    Metric -> [ExpressionVariable]
variables :: [ExpressionVariable],
    -- | The window (time interval) over which IoT SiteWise computes the
    -- metric\'s aggregation expression. IoT SiteWise computes one data point
    -- per @window@.
    Metric -> MetricWindow
window :: MetricWindow
  }
  deriving (Metric -> Metric -> Bool
(Metric -> Metric -> Bool)
-> (Metric -> Metric -> Bool) -> Eq Metric
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Metric -> Metric -> Bool
$c/= :: Metric -> Metric -> Bool
== :: Metric -> Metric -> Bool
$c== :: Metric -> Metric -> Bool
Prelude.Eq, ReadPrec [Metric]
ReadPrec Metric
Int -> ReadS Metric
ReadS [Metric]
(Int -> ReadS Metric)
-> ReadS [Metric]
-> ReadPrec Metric
-> ReadPrec [Metric]
-> Read Metric
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Metric]
$creadListPrec :: ReadPrec [Metric]
readPrec :: ReadPrec Metric
$creadPrec :: ReadPrec Metric
readList :: ReadS [Metric]
$creadList :: ReadS [Metric]
readsPrec :: Int -> ReadS Metric
$creadsPrec :: Int -> ReadS Metric
Prelude.Read, Int -> Metric -> ShowS
[Metric] -> ShowS
Metric -> String
(Int -> Metric -> ShowS)
-> (Metric -> String) -> ([Metric] -> ShowS) -> Show Metric
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Metric] -> ShowS
$cshowList :: [Metric] -> ShowS
show :: Metric -> String
$cshow :: Metric -> String
showsPrec :: Int -> Metric -> ShowS
$cshowsPrec :: Int -> Metric -> ShowS
Prelude.Show, (forall x. Metric -> Rep Metric x)
-> (forall x. Rep Metric x -> Metric) -> Generic Metric
forall x. Rep Metric x -> Metric
forall x. Metric -> Rep Metric x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Metric x -> Metric
$cfrom :: forall x. Metric -> Rep Metric x
Prelude.Generic)

-- |
-- Create a value of 'Metric' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'processingConfig', 'metric_processingConfig' - The processing configuration for the given metric property. You can
-- configure metrics to be computed at the edge or in the Amazon Web
-- Services Cloud. By default, metrics are forwarded to the cloud.
--
-- 'expression', 'metric_expression' - The mathematical expression that defines the metric aggregation
-- function. You can specify up to 10 variables per expression. You can
-- specify up to 10 functions per expression.
--
-- For more information, see
-- <https://docs.aws.amazon.com/iot-sitewise/latest/userguide/quotas.html Quotas>
-- in the /IoT SiteWise User Guide/.
--
-- 'variables', 'metric_variables' - The list of variables used in the expression.
--
-- 'window', 'metric_window' - The window (time interval) over which IoT SiteWise computes the
-- metric\'s aggregation expression. IoT SiteWise computes one data point
-- per @window@.
newMetric ::
  -- | 'expression'
  Prelude.Text ->
  -- | 'window'
  MetricWindow ->
  Metric
newMetric :: Text -> MetricWindow -> Metric
newMetric Text
pExpression_ MetricWindow
pWindow_ =
  Metric' :: Maybe MetricProcessingConfig
-> Text -> [ExpressionVariable] -> MetricWindow -> Metric
Metric'
    { $sel:processingConfig:Metric' :: Maybe MetricProcessingConfig
processingConfig = Maybe MetricProcessingConfig
forall a. Maybe a
Prelude.Nothing,
      $sel:expression:Metric' :: Text
expression = Text
pExpression_,
      $sel:variables:Metric' :: [ExpressionVariable]
variables = [ExpressionVariable]
forall a. Monoid a => a
Prelude.mempty,
      $sel:window:Metric' :: MetricWindow
window = MetricWindow
pWindow_
    }

-- | The processing configuration for the given metric property. You can
-- configure metrics to be computed at the edge or in the Amazon Web
-- Services Cloud. By default, metrics are forwarded to the cloud.
metric_processingConfig :: Lens.Lens' Metric (Prelude.Maybe MetricProcessingConfig)
metric_processingConfig :: (Maybe MetricProcessingConfig -> f (Maybe MetricProcessingConfig))
-> Metric -> f Metric
metric_processingConfig = (Metric -> Maybe MetricProcessingConfig)
-> (Metric -> Maybe MetricProcessingConfig -> Metric)
-> Lens
     Metric
     Metric
     (Maybe MetricProcessingConfig)
     (Maybe MetricProcessingConfig)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {Maybe MetricProcessingConfig
processingConfig :: Maybe MetricProcessingConfig
$sel:processingConfig:Metric' :: Metric -> Maybe MetricProcessingConfig
processingConfig} -> Maybe MetricProcessingConfig
processingConfig) (\s :: Metric
s@Metric' {} Maybe MetricProcessingConfig
a -> Metric
s {$sel:processingConfig:Metric' :: Maybe MetricProcessingConfig
processingConfig = Maybe MetricProcessingConfig
a} :: Metric)

-- | The mathematical expression that defines the metric aggregation
-- function. You can specify up to 10 variables per expression. You can
-- specify up to 10 functions per expression.
--
-- For more information, see
-- <https://docs.aws.amazon.com/iot-sitewise/latest/userguide/quotas.html Quotas>
-- in the /IoT SiteWise User Guide/.
metric_expression :: Lens.Lens' Metric Prelude.Text
metric_expression :: (Text -> f Text) -> Metric -> f Metric
metric_expression = (Metric -> Text)
-> (Metric -> Text -> Metric) -> Lens Metric Metric Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {Text
expression :: Text
$sel:expression:Metric' :: Metric -> Text
expression} -> Text
expression) (\s :: Metric
s@Metric' {} Text
a -> Metric
s {$sel:expression:Metric' :: Text
expression = Text
a} :: Metric)

-- | The list of variables used in the expression.
metric_variables :: Lens.Lens' Metric [ExpressionVariable]
metric_variables :: ([ExpressionVariable] -> f [ExpressionVariable])
-> Metric -> f Metric
metric_variables = (Metric -> [ExpressionVariable])
-> (Metric -> [ExpressionVariable] -> Metric)
-> Lens Metric Metric [ExpressionVariable] [ExpressionVariable]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {[ExpressionVariable]
variables :: [ExpressionVariable]
$sel:variables:Metric' :: Metric -> [ExpressionVariable]
variables} -> [ExpressionVariable]
variables) (\s :: Metric
s@Metric' {} [ExpressionVariable]
a -> Metric
s {$sel:variables:Metric' :: [ExpressionVariable]
variables = [ExpressionVariable]
a} :: Metric) (([ExpressionVariable] -> f [ExpressionVariable])
 -> Metric -> f Metric)
-> (([ExpressionVariable] -> f [ExpressionVariable])
    -> [ExpressionVariable] -> f [ExpressionVariable])
-> ([ExpressionVariable] -> f [ExpressionVariable])
-> Metric
-> f Metric
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([ExpressionVariable] -> f [ExpressionVariable])
-> [ExpressionVariable] -> f [ExpressionVariable]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The window (time interval) over which IoT SiteWise computes the
-- metric\'s aggregation expression. IoT SiteWise computes one data point
-- per @window@.
metric_window :: Lens.Lens' Metric MetricWindow
metric_window :: (MetricWindow -> f MetricWindow) -> Metric -> f Metric
metric_window = (Metric -> MetricWindow)
-> (Metric -> MetricWindow -> Metric)
-> Lens Metric Metric MetricWindow MetricWindow
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {MetricWindow
window :: MetricWindow
$sel:window:Metric' :: Metric -> MetricWindow
window} -> MetricWindow
window) (\s :: Metric
s@Metric' {} MetricWindow
a -> Metric
s {$sel:window:Metric' :: MetricWindow
window = MetricWindow
a} :: Metric)

instance Core.FromJSON Metric where
  parseJSON :: Value -> Parser Metric
parseJSON =
    String -> (Object -> Parser Metric) -> Value -> Parser Metric
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"Metric"
      ( \Object
x ->
          Maybe MetricProcessingConfig
-> Text -> [ExpressionVariable] -> MetricWindow -> Metric
Metric'
            (Maybe MetricProcessingConfig
 -> Text -> [ExpressionVariable] -> MetricWindow -> Metric)
-> Parser (Maybe MetricProcessingConfig)
-> Parser (Text -> [ExpressionVariable] -> MetricWindow -> Metric)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe MetricProcessingConfig)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"processingConfig")
            Parser (Text -> [ExpressionVariable] -> MetricWindow -> Metric)
-> Parser Text
-> Parser ([ExpressionVariable] -> MetricWindow -> Metric)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"expression")
            Parser ([ExpressionVariable] -> MetricWindow -> Metric)
-> Parser [ExpressionVariable] -> Parser (MetricWindow -> Metric)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe [ExpressionVariable])
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"variables" Parser (Maybe [ExpressionVariable])
-> [ExpressionVariable] -> Parser [ExpressionVariable]
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= [ExpressionVariable]
forall a. Monoid a => a
Prelude.mempty)
            Parser (MetricWindow -> Metric)
-> Parser MetricWindow -> Parser Metric
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser MetricWindow
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"window")
      )

instance Prelude.Hashable Metric

instance Prelude.NFData Metric

instance Core.ToJSON Metric where
  toJSON :: Metric -> Value
toJSON Metric' {[ExpressionVariable]
Maybe MetricProcessingConfig
Text
MetricWindow
window :: MetricWindow
variables :: [ExpressionVariable]
expression :: Text
processingConfig :: Maybe MetricProcessingConfig
$sel:window:Metric' :: Metric -> MetricWindow
$sel:variables:Metric' :: Metric -> [ExpressionVariable]
$sel:expression:Metric' :: Metric -> Text
$sel:processingConfig:Metric' :: Metric -> Maybe MetricProcessingConfig
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"processingConfig" Text -> MetricProcessingConfig -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (MetricProcessingConfig -> Pair)
-> Maybe MetricProcessingConfig -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe MetricProcessingConfig
processingConfig,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"expression" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
expression),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"variables" Text -> [ExpressionVariable] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= [ExpressionVariable]
variables),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"window" Text -> MetricWindow -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= MetricWindow
window)
          ]
      )