{-# 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.LookoutMetrics.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.LookoutMetrics.Types.Metric where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import Amazonka.LookoutMetrics.Types.AggregationFunction
import qualified Amazonka.Prelude as Prelude

-- | A calculation made by contrasting a measure and a dimension from your
-- source data.
--
-- /See:/ 'newMetric' smart constructor.
data Metric = Metric'
  { -- | The namespace for the metric.
    Metric -> Maybe Text
namespace :: Prelude.Maybe Prelude.Text,
    -- | The name of the metric.
    Metric -> Text
metricName :: Prelude.Text,
    -- | The function with which the metric is calculated.
    Metric -> AggregationFunction
aggregationFunction :: AggregationFunction
  }
  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:
--
-- 'namespace', 'metric_namespace' - The namespace for the metric.
--
-- 'metricName', 'metric_metricName' - The name of the metric.
--
-- 'aggregationFunction', 'metric_aggregationFunction' - The function with which the metric is calculated.
newMetric ::
  -- | 'metricName'
  Prelude.Text ->
  -- | 'aggregationFunction'
  AggregationFunction ->
  Metric
newMetric :: Text -> AggregationFunction -> Metric
newMetric Text
pMetricName_ AggregationFunction
pAggregationFunction_ =
  Metric' :: Maybe Text -> Text -> AggregationFunction -> Metric
Metric'
    { $sel:namespace:Metric' :: Maybe Text
namespace = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:metricName:Metric' :: Text
metricName = Text
pMetricName_,
      $sel:aggregationFunction:Metric' :: AggregationFunction
aggregationFunction = AggregationFunction
pAggregationFunction_
    }

-- | The namespace for the metric.
metric_namespace :: Lens.Lens' Metric (Prelude.Maybe Prelude.Text)
metric_namespace :: (Maybe Text -> f (Maybe Text)) -> Metric -> f Metric
metric_namespace = (Metric -> Maybe Text)
-> (Metric -> Maybe Text -> Metric)
-> Lens Metric Metric (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {Maybe Text
namespace :: Maybe Text
$sel:namespace:Metric' :: Metric -> Maybe Text
namespace} -> Maybe Text
namespace) (\s :: Metric
s@Metric' {} Maybe Text
a -> Metric
s {$sel:namespace:Metric' :: Maybe Text
namespace = Maybe Text
a} :: Metric)

-- | The name of the metric.
metric_metricName :: Lens.Lens' Metric Prelude.Text
metric_metricName :: (Text -> f Text) -> Metric -> f Metric
metric_metricName = (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
metricName :: Text
$sel:metricName:Metric' :: Metric -> Text
metricName} -> Text
metricName) (\s :: Metric
s@Metric' {} Text
a -> Metric
s {$sel:metricName:Metric' :: Text
metricName = Text
a} :: Metric)

-- | The function with which the metric is calculated.
metric_aggregationFunction :: Lens.Lens' Metric AggregationFunction
metric_aggregationFunction :: (AggregationFunction -> f AggregationFunction)
-> Metric -> f Metric
metric_aggregationFunction = (Metric -> AggregationFunction)
-> (Metric -> AggregationFunction -> Metric)
-> Lens Metric Metric AggregationFunction AggregationFunction
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Metric' {AggregationFunction
aggregationFunction :: AggregationFunction
$sel:aggregationFunction:Metric' :: Metric -> AggregationFunction
aggregationFunction} -> AggregationFunction
aggregationFunction) (\s :: Metric
s@Metric' {} AggregationFunction
a -> Metric
s {$sel:aggregationFunction:Metric' :: AggregationFunction
aggregationFunction = AggregationFunction
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 Text -> Text -> AggregationFunction -> Metric
Metric'
            (Maybe Text -> Text -> AggregationFunction -> Metric)
-> Parser (Maybe Text)
-> Parser (Text -> AggregationFunction -> Metric)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Namespace")
            Parser (Text -> AggregationFunction -> Metric)
-> Parser Text -> Parser (AggregationFunction -> 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
"MetricName")
            Parser (AggregationFunction -> Metric)
-> Parser AggregationFunction -> Parser Metric
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser AggregationFunction
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"AggregationFunction")
      )

instance Prelude.Hashable Metric

instance Prelude.NFData Metric

instance Core.ToJSON Metric where
  toJSON :: Metric -> Value
toJSON Metric' {Maybe Text
Text
AggregationFunction
aggregationFunction :: AggregationFunction
metricName :: Text
namespace :: Maybe Text
$sel:aggregationFunction:Metric' :: Metric -> AggregationFunction
$sel:metricName:Metric' :: Metric -> Text
$sel:namespace:Metric' :: Metric -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"Namespace" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
namespace,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"MetricName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
metricName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"AggregationFunction" Text -> AggregationFunction -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= AggregationFunction
aggregationFunction)
          ]
      )