<commit_before>{-# LANGUAGE TemplateHaskell #-}
module Data where

import Control.Lens
import Snap.Snaplet.Auth (UserId)
import Data.Map.Lazy (Map)
import Data.Time
import Data.ByteString.Lazy

data Status = OK | TODO | CANCELLED | PENDING

data Confirmation = Accepted | Percent Int | Declined

data Coordinate = Coord { _location  :: String
                        , _startTime :: UTCTime
                        , _endTime   :: UTCTime
                        , _timeZone  :: TimeZone}

makeLenses ''Coordinate

data Attachment = Attachment { _attId      :: Int
                             , _attName    :: String
                             , _attMime    :: String
                             , _attContent :: ByteString}
makeLenses ''Attachment


data Rendezvous = Rdv  { _rdvId            :: Int
                       , _rdvTitle         :: String
                       , _rdvInitiator     :: UserId
                       , _rdvConfirms      :: Map UserId [(Coordinate, Confirmation)]
                       , _rdvComments      :: [(UserId, UTCTime, String)]
                       , _rdvAttachments   :: [Attachment]
                       , _rdvFix           :: Maybe Coordinate
                       }

makeLenses ''Rendezvous

-- TODO make lenses for participants,
-- TODO hash for identifying changes ??<commit_msg>wrote consensus algorithm and accessor functions<commit_after>{-# LANGUAGE TemplateHaskell #-}
module Data where

import Control.Lens
import Snap.Snaplet.Auth (UserId)
import Data.Map.Lazy as Map (Map, keys, lookup, foldl)
import Data.Time
import Data.ByteString.Lazy (ByteString)
import Data.List (intersect)
import Data.Maybe (fromMaybe)

data Status = OK | TODO | CANCELLED | PENDING

data Confirmation = Accepted | Percent Int | Declined

acceptable :: Confirmation -> Bool
acceptable Accepted = True
acceptable (Percent n) = n >= 66
acceptable _ = False

data Coordinate = Coord { _location  :: String
                        , _startTime :: UTCTime
                        , _endTime   :: UTCTime
                        , _timeZone  :: TimeZone} deriving (Eq)

makeLenses ''Coordinate

data Attachment = Attachment { _attId      :: Int
                             , _attName    :: String
                             , _attMime    :: String
                             , _attContent :: ByteString}
makeLenses ''Attachment

data Comment = Comment (UserId, UTCTime, String)

data Rendezvous = Rdv  { _rdvId            :: Int
                       , _rdvTitle         :: String
                       , _rdvInitiator     :: UserId
                       , _rdvConfirms      :: Map UserId [(Coordinate, Confirmation)]
                       , _rdvComments      :: [Comment]
                       , _rdvAttachments   :: [Attachment]
                       , _rdvFix           :: Maybe Coordinate
                       }

makeLenses ''Rendezvous

rdvParticipants :: Rendezvous -> [UserId]
rdvParticipants = keys . view rdvConfirms

rdvCoordinates :: Rendezvous -> [Coordinate]
rdvCoordinates = map fst . rdvCoordConfirm

rdvCoordConfirm :: Rendezvous -> [(Coordinate, Confirmation)]
rdvCoordConfirm r =  fromMaybe [] coords
                 where coords = (r^.rdvInitiator) `Map.lookup` (r^.rdvConfirms)

findConsensus :: Rendezvous -> [Coordinate]
findConsensus r = let coords = rdvCoordinates r
                      confirms = r ^. rdvConfirms
                  in Map.foldl aux coords confirms
    where aux :: [Coordinate] -> [(Coordinate, Confirmation)] -> [Coordinate]
          aux lst lstPair = lst ∩ (map fst $ filter (acceptable.snd) lstPair)

(∩) :: Eq a => [a] -> [a] -> [a]
(∩) = intersect
{'fraction_non_alphanumeric': 0.08896210873146623, 'fraction_numerical': 0.0005491488193300384, 'mean_word_length': 1.8001537279016142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7074970', 'n_tokens_mistral': 1079, 'n_tokens_neox': 996, 'n_words': 276}