diff options
author | Joris | 2016-11-13 00:49:32 +0100 |
---|---|---|
committer | Joris | 2016-11-13 00:49:32 +0100 |
commit | 86a96decdb8892b10c5314eb916ef15a64204450 (patch) | |
tree | 6f41742d0466f77948680964188144fbff036902 /src/server/Resource.hs | |
parent | bf6a0a0b32a7efb88f75c2e89b84d6907aeb10bc (diff) |
Send weekly activity at start of week about previous week
Diffstat (limited to 'src/server/Resource.hs')
-rw-r--r-- | src/server/Resource.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/server/Resource.hs b/src/server/Resource.hs new file mode 100644 index 0000000..4dd8615 --- /dev/null +++ b/src/server/Resource.hs @@ -0,0 +1,50 @@ +module Resource + ( Resource + , createdAt + , editedAt + , deletedAt + , Status(..) + , groupByStatus + , statusDuring + ) where + +import Data.Maybe (fromMaybe) +import Data.Map (Map) +import qualified Data.Map as M +import Data.Time.Clock (UTCTime) + +class Resource a where + createdAt :: a -> UTCTime + editedAt :: a -> Maybe UTCTime + deletedAt :: a -> Maybe UTCTime + +data Status = + Created + | Edited + | Deleted + deriving (Eq, Show, Read, Ord) + +groupByStatus :: Resource a => UTCTime -> UTCTime -> [a] -> Map Status [a] +groupByStatus start end resources = + foldl + (\m resource -> + case statusDuring start end resource of + Just status -> M.insertWith (++) status [resource] m + Nothing -> m + ) + M.empty + resources + +statusDuring :: Resource a => UTCTime -> UTCTime -> a -> Maybe Status +statusDuring start end resource + | created && not deleted = Just Created + | not created && edited && not deleted = Just Edited + | not created && deleted = Just Deleted + | otherwise = Nothing + where + created = belongs (createdAt resource) start end + edited = fromMaybe False (fmap (\t -> belongs t start end) $ editedAt resource) + deleted = fromMaybe False (fmap (\t -> belongs t start end) $ deletedAt resource) + +belongs :: UTCTime -> UTCTime -> UTCTime -> Bool +belongs time start end = time >= start && time < end |