<commit_before>{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns        #-}

-- | Slotting functionality.

module Pos.Slotting
       ( MonadSlots (..)
       , getCurrentSlot
       , getCurrentSlotFlat
       , getSlotStart
       , onNewSlot
       ) where

import           Control.Monad.Catch      (MonadCatch, catch)
import           Control.TimeWarp.Timed   (Microsecond, MonadTimed, for, fork_, wait)
import           Formatting               (build, sformat, shown, (%))
import           Serokell.Util.Exceptions ()
import           System.Wlog              (WithNamedLogger, logError, logInfo,
                                           modifyLoggerName)
import           Universum                hiding (catch)

import           Pos.Constants            (slotDuration)
import           Pos.DHT                  (DHTResponseT)
import           Pos.Types                (FlatSlotId, SlotId (..), Timestamp (..),
                                           flattenSlotId, unflattenSlotId)

-- | Type class providing information about time when system started
-- functioning.
class Monad m => MonadSlots m where
    getSystemStartTime :: m Timestamp
    getCurrentTime :: m Timestamp

instance MonadSlots m => MonadSlots (DHTResponseT m) where
    getSystemStartTime = lift getSystemStartTime
    getCurrentTime = lift getCurrentTime

-- | Get id of current slot based on MonadSlots.
getCurrentSlot :: MonadSlots m => m SlotId
getCurrentSlot =
    f . getTimestamp <$> ((-) <$> getCurrentTime <*> getSystemStartTime)
  where
    f :: Microsecond -> SlotId
    f t = unflattenSlotId (fromIntegral $ t `div` slotDuration)
<commit_msg>Define MonadSlots instances for ReaderT and StateT<commit_after>{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns        #-}

-- | Slotting functionality.

module Pos.Slotting
       ( MonadSlots (..)
       , getCurrentSlot
       , getCurrentSlotFlat
       , getSlotStart
       , onNewSlot
       ) where

import           Control.Monad.Catch      (MonadCatch, catch)
import           Control.TimeWarp.Timed   (Microsecond, MonadTimed, for, fork_, wait)
import           Formatting               (build, sformat, shown, (%))
import           Serokell.Util.Exceptions ()
import           System.Wlog              (WithNamedLogger, logError, logInfo,
                                           modifyLoggerName)
import           Universum

import           Pos.Constants            (slotDuration)
import           Pos.DHT                  (DHTResponseT)
import           Pos.Types                (FlatSlotId, SlotId (..), Timestamp (..),
                                           flattenSlotId, unflattenSlotId)

-- | Type class providing information about time when system started
-- functioning.
class Monad m => MonadSlots m where
    getSystemStartTime :: m Timestamp
    getCurrentTime :: m Timestamp

instance MonadSlots m => MonadSlots (ReaderT s m) where
    getSystemStartTime = lift getSystemStartTime
    getCurrentTime = lift getCurrentTime

instance MonadSlots m => MonadSlots (StateT s m) where
    getSystemStartTime = lift getSystemStartTime
    getCurrentTime = lift getCurrentTime

instance MonadSlots m => MonadSlots (DHTResponseT m) where
    getSystemStartTime = lift getSystemStartTime
    getCurrentTime = lift getCurrentTime

-- | Get id of current slot based on MonadSlots.
getCurrentSlot :: MonadSlots m => m SlotId
getCurrentSlot =
    f . getTimestamp <$> ((-) <$> getCurrentTime <*> getSystemStartTime)
  where
    f :: Microsecond -> SlotId
    f t = unflattenSlotId (fromIntegral $ t `div` slotDuration)

{'fraction_non_alphanumeric': 0.07828983897834536, 'fraction_numerical': 0.0, 'mean_word_length': 2.1660808435852372, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '468463', 'n_tokens_mistral': 994, 'n_tokens_neox': 989, 'n_words': 272}