1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
10 -- | Parse TSN XML for the DTD "Odds_XML.dtd". Each document contains
11 -- a root element \<message\> that contains a bunch of other
18 -- * WARNING: these are private but exported to silence warnings
19 Odds_OddsGameConstructor(..),
20 OddsCasinoConstructor(..),
22 OddsGame_OddsGameTeamConstructor(..),
23 OddsGameConstructor(..),
24 OddsGameLineConstructor(..),
25 OddsGameTeamConstructor(..) )
29 import Control.Monad ( forM_, join )
30 import Data.Time ( UTCTime )
31 import Data.Tuple.Curry ( uncurryN )
32 import Database.Groundhog (
39 import Database.Groundhog.Core ( DefaultKey )
40 import Database.Groundhog.TH (
43 import Test.Tasty ( TestTree, testGroup )
44 import Test.Tasty.HUnit ( (@?=), testCase )
45 import Text.Read ( readMaybe )
46 import Text.XML.HXT.Core (
64 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
65 import TSN.Picklers ( xp_date, xp_time )
66 import TSN.XmlImport ( XmlImport(..) )
67 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
71 -- | The home/away lines are 'Double's, but the over/under lines are
72 -- textual. If we want to use one data type for both, we have to go
73 -- with a 'String' and then attempt to 'read' a 'Double' later when we
74 -- go to insert the thing.
76 data OddsGameCasinoXml =
78 xml_casino_client_id :: Int,
79 xml_casino_name :: String,
80 xml_casino_line :: Maybe String }
84 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
85 -- priori textual (because it might be an over/under line).
87 home_away_line :: OddsGameCasinoXml -> Maybe Double
88 home_away_line = join . (fmap readMaybe) . xml_casino_line
91 -- | The casinos should have their own table, but the lines don't
92 -- belong in that table (there should be another table joining the
93 -- casinos and the thing the lines are for together.)
95 -- We drop the 'Game' prefix because the Casinos really aren't
96 -- children of the games; the XML just makes it seem that way.
100 casino_client_id :: Int,
101 casino_name :: String }
105 instance FromXml OddsGameCasinoXml where
106 -- | The database representation of an 'OddsGameCasinoXml' is an
109 type Db OddsGameCasinoXml = OddsCasino
111 -- | We convert from XML to the database by dropping the line field.
112 from_xml OddsGameCasinoXml{..} =
114 casino_client_id = xml_casino_client_id,
115 casino_name = xml_casino_name }
117 -- | This allows us to call 'insert_xml' on an 'OddsGameCasinoXml'
118 -- without first converting it to the database representation.
119 instance XmlImport OddsGameCasinoXml
122 -- | The database representation of teams as they appear in odds
127 db_team_id :: String, -- ^ The home/away team IDs are
128 -- three characters but Postgres
129 -- imposes no performance penalty
130 -- on lengthless text fields, so
131 -- we ignore the probable upper
132 -- bound of three characters.
134 db_team_name :: String }
138 -- | The XML representation of a \<HomeTeam\>, as found in \<Game\>s.
140 data OddsGameHomeTeamXml =
141 OddsGameHomeTeamXml {
142 xml_home_team_id :: String, -- ^ The home/away team IDs
143 -- are three characters but
144 -- Postgres imposes no
145 -- performance penalty on
146 -- lengthless text fields,
147 -- so we ignore the probable
148 -- upper bound of three
150 xml_home_rotation_number :: Int,
151 xml_home_abbr :: String,
152 xml_home_team_name :: String,
153 xml_home_casinos :: [OddsGameCasinoXml] }
156 instance FromXml OddsGameHomeTeamXml where
157 -- | The database representation of an 'OddsGameHomeTeamXml' is an
160 type Db OddsGameHomeTeamXml = OddsGameTeam
162 -- | We convert from XML to the database by dropping the lines and
163 -- rotation number (which are specific to the games, not the teams
166 from_xml OddsGameHomeTeamXml{..} =
168 db_team_id = xml_home_team_id,
169 db_abbr = xml_home_abbr,
170 db_team_name = xml_home_team_name }
172 -- | XmlImport allows us to call 'insert_xml' directly on an
173 -- 'OddsGameHomeTeamXml' without explicitly converting it to the
174 -- associated database type.
176 instance XmlImport OddsGameHomeTeamXml where
179 -- | The XML representation of a \<AwayTeam\>, as found in \<Game\>s.
181 data OddsGameAwayTeamXml =
182 OddsGameAwayTeamXml {
183 xml_away_team_id :: String, -- ^ The home/away team IDs are
184 -- three characters but Postgres
185 -- imposes no performance penalty
186 -- on lengthless text fields, so
187 -- we ignore the probable upper
188 -- bound of three characters
189 xml_away_rotation_number :: Int,
190 xml_away_abbr :: String,
191 xml_away_team_name :: String,
192 xml_away_casinos :: [OddsGameCasinoXml] }
195 instance FromXml OddsGameAwayTeamXml where
196 -- | The database representation of an 'OddsGameAwayTeamXml' is an
199 type Db OddsGameAwayTeamXml = OddsGameTeam
201 -- | We convert from XML to the database by dropping the lines and
202 -- rotation number (which are specific to the games, not the teams
205 from_xml OddsGameAwayTeamXml{..} = OddsGameTeam
210 -- | XmlImport allows us to call 'insert_xml' directly on an
211 -- 'OddsGameAwayTeamXml' without explicitly converting it to the
212 -- associated database type.
214 instance XmlImport OddsGameAwayTeamXml where
217 -- | Database mapping between games and their home/away teams.
218 data OddsGame_OddsGameTeam =
219 OddsGame_OddsGameTeam {
220 ogogt_odds_games_id :: DefaultKey OddsGame,
221 ogogt_away_team_id :: DefaultKey OddsGameTeam,
222 ogogt_home_team_id :: DefaultKey OddsGameTeam }
225 -- | XML representation of the over/under. A wrapper around a bunch of
228 newtype OddsGameOverUnderXml =
229 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
233 -- | This database representation of the casino lines can't be
234 -- constructed from the one in the XML. The casinos within
235 -- Game>HomeTeam, Game>AwayTeam, and Game>Over_Under are all more or
236 -- less the same. We don't need a bajillion different tables to
237 -- store that, just one tying the casino/game pair to the three
240 -- The one small difference between the over/under casinos and the
241 -- home/away ones is that the home/away lines are all 'Double's, but
242 -- the over/under lines appear to be textual.
246 ogl_odds_games_id :: DefaultKey OddsGame,
247 ogl_odds_casinos_id :: DefaultKey OddsCasino,
248 ogl_over_under :: Maybe String,
249 ogl_away_line :: Maybe Double,
250 ogl_home_line :: Maybe Double }
253 -- | Database representation of a game. We retain the rotation number
254 -- of the home/away teams, since those are specific to the game and
260 db_game_date :: UTCTime,
261 db_game_time :: UTCTime,
262 db_game_away_team_rotation_number :: Int,
263 db_game_home_team_rotation_number :: Int }
266 -- | XML representation of a game.
271 xml_game_date :: UTCTime,
272 xml_game_time :: UTCTime,
273 xml_game_away_team :: OddsGameAwayTeamXml,
274 xml_game_home_team :: OddsGameHomeTeamXml,
275 xml_game_over_under :: OddsGameOverUnderXml }
278 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
279 -- xml_game_over_under.
281 xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
282 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
285 instance FromXml OddsGameXml where
286 -- | The database representation of an 'OddsGameXml' is an
289 type Db OddsGameXml = OddsGame
291 -- | To convert from the XML representation to the database one, we
292 -- drop the home/away teams and the casino lines, but retain the
293 -- home/away rotation numbers.
295 from_xml OddsGameXml{..} =
297 db_game_id = xml_game_id,
298 db_game_date = xml_game_date,
299 db_game_time = xml_game_time,
300 db_game_away_team_rotation_number =
301 (xml_away_rotation_number xml_game_away_team),
302 db_game_home_team_rotation_number =
303 (xml_home_rotation_number xml_game_home_team) }
305 -- | This lets us call 'insert_xml' directly on an 'OddsGameXml'
306 -- without converting it to the database representation explicitly.
308 instance XmlImport OddsGameXml
311 -- | Database and representation of the top-level Odds object (a
317 db_line_time :: String -- ^ We don't parse these as a 'UTCTime'
318 -- because their timezones are ambiguous
319 -- (and the date is less than useful when
320 -- it might be off by an hour).
324 -- | Map 'Odds' to their children 'OddsGame's.
326 data Odds_OddsGame = Odds_OddsGame
328 (DefaultKey OddsGame)
331 -- | This is our best guess at what occurs in the Odds_XML
332 -- documents. It looks like each consecutive set of games can
333 -- optionally have some notes appear before it. Each "note" comes as
334 -- its own <Notes>...</Notes> element.
336 -- The notes are ignored completely in the database; we only bother
337 -- with them to ensure that we're (un)pickling correctly.
339 -- We can't group the notes with a "set" of 'OddsGame's, because that
340 -- leads to ambiguity in parsing. Since we're going to ignore the
341 -- notes anyway, we just stick them with an arbitrary game. C'est la
344 data OddsGameWithNotes =
347 game :: OddsGameXml }
350 -- | The XML representation of 'Odds'.
353 xml_xml_file_id :: Int,
354 xml_heading :: String,
355 xml_category :: String,
358 xml_line_time :: String,
359 xml_games_with_notes :: [OddsGameWithNotes],
360 xml_time_stamp :: String }
363 -- | Pseudo-field that lets us get the 'OddsGame's out of
364 -- 'xml_games_with_notes'.
366 xml_games :: Message -> [OddsGameXml]
367 xml_games m = map game (xml_games_with_notes m)
370 instance FromXml Message where
371 -- | The database representation of a 'Message' is 'Odds'.
373 type Db Message = Odds
375 -- | To convert from the XML representation to the database one, we
376 -- just drop a bunch of fields.
378 from_xml Message{..} =
380 db_sport = xml_sport,
381 db_title = xml_title,
382 db_line_time = xml_line_time }
384 -- | This lets us call 'insert_xml' on a Message directly, without
385 -- having to convert it to its database representation explicitly.
387 instance XmlImport Message
391 -- Groundhog database schema. This must come before the DbImport
392 -- instance definition.
393 mkPersist tsn_codegen_config [groundhog|
401 - name: unique_odds_casino
403 fields: [casino_client_id]
405 - entity: OddsGameTeam
406 dbName: odds_games_teams
410 - name: unique_odds_games_team
420 - name: unique_odds_game
424 - entity: OddsGameLine
425 dbName: odds_games_lines
427 - entity: Odds_OddsGame
428 dbName: odds__odds_games
430 - name: Odds_OddsGame
432 - name: odds_OddsGame0 # Default created by mkNormalFieldName
434 - name: odds_OddsGame1 # Default created by mkNormalFieldName
435 dbName: odds_games_id
437 - entity: OddsGame_OddsGameTeam
438 dbName: odds_games__odds_games_teams
441 instance DbImport Message where
444 migrate (undefined :: Odds)
445 migrate (undefined :: OddsCasino)
446 migrate (undefined :: OddsGameTeam)
447 migrate (undefined :: OddsGame)
448 migrate (undefined :: Odds_OddsGame)
449 migrate (undefined :: OddsGame_OddsGameTeam)
450 migrate (undefined :: OddsGameLine)
453 -- Insert the root "odds" element and acquire its primary key (id).
454 odds_id <- insert_xml m
456 -- Next, we insert the home and away teams. We do this before
457 -- inserting the game itself because the game has two foreign keys
458 -- pointing to odds_games_teams.
459 forM_ (xml_games m) $ \g -> do
460 game_id <- insert_xml_or_select g
461 -- Insert a record into odds__odds_game mapping this game
462 -- to its parent in the odds table.
463 insert_ (Odds_OddsGame odds_id game_id)
465 -- Next to insert the home and away teams.
466 away_team_id <- insert_xml_or_select (xml_game_away_team g)
467 home_team_id <- insert_xml_or_select (xml_game_home_team g)
469 -- Insert a record into odds_games__odds_games_teams mapping the
470 -- home/away teams to this game. Use the full record syntax
471 -- because the types would let us mix up the home/away teams.
472 insert_ OddsGame_OddsGameTeam {
473 ogogt_odds_games_id = game_id,
474 ogogt_away_team_id = away_team_id,
475 ogogt_home_team_id = home_team_id }
477 -- Finaly, we insert the lines. The over/under entries for this
478 -- game and the lines for the casinos all wind up in the same
479 -- table, odds_games_lines. We can insert the over/under entries
480 -- freely with empty away/home lines:
481 forM_ (xml_game_over_under_casinos g) $ \c -> do
482 -- Start by inderting the casino.
483 ou_casino_id <- insert_xml_or_select c
485 -- Now add the over/under entry with the casino's id.
486 let ogl = OddsGameLine {
487 ogl_odds_games_id = game_id,
488 ogl_odds_casinos_id = ou_casino_id,
489 ogl_over_under = (xml_casino_line c),
490 ogl_away_line = Nothing,
491 ogl_home_line = Nothing }
495 -- ...but then when we insert the home/away team lines, we
496 -- prefer to update the existing entry rather than overwrite it
497 -- or add a new record.
498 forM_ (xml_away_casinos $ xml_game_away_team g) $ \c ->do
499 -- insert, or more likely retrieve the existing, casino
500 a_casino_id <- insert_xml_or_select c
502 -- Get a Maybe Double instead of the Maybe String that's in there.
503 let away_line = home_away_line c
505 -- Unconditionally update that casino's away team line with ours.
506 update [Ogl_Away_Line =. away_line] $ -- WHERE
507 Ogl_Odds_Casinos_Id ==. a_casino_id
509 -- Repeat all that for the home team.
510 forM_ (xml_home_casinos $ xml_game_home_team g) $ \c ->do
511 h_casino_id <- insert_xml_or_select c
512 let home_line = home_away_line c
513 update [Ogl_Home_Line =. home_line] $ -- WHERE
514 Ogl_Odds_Casinos_Id ==. h_casino_id
518 return ImportSucceeded
520 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
522 pickle_game_with_notes :: PU OddsGameWithNotes
523 pickle_game_with_notes =
524 xpWrap (from_pair, to_pair) $
526 (xpList $ xpElem "Notes" xpText)
529 from_pair = uncurry OddsGameWithNotes
530 to_pair OddsGameWithNotes{..} = (notes, game)
533 -- | Pickler for an 'OddsGameCasinoXml'.
535 pickle_casino :: PU OddsGameCasinoXml
538 xpWrap (from_tuple, to_tuple) $
540 (xpAttr "ClientID" xpInt)
541 (xpAttr "Name" xpText)
544 from_tuple = uncurryN OddsGameCasinoXml
545 -- Use record wildcards to avoid unused field warnings.
546 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
551 -- | Pickler for an 'OddsGameHomeTeamXml'.
553 pickle_home_team :: PU OddsGameHomeTeamXml
556 xpWrap (from_tuple, to_tuple) $
558 (xpElem "HomeTeamID" xpText)
559 (xpElem "HomeRotationNumber" xpInt)
560 (xpElem "HomeAbbr" xpText)
561 (xpElem "HomeTeamName" xpText)
562 (xpList pickle_casino)
564 from_tuple = uncurryN OddsGameHomeTeamXml
565 -- Use record wildcards to avoid unused field warnings.
566 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
567 xml_home_rotation_number,
573 -- | Pickler for an 'OddsGameAwayTeamXml'.
575 pickle_away_team :: PU OddsGameAwayTeamXml
578 xpWrap (from_tuple, to_tuple) $
580 (xpElem "AwayTeamID" xpText)
581 (xpElem "AwayRotationNumber" xpInt)
582 (xpElem "AwayAbbr" xpText)
583 (xpElem "AwayTeamName" xpText)
584 (xpList pickle_casino)
586 from_tuple = uncurryN OddsGameAwayTeamXml
587 -- Use record wildcards to avoid unused field warnings.
588 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
589 xml_away_rotation_number,
596 -- | Pickler for an 'OddsGameOverUnderXml'.
598 pickle_over_under :: PU OddsGameOverUnderXml
600 xpElem "Over_Under" $
601 xpWrap (to_newtype, from_newtype) $
604 from_newtype (OddsGameOverUnderXml cs) = cs
605 to_newtype = OddsGameOverUnderXml
608 -- | Pickler for an 'OddsGameXml'.
610 pickle_game :: PU OddsGameXml
613 xpWrap (from_tuple, to_tuple) $
615 (xpElem "GameID" xpInt)
616 (xpElem "Game_Date" xp_date)
617 (xpElem "Game_Time" xp_time)
622 from_tuple = uncurryN OddsGameXml
623 -- Use record wildcards to avoid unused field warnings.
624 to_tuple OddsGameXml{..} = (xml_game_id,
632 -- | Pickler for the top-level 'Message'.
634 pickle_message :: PU Message
637 xpWrap (from_tuple, to_tuple) $
638 xp8Tuple (xpElem "XML_File_ID" xpInt)
639 (xpElem "heading" xpText)
640 (xpElem "category" xpText)
641 (xpElem "sport" xpText)
642 (xpElem "Title" xpText)
643 (xpElem "Line_Time" xpText)
644 (xpList pickle_game_with_notes)
645 (xpElem "time_stamp" xpText)
647 from_tuple = uncurryN Message
648 to_tuple m = (xml_xml_file_id m,
654 xml_games_with_notes m,
662 -- | A list of all tests for this module.
664 odds_tests :: TestTree
668 [ test_pickle_of_unpickle_is_identity,
669 test_unpickle_succeeds ]
672 -- | If we unpickle something and then pickle it, we should wind up
673 -- with the same thing we started with. WARNING: succeess of this
674 -- test does not mean that unpickling succeeded.
676 test_pickle_of_unpickle_is_identity :: TestTree
677 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
678 [ check "pickle composed with unpickle is the identity"
679 "test/xml/Odds_XML.xml",
681 check "pickle composed with unpickle is the identity (non-int team_id)"
682 "test/xml/Odds_XML-noninteger-team-id.xml",
684 check "pickle composed with unpickle is the identity (positive(+) line)"
685 "test/xml/Odds_XML-positive-line.xml",
687 check "pickle composed with unpickle is the identity (large file)"
688 "test/xml/Odds_XML-largefile.xml" ]
690 check desc path = testCase desc $ do
691 (expected, actual) <- pickle_unpickle pickle_message path
695 -- | Make sure we can actually unpickle these things.
697 test_unpickle_succeeds :: TestTree
698 test_unpickle_succeeds = testGroup "unpickle tests"
699 [ check "unpickling succeeds"
700 "test/xml/Odds_XML.xml",
702 check "unpickling succeeds (non-int team_id)"
703 "test/xml/Odds_XML-noninteger-team-id.xml",
705 check "unpickling succeeds (positive(+) line)"
706 "test/xml/Odds_XML-positive-line.xml",
708 check "unpickling succeeds (large file)"
709 "test/xml/Odds_XML-largefile.xml" ]
711 check desc path = testCase desc $ do
712 actual <- unpickleable path pickle_message