]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Odds.hs
Fix remaining compiler warnings.
[dead/htsn-import.git] / src / TSN / XML / Odds.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
9
10 -- | Parse TSN XML for the DTD "Odds_XML.dtd". Each document contains
11 -- a root element \<message\> that contains a bunch of other
12 -- unorganized crap.
13 --
14 module TSN.XML.Odds (
15 odds_tests,
16 pickle_message,
17 -- * WARNING: these are private but exported to silence warnings
18 Odds_OddsGameConstructor(..),
19 OddsCasinoConstructor(..),
20 OddsConstructor(..),
21 OddsGame_OddsGameTeamConstructor(..),
22 OddsGameConstructor(..),
23 OddsGameLineConstructor(..),
24 OddsGameTeamConstructor(..) )
25 where
26
27 import Control.Monad ( forM_, join )
28 import Data.Tuple.Curry ( uncurryN )
29 import Database.Groundhog (
30 (=.),
31 (==.),
32 insert_,
33 insertByAll,
34 migrate,
35 update )
36 import Database.Groundhog.Core ( DefaultKey )
37 import Database.Groundhog.TH (
38 groundhog,
39 mkPersist )
40 import Test.Tasty ( TestTree, testGroup )
41 import Test.Tasty.HUnit ( (@?=), testCase )
42 import Text.Read ( readMaybe )
43 import Text.XML.HXT.Core (
44 PU,
45 xp5Tuple,
46 xp6Tuple,
47 xp8Tuple,
48 xpAttr,
49 xpElem,
50 xpInt,
51 xpList,
52 xpOption,
53 xpPair,
54 xpText,
55 xpTriple,
56 xpWrap )
57
58 import TSN.Codegen (
59 tsn_codegen_config )
60 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
61 import TSN.Picklers ( xp_team_id )
62 import TSN.XmlImport ( XmlImport(..) )
63 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
64
65
66
67 -- | The home/away lines are 'Double's, but the over/under lines are
68 -- textual. If we want to use one data type for both, we have to go
69 -- with a String and then attempt to 'read' a 'Double' later when we
70 -- go to insert the thing.
71 --
72 data OddsGameCasinoXml =
73 OddsGameCasinoXml {
74 xml_casino_client_id :: Int,
75 xml_casino_name :: String,
76 xml_casino_line :: Maybe String }
77 deriving (Eq, Show)
78
79
80 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
81 -- priori textual (because it might be an over/under line).
82 --
83 home_away_line :: OddsGameCasinoXml -> Maybe Double
84 home_away_line = join . (fmap readMaybe) . xml_casino_line
85
86 -- | The casinos should have their own table, but the lines don't
87 -- belong in that table. (There should be another table joining the
88 -- casinos and the thing the lines are for together.)
89 --
90 -- We drop the 'Game' prefix because the Casinos really aren't
91 -- children of the games; the XML just makes it seem that way.
92 --
93 data OddsCasino =
94 OddsCasino {
95 casino_client_id :: Int,
96 casino_name :: String }
97 deriving (Eq, Show)
98
99 instance FromXml OddsGameCasinoXml where
100 type Db OddsGameCasinoXml = OddsCasino
101
102 -- We don't need the key argument (from_xml_fk) since the XML type
103 -- contains more information in this case.
104 from_xml OddsGameCasinoXml{..} =
105 OddsCasino {
106 casino_client_id = xml_casino_client_id,
107 casino_name = xml_casino_name }
108
109
110 instance XmlImport OddsGameCasinoXml
111
112
113 data OddsGameHomeTeamXml =
114 OddsGameHomeTeamXml {
115 xml_home_team_id :: String, -- ^ These are three-character IDs.
116 xml_home_rotation_number :: Int,
117 xml_home_abbr :: String,
118 xml_home_team_name :: String,
119 xml_home_casinos :: [OddsGameCasinoXml] }
120 deriving (Eq, Show)
121
122 instance FromXml OddsGameHomeTeamXml where
123 type Db OddsGameHomeTeamXml = OddsGameTeam
124 from_xml OddsGameHomeTeamXml{..} =
125 OddsGameTeam {
126 db_team_id = xml_home_team_id,
127 db_abbr = xml_home_abbr,
128 db_team_name = xml_home_team_name }
129
130 instance XmlImport OddsGameHomeTeamXml where
131
132
133 data OddsGameTeam =
134 OddsGameTeam {
135 db_team_id :: String, -- ^ The home/away team IDs are 3 characters
136 db_abbr :: String,
137 db_team_name :: String }
138 deriving (Eq, Show)
139
140
141 -- | Database mapping between games and their home/away teams.
142 data OddsGame_OddsGameTeam =
143 OddsGame_OddsGameTeam {
144 ogogt_odds_games_id :: DefaultKey OddsGame,
145 ogogt_away_team_id :: DefaultKey OddsGameTeam,
146 ogogt_home_team_id :: DefaultKey OddsGameTeam }
147
148 data OddsGameAwayTeamXml =
149 OddsGameAwayTeamXml {
150 xml_away_team_id :: String, -- ^ These are 3 character IDs.
151 xml_away_rotation_number :: Int,
152 xml_away_abbr :: String,
153 xml_away_team_name :: String,
154 xml_away_casinos :: [OddsGameCasinoXml] }
155 deriving (Eq, Show)
156
157 instance FromXml OddsGameAwayTeamXml where
158 type Db OddsGameAwayTeamXml = OddsGameTeam
159 from_xml OddsGameAwayTeamXml{..} = OddsGameTeam
160 xml_away_team_id
161 xml_away_abbr
162 xml_away_team_name
163
164 instance XmlImport OddsGameAwayTeamXml where
165
166 -- | Can't use a newtype with Groundhog.
167 newtype OddsGameOverUnderXml =
168 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
169 deriving (Eq, Show)
170
171 -- | This database representation of the casino lines can't be
172 -- constructed from the one in the XML. The casinos within
173 -- Game>HomeTeam, Game>AwayTeam, and Game>Over_Under are all more or
174 -- less the same. We don't need a bajillion different tables to
175 -- store that, just one tying the casino/game pair to the three
176 -- lines.
177 --
178 -- The one small difference between the over/under casinos and the
179 -- home/away ones is that the home/away lines are all 'Double's, but
180 -- the over/under lines appear to be textual.
181 --
182 data OddsGameLine =
183 OddsGameLine {
184 ogl_odds_games_id :: DefaultKey OddsGame,
185 ogl_odds_casinos_id :: DefaultKey OddsCasino,
186 ogl_over_under :: Maybe String,
187 ogl_away_line :: Maybe Double,
188 ogl_home_line :: Maybe Double }
189
190 data OddsGame =
191 OddsGame {
192 db_game_id :: Int,
193 db_game_date :: String, -- TODO
194 db_game_time :: String, -- TODO
195 db_game_away_team_rotation_number :: Int,
196 db_game_home_team_rotation_number :: Int }
197 deriving instance Eq OddsGame
198 deriving instance Show OddsGame
199
200 data OddsGameXml =
201 OddsGameXml {
202 xml_game_id :: Int,
203 xml_game_date :: String, -- TODO
204 xml_game_time :: String, -- TODO
205 xml_game_away_team :: OddsGameAwayTeamXml,
206 xml_game_home_team :: OddsGameHomeTeamXml,
207 xml_game_over_under :: OddsGameOverUnderXml }
208 deriving (Eq, Show)
209
210 -- | Pseudo-field that lets us get the 'OddsCasino's out of
211 -- xml_game_over_under.
212 xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
213 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
214
215
216 instance FromXml OddsGameXml where
217 type Db OddsGameXml = OddsGame
218 from_xml OddsGameXml{..} =
219 OddsGame {
220 db_game_id = xml_game_id,
221 db_game_date = xml_game_date,
222 db_game_time = xml_game_time,
223 db_game_away_team_rotation_number =
224 (xml_away_rotation_number xml_game_away_team),
225 db_game_home_team_rotation_number =
226 (xml_home_rotation_number xml_game_home_team) }
227
228 instance XmlImport OddsGameXml
229
230
231
232 data Odds =
233 Odds {
234 db_sport :: String,
235 db_title :: String,
236 db_line_time :: String }
237
238
239 -- | Map 'Odds' to their children 'OddsGame's.
240 --
241 data Odds_OddsGame = Odds_OddsGame
242 (DefaultKey Odds)
243 (DefaultKey OddsGame)
244
245
246 -- | This is our best guess at what occurs in the Odds_XML
247 -- documents. It looks like each consecutive set of games can
248 -- optionally have some notes appear before it. Each "note" comes as
249 -- its own <Notes>...</Notes> element.
250 --
251 -- The notes are ignored completely in the database; we only bother
252 -- with them to ensure that we're (un)pickling correctly.
253 --
254 -- We can't group the notes with a "set" of 'OddsGame's, because that
255 -- leads to ambiguity in parsing. Since we're going to ignore the
256 -- notes anyway, we just stick them with an arbitrary game. C'est la
257 -- vie.
258 --
259 data OddsGameWithNotes =
260 OddsGameWithNotes {
261 notes :: [String],
262 game :: OddsGameXml }
263 deriving (Eq, Show)
264
265 -- | The XML representation of Odds.
266 data Message =
267 Message {
268 xml_xml_file_id :: Int,
269 xml_heading :: String,
270 xml_category :: String,
271 xml_sport :: String,
272 xml_title :: String,
273 xml_line_time :: String,
274 xml_games_with_notes :: [OddsGameWithNotes],
275 xml_time_stamp :: String }
276 deriving (Eq, Show)
277
278 -- | Pseudo-field that lets us get the 'OddsGame's out of
279 -- 'xml_games_with_notes'.
280 xml_games :: Message -> [OddsGameXml]
281 xml_games m = map game (xml_games_with_notes m)
282
283
284 instance FromXml Message where
285 type Db Message = Odds
286
287 -- We don't need the key argument (from_xml_fk) since the XML type
288 -- contains more information in this case.
289 from_xml Message{..} =
290 Odds {
291 db_sport = xml_sport,
292 db_title = xml_title,
293 db_line_time = xml_line_time }
294
295 instance XmlImport Message
296
297
298
299 -- * Groundhog database schema.
300 -- | This must come before the dbimport code.
301 --
302 mkPersist tsn_codegen_config [groundhog|
303 - entity: Odds
304
305 - entity: OddsCasino
306 dbName: odds_casinos
307 constructors:
308 - name: OddsCasino
309 uniques:
310 - name: unique_odds_casino
311 type: constraint
312 fields: [casino_client_id]
313
314 - entity: OddsGameTeam
315 dbName: odds_games_teams
316 constructors:
317 - name: OddsGameTeam
318 fields:
319 - name: db_team_id
320 type: varchar(3)
321 uniques:
322 - name: unique_odds_games_team
323 type: constraint
324 fields: [db_team_id]
325
326
327 - entity: OddsGame
328 dbName: odds_games
329 constructors:
330 - name: OddsGame
331 uniques:
332 - name: unique_odds_game
333 type: constraint
334 fields: [db_game_id]
335
336 - entity: OddsGameLine
337 dbName: odds_games_lines
338
339 - entity: Odds_OddsGame
340 dbName: odds__odds_games
341 constructors:
342 - name: Odds_OddsGame
343 fields:
344 - name: odds_OddsGame0
345 dbName: odds_id
346 - name: odds_OddsGame1
347 dbName: odds_games_id
348
349 - entity: OddsGame_OddsGameTeam
350 dbName: odds_games__odds_games_teams
351 |]
352
353 instance DbImport Message where
354 dbmigrate _=
355 run_dbmigrate $ do
356 migrate (undefined :: Odds)
357 migrate (undefined :: OddsCasino)
358 migrate (undefined :: OddsGameTeam)
359 migrate (undefined :: OddsGame)
360 migrate (undefined :: Odds_OddsGame)
361 migrate (undefined :: OddsGame_OddsGameTeam)
362 migrate (undefined :: OddsGameLine)
363
364 dbimport m = do
365 -- Insert the root "odds" element and acquire its primary key (id).
366 odds_id <- insert_xml m
367
368 -- Next, we insert the home and away teams. We do this before
369 -- inserting the game itself because the game has two foreign keys
370 -- pointing to odds_games_teams.
371 forM_ (xml_games m) $ \g -> do
372 game_id <- insert_xml_or_select g
373 -- Insert a record into odds__odds_game mapping this game
374 -- to its parent in the odds table.
375 insert_ (Odds_OddsGame odds_id game_id)
376
377 -- Next to insert the home and away teams.
378 away_team_id <- insert_xml_or_select (xml_game_away_team g)
379 home_team_id <- insert_xml_or_select (xml_game_home_team g)
380
381 -- Insert a record into odds_games__odds_games_teams mapping the
382 -- home/away teams to this game. Use the full record syntax
383 -- because the types would let us mix up the home/away teams.
384 insert_ OddsGame_OddsGameTeam {
385 ogogt_odds_games_id = game_id,
386 ogogt_away_team_id = away_team_id,
387 ogogt_home_team_id = home_team_id }
388
389 -- Finaly, we insert the lines. The over/under entries for this
390 -- game and the lines for the casinos all wind up in the same
391 -- table, odds_games_lines. We can insert the over/under entries
392 -- freely with empty away/home lines:
393 forM_ (xml_game_over_under_casinos g) $ \c -> do
394 -- Start by inderting the casino.
395 ou_casino_id <- insert_xml_or_select c
396
397 -- Now add the over/under entry with the casino's id.
398 let ogl = OddsGameLine {
399 ogl_odds_games_id = game_id,
400 ogl_odds_casinos_id = ou_casino_id,
401 ogl_over_under = (xml_casino_line c),
402 ogl_away_line = Nothing,
403 ogl_home_line = Nothing }
404
405 insertByAll ogl
406
407 -- ...but then when we insert the home/away team lines, we
408 -- prefer to update the existing entry rather than overwrite it
409 -- or add a new record.
410 forM_ (xml_away_casinos $ xml_game_away_team g) $ \c ->do
411 -- insert, or more likely retrieve the existing, casino
412 a_casino_id <- insert_xml_or_select c
413
414 -- Get a Maybe Double instead of the Maybe String that's in there.
415 let away_line = home_away_line c
416
417 -- Unconditionally update that casino's away team line with ours.
418 update [Ogl_Away_Line =. away_line] $ -- WHERE
419 Ogl_Odds_Casinos_Id ==. a_casino_id
420
421 -- Repeat all that for the home team.
422 forM_ (xml_home_casinos $ xml_game_home_team g) $ \c ->do
423 h_casino_id <- insert_xml_or_select c
424 let home_line = home_away_line c
425 update [Ogl_Home_Line =. home_line] $ -- WHERE
426 Ogl_Odds_Casinos_Id ==. h_casino_id
427
428 return game_id
429
430 return ImportSucceeded
431
432 pickle_game_with_notes :: PU OddsGameWithNotes
433 pickle_game_with_notes =
434 xpWrap (from_pair, to_pair) $
435 xpPair
436 (xpList $ xpElem "Notes" xpText)
437 pickle_game
438 where
439 from_pair = uncurry OddsGameWithNotes
440 to_pair OddsGameWithNotes{..} = (notes, game)
441
442
443
444 pickle_casino :: PU OddsGameCasinoXml
445 pickle_casino =
446 xpElem "Casino" $
447 xpWrap (from_tuple, to_tuple) $
448 xpTriple
449 (xpAttr "ClientID" xpInt)
450 (xpAttr "Name" xpText)
451 (xpOption xpText)
452 where
453 from_tuple = uncurryN OddsGameCasinoXml
454 -- Use record wildcards to avoid unused field warnings.
455 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
456 xml_casino_name,
457 xml_casino_line)
458
459
460 pickle_home_team :: PU OddsGameHomeTeamXml
461 pickle_home_team =
462 xpElem "HomeTeam" $
463 xpWrap (from_tuple, to_tuple) $
464 xp5Tuple
465 (xpElem "HomeTeamID" xp_team_id)
466 (xpElem "HomeRotationNumber" xpInt)
467 (xpElem "HomeAbbr" xpText)
468 (xpElem "HomeTeamName" xpText)
469 (xpList pickle_casino)
470 where
471 from_tuple = uncurryN OddsGameHomeTeamXml
472 -- Use record wildcards to avoid unused field warnings.
473 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
474 xml_home_rotation_number,
475 xml_home_abbr,
476 xml_home_team_name,
477 xml_home_casinos)
478
479
480
481 pickle_away_team :: PU OddsGameAwayTeamXml
482 pickle_away_team =
483 xpElem "AwayTeam" $
484 xpWrap (from_tuple, to_tuple) $
485 xp5Tuple
486 (xpElem "AwayTeamID" xp_team_id)
487 (xpElem "AwayRotationNumber" xpInt)
488 (xpElem "AwayAbbr" xpText)
489 (xpElem "AwayTeamName" xpText)
490 (xpList pickle_casino)
491 where
492 from_tuple = uncurryN OddsGameAwayTeamXml
493 -- Use record wildcards to avoid unused field warnings.
494 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
495 xml_away_rotation_number,
496 xml_away_abbr,
497 xml_away_team_name,
498 xml_away_casinos)
499
500
501
502 pickle_over_under :: PU OddsGameOverUnderXml
503 pickle_over_under =
504 xpElem "Over_Under" $
505 xpWrap (to_newtype, from_newtype) $
506 xpList pickle_casino
507 where
508 from_newtype (OddsGameOverUnderXml cs) = cs
509 to_newtype = OddsGameOverUnderXml
510
511
512
513 pickle_game :: PU OddsGameXml
514 pickle_game =
515 xpElem "Game" $
516 xpWrap (from_tuple, to_tuple) $
517 xp6Tuple
518 (xpElem "GameID" xpInt)
519 (xpElem "Game_Date" xpText)
520 (xpElem "Game_Time" xpText)
521 pickle_away_team
522 pickle_home_team
523 pickle_over_under
524 where
525 from_tuple = uncurryN OddsGameXml
526 -- Use record wildcards to avoid unused field warnings.
527 to_tuple OddsGameXml{..} = (xml_game_id,
528 xml_game_date,
529 xml_game_time,
530 xml_game_away_team,
531 xml_game_home_team,
532 xml_game_over_under)
533
534
535 pickle_message :: PU Message
536 pickle_message =
537 xpElem "message" $
538 xpWrap (from_tuple, to_tuple) $
539 xp8Tuple (xpElem "XML_File_ID" xpInt)
540 (xpElem "heading" xpText)
541 (xpElem "category" xpText)
542 (xpElem "sport" xpText)
543 (xpElem "Title" xpText)
544 (xpElem "Line_Time" xpText)
545 (xpList pickle_game_with_notes)
546 (xpElem "time_stamp" xpText)
547 where
548 from_tuple = uncurryN Message
549 to_tuple m = (xml_xml_file_id m,
550 xml_heading m,
551 xml_category m,
552 xml_sport m,
553 xml_title m,
554 xml_line_time m,
555 xml_games_with_notes m,
556 xml_time_stamp m)
557
558
559
560 -- * Tasty Tests
561 odds_tests :: TestTree
562 odds_tests =
563 testGroup
564 "Odds tests"
565 [ test_pickle_of_unpickle_is_identity,
566 test_unpickle_succeeds ]
567
568
569 -- | Warning, succeess of this test does not mean that unpickling
570 -- succeeded.
571 test_pickle_of_unpickle_is_identity :: TestTree
572 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
573 [ check "pickle composed with unpickle is the identity"
574 "test/xml/Odds_XML.xml",
575
576 check "pickle composed with unpickle is the identity (non-int team_id)"
577 "test/xml/Odds_XML-noninteger-team-id.xml",
578
579 check "pickle composed with unpickle is the identity (positive(+) line)"
580 "test/xml/Odds_XML-positive-line.xml",
581
582 check "pickle composed with unpickle is the identity (large file)"
583 "test/xml/Odds_XML-largefile.xml" ]
584 where
585 check desc path = testCase desc $ do
586 (expected, actual) <- pickle_unpickle pickle_message path
587 actual @?= expected
588
589
590 test_unpickle_succeeds :: TestTree
591 test_unpickle_succeeds = testGroup "unpickle tests"
592 [ check "unpickling succeeds"
593 "test/xml/Odds_XML.xml",
594
595 check "unpickling succeeds (non-int team_id)"
596 "test/xml/Odds_XML-noninteger-team-id.xml",
597
598 check "unpickling succeeds (positive(+) line)"
599 "test/xml/Odds_XML-positive-line.xml",
600
601 check "unpickling succeeds (large file)"
602 "test/xml/Odds_XML-largefile.xml" ]
603 where
604 check desc path = testCase desc $ do
605 actual <- unpickleable path pickle_message
606 let expected = True
607 actual @?= expected