1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
9 -- | Parse TSN XML for the DTD \"Odds_XML.dtd\". Each document
10 -- contains a root element \<message\> that contains a bunch of
11 -- other... disorganized... information.
18 -- * WARNING: these are private but exported to silence warnings
19 OddsCasinoConstructor(..),
21 OddsGameConstructor(..),
22 OddsGameLineConstructor(..) )
26 import Control.Applicative ( (<$>) )
27 import Control.Monad ( forM_, join )
28 import Data.Time ( UTCTime(..) )
29 import Data.Tuple.Curry ( uncurryN )
30 import Database.Groundhog (
38 silentMigrationLogger,
40 import Database.Groundhog.Core ( DefaultKey )
41 import Database.Groundhog.Generic ( runDbConn )
42 import Database.Groundhog.Sqlite ( withSqliteConn )
43 import Database.Groundhog.TH (
46 import Test.Tasty ( TestTree, testGroup )
47 import Test.Tasty.HUnit ( (@?=), testCase )
48 import Text.Read ( readMaybe )
49 import Text.XML.HXT.Core (
66 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
67 import TSN.Picklers ( xp_date_padded, xp_time, xp_time_stamp )
68 import TSN.Team ( FromXmlFkTeams(..), Team(..) )
69 import TSN.XmlImport ( XmlImport(..), XmlImportFkTeams(..) )
79 -- | The DTD to which this module corresponds. Used to invoke dbimport.
89 -- * OddsGameCasino/OddsGameCasinoXml
92 -- | The casinos should have their own table, but the lines don't
93 -- belong in that table (there is a separate table for
94 -- 'OddsGameLine' which associates the two).
96 -- We drop the \"Game\" prefix because the casinos really aren't
97 -- children of the games; the XML just makes it seem that way.
101 casino_client_id :: Int,
102 casino_name :: String }
106 -- | The home/away lines are 'Double's, but the over/under lines are
107 -- textual. If we want to use one data type for both, we have to go
108 -- with a 'String' and then attempt to 'read' a 'Double' later when we
109 -- go to insert the thing.
111 data OddsGameCasinoXml =
113 xml_casino_client_id :: Int,
114 xml_casino_name :: String,
115 xml_casino_line :: Maybe String }
119 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
120 -- priori textual (because it might be an over/under line).
122 home_away_line :: OddsGameCasinoXml -> Maybe Double
123 home_away_line = join . (fmap readMaybe) . xml_casino_line
127 instance ToDb OddsGameCasinoXml where
128 -- | The database representation of an 'OddsGameCasinoXml' is an
131 type Db OddsGameCasinoXml = OddsCasino
134 instance FromXml OddsGameCasinoXml where
135 -- | We convert from XML to the database by dropping the line field.
137 from_xml OddsGameCasinoXml{..} =
139 casino_client_id = xml_casino_client_id,
140 casino_name = xml_casino_name }
143 -- | This allows us to insert the XML representation 'OddsGameCasinoXml'
146 instance XmlImport OddsGameCasinoXml
149 -- * OddsGameTeamXml / OddsGameTeamStarterXml
151 -- | The XML representation of a \"starter\". It contains both an ID
152 -- and a name. The ID does not appear to be optional, but the name
153 -- can be absent. When the name is absent, the ID has always been
154 -- set to \"0\". This occurs even though the entire starter element
155 -- is optional (see 'OddsGameTeamXml' below).
157 data OddsGameTeamStarterXml =
158 OddsGameTeamStarterXml {
159 xml_starter_id :: Int,
160 xml_starter_name :: Maybe String }
164 -- | The XML representation of a \<HomeTeam\> or \<AwayTeam\>, as
165 -- found in \<Game\>s. We can't use the 'Team' representation
166 -- directly because there are some other fields we need to parse.
168 data OddsGameTeamXml =
170 xml_team_id :: String, -- ^ The home/away team IDs
171 -- are three characters but
172 -- Postgres imposes no
173 -- performance penalty on
174 -- lengthless text fields,
175 -- so we ignore the probable
176 -- upper bound of three
178 xml_team_rotation_number :: Maybe Int,
179 xml_team_abbr :: String,
180 xml_team_name :: String,
181 xml_team_starter :: Maybe OddsGameTeamStarterXml,
182 xml_team_casinos :: [OddsGameCasinoXml] }
185 instance ToDb OddsGameTeamXml where
186 -- | The database representation of an 'OddsGameTeamXml' is an
189 type Db OddsGameTeamXml = Team
191 instance FromXml OddsGameTeamXml where
192 -- | We convert from XML to the database by dropping the lines and
193 -- rotation number (which are specific to the games, not the teams
196 from_xml OddsGameTeamXml{..} =
198 team_id = xml_team_id,
199 abbreviation = Just xml_team_abbr,
200 name = Just xml_team_name }
202 -- | This allows us to insert the XML representation
203 -- 'OddsGameTeamXml' directly.
205 instance XmlImport OddsGameTeamXml where
210 -- * OddsGameOverUnderXml
212 -- | XML representation of the over/under. A wrapper around a bunch of
215 newtype OddsGameOverUnderXml =
216 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
222 -- | This database representation of the casino lines can't be
223 -- constructed from the one in the XML. The casinos within
224 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
225 -- less the same. We don't need a bajillion different tables to
226 -- store that, just one tying the casino/game pair to the three
229 -- The one small difference between the over/under casinos and the
230 -- home/away ones is that the home/away lines are all 'Double's, but
231 -- the over/under lines appear to be textual.
235 ogl_odds_games_id :: DefaultKey OddsGame,
236 ogl_odds_casinos_id :: DefaultKey OddsCasino,
237 ogl_over_under :: Maybe String,
238 ogl_away_line :: Maybe Double,
239 ogl_home_line :: Maybe Double }
242 -- * OddsGame/OddsGameXml
244 -- | Database representation of a game. We retain the rotation number
245 -- of the home/away teams, since those are specific to the game and
250 db_odds_id :: DefaultKey Odds,
251 db_away_team_id :: DefaultKey Team,
252 db_home_team_id :: DefaultKey Team,
254 db_game_time :: UTCTime, -- ^ Contains both the date and time.
255 db_away_team_rotation_number :: Maybe Int,
256 db_home_team_rotation_number :: Maybe Int,
257 db_away_team_starter_id :: Maybe Int,
258 db_away_team_starter_name :: Maybe String,
259 db_home_team_starter_id :: Maybe Int,
260 db_home_team_starter_name :: Maybe String }
263 -- | XML representation of an 'OddsGame'.
268 xml_game_date :: UTCTime, -- ^ Contains only the date
269 xml_game_time :: UTCTime, -- ^ Contains only the time
270 xml_away_team :: OddsGameTeamXml,
271 xml_home_team :: OddsGameTeamXml,
272 xml_over_under :: OddsGameOverUnderXml }
275 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
278 xml_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
279 xml_over_under_casinos = xml_casinos . xml_over_under
282 instance ToDb OddsGameXml where
283 -- | The database representation of an 'OddsGameXml' is an
286 type Db OddsGameXml = OddsGame
289 instance Child OddsGameXml where
290 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
291 -- the foreign key for 'OddsGame' points to an 'Odds'.
293 type Parent OddsGameXml = Odds
296 instance FromXmlFkTeams OddsGameXml where
297 -- | To convert from the XML representation to the database one, we
298 -- drop the casino lines, but retain the home/away rotation
299 -- numbers and the starters. The foreign keys to 'Odds' and the
300 -- home/away teams are passed in.
302 from_xml_fk_teams fk fk_away fk_home OddsGameXml{..} =
305 db_away_team_id = fk_away,
306 db_home_team_id = fk_home,
307 db_game_id = xml_game_id,
309 db_game_time = UTCTime
310 (utctDay xml_game_date) -- Take the day part from one,
311 (utctDayTime xml_game_time), -- the time from the other.
313 db_away_team_rotation_number =
314 (xml_team_rotation_number xml_away_team),
316 db_home_team_rotation_number =
317 (xml_team_rotation_number xml_home_team),
319 db_away_team_starter_id =
320 (xml_starter_id <$> xml_team_starter xml_away_team),
322 -- Sometimes the starter element is present but the name isn't,
323 -- so we combine the two maybes with join.
324 db_away_team_starter_name = join
325 (xml_starter_name <$> xml_team_starter xml_away_team),
327 db_home_team_starter_id =
328 (xml_starter_id <$> xml_team_starter xml_home_team),
330 -- Sometimes the starter element is present but the name isn't,
331 -- so we combine the two maybes with join.
332 db_home_team_starter_name = join
333 (xml_starter_name <$> xml_team_starter xml_home_team) }
336 -- | This lets us insert the XML representation 'OddsGameXml' directly.
338 instance XmlImportFkTeams OddsGameXml
341 -- * OddsGameWithNotes
343 -- | This is our best guess at what occurs in the Odds_XML
344 -- documents. It looks like each consecutive set of games can
345 -- optionally have some notes appear before it. Each \"note\" comes
346 -- as its own \<Notes\>...\</Notes\> element.
348 -- The notes are ignored completely in the database; we only bother
349 -- with them to ensure that we're (un)pickling correctly.
351 -- We can't group the notes with a \"set\" of 'OddsGame's, because
352 -- that leads to ambiguity in parsing. Since we're going to ignore
353 -- the notes anyway, we just stick them with an arbitrary
354 -- game. C'est la vie.
356 -- We have to take the same approach with the league. The
357 -- \<League_Name\> elements are sitting outside of the games, and
358 -- are presumably supposed to be interpreted in \"chronological\"
359 -- order; i.e. the current league stays the same until we see
360 -- another \<League_Name\> element. Unfortunately, that's not how
361 -- XML works. So we're forced to ignore the league in the database
362 -- and pull the same trick, pairing them with games.
364 data OddsGameWithNotes =
366 league :: Maybe String,
368 game :: OddsGameXml }
374 -- | Database representation of a 'Message'.
378 db_xml_file_id :: Int,
381 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
382 -- because their timezones are ambiguous
383 -- (and the date is less than useful when
384 -- it might be off by an hour).
385 db_time_stamp :: UTCTime }
388 -- | The XML representation of 'Odds'.
392 xml_xml_file_id :: Int,
393 xml_heading :: String,
394 xml_category :: String,
397 xml_line_time :: String,
398 xml_games_with_notes :: [OddsGameWithNotes],
399 xml_time_stamp :: UTCTime }
402 -- | Pseudo-field that lets us get the 'OddsGame's out of
403 -- 'xml_games_with_notes'.
405 xml_games :: Message -> [OddsGameXml]
406 xml_games m = map game (xml_games_with_notes m)
409 instance ToDb Message where
410 -- | The database representation of a 'Message' is 'Odds'.
412 type Db Message = Odds
414 instance FromXml Message where
415 -- | To convert from the XML representation to the database one, we
416 -- just drop a bunch of fields.
418 from_xml Message{..} =
420 db_xml_file_id = xml_xml_file_id,
421 db_sport = xml_sport,
422 db_title = xml_title,
423 db_line_time = xml_line_time,
424 db_time_stamp = xml_time_stamp }
426 -- | This lets us insert the XML representation 'Message' directly.
428 instance XmlImport Message
435 -- Groundhog database schema. This must come before the DbImport
436 -- instance definition. Don't know why.
437 mkPersist tsn_codegen_config [groundhog|
444 # Prevent multiple imports of the same message.
445 fields: [db_xml_file_id]
452 - name: unique_odds_casinos
454 fields: [casino_client_id]
464 - name: db_away_team_id
467 - name: db_home_team_id
471 - entity: OddsGameLine
472 dbName: odds_games_lines
476 - name: ogl_odds_games_id
479 - name: ogl_odds_casinos_id
485 instance DbImport Message where
488 migrate (undefined :: Team)
489 migrate (undefined :: Odds)
490 migrate (undefined :: OddsCasino)
491 migrate (undefined :: OddsGame)
492 migrate (undefined :: OddsGameLine)
495 -- Insert the root "odds" element and acquire its primary key (id).
496 odds_id <- insert_xml m
498 forM_ (xml_games m) $ \game -> do
499 -- First we insert the home and away teams.
500 away_team_id <- insert_xml_or_select (xml_away_team game)
501 home_team_id <- insert_xml_or_select (xml_home_team game)
503 -- Now insert the game, keyed to the "odds" and its teams.
504 game_id <- insert_xml_fk_teams odds_id away_team_id home_team_id game
506 -- Finally, we insert the lines. The over/under entries for this
507 -- game and the lines for the casinos all wind up in the same
508 -- table, odds_games_lines. We can insert the over/under entries
509 -- freely with empty away/home lines:
510 forM_ (xml_over_under_casinos game) $ \c -> do
511 -- Start by inderting the casino.
512 ou_casino_id <- insert_xml_or_select c
514 -- Now add the over/under entry with the casino's id.
515 let ogl = OddsGameLine {
516 ogl_odds_games_id = game_id,
517 ogl_odds_casinos_id = ou_casino_id,
518 ogl_over_under = (xml_casino_line c),
519 ogl_away_line = Nothing,
520 ogl_home_line = Nothing }
524 -- ...but then when we insert the home/away team lines, we
525 -- prefer to update the existing entry rather than overwrite it
526 -- or add a new record.
527 forM_ (xml_team_casinos $ xml_away_team game) $ \c -> do
528 -- insert, or more likely retrieve the existing, casino
529 a_casino_id <- insert_xml_or_select c
531 -- Get a Maybe Double instead of the Maybe String that's in there.
532 let away_line = home_away_line c
534 -- Unconditionally update that casino's away team line with ours.
535 update [Ogl_Away_Line =. away_line] $ -- WHERE
536 Ogl_Odds_Casinos_Id ==. a_casino_id
538 -- Repeat all that for the home team.
539 forM_ (xml_team_casinos $ xml_home_team game) $ \c ->do
540 h_casino_id <- insert_xml_or_select c
541 let home_line = home_away_line c
542 update [Ogl_Home_Line =. home_line] $ -- WHERE
543 Ogl_Odds_Casinos_Id ==. h_casino_id
547 return ImportSucceeded
554 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
556 pickle_game_with_notes :: PU OddsGameWithNotes
557 pickle_game_with_notes =
558 xpWrap (from_pair, to_pair) $
560 (xpOption $ xpElem "League_Name" xpText)
561 (xpList $ xpElem "Notes" xpText)
564 from_pair = uncurryN OddsGameWithNotes
565 to_pair OddsGameWithNotes{..} = (league, notes, game)
568 -- | Pickler for an 'OddsGameCasinoXml'.
570 pickle_casino :: PU OddsGameCasinoXml
573 xpWrap (from_tuple, to_tuple) $
575 (xpAttr "ClientID" xpInt)
576 (xpAttr "Name" xpText)
579 from_tuple = uncurryN OddsGameCasinoXml
580 -- Use record wildcards to avoid unused field warnings.
581 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
586 -- | Pickler for an 'OddsGameTeamXml'.
588 pickle_home_team :: PU OddsGameTeamXml
591 xpWrap (from_tuple, to_tuple) $
593 (xpElem "HomeTeamID" xpText)
594 (xpElem "HomeRotationNumber" (xpOption xpInt))
595 (xpElem "HomeAbbr" xpText)
596 (xpElem "HomeTeamName" xpText)
597 (xpOption pickle_home_starter)
598 (xpList pickle_casino)
600 from_tuple = uncurryN OddsGameTeamXml
602 -- Use record wildcards to avoid unused field warnings.
603 to_tuple OddsGameTeamXml{..} = (xml_team_id,
604 xml_team_rotation_number,
611 -- | Portion of the 'OddsGameTeamStarterXml' pickler that is not
612 -- specific to the home/away teams.
614 pickle_starter :: PU OddsGameTeamStarterXml
616 xpWrap (from_tuple, to_tuple) $
617 xpPair (xpAttr "ID" xpInt) (xpOption xpText)
619 from_tuple = uncurry OddsGameTeamStarterXml
620 to_tuple OddsGameTeamStarterXml{..} = (xml_starter_id,
623 -- | Pickler for an home team 'OddsGameTeamStarterXml'
625 pickle_home_starter :: PU OddsGameTeamStarterXml
626 pickle_home_starter = xpElem "HStarter" pickle_starter
629 -- | Pickler for an away team 'OddsGameTeamStarterXml'
631 pickle_away_starter :: PU OddsGameTeamStarterXml
632 pickle_away_starter = xpElem "AStarter" pickle_starter
636 -- | Pickler for an 'OddsGameTeamXml'.
638 pickle_away_team :: PU OddsGameTeamXml
641 xpWrap (from_tuple, to_tuple) $
643 (xpElem "AwayTeamID" xpText)
644 (xpElem "AwayRotationNumber" (xpOption xpInt))
645 (xpElem "AwayAbbr" xpText)
646 (xpElem "AwayTeamName" xpText)
647 (xpOption pickle_away_starter)
648 (xpList pickle_casino)
650 from_tuple = uncurryN OddsGameTeamXml
652 -- Use record wildcards to avoid unused field warnings.
653 to_tuple OddsGameTeamXml{..} = (xml_team_id,
654 xml_team_rotation_number,
662 -- | Pickler for an 'OddsGameOverUnderXml'.
664 pickle_over_under :: PU OddsGameOverUnderXml
666 xpElem "Over_Under" $
667 xpWrap (to_newtype, from_newtype) $
670 from_newtype (OddsGameOverUnderXml cs) = cs
671 to_newtype = OddsGameOverUnderXml
674 -- | Pickler for an 'OddsGameXml'.
676 pickle_game :: PU OddsGameXml
679 xpWrap (from_tuple, to_tuple) $
681 (xpElem "GameID" xpInt)
682 (xpElem "Game_Date" xp_date_padded)
683 (xpElem "Game_Time" xp_time)
688 from_tuple = uncurryN OddsGameXml
689 -- Use record wildcards to avoid unused field warnings.
690 to_tuple OddsGameXml{..} = (xml_game_id,
698 -- | Pickler for the top-level 'Message'.
700 pickle_message :: PU Message
703 xpWrap (from_tuple, to_tuple) $
704 xp8Tuple (xpElem "XML_File_ID" xpInt)
705 (xpElem "heading" xpText)
706 (xpElem "category" xpText)
707 (xpElem "sport" xpText)
708 (xpElem "Title" xpText)
709 (xpElem "Line_Time" xpText)
710 (xpList pickle_game_with_notes)
711 (xpElem "time_stamp" xp_time_stamp)
713 from_tuple = uncurryN Message
714 to_tuple m = (xml_xml_file_id m,
720 xml_games_with_notes m,
728 -- | A list of all tests for this module.
730 odds_tests :: TestTree
734 [ test_on_delete_cascade,
735 test_pickle_of_unpickle_is_identity,
736 test_unpickle_succeeds ]
739 -- | If we unpickle something and then pickle it, we should wind up
740 -- with the same thing we started with. WARNING: success of this
741 -- test does not mean that unpickling succeeded.
743 test_pickle_of_unpickle_is_identity :: TestTree
744 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
745 [ check "pickle composed with unpickle is the identity"
746 "test/xml/Odds_XML.xml",
748 check "pickle composed with unpickle is the identity (non-int team_id)"
749 "test/xml/Odds_XML-noninteger-team-id.xml",
751 check "pickle composed with unpickle is the identity (positive(+) line)"
752 "test/xml/Odds_XML-positive-line.xml",
754 check "pickle composed with unpickle is the identity (large file)"
755 "test/xml/Odds_XML-largefile.xml",
757 check "pickle composed with unpickle is the identity (league name)"
758 "test/xml/Odds_XML-league-name.xml",
760 check "pickle composed with unpickle is the identity (missing starters)"
761 "test/xml/Odds_XML-missing-starters.xml" ]
763 check desc path = testCase desc $ do
764 (expected, actual) <- pickle_unpickle pickle_message path
768 -- | Make sure we can actually unpickle these things.
770 test_unpickle_succeeds :: TestTree
771 test_unpickle_succeeds = testGroup "unpickle tests"
772 [ check "unpickling succeeds"
773 "test/xml/Odds_XML.xml",
775 check "unpickling succeeds (non-int team_id)"
776 "test/xml/Odds_XML-noninteger-team-id.xml",
778 check "unpickling succeeds (positive(+) line)"
779 "test/xml/Odds_XML-positive-line.xml",
781 check "unpickling succeeds (large file)"
782 "test/xml/Odds_XML-largefile.xml",
784 check "unpickling succeeds (league name)"
785 "test/xml/Odds_XML-league-name.xml",
787 check "unpickling succeeds (missing starters)"
788 "test/xml/Odds_XML-missing-starters.xml" ]
790 check desc path = testCase desc $ do
791 actual <- unpickleable path pickle_message
796 -- | Make sure everything gets deleted when we delete the top-level
797 -- record. The casinos and teams should be left behind.
799 test_on_delete_cascade :: TestTree
800 test_on_delete_cascade = testGroup "cascading delete tests"
801 [ check "deleting odds deletes its children"
802 "test/xml/Odds_XML.xml"
803 13 -- 5 casinos, 8 teams
806 check "deleting odds deletes its children (non-int team_id)"
807 "test/xml/Odds_XML-noninteger-team-id.xml"
808 51 -- 5 casinos, 46 teams
811 check "deleting odds deleted its children (positive(+) line)"
812 "test/xml/Odds_XML-positive-line.xml"
813 17 -- 5 casinos, 12 teams
816 check "deleting odds deleted its children (large file)"
817 "test/xml/Odds_XML-largefile.xml"
818 189 -- 5 casinos, 184 teams
820 check "deleting odds deleted its children (league name)"
821 "test/xml/Odds_XML-league-name.xml"
822 35 -- 5 casinos, 30 teams
824 check "deleting odds deleted its children (missing starters)"
825 "test/xml/Odds_XML-missing-starters.xml"
826 7 -- 5 casinos, 2 teams
829 check desc path expected = testCase desc $ do
830 odds <- unsafe_unpickle path pickle_message
831 let a = undefined :: Team
832 let b = undefined :: Odds
833 let c = undefined :: OddsCasino
834 let d = undefined :: OddsGame
835 let e = undefined :: OddsGameLine
836 actual <- withSqliteConn ":memory:" $ runDbConn $ do
837 runMigration silentMigrationLogger $ do
845 count_a <- countAll a
846 count_b <- countAll b
847 count_c <- countAll c
848 count_d <- countAll d
849 count_e <- countAll e
850 return $ sum [count_a, count_b, count_c,