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 OddsGame_TeamConstructor(..),
22 OddsGameConstructor(..),
23 OddsGameLineConstructor(..) )
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 ( Team(..) )
69 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
80 -- | The DTD to which this module corresponds. Used to invoke dbimport.
90 -- * OddsGameCasino/OddsGameCasinoXml
93 -- | The casinos should have their own table, but the lines don't
94 -- belong in that table (there is a separate table for
95 -- 'OddsGameLine' which associates the two).
97 -- We drop the \"Game\" prefix because the casinos really aren't
98 -- children of the games; the XML just makes it seem that way.
102 casino_client_id :: Int,
103 casino_name :: String }
107 -- | The home/away lines are 'Double's, but the over/under lines are
108 -- textual. If we want to use one data type for both, we have to go
109 -- with a 'String' and then attempt to 'read' a 'Double' later when we
110 -- go to insert the thing.
112 data OddsGameCasinoXml =
114 xml_casino_client_id :: Int,
115 xml_casino_name :: String,
116 xml_casino_line :: Maybe String }
120 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
121 -- priori textual (because it might be an over/under line).
123 home_away_line :: OddsGameCasinoXml -> Maybe Double
124 home_away_line = join . (fmap readMaybe) . xml_casino_line
128 instance ToDb OddsGameCasinoXml where
129 -- | The database representation of an 'OddsGameCasinoXml' is an
132 type Db OddsGameCasinoXml = OddsCasino
135 instance FromXml OddsGameCasinoXml where
136 -- | We convert from XML to the database by dropping the line field.
138 from_xml OddsGameCasinoXml{..} =
140 casino_client_id = xml_casino_client_id,
141 casino_name = xml_casino_name }
144 -- | This allows us to insert the XML representation 'OddsGameCasinoXml'
147 instance XmlImport OddsGameCasinoXml
150 -- * OddsGameHomeTeamXml / OddsGameAwayTeamXml
152 -- | The XML representation of a \<HomeTeam\>, as found in \<Game\>s.
153 -- This is basically the same as 'OddsGameAwayTeamXml', but the two
154 -- types have different picklers.
156 -- The starter id/name could perhaps be combined into an embedded
157 -- type, but can you make an entire embedded type optional with
158 -- Maybe? I doubt it works.
160 data OddsGameHomeTeamXml =
161 OddsGameHomeTeamXml {
162 xml_home_team_id :: String, -- ^ The home/away team IDs
163 -- are three characters but
164 -- Postgres imposes no
165 -- performance penalty on
166 -- lengthless text fields,
167 -- so we ignore the probable
168 -- upper bound of three
170 xml_home_team_rotation_number :: Int,
171 xml_home_team_abbr :: String,
172 xml_home_team_name :: String,
173 xml_home_team_starter :: Maybe (Int, String), -- ^ (id, name)
174 xml_home_team_casinos :: [OddsGameCasinoXml] }
177 instance ToDb OddsGameHomeTeamXml where
178 -- | The database representation of an 'OddsGameHomeTeamXml' is an
181 type Db OddsGameHomeTeamXml = Team
183 instance FromXml OddsGameHomeTeamXml where
184 -- | We convert from XML to the database by dropping the lines and
185 -- rotation number (which are specific to the games, not the teams
188 from_xml OddsGameHomeTeamXml{..} =
190 team_id = xml_home_team_id,
191 abbreviation = Just xml_home_team_abbr,
192 name = Just xml_home_team_name }
194 -- | This allows us to insert the XML representation
195 -- 'OddsGameHomeTeamXml' directly.
197 instance XmlImport OddsGameHomeTeamXml where
201 -- | The XML representation of a \<AwayTeam\>, as found in \<Game\>s.
202 -- This is basically the same as 'OddsGameHomeTeamXml', but the two
203 -- types have different picklers.
205 data OddsGameAwayTeamXml =
206 OddsGameAwayTeamXml {
207 xml_away_team_id :: String, -- ^ The home/away team IDs are
208 -- three characters but Postgres
209 -- imposes no performance penalty
210 -- on lengthless text fields, so
211 -- we ignore the probable upper
212 -- bound of three characters
213 xml_away_team_rotation_number :: Int,
214 xml_away_team_abbr :: String,
215 xml_away_team_name :: String,
216 xml_away_team_starter :: Maybe (Int, String), -- ^ (id, name)
217 xml_away_team_casinos :: [OddsGameCasinoXml] }
220 instance ToDb OddsGameAwayTeamXml where
221 -- | The database representation of an 'OddsGameAwayTeamXml' is a
224 type Db OddsGameAwayTeamXml = Team
226 instance FromXml OddsGameAwayTeamXml where
227 -- | We convert from XML to the database by dropping the lines and
228 -- rotation number (which are specific to the games, not the teams
231 from_xml OddsGameAwayTeamXml{..} = Team
233 (Just xml_away_team_abbr)
234 (Just xml_away_team_name)
236 -- | This allows us to insert the XML representation
237 -- 'OddsGameAwayTeamXml' directly.
239 instance XmlImport OddsGameAwayTeamXml where
242 -- * OddsGame_OddsGameTeam
244 -- | Database mapping between games and their home/away teams.
248 ogt_odds_games_id :: DefaultKey OddsGame,
249 ogt_away_team_id :: DefaultKey Team,
250 ogt_home_team_id :: DefaultKey Team }
253 -- * OddsGameOverUnderXml
255 -- | XML representation of the over/under. A wrapper around a bunch of
258 newtype OddsGameOverUnderXml =
259 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
265 -- | This database representation of the casino lines can't be
266 -- constructed from the one in the XML. The casinos within
267 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
268 -- less the same. We don't need a bajillion different tables to
269 -- store that, just one tying the casino/game pair to the three
272 -- The one small difference between the over/under casinos and the
273 -- home/away ones is that the home/away lines are all 'Double's, but
274 -- the over/under lines appear to be textual.
278 ogl_odds_games_id :: DefaultKey OddsGame,
279 ogl_odds_casinos_id :: DefaultKey OddsCasino,
280 ogl_over_under :: Maybe String,
281 ogl_away_line :: Maybe Double,
282 ogl_home_line :: Maybe Double }
285 -- * OddsGame/OddsGameXml
287 -- | Database representation of a game. We retain the rotation number
288 -- of the home/away teams, since those are specific to the game and
293 db_odds_id :: DefaultKey Odds,
295 db_game_time :: UTCTime, -- ^ Contains both the date and time.
296 db_away_team_rotation_number :: Int,
297 db_home_team_rotation_number :: Int,
298 db_away_team_starter_id :: Maybe Int,
299 db_away_team_starter_name :: Maybe String,
300 db_home_team_starter_id :: Maybe Int,
301 db_home_team_starter_name :: Maybe String }
304 -- | XML representation of an 'OddsGame'.
309 xml_game_date :: UTCTime, -- ^ Contains only the date
310 xml_game_time :: UTCTime, -- ^ Contains only the time
311 xml_away_team :: OddsGameAwayTeamXml,
312 xml_home_team :: OddsGameHomeTeamXml,
313 xml_over_under :: OddsGameOverUnderXml }
316 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
319 xml_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
320 xml_over_under_casinos = xml_casinos . xml_over_under
323 instance ToDb OddsGameXml where
324 -- | The database representation of an 'OddsGameXml' is an
327 type Db OddsGameXml = OddsGame
330 instance Child OddsGameXml where
331 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
332 -- the foreign key for 'OddsGame' points to an 'Odds'.
334 type Parent OddsGameXml = Odds
337 instance FromXmlFk OddsGameXml where
338 -- | To convert from the XML representation to the database one, we
339 -- drop the home/away teams and the casino lines, but retain the
340 -- home/away rotation numbers and the starters.
342 from_xml_fk fk OddsGameXml{..} =
345 db_game_id = xml_game_id,
347 db_game_time = UTCTime
348 (utctDay xml_game_date) -- Take the day part from one,
349 (utctDayTime xml_game_time), -- the time from the other.
351 db_away_team_rotation_number =
352 (xml_away_team_rotation_number xml_away_team),
354 db_home_team_rotation_number =
355 (xml_home_team_rotation_number xml_home_team),
357 db_away_team_starter_id =
358 (fmap fst $ xml_away_team_starter xml_away_team),
360 db_away_team_starter_name =
361 (fmap snd $ xml_away_team_starter xml_away_team),
363 db_home_team_starter_id =
364 (fmap fst $ xml_home_team_starter xml_home_team),
366 db_home_team_starter_name =
367 (fmap snd $ xml_home_team_starter xml_home_team) }
370 -- | This lets us insert the XML representation 'OddsGameXml' directly.
372 instance XmlImportFk OddsGameXml
375 -- * OddsGameWithNotes
377 -- | This is our best guess at what occurs in the Odds_XML
378 -- documents. It looks like each consecutive set of games can
379 -- optionally have some notes appear before it. Each \"note\" comes
380 -- as its own \<Notes\>...\</Notes\> element.
382 -- The notes are ignored completely in the database; we only bother
383 -- with them to ensure that we're (un)pickling correctly.
385 -- We can't group the notes with a \"set\" of 'OddsGame's, because
386 -- that leads to ambiguity in parsing. Since we're going to ignore
387 -- the notes anyway, we just stick them with an arbitrary
388 -- game. C'est la vie.
390 -- We have to take the same approach with the league. The
391 -- \<League_Name\> elements are sitting outside of the games, and
392 -- are presumably supposed to be interpreted in \"chronological\"
393 -- order; i.e. the current league stays the same until we see
394 -- another \<League_Name\> element. Unfortunately, that's not how
395 -- XML works. So we're forced to ignore the league in the database
396 -- and pull the same trick, pairing them with games.
398 data OddsGameWithNotes =
400 league :: Maybe String,
402 game :: OddsGameXml }
408 -- | Database representation of a 'Message'.
412 db_xml_file_id :: Int,
415 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
416 -- because their timezones are ambiguous
417 -- (and the date is less than useful when
418 -- it might be off by an hour).
419 db_time_stamp :: UTCTime }
422 -- | The XML representation of 'Odds'.
426 xml_xml_file_id :: Int,
427 xml_heading :: String,
428 xml_category :: String,
431 xml_line_time :: String,
432 xml_games_with_notes :: [OddsGameWithNotes],
433 xml_time_stamp :: UTCTime }
436 -- | Pseudo-field that lets us get the 'OddsGame's out of
437 -- 'xml_games_with_notes'.
439 xml_games :: Message -> [OddsGameXml]
440 xml_games m = map game (xml_games_with_notes m)
443 instance ToDb Message where
444 -- | The database representation of a 'Message' is 'Odds'.
446 type Db Message = Odds
448 instance FromXml Message where
449 -- | To convert from the XML representation to the database one, we
450 -- just drop a bunch of fields.
452 from_xml Message{..} =
454 db_xml_file_id = xml_xml_file_id,
455 db_sport = xml_sport,
456 db_title = xml_title,
457 db_line_time = xml_line_time,
458 db_time_stamp = xml_time_stamp }
460 -- | This lets us insert the XML representation 'Message' directly.
462 instance XmlImport Message
469 -- Groundhog database schema. This must come before the DbImport
470 -- instance definition. Don't know why.
471 mkPersist tsn_codegen_config [groundhog|
478 # Prevent multiple imports of the same message.
479 fields: [db_xml_file_id]
486 - name: unique_odds_casino
488 fields: [casino_client_id]
499 - entity: OddsGameLine
500 dbName: odds_games_lines
504 - name: ogl_odds_games_id
507 - name: ogl_odds_casinos_id
511 - entity: OddsGame_Team
512 dbName: odds_games__teams
514 - name: OddsGame_Team
516 - name: ogt_odds_games_id
519 - name: ogt_away_team_id
522 - name: ogt_home_team_id
527 instance DbImport Message where
530 migrate (undefined :: Team)
531 migrate (undefined :: Odds)
532 migrate (undefined :: OddsCasino)
533 migrate (undefined :: OddsGame)
534 migrate (undefined :: OddsGame_Team)
535 migrate (undefined :: OddsGameLine)
538 -- Insert the root "odds" element and acquire its primary key (id).
539 odds_id <- insert_xml m
541 forM_ (xml_games m) $ \g -> do
542 -- First insert the game, keyed to the "odds",
543 game_id <- insert_xml_fk odds_id g
545 -- Next, we insert the home and away teams.
546 away_team_id <- insert_xml_or_select (xml_away_team g)
547 home_team_id <- insert_xml_or_select (xml_home_team g)
549 -- Insert a record into odds_games__teams mapping the
550 -- home/away teams to this game. Use the full record syntax
551 -- because the types would let us mix up the home/away teams.
552 insert_ OddsGame_Team {
553 ogt_odds_games_id = game_id,
554 ogt_away_team_id = away_team_id,
555 ogt_home_team_id = home_team_id }
557 -- Finaly, we insert the lines. The over/under entries for this
558 -- game and the lines for the casinos all wind up in the same
559 -- table, odds_games_lines. We can insert the over/under entries
560 -- freely with empty away/home lines:
561 forM_ (xml_over_under_casinos g) $ \c -> do
562 -- Start by inderting the casino.
563 ou_casino_id <- insert_xml_or_select c
565 -- Now add the over/under entry with the casino's id.
566 let ogl = OddsGameLine {
567 ogl_odds_games_id = game_id,
568 ogl_odds_casinos_id = ou_casino_id,
569 ogl_over_under = (xml_casino_line c),
570 ogl_away_line = Nothing,
571 ogl_home_line = Nothing }
575 -- ...but then when we insert the home/away team lines, we
576 -- prefer to update the existing entry rather than overwrite it
577 -- or add a new record.
578 forM_ (xml_away_team_casinos $ xml_away_team g) $ \c -> do
579 -- insert, or more likely retrieve the existing, casino
580 a_casino_id <- insert_xml_or_select c
582 -- Get a Maybe Double instead of the Maybe String that's in there.
583 let away_line = home_away_line c
585 -- Unconditionally update that casino's away team line with ours.
586 update [Ogl_Away_Line =. away_line] $ -- WHERE
587 Ogl_Odds_Casinos_Id ==. a_casino_id
589 -- Repeat all that for the home team.
590 forM_ (xml_home_team_casinos $ xml_home_team g) $ \c ->do
591 h_casino_id <- insert_xml_or_select c
592 let home_line = home_away_line c
593 update [Ogl_Home_Line =. home_line] $ -- WHERE
594 Ogl_Odds_Casinos_Id ==. h_casino_id
598 return ImportSucceeded
605 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
607 pickle_game_with_notes :: PU OddsGameWithNotes
608 pickle_game_with_notes =
609 xpWrap (from_pair, to_pair) $
611 (xpOption $ xpElem "League_Name" xpText)
612 (xpList $ xpElem "Notes" xpText)
615 from_pair = uncurryN OddsGameWithNotes
616 to_pair OddsGameWithNotes{..} = (league, notes, game)
619 -- | Pickler for an 'OddsGameCasinoXml'.
621 pickle_casino :: PU OddsGameCasinoXml
624 xpWrap (from_tuple, to_tuple) $
626 (xpAttr "ClientID" xpInt)
627 (xpAttr "Name" xpText)
630 from_tuple = uncurryN OddsGameCasinoXml
631 -- Use record wildcards to avoid unused field warnings.
632 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
637 -- | Pickler for an 'OddsGameHomeTeamXml'.
639 pickle_home_team :: PU OddsGameHomeTeamXml
642 xpWrap (from_tuple, to_tuple) $
644 (xpElem "HomeTeamID" xpText)
645 (xpElem "HomeRotationNumber" xpInt)
646 (xpElem "HomeAbbr" xpText)
647 (xpElem "HomeTeamName" xpText)
648 (-- This is an ugly way to get both the HStarter ID attribute
650 xpOption (xpElem "HStarter" $ xpPair (xpAttr "ID" xpInt) xpText))
651 (xpList pickle_casino)
653 from_tuple = uncurryN OddsGameHomeTeamXml
655 -- Use record wildcards to avoid unused field warnings.
656 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
657 xml_home_team_rotation_number,
660 xml_home_team_starter,
661 xml_home_team_casinos)
663 -- | Pickler for an 'OddsGameAwayTeamXml'.
665 pickle_away_team :: PU OddsGameAwayTeamXml
668 xpWrap (from_tuple, to_tuple) $
670 (xpElem "AwayTeamID" xpText)
671 (xpElem "AwayRotationNumber" xpInt)
672 (xpElem "AwayAbbr" xpText)
673 (xpElem "AwayTeamName" xpText)
674 (-- This is an ugly way to get both the AStarter ID attribute
676 xpOption (xpElem "AStarter" $ xpPair (xpAttr "ID" xpInt) xpText))
677 (xpList pickle_casino)
679 from_tuple = uncurryN OddsGameAwayTeamXml
681 -- Use record wildcards to avoid unused field warnings.
682 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
683 xml_away_team_rotation_number,
686 xml_away_team_starter,
687 xml_away_team_casinos)
691 -- | Pickler for an 'OddsGameOverUnderXml'.
693 pickle_over_under :: PU OddsGameOverUnderXml
695 xpElem "Over_Under" $
696 xpWrap (to_newtype, from_newtype) $
699 from_newtype (OddsGameOverUnderXml cs) = cs
700 to_newtype = OddsGameOverUnderXml
703 -- | Pickler for an 'OddsGameXml'.
705 pickle_game :: PU OddsGameXml
708 xpWrap (from_tuple, to_tuple) $
710 (xpElem "GameID" xpInt)
711 (xpElem "Game_Date" xp_date_padded)
712 (xpElem "Game_Time" xp_time)
717 from_tuple = uncurryN OddsGameXml
718 -- Use record wildcards to avoid unused field warnings.
719 to_tuple OddsGameXml{..} = (xml_game_id,
727 -- | Pickler for the top-level 'Message'.
729 pickle_message :: PU Message
732 xpWrap (from_tuple, to_tuple) $
733 xp8Tuple (xpElem "XML_File_ID" xpInt)
734 (xpElem "heading" xpText)
735 (xpElem "category" xpText)
736 (xpElem "sport" xpText)
737 (xpElem "Title" xpText)
738 (xpElem "Line_Time" xpText)
739 (xpList pickle_game_with_notes)
740 (xpElem "time_stamp" xp_time_stamp)
742 from_tuple = uncurryN Message
743 to_tuple m = (xml_xml_file_id m,
749 xml_games_with_notes m,
757 -- | A list of all tests for this module.
759 odds_tests :: TestTree
763 [ test_on_delete_cascade,
764 test_pickle_of_unpickle_is_identity,
765 test_unpickle_succeeds ]
768 -- | If we unpickle something and then pickle it, we should wind up
769 -- with the same thing we started with. WARNING: success of this
770 -- test does not mean that unpickling succeeded.
772 test_pickle_of_unpickle_is_identity :: TestTree
773 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
774 [ check "pickle composed with unpickle is the identity"
775 "test/xml/Odds_XML.xml",
777 check "pickle composed with unpickle is the identity (non-int team_id)"
778 "test/xml/Odds_XML-noninteger-team-id.xml",
780 check "pickle composed with unpickle is the identity (positive(+) line)"
781 "test/xml/Odds_XML-positive-line.xml",
783 check "pickle composed with unpickle is the identity (large file)"
784 "test/xml/Odds_XML-largefile.xml",
786 check "pickle composed with unpickle is the identity (league name)"
787 "test/xml/Odds_XML-league-name.xml" ]
789 check desc path = testCase desc $ do
790 (expected, actual) <- pickle_unpickle pickle_message path
794 -- | Make sure we can actually unpickle these things.
796 test_unpickle_succeeds :: TestTree
797 test_unpickle_succeeds = testGroup "unpickle tests"
798 [ check "unpickling succeeds"
799 "test/xml/Odds_XML.xml",
801 check "unpickling succeeds (non-int team_id)"
802 "test/xml/Odds_XML-noninteger-team-id.xml",
804 check "unpickling succeeds (positive(+) line)"
805 "test/xml/Odds_XML-positive-line.xml",
807 check "unpickling succeeds (large file)"
808 "test/xml/Odds_XML-largefile.xml",
810 check "unpickling succeeds (league name)"
811 "test/xml/Odds_XML-league-name.xml" ]
813 check desc path = testCase desc $ do
814 actual <- unpickleable path pickle_message
819 -- | Make sure everything gets deleted when we delete the top-level
820 -- record. The casinos and teams should be left behind.
822 test_on_delete_cascade :: TestTree
823 test_on_delete_cascade = testGroup "cascading delete tests"
824 [ check "deleting odds deletes its children"
825 "test/xml/Odds_XML.xml"
826 13 -- 5 casinos, 8 teams
829 check "deleting odds deletes its children (non-int team_id)"
830 "test/xml/Odds_XML-noninteger-team-id.xml"
831 51 -- 5 casinos, 46 teams
834 check "deleting odds deleted its children (positive(+) line)"
835 "test/xml/Odds_XML-positive-line.xml"
836 17 -- 5 casinos, 12 teams
839 check "deleting odds deleted its children (large file)"
840 "test/xml/Odds_XML-largefile.xml"
841 189 -- 5 casinos, 184 teams
843 check "deleting odds deleted its children (league name)"
844 "test/xml/Odds_XML-league-name.xml"
845 35 -- 5 casinos, 30 teams
848 check desc path expected = testCase desc $ do
849 odds <- unsafe_unpickle path pickle_message
850 let a = undefined :: Team
851 let b = undefined :: Odds
852 let c = undefined :: OddsCasino
853 let d = undefined :: OddsGame
854 let e = undefined :: OddsGame_Team
855 let f = undefined :: OddsGameLine
856 actual <- withSqliteConn ":memory:" $ runDbConn $ do
857 runMigration silentMigrationLogger $ do
866 count_a <- countAll a
867 count_b <- countAll b
868 count_c <- countAll c
869 count_d <- countAll d
870 count_e <- countAll e
871 count_f <- countAll f
872 return $ sum [count_a, count_b, count_c,
873 count_d, count_e, count_f ]