1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#!/usr/bin/env stack
{- stack
--nix
--resolver lts-10.3
--install-ghc
runghc
--package http-types
--package yesod
--package yesod-core
--package text
--package aeson
--package acid-state
--package cassava
--package ixset
-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Acid
import Data.Acid.Local (createCheckpointAndClose)
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Csv as Csv
import Data.IxSet (Indexable(..), IxSet(..), (@=), Proxy(..), getOne, ixFun, ixSet)
import qualified Data.IxSet as IxSet
import Data.SafeCopy
import Data.Semigroup
import qualified Data.Vector as Vector
import Data.Data (Data, Typeable)
import GHC.Generics
import Control.Exception (bracket)
import Control.Monad
import Control.Monad.Reader (ask)
import Control.Monad.State (get, put)
import Data.Maybe (isJust)
import Data.Text (Text, pack, unpack)
import Yesod hiding (Number, Update, update, get)
import Network.HTTP.Types.Status (status400, status200)
import Data.Aeson hiding (Number)
--------------------------------------------------------------------
-- | Here be the state and data model stuff.
-- | Wrap a regular Int for CallerId, so we can track unique callers.
newtype CallerId = CallerId { unCallerId :: Int }
deriving (Show, Eq, Ord, Data, Enum, Typeable, Generic)
$(deriveSafeCopy 0 'base ''CallerId)
instance ToJSON CallerId where
toJSON CallerId{..} = toJSON unCallerId
instance FromJSON CallerId
-- | A type to describe the shape of our core model
data Caller = Caller
{ callerId :: CallerId
, name :: Text
, number :: Text
, context :: Text
}
deriving (Show, Eq, Ord, Generic, ToJSON, FromJSON)
$(deriveSafeCopy 0 'base ''Caller)
-- | Create reified types for each field we want to query
newtype Name = Name Text deriving (Eq, Ord, Data, Typeable)
newtype PhoneNumber = PhoneNumber Text deriving (Eq, Ord, Data, Typeable)
newtype Context = Context Text deriving (Eq, Ord, Data, Typeable)
$(deriveSafeCopy 0 'base ''Name)
$(deriveSafeCopy 0 'base ''PhoneNumber)
$(deriveSafeCopy 0 'base ''Context)
-- | Create the composite index
instance Indexable Caller where
empty = ixSet [ ixFun $ (:[]) . callerId
, ixFun $ \c -> [ Name $ name c ]
, ixFun $ \c -> [ PhoneNumber $ number c ]
, ixFun $ \c -> [ Context $ context c ]
]
-- | The database is a set of @Caller@ records, plus a record counter, so we
-- know the next CallerId to use.
data Database = Database
{ nextCallerId :: CallerId
, callers :: IxSet Caller
}
deriving (Typeable)
instance Data Database
$(deriveSafeCopy 0 'base ''Database)
initDatabase :: Database
initDatabase =
Database
{ nextCallerId = CallerId 1 -- ^ Index starting a 1
, callers = empty
}
---------------------------------------------------------------------------------
-- | CRUD operations on the state
-- | Insert the caller into database.
addCaller :: Text -> Text -> Text -> Update Database Caller
addCaller name number context = do
db@Database{..} <- get
let caller = Caller { callerId = nextCallerId
, name = name
, number = number
, context = context }
put $ db { nextCallerId = succ nextCallerId
, callers = IxSet.insert caller callers
}
return caller
-- | Return a list of the callers
viewCallers :: Int -> Query Database [Caller]
viewCallers limit = do
Database{..} <- ask
return $ take limit $ IxSet.toList callers
-- | Update a single caller record
updateCaller :: Caller -> Update Database ()
updateCaller updatedCaller =
do db@Database{..} <- get
put $ db { callers = IxSet.updateIx (callerId updatedCaller) updatedCaller callers }
-- | Lookup caller by CallerId
callerById :: CallerId -> Query Database (Maybe Caller)
callerById cid =
do Database{..} <- ask
return $ getOne $ callers @= cid
-- | Lookup caller by PhoneNumber
callerByNumber :: PhoneNumber -> Query Database [Caller]
callerByNumber num =
do Database{..} <- ask
return $ IxSet.toList $ callers @= num
$(makeAcidic ''Database ['addCaller, 'updateCaller, 'callerById, 'callerByNumber, 'viewCallers])
-----------------------------------------------------------------------
-- | Here be the HTTP stuff
data App = App
{ appState :: AcidState Database
}
mkYesod "App" [parseRoutes|
/query QueryR GET
/number NumberR POST
|]
-- | Initiate Yesod. The default method instances are fine for a prototype or
-- demo app.
instance Yesod App
data ApiError = ApiError
{ msg :: Text
}
deriving (Show, Eq, Generic, ToJSON, FromJSON)
getQueryR :: Handler RepJson
getQueryR = do
qm <- lookupGetParam "number"
case qm of
Nothing -> do
app <- getYesod
let db = appState app
callers <- liftIO $ query db $ ViewCallers 20
sendStatusJSON status200 $ object [ "results" .= callers ]
Just q -> do
app <- getYesod
let db = appState app
caller <- liftIO $ query db $ CallerByNumber $ PhoneNumber q
sendStatusJSON status200 $ object [ "results" .= caller ]
data PostRequest = PostRequest
{ _name :: Text
, _number :: Text
, _context :: Text
}
deriving (Show, Eq, Generic)
instance FromJSON PostRequest where
parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = drop 1 }
instance ToJSON PostRequest where
toJSON = genericToJSON defaultOptions { fieldLabelModifier = drop 1 }
postNumberR :: Handler RepJson
postNumberR = do
(obj :: Result PostRequest) <- parseJsonBody
case obj of
Error err ->
sendStatusJSON status400 $ ApiError $ "Invalid request. Could not parse JSON body: " <> pack err
Success PostRequest{..} -> do
app <- getYesod
let db = appState app
caller <- liftIO $ update db $ AddCaller _name _number _context
sendStatusJSON status200 $ caller
-- | Start a simple warp server on 3000
main :: IO ()
main = do
bracket (openLocalState initDatabase)
(createCheckpointAndClose)
(\db -> do
seedData <- BSL.readFile "interview-callerid-data.csv"
case Csv.decode Csv.NoHeader seedData of
Left err -> fail err
Right v ->
Vector.forM_ (Vector.indexed v) $ \(callerId, record) -> do
let (number, context, name) = record
update' db $ UpdateCaller $ Caller (CallerId callerId) name number context
putStrLn "Ready"
warp 3000 (App db))
|