2022-07-20 08:20:49 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.DataConnector.API.V0.Aggregate
|
|
|
|
( Aggregate (..),
|
|
|
|
SingleColumnAggregate (..),
|
2022-08-19 10:00:46 +03:00
|
|
|
singleColumnAggregateObjectCodec,
|
2022-07-20 08:20:49 +03:00
|
|
|
ColumnCountAggregate (..),
|
|
|
|
SingleColumnAggregateFunction (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-07-27 08:27:34 +03:00
|
|
|
import Autodocodec
|
2022-07-20 08:20:49 +03:00
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
|
|
|
import Data.Data (Data)
|
2022-07-27 08:27:34 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-07-20 08:20:49 +03:00
|
|
|
import Data.OpenApi (ToSchema)
|
|
|
|
import GHC.Generics (Generic)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.Column qualified as API.V0
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
data SingleColumnAggregate = SingleColumnAggregate
|
|
|
|
{ _scaFunction :: SingleColumnAggregateFunction,
|
|
|
|
_scaColumn :: API.V0.ColumnName
|
|
|
|
}
|
|
|
|
deriving stock (Eq, Ord, Show, Generic, Data)
|
|
|
|
|
2022-07-27 08:27:34 +03:00
|
|
|
singleColumnAggregateObjectCodec :: JSONObjectCodec SingleColumnAggregate
|
|
|
|
singleColumnAggregateObjectCodec =
|
|
|
|
SingleColumnAggregate
|
|
|
|
<$> requiredField "function" "The aggregation function" .= _scaFunction
|
|
|
|
<*> requiredField "column" "The column to apply the aggregation function to" .= _scaColumn
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
data SingleColumnAggregateFunction
|
|
|
|
= Average
|
|
|
|
| Max
|
|
|
|
| Min
|
|
|
|
| StandardDeviationPopulation
|
|
|
|
| StandardDeviationSample
|
|
|
|
| Sum
|
|
|
|
| VariancePopulation
|
|
|
|
| VarianceSample
|
|
|
|
deriving stock (Eq, Ord, Show, Generic, Data, Enum, Bounded)
|
|
|
|
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec SingleColumnAggregateFunction
|
|
|
|
|
|
|
|
instance HasCodec SingleColumnAggregateFunction where
|
|
|
|
codec =
|
|
|
|
named "SingleColumnAggregateFunction" $
|
2022-07-27 08:27:34 +03:00
|
|
|
stringConstCodec
|
2022-07-20 08:20:49 +03:00
|
|
|
[ (Average, "avg"),
|
|
|
|
(Max, "max"),
|
|
|
|
(Min, "min"),
|
|
|
|
(StandardDeviationPopulation, "stddev_pop"),
|
|
|
|
(StandardDeviationSample, "stddev_samp"),
|
|
|
|
(Sum, "sum"),
|
|
|
|
(VariancePopulation, "var_pop"),
|
|
|
|
(VarianceSample, "var_samp")
|
|
|
|
]
|
|
|
|
|
|
|
|
data ColumnCountAggregate = ColumnCountAggregate
|
2022-07-28 10:24:13 +03:00
|
|
|
{ _ccaColumn :: API.V0.ColumnName,
|
2022-07-20 08:20:49 +03:00
|
|
|
_ccaDistinct :: Bool
|
|
|
|
}
|
|
|
|
deriving stock (Eq, Ord, Show, Generic, Data)
|
|
|
|
|
2022-07-27 08:27:34 +03:00
|
|
|
columnCountAggregateObjectCodec :: JSONObjectCodec ColumnCountAggregate
|
|
|
|
columnCountAggregateObjectCodec =
|
|
|
|
ColumnCountAggregate
|
2022-07-28 10:24:13 +03:00
|
|
|
<$> requiredField "column" "The column to apply the count aggregate function to" .= _ccaColumn
|
2022-07-27 08:27:34 +03:00
|
|
|
<*> requiredField "distinct" "Whether or not only distinct items should be counted" .= _ccaDistinct
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
data Aggregate
|
|
|
|
= SingleColumn SingleColumnAggregate
|
|
|
|
| ColumnCount ColumnCountAggregate
|
|
|
|
| StarCount
|
|
|
|
deriving stock (Eq, Ord, Show, Generic, Data)
|
2022-07-27 08:27:34 +03:00
|
|
|
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec Aggregate
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
instance HasCodec Aggregate where
|
|
|
|
codec =
|
2022-07-27 08:27:34 +03:00
|
|
|
object "Aggregate" $
|
|
|
|
discriminatedUnionCodec "type" enc dec
|
|
|
|
where
|
|
|
|
enc = \case
|
|
|
|
SingleColumn sca -> ("single_column", mapToEncoder sca singleColumnAggregateObjectCodec)
|
|
|
|
ColumnCount cca -> ("column_count", mapToEncoder cca columnCountAggregateObjectCodec)
|
|
|
|
StarCount -> ("star_count", mapToEncoder () (pureCodec ()))
|
|
|
|
dec =
|
|
|
|
HashMap.fromList
|
|
|
|
[ ("single_column", ("SingleColumnAggregate", mapToDecoder SingleColumn singleColumnAggregateObjectCodec)),
|
|
|
|
("column_count", ("ColumnCountAggregate", mapToDecoder ColumnCount columnCountAggregateObjectCodec)),
|
|
|
|
("star_count", ("StarCountAggregate", mapToDecoder (const StarCount) (pureCodec ())))
|
|
|
|
]
|