{-# 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.MarketplaceMetering.Types.UsageRecord
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.MarketplaceMetering.Types.UsageRecord where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.MarketplaceMetering.Types.UsageAllocation
import qualified Amazonka.Prelude as Prelude

-- | A @UsageRecord@ indicates a quantity of usage for a given product,
-- customer, dimension and time.
--
-- Multiple requests with the same @UsageRecords@ as input will be
-- de-duplicated to prevent double charges.
--
-- /See:/ 'newUsageRecord' smart constructor.
data UsageRecord = UsageRecord'
  { -- | The quantity of usage consumed by the customer for the given dimension
    -- and time. Defaults to @0@ if not specified.
    UsageRecord -> Maybe Natural
quantity :: Prelude.Maybe Prelude.Natural,
    -- | The set of @UsageAllocations@ to submit. The sum of all
    -- @UsageAllocation@ quantities must equal the Quantity of the
    -- @UsageRecord@.
    UsageRecord -> Maybe (NonEmpty UsageAllocation)
usageAllocations :: Prelude.Maybe (Prelude.NonEmpty UsageAllocation),
    -- | Timestamp, in UTC, for which the usage is being reported.
    --
    -- Your application can meter usage for up to one hour in the past. Make
    -- sure the @timestamp@ value is not before the start of the software
    -- usage.
    UsageRecord -> POSIX
timestamp :: Data.POSIX,
    -- | The @CustomerIdentifier@ is obtained through the @ResolveCustomer@
    -- operation and represents an individual buyer in your application.
    UsageRecord -> Text
customerIdentifier :: Prelude.Text,
    -- | During the process of registering a product on AWS Marketplace,
    -- dimensions are specified. These represent different units of value in
    -- your application.
    UsageRecord -> Text
dimension :: Prelude.Text
  }
  deriving (UsageRecord -> UsageRecord -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UsageRecord -> UsageRecord -> Bool
$c/= :: UsageRecord -> UsageRecord -> Bool
== :: UsageRecord -> UsageRecord -> Bool
$c== :: UsageRecord -> UsageRecord -> Bool
Prelude.Eq, ReadPrec [UsageRecord]
ReadPrec UsageRecord
Int -> ReadS UsageRecord
ReadS [UsageRecord]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [UsageRecord]
$creadListPrec :: ReadPrec [UsageRecord]
readPrec :: ReadPrec UsageRecord
$creadPrec :: ReadPrec UsageRecord
readList :: ReadS [UsageRecord]
$creadList :: ReadS [UsageRecord]
readsPrec :: Int -> ReadS UsageRecord
$creadsPrec :: Int -> ReadS UsageRecord
Prelude.Read, Int -> UsageRecord -> ShowS
[UsageRecord] -> ShowS
UsageRecord -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UsageRecord] -> ShowS
$cshowList :: [UsageRecord] -> ShowS
show :: UsageRecord -> String
$cshow :: UsageRecord -> String
showsPrec :: Int -> UsageRecord -> ShowS
$cshowsPrec :: Int -> UsageRecord -> ShowS
Prelude.Show, forall x. Rep UsageRecord x -> UsageRecord
forall x. UsageRecord -> Rep UsageRecord x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UsageRecord x -> UsageRecord
$cfrom :: forall x. UsageRecord -> Rep UsageRecord x
Prelude.Generic)

-- |
-- Create a value of 'UsageRecord' 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:
--
-- 'quantity', 'usageRecord_quantity' - The quantity of usage consumed by the customer for the given dimension
-- and time. Defaults to @0@ if not specified.
--
-- 'usageAllocations', 'usageRecord_usageAllocations' - The set of @UsageAllocations@ to submit. The sum of all
-- @UsageAllocation@ quantities must equal the Quantity of the
-- @UsageRecord@.
--
-- 'timestamp', 'usageRecord_timestamp' - Timestamp, in UTC, for which the usage is being reported.
--
-- Your application can meter usage for up to one hour in the past. Make
-- sure the @timestamp@ value is not before the start of the software
-- usage.
--
-- 'customerIdentifier', 'usageRecord_customerIdentifier' - The @CustomerIdentifier@ is obtained through the @ResolveCustomer@
-- operation and represents an individual buyer in your application.
--
-- 'dimension', 'usageRecord_dimension' - During the process of registering a product on AWS Marketplace,
-- dimensions are specified. These represent different units of value in
-- your application.
newUsageRecord ::
  -- | 'timestamp'
  Prelude.UTCTime ->
  -- | 'customerIdentifier'
  Prelude.Text ->
  -- | 'dimension'
  Prelude.Text ->
  UsageRecord
newUsageRecord :: UTCTime -> Text -> Text -> UsageRecord
newUsageRecord
  UTCTime
pTimestamp_
  Text
pCustomerIdentifier_
  Text
pDimension_ =
    UsageRecord'
      { $sel:quantity:UsageRecord' :: Maybe Natural
quantity = forall a. Maybe a
Prelude.Nothing,
        $sel:usageAllocations:UsageRecord' :: Maybe (NonEmpty UsageAllocation)
usageAllocations = forall a. Maybe a
Prelude.Nothing,
        $sel:timestamp:UsageRecord' :: POSIX
timestamp = forall (a :: Format). Iso' (Time a) UTCTime
Data._Time forall t b. AReview t b -> b -> t
Lens.# UTCTime
pTimestamp_,
        $sel:customerIdentifier:UsageRecord' :: Text
customerIdentifier = Text
pCustomerIdentifier_,
        $sel:dimension:UsageRecord' :: Text
dimension = Text
pDimension_
      }

-- | The quantity of usage consumed by the customer for the given dimension
-- and time. Defaults to @0@ if not specified.
usageRecord_quantity :: Lens.Lens' UsageRecord (Prelude.Maybe Prelude.Natural)
usageRecord_quantity :: Lens' UsageRecord (Maybe Natural)
usageRecord_quantity = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Maybe Natural
quantity :: Maybe Natural
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
quantity} -> Maybe Natural
quantity) (\s :: UsageRecord
s@UsageRecord' {} Maybe Natural
a -> UsageRecord
s {$sel:quantity:UsageRecord' :: Maybe Natural
quantity = Maybe Natural
a} :: UsageRecord)

-- | The set of @UsageAllocations@ to submit. The sum of all
-- @UsageAllocation@ quantities must equal the Quantity of the
-- @UsageRecord@.
usageRecord_usageAllocations :: Lens.Lens' UsageRecord (Prelude.Maybe (Prelude.NonEmpty UsageAllocation))
usageRecord_usageAllocations :: Lens' UsageRecord (Maybe (NonEmpty UsageAllocation))
usageRecord_usageAllocations = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Maybe (NonEmpty UsageAllocation)
usageAllocations :: Maybe (NonEmpty UsageAllocation)
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
usageAllocations} -> Maybe (NonEmpty UsageAllocation)
usageAllocations) (\s :: UsageRecord
s@UsageRecord' {} Maybe (NonEmpty UsageAllocation)
a -> UsageRecord
s {$sel:usageAllocations:UsageRecord' :: Maybe (NonEmpty UsageAllocation)
usageAllocations = Maybe (NonEmpty UsageAllocation)
a} :: UsageRecord) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Timestamp, in UTC, for which the usage is being reported.
--
-- Your application can meter usage for up to one hour in the past. Make
-- sure the @timestamp@ value is not before the start of the software
-- usage.
usageRecord_timestamp :: Lens.Lens' UsageRecord Prelude.UTCTime
usageRecord_timestamp :: Lens' UsageRecord UTCTime
usageRecord_timestamp = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {POSIX
timestamp :: POSIX
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
timestamp} -> POSIX
timestamp) (\s :: UsageRecord
s@UsageRecord' {} POSIX
a -> UsageRecord
s {$sel:timestamp:UsageRecord' :: POSIX
timestamp = POSIX
a} :: UsageRecord) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (a :: Format). Iso' (Time a) UTCTime
Data._Time

-- | The @CustomerIdentifier@ is obtained through the @ResolveCustomer@
-- operation and represents an individual buyer in your application.
usageRecord_customerIdentifier :: Lens.Lens' UsageRecord Prelude.Text
usageRecord_customerIdentifier :: Lens' UsageRecord Text
usageRecord_customerIdentifier = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Text
customerIdentifier :: Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
customerIdentifier} -> Text
customerIdentifier) (\s :: UsageRecord
s@UsageRecord' {} Text
a -> UsageRecord
s {$sel:customerIdentifier:UsageRecord' :: Text
customerIdentifier = Text
a} :: UsageRecord)

-- | During the process of registering a product on AWS Marketplace,
-- dimensions are specified. These represent different units of value in
-- your application.
usageRecord_dimension :: Lens.Lens' UsageRecord Prelude.Text
usageRecord_dimension :: Lens' UsageRecord Text
usageRecord_dimension = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Text
dimension :: Text
$sel:dimension:UsageRecord' :: UsageRecord -> Text
dimension} -> Text
dimension) (\s :: UsageRecord
s@UsageRecord' {} Text
a -> UsageRecord
s {$sel:dimension:UsageRecord' :: Text
dimension = Text
a} :: UsageRecord)

instance Data.FromJSON UsageRecord where
  parseJSON :: Value -> Parser UsageRecord
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"UsageRecord"
      ( \Object
x ->
          Maybe Natural
-> Maybe (NonEmpty UsageAllocation)
-> POSIX
-> Text
-> Text
-> UsageRecord
UsageRecord'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"Quantity")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"UsageAllocations")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"Timestamp")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"CustomerIdentifier")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"Dimension")
      )

instance Prelude.Hashable UsageRecord where
  hashWithSalt :: Int -> UsageRecord -> Int
hashWithSalt Int
_salt UsageRecord' {Maybe Natural
Maybe (NonEmpty UsageAllocation)
Text
POSIX
dimension :: Text
customerIdentifier :: Text
timestamp :: POSIX
usageAllocations :: Maybe (NonEmpty UsageAllocation)
quantity :: Maybe Natural
$sel:dimension:UsageRecord' :: UsageRecord -> Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
quantity
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty UsageAllocation)
usageAllocations
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` POSIX
timestamp
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
customerIdentifier
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
dimension

instance Prelude.NFData UsageRecord where
  rnf :: UsageRecord -> ()
rnf UsageRecord' {Maybe Natural
Maybe (NonEmpty UsageAllocation)
Text
POSIX
dimension :: Text
customerIdentifier :: Text
timestamp :: POSIX
usageAllocations :: Maybe (NonEmpty UsageAllocation)
quantity :: Maybe Natural
$sel:dimension:UsageRecord' :: UsageRecord -> Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
quantity
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty UsageAllocation)
usageAllocations
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf POSIX
timestamp
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
customerIdentifier
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
dimension

instance Data.ToJSON UsageRecord where
  toJSON :: UsageRecord -> Value
toJSON UsageRecord' {Maybe Natural
Maybe (NonEmpty UsageAllocation)
Text
POSIX
dimension :: Text
customerIdentifier :: Text
timestamp :: POSIX
usageAllocations :: Maybe (NonEmpty UsageAllocation)
quantity :: Maybe Natural
$sel:dimension:UsageRecord' :: UsageRecord -> Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"Quantity" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
quantity,
            (Key
"UsageAllocations" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty UsageAllocation)
usageAllocations,
            forall a. a -> Maybe a
Prelude.Just (Key
"Timestamp" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= POSIX
timestamp),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"CustomerIdentifier" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
customerIdentifier),
            forall a. a -> Maybe a
Prelude.Just (Key
"Dimension" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
dimension)
          ]
      )