Repository: hasura/graphql-engine
File: server/src-lib/Hasura/GraphQL/Execute/LiveQuery/Poll.hs (Haskell)

Code before:
import qualified ListT
import qualified StmContainers.Map                           as STMMap

import           Data.Hashable
import           Data.List.Split                             (chunksOf)
import           GHC.AssertNF
import           Control.Lens

import qualified Hasura.GraphQL.Execute.LiveQuery.TMap       as TMap
import qualified Hasura.GraphQL.Transport.WebSocket.Protocol as WS
import qualified Hasura.GraphQL.Transport.WebSocket.Server   as WS
import qualified Hasura.Logging                              as L

import           Hasura.Db
import           Hasura.EncJSON
import           Hasura.GraphQL.Execute.LiveQuery.Options
import           Hasura.GraphQL.Execute.LiveQuery.Plan
import           Hasura.GraphQL.Transport.HTTP.Protocol
import           Hasura.RQL.Types.Error
import           Hasura.Session

-- -------------------------------------------------------------------------------------------------
-- Subscribers

-- | Identifies a subscriber of a live query based on their (websocket id,
-- operation id) - this is useful for collecting metrics and then correlating.
-- The 'createdAt' to ensure that there is uniqueness even if the operationId
-- is later repeated on the same websocket
data SubscriberId
  = SubscriberId
  { _siWebsocketId :: !WS.WSId
  , _siOperationId :: !WS.OperationId
  , _siCreatedAt   :: !Clock.UTCTime
  } deriving (Show, Eq, Generic)

$(J.deriveToJSON (J.aesonDrop 3 J.snakeCase) ''SubscriberId)

instance Hashable SubscriberId where
  -- UTCTime has no Hashable instance however we can hash using only (WSId,
  -- OperationId) as it is unlikely for us to see the same (WSId, OperationId)
  -- with different 'CreatedAt' fields
  hashWithSalt salt (SubscriberId websocketId operationId _) =
    hashWithSalt salt (websocketId, operationId)

mkSubscriberId :: WS.WSId -> WS.OperationId -> IO SubscriberId
mkSubscriberId weboscketId operationId =
  SubscriberId weboscketId operationId <$> Clock.getCurrentTime

data Subscriber
  = Subscriber
  { _sOnChangeCallback :: !OnChange

Review comment (refactor):
I missed this comment on my first read-through and was confused by the `_siCreatedAt` field. Could you move this to a Haddock comment on the field itself?

Code after:
import qualified Data.UUID.V4                             as UUID
import qualified Database.PG.Query                        as Q
import qualified ListT
import qualified StmContainers.Map                        as STMMap

import           Data.List.Split                          (chunksOf)
import           GHC.AssertNF
import           Control.Lens

import qualified Hasura.GraphQL.Execute.LiveQuery.TMap    as TMap
import qualified Hasura.Logging                           as L

import           Hasura.Db
import           Hasura.GraphQL.Execute.LiveQuery.Options
import           Hasura.GraphQL.Execute.LiveQuery.Plan
import           Hasura.GraphQL.Transport.HTTP.Protocol
import           Hasura.RQL.Types.Error
import           Hasura.Session

-- ----------------------------------------------------------------------------------------------
-- Subscribers

newtype SubscriberId
  = SubscriberId { unSubscriberId :: UUID.UUID }
  deriving (Show, Eq, Generic, Hashable, J.ToJSON)

newSubscriberId :: IO SubscriberId
newSubscriberId =
  SubscriberId <$> UUID.nextRandom

-- | Allows a user of the live query subsystem (currently websocket transport)
-- to attach arbitrary metadata about a subscriber. This information is available
-- as part of Subscriber in CohortExecutionDetails and can be logged by customizing
-- in pollerlog
newtype SubscriberMetadata
  = SubscriberMetadata { unSubscriberMetadata :: J.Value }
  deriving (Show, Eq, J.ToJSON)

mkSubscriberMetadata :: J.Value -> SubscriberMetadata
mkSubscriberMetadata = SubscriberMetadata

data Subscriber
  = Subscriber
  { _sId               :: !SubscriberId
  , _sMetadata         :: !SubscriberMetadata
  , _sOnChangeCallback :: !OnChange
  }

-- | live query onChange metadata, used for adding more extra analytics data
data LiveQueryMetadata
  = LiveQueryMetadata