forked from SirDifferential/frp_utu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
330 lines (305 loc) · 12.9 KB
/
Main.hs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{-# Language OverloadedStrings, TemplateHaskell #-}
module Main where
import Safe
import Happstack.Server
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as TI
import qualified Data.Text.Lazy.Encoding as E
import Data.Text.Lazy (Text)
import qualified Data.Vector as V
import qualified System.IO as IO
import Control.Applicative
import Control.Monad
import Data.Maybe (maybe, catMaybes, listToMaybe)
import Data.List (groupBy, sortBy, group, find)
import Data.Function (on)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5 (AttributeValue, Html, (!))
import Text.Blaze.Internal (attribute)
import qualified Text.Blaze.Html5.Attributes as A
import qualified Data.Map as M
import Data.Map (Map)
import Data.Monoid
import Data.Aeson
import Data.Aeson.TH
import Data.Time
import qualified Data.Set as S
import Data.Set (Set)
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Char8 as B
import Control.Monad.State
data MinedData = MinedData {
students :: Map StudentID Student
, thesis :: [Thesis]
, credits :: [Credit]
}
type Mining a = StateT MinedData (ServerPartT IO) a
type CourseCode = Text
type Name = Text
type StudentPoints = Int
type StudentID = Text
type Major = Text
type Degree = Text
type Year = Int
data Course = Course {
courseCode :: CourseCode
, courseCredits :: Int
}
data Thesis = Thesis {
thesisName :: Name
, thesisCourses :: Set CourseCode
} deriving (Show, Eq, Ord)
data Date = Date Year Season deriving (Show, Eq, Ord)
data Season = Autumn | Spring deriving (Show, Eq, Ord)
data Student = Student {
date :: Date
, studentId :: StudentID
, name :: Name
, studentPoints :: StudentPoints
, degree :: Degree
, major :: Major
} deriving Show
data Credit = Credit {
creditStudentId :: StudentID
, creditId :: Text
, creditName :: Text
, creditDate :: Text
, creditCredits :: Int
} deriving Show
newtype DatatableStudent = DatatableStudent Student
newtype DatatableThesis = DatatableThesis Thesis
$(deriveJSON id ''Season)
$(deriveJSON id ''Date)
$(deriveJSON id ''Student)
instance ToJSON DatatableStudent where
toJSON (DatatableStudent student) = toJSON [
studentId student
, name student
, degree student
, major student
, T.pack $ show $ studentPoints student
, T.pack $ drop 5 $ show $ date student
]
instance ToJSON DatatableThesis where
toJSON (DatatableThesis thesis) = toJSON [
thesisName thesis
, T.pack $ show $ S.size $ thesisCourses thesis
]
instance ToMessage Value where
toMessage x = encode x
toContentType _ = B.pack ("application/json" :: String)
parseCredits :: FilePath -> IO [Credit]
parseCredits path =
catMaybes . map parseCredit . map (T.splitOn ";") . T.splitOn "\r\n" <$> TI.readFile path
where
parseCredit (sid : cid : name : date : credits : _ ) = do
credits' <- readMay $ T.unpack credits
return $ Credit sid cid name date credits'
parseCredit _ = Nothing
parseStudents :: FilePath -> IO (Map StudentID Student)
parseStudents path =
toMap . catMaybes . map parseStudent . map (T.splitOn ";") . T.splitOn "\r\n" <$> TI.readFile path
where
toMap students = M.fromList [(studentId s, s) | s <- students]
parseStudent (date : studentId : name : points : degree : major : _ ) = do
let (date', season) = T.splitAt 4 date
points' <- readMay (T.unpack points)
date'' <- readMay $ T.unpack date'
season' <- case T.unpack season of
"S" -> return Autumn
"K" -> return Spring
_ -> Nothing
return $ Student (Date date'' season') studentId name points' degree major
parseStudent _ = Nothing
parseThesis :: FilePath -> IO [Thesis]
parseThesis path = do
contents <- TI.readFile path
return $ maybe [] id $ parse contents
where
parseThesisCount (x:_) = readMay $ T.unpack x
parseThesisNames xs n = listToMaybe . take n . drop 1 $ xs
listToMaybe [] = Nothing
listToMaybe xs = Just xs
parse contents = do
let contents' = T.splitOn "\r\n" contents
n <- parseThesisCount contents'
names <- parseThesisNames contents' n
courses <- listToMaybe $ groupBy ((==) `on` fst) [T.breakOn " " course | course <- drop (n+1) contents']
return $ zipWith (\name course -> Thesis name (S.fromList $ map snd course)) names courses
studentModal :: Html
studentModal = H.div ! A.id "modal" ! A.class_ "modal hide fade" $ do
H.div ! A.class_ "modal-header" $ do
H.button ! A.type_ "button" ! A.class_ "close" ! data_dismiss "modal" ! aria_hidden "true" $
"x"
H.h3 ! A.id "student_header" $ mempty
H.div ! A.class_ "modal-body" ! A.id "modalBody" $ do
mempty
H.div ! A.class_ "modal-footer" $ do
H.a ! A.href "#" ! A.class_ "btn close" $ "Close"
where
data_dismiss = attribute "data-dismiss" "data-dismiss=\""
aria_hidden = attribute "aria-hidden" "aria-hidden=\""
studentQuery :: Mining Response
studentQuery = do
students <- gets students
id' <- lookText "studentId"
let student = M.lookup id' students
case student of
Just student' -> ok $ toResponse $ toJSON student'
Nothing -> notFound $ toResponse $ notFoundView $
H.p "User is not found"
fileResponse :: Html -> Html
fileResponse fun = H.docTypeHtml $ do
H.head $
H.script ! A.type_ "application/javascript" $
("parent." `mappend` fun `mappend` "()")
H.body $ mempty
thesisData :: Mining Response
thesisData = do
t <- gets thesis
ok $ toResponse $ toJSON $ map DatatableThesis $ t
studentsData :: Mining Response
studentsData = do
s <- gets students
ok $ toResponse $ toJSON $ map DatatableStudent $ M.elems s
studentsUpload :: Mining Response
studentsUpload = do
(path, _, _) <- lookFile "studentFile"
newStudents <- liftIO $ parseStudents path
modify (\m -> m{students=newStudents})
ok $ toResponse $ fileResponse "touchStudents"
thesisUpload :: Mining Response
thesisUpload = do
(path, _, _) <- lookFile "thesisFile"
newThesis <- liftIO $ parseThesis path
modify (\m -> m{thesis=newThesis})
ok $ toResponse $ fileResponse "touchThesis"
creditsUpload :: Mining Response
creditsUpload = do
(path, _, _) <- lookFile "creditsFile"
newCredits <- liftIO $ parseCredits path
modify (\m -> m{credits=newCredits})
ok $ toResponse $ fileResponse "touchCredits"
notFoundView :: Html -> Html
notFoundView inner = H.docTypeHtml $ do
H.head $ do
H.title "Not found"
H.meta ! A.charset "utf-8"
H.meta ! A.name "viewport" ! A.content "width=device-width, initial-scale=1.0"
H.meta ! A.name "description" ! A.content ""
H.meta ! A.name "author" ! A.content ""
H.link ! A.href "/static/bootstrap/css/bootstrap.css" ! A.rel "stylesheet"
H.link ! A.href "/static/css/style.css" ! A.rel "stylesheet"
H.link ! A.href "/static/bootstrap/css/bootstrap-responsive.css" ! A.rel "stylesheet"
H.link ! A.href "http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" ! A.rel "stylesheet"
H.script ! A.type_ "text/html" ! A.id "student-template" $
H.div mempty
H.body $ do
H.h1 "404 Not found"
H.div ! A.class_ "span9 well" $
inner
uploadForm :: AttributeValue -> Text -> AttributeValue -> Html
uploadForm name title action = let
iframeName = ("iframe" `mappend` name)
in H.div ! A.class_ "fluid-row" ! A.style "display: none" $
H.div ! A.class_ "span3" $ do
H.h3 $ H.toHtml title
H.iframe ! A.name iframeName ! A.style "display: none" $ mempty
H.form ! A.action action ! A.target iframeName ! A.class_ "form-inline" ! A.enctype "multipart/form-data" ! A.method "post" $ do
H.input ! A.type_ "file" ! A.name name
H.input ! A.type_ "submit" ! A.class_ "btn" ! A.value "Update"
mainView :: Map StudentID Student -> Html
mainView students = H.docTypeHtml $ do
H.head $ do
H.title title
H.meta ! A.charset "utf-8"
H.meta ! A.name "viewport" ! A.content "width=device-width, initial-scale=1.0"
H.meta ! A.name "description" ! A.content ""
H.meta ! A.name "author" ! A.content ""
H.link ! A.href "/static/bootstrap/css/bootstrap.css" ! A.rel "stylesheet"
H.link ! A.href "/static/css/style.css" ! A.rel "stylesheet"
H.link ! A.href "/static/bootstrap/css/bootstrap-responsive.css" ! A.rel "stylesheet"
H.link ! A.href "http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" ! A.rel "stylesheet"
H.script ! A.type_ "application/javascript" $
H.toHtml $ "var studentData = " `T.append` (E.decodeUtf8 $ encode $ map DatatableStudent $ M.elems students)
H.script ! A.type_ "application/javascript" ! A.src "/static/jquery/jquery-1.9.1.min.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "/static/bootstrap/js/bootstrap.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "http://code.jquery.com/ui/1.10.2/jquery-ui.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "/static/bacon/js/Bacon.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "/static/mustache/mustache.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "http://datatables.net/download/build/jquery.dataTables.nightly.js" $ mempty
H.script ! A.type_ "application/javascript" ! A.src "/static/js/doThings.js" $ mempty
H.script ! A.type_ "text/html" ! A.id "studentModalTemplate" $
H.div ! A.class_ "fluid-row" $ do
H.div ! A.class_ "span4" $ do
H.h2 $ "{{name}}"
H.hr
H.p $ "{{degree}}"
H.p $ "{{major}}"
H.p $ "{{date}}"
H.p $ "{{studentId}}"
H.p $ "{{studentPoints}}"
H.body $ do
H.div ! A.class_ "navbar navbar-inverse navbar-fixed-top" $ do
H.div ! A.class_ "navbar-inner" $ do
H.div ! A.class_ "container-fluid" $ do
H.button ! A.type_ "button" ! A.class_ "btn btn-navbar" ! data_toggle "collapse" ! data_target ".nav-collapse" $ do
H.span ! A.class_ "icon-bar" $ mempty
H.span ! A.class_ "icon-bar" $ mempty
H.span ! A.class_ "icon-bar" $ mempty
H.a ! A.class_ "brand" ! A.href "#" $ title
H.div ! A.class_ "nav-collapse collapse" $ do
H.p ! A.class_ "navbar-text pull-right" $
"Placeholder login text. What to do with this?"
H.ul ! A.class_ "nav" $ do
H.li $ "emptymenu"
studentModal
H.div ! A.class_ "container-fluid" $ do
H.div ! A.class_ "row-fluid" $ do
H.div ! A.class_ "span8" $ do
H.div ! A.class_ "span3" $
H.div ! A.class_ "well sidebar-nav" $
H.ul ! A.class_ "nav nav-list" $ do
H.li ! A.class_ "nav-header" $ "Sidebar"
H.li $ H.a ! data_target "students" ! A.href "#" $ "Students"
H.li $ H.a ! data_target "degrees" ! A.href "#" $ "Degreees"
H.li $ H.a ! data_target "credits" ! A.href "#" $ "Credits"
H.div ! A.class_ "span4" $ do
uploadForm "studentFile" "Update students file" "student/upload"
uploadForm "thesisFile" "Update thesis file" "thesis/upload"
uploadForm "creditsFile" "Update credits file" "credits/upload"
H.div ! A.class_ "span9" $ do
H.div ! A.id "userData" ! A.class_ "hero-unit" $ do
H.table ! A.class_ "databox" $ mempty
H.div ! A.id "degreeData" ! A.style "display: none" ! A.class_ "hero-unit" $ do
H.table ! A.class_ "databox" $ mempty
H.div ! A.id "creditData" ! A.style "display: none" ! A.class_ "hero-unit" $ do
H.table ! A.class_ "databox" $ mempty
where
data_toggle = attribute "data-toggle" " data-toggle=\""
data_target = attribute "data-target" " data-target=\""
title = "Käyttöliittymät harkka"
lookBSsafe :: (Monad m, Functor m, HasRqData m) => String -> m (Maybe ByteString)
lookBSsafe key = listToMaybe <$> lookBSs key
looksafe :: (Monad m, Functor m, HasRqData m) => String -> m (Maybe String)
looksafe key = listToMaybe <$> looks key
studentsCredits :: Student -> [Credit] -> [Credit]
studentsCredits student credits =
filter (\c -> studentId student == creditStudentId c) credits
main :: IO ()
main = do
thesis <- parseThesis "data/kandit.txt"
students <- parseStudents "data/opiskelijat.txt"
credits <- parseCredits "data/suoritukset.txt"
let state = MinedData students thesis credits
simpleHTTP nullConf{port=25565} $ do
decodeBody (defaultBodyPolicy "/tmp" 16384 16384 16384)
evalStateT (msum [
nullDir >> ok (toResponse $ mainView students)
, dir "student" $ studentQuery
, dirs "student/upload" $ studentsUpload
, dirs "student/data" $ studentsData
, dirs "degree/data" $ thesisData
, dirs "thesis/upload" $ thesisUpload
, dir "static" $ serveDirectory EnableBrowsing [] "public/"
]) state