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 ( Team(..) )
69 import TSN.XmlImport ( XmlImport(..), XmlImportFkTeams(..) )
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 -- * OddsGameTeamXml / OddsGameTeamStarterXml
152 -- | The XML representation of a \"starter\". It contains both an ID
153 -- and a name. The ID does not appear to be optional, but the name
154 -- can be absent. When the name is absent, the ID has always been
155 -- set to \"0\". This occurs even though the entire starter element
156 -- is optional (see 'OddsGameTeamXml' below).
158 data OddsGameTeamStarterXml =
159 OddsGameTeamStarterXml {
160 xml_starter_id :: Int,
161 xml_starter_name :: Maybe String }
165 -- | The XML representation of a \<HomeTeam\> or \<AwayTeam\>, as
166 -- found in \<Game\>s. We can't use the 'Team' representation
167 -- directly because there are some other fields we need to parse.
169 data OddsGameTeamXml =
171 xml_team_id :: String, -- ^ The home/away team IDs
172 -- are three characters but
173 -- Postgres imposes no
174 -- performance penalty on
175 -- lengthless text fields,
176 -- so we ignore the probable
177 -- upper bound of three
179 xml_team_rotation_number :: Maybe Int,
180 xml_team_abbr :: String,
181 xml_team_name :: String,
182 xml_team_starter :: Maybe OddsGameTeamStarterXml,
183 xml_team_casinos :: [OddsGameCasinoXml] }
186 instance ToDb OddsGameTeamXml where
187 -- | The database representation of an 'OddsGameTeamXml' is an
190 type Db OddsGameTeamXml = Team
192 instance FromXml OddsGameTeamXml where
193 -- | We convert from XML to the database by dropping the lines and
194 -- rotation number (which are specific to the games, not the teams
197 from_xml OddsGameTeamXml{..} =
199 team_id = xml_team_id,
200 abbreviation = Just xml_team_abbr,
201 name = Just xml_team_name }
203 -- | This allows us to insert the XML representation
204 -- 'OddsGameTeamXml' directly.
206 instance XmlImport OddsGameTeamXml where
211 -- * OddsGameOverUnderXml
213 -- | XML representation of the over/under. A wrapper around a bunch of
216 newtype OddsGameOverUnderXml =
217 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
223 -- | This database representation of the casino lines can't be
224 -- constructed from the one in the XML. The casinos within
225 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
226 -- less the same. We don't need a bajillion different tables to
227 -- store that, just one tying the casino/game pair to the three
230 -- The one small difference between the over/under casinos and the
231 -- home/away ones is that the home/away lines are all 'Double's, but
232 -- the over/under lines appear to be textual.
236 ogl_odds_games_id :: DefaultKey OddsGame,
237 ogl_odds_casinos_id :: DefaultKey OddsCasino,
238 ogl_over_under :: Maybe String,
239 ogl_away_line :: Maybe Double,
240 ogl_home_line :: Maybe Double }
243 -- * OddsGame/OddsGameXml
245 -- | Database representation of a game. We retain the rotation number
246 -- of the home/away teams, since those are specific to the game and
251 db_odds_id :: DefaultKey Odds,
252 db_away_team_id :: DefaultKey Team,
253 db_home_team_id :: DefaultKey Team,
255 db_game_time :: UTCTime, -- ^ Contains both the date and time.
256 db_away_team_rotation_number :: Maybe Int,
257 db_home_team_rotation_number :: Maybe Int,
258 db_away_team_starter_id :: Maybe Int,
259 db_away_team_starter_name :: Maybe String,
260 db_home_team_starter_id :: Maybe Int,
261 db_home_team_starter_name :: Maybe String }
264 -- | XML representation of an 'OddsGame'.
269 xml_game_date :: UTCTime, -- ^ Contains only the date
270 xml_game_time :: UTCTime, -- ^ Contains only the time
271 xml_away_team :: OddsGameTeamXml,
272 xml_home_team :: OddsGameTeamXml,
273 xml_over_under :: OddsGameOverUnderXml }
276 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
279 xml_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
280 xml_over_under_casinos = xml_casinos . xml_over_under
283 instance ToDb OddsGameXml where
284 -- | The database representation of an 'OddsGameXml' is an
287 type Db OddsGameXml = OddsGame
290 instance Child OddsGameXml where
291 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
292 -- the foreign key for 'OddsGame' points to an 'Odds'.
294 type Parent OddsGameXml = Odds
297 instance FromXmlFkTeams OddsGameXml where
298 -- | To convert from the XML representation to the database one, we
299 -- drop the casino lines, but retain the home/away rotation
300 -- numbers and the starters. The foreign keys to 'Odds' and the
301 -- home/away teams are passed in.
303 from_xml_fk_teams fk fk_away fk_home OddsGameXml{..} =
306 db_away_team_id = fk_away,
307 db_home_team_id = fk_home,
308 db_game_id = xml_game_id,
310 db_game_time = UTCTime
311 (utctDay xml_game_date) -- Take the day part from one,
312 (utctDayTime xml_game_time), -- the time from the other.
314 db_away_team_rotation_number =
315 (xml_team_rotation_number xml_away_team),
317 db_home_team_rotation_number =
318 (xml_team_rotation_number xml_home_team),
320 db_away_team_starter_id =
321 (xml_starter_id <$> xml_team_starter xml_away_team),
323 -- Sometimes the starter element is present but the name isn't,
324 -- so we combine the two maybes with join.
325 db_away_team_starter_name = join
326 (xml_starter_name <$> xml_team_starter xml_away_team),
328 db_home_team_starter_id =
329 (xml_starter_id <$> xml_team_starter xml_home_team),
331 -- Sometimes the starter element is present but the name isn't,
332 -- so we combine the two maybes with join.
333 db_home_team_starter_name = join
334 (xml_starter_name <$> xml_team_starter xml_home_team) }
337 -- | This lets us insert the XML representation 'OddsGameXml' directly.
339 instance XmlImportFkTeams OddsGameXml
342 -- * OddsGameWithNotes
344 -- | This is our best guess at what occurs in the Odds_XML
345 -- documents. It looks like each consecutive set of games can
346 -- optionally have some notes appear before it. Each \"note\" comes
347 -- as its own \<Notes\>...\</Notes\> element.
349 -- The notes are ignored completely in the database; we only bother
350 -- with them to ensure that we're (un)pickling correctly.
352 -- We can't group the notes with a \"set\" of 'OddsGame's, because
353 -- that leads to ambiguity in parsing. Since we're going to ignore
354 -- the notes anyway, we just stick them with an arbitrary
355 -- game. C'est la vie.
357 -- We have to take the same approach with the league. The
358 -- \<League_Name\> elements are sitting outside of the games, and
359 -- are presumably supposed to be interpreted in \"chronological\"
360 -- order; i.e. the current league stays the same until we see
361 -- another \<League_Name\> element. Unfortunately, that's not how
362 -- XML works. So we're forced to ignore the league in the database
363 -- and pull the same trick, pairing them with games.
365 data OddsGameWithNotes =
367 league :: Maybe String,
369 game :: OddsGameXml }
375 -- | Database representation of a 'Message'.
379 db_xml_file_id :: Int,
382 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
383 -- because their timezones are ambiguous
384 -- (and the date is less than useful when
385 -- it might be off by an hour).
386 db_time_stamp :: UTCTime }
389 -- | The XML representation of 'Odds'.
393 xml_xml_file_id :: Int,
394 xml_heading :: String,
395 xml_category :: String,
398 xml_line_time :: String,
399 xml_games_with_notes :: [OddsGameWithNotes],
400 xml_time_stamp :: UTCTime }
403 -- | Pseudo-field that lets us get the 'OddsGame's out of
404 -- 'xml_games_with_notes'.
406 xml_games :: Message -> [OddsGameXml]
407 xml_games m = map game (xml_games_with_notes m)
410 instance ToDb Message where
411 -- | The database representation of a 'Message' is 'Odds'.
413 type Db Message = Odds
415 instance FromXml Message where
416 -- | To convert from the XML representation to the database one, we
417 -- just drop a bunch of fields.
419 from_xml Message{..} =
421 db_xml_file_id = xml_xml_file_id,
422 db_sport = xml_sport,
423 db_title = xml_title,
424 db_line_time = xml_line_time,
425 db_time_stamp = xml_time_stamp }
427 -- | This lets us insert the XML representation 'Message' directly.
429 instance XmlImport Message
436 -- Groundhog database schema. This must come before the DbImport
437 -- instance definition. Don't know why.
438 mkPersist tsn_codegen_config [groundhog|
445 # Prevent multiple imports of the same message.
446 fields: [db_xml_file_id]
453 - name: unique_odds_casino
455 fields: [casino_client_id]
465 - name: db_away_team_id
468 - name: db_home_team_id
472 - entity: OddsGameLine
473 dbName: odds_games_lines
477 - name: ogl_odds_games_id
480 - name: ogl_odds_casinos_id
486 instance DbImport Message where
489 migrate (undefined :: Team)
490 migrate (undefined :: Odds)
491 migrate (undefined :: OddsCasino)
492 migrate (undefined :: OddsGame)
493 migrate (undefined :: OddsGameLine)
496 -- Insert the root "odds" element and acquire its primary key (id).
497 odds_id <- insert_xml m
499 forM_ (xml_games m) $ \game -> do
500 -- First we insert the home and away teams.
501 away_team_id <- insert_xml_or_select (xml_away_team game)
502 home_team_id <- insert_xml_or_select (xml_home_team game)
504 -- Now insert the game, keyed to the "odds" and its teams.
505 game_id <- insert_xml_fk_teams odds_id away_team_id home_team_id game
507 -- Finally, we insert the lines. The over/under entries for this
508 -- game and the lines for the casinos all wind up in the same
509 -- table, odds_games_lines. We can insert the over/under entries
510 -- freely with empty away/home lines:
511 forM_ (xml_over_under_casinos game) $ \c -> do
512 -- Start by inderting the casino.
513 ou_casino_id <- insert_xml_or_select c
515 -- Now add the over/under entry with the casino's id.
516 let ogl = OddsGameLine {
517 ogl_odds_games_id = game_id,
518 ogl_odds_casinos_id = ou_casino_id,
519 ogl_over_under = (xml_casino_line c),
520 ogl_away_line = Nothing,
521 ogl_home_line = Nothing }
525 -- ...but then when we insert the home/away team lines, we
526 -- prefer to update the existing entry rather than overwrite it
527 -- or add a new record.
528 forM_ (xml_team_casinos $ xml_away_team game) $ \c -> do
529 -- insert, or more likely retrieve the existing, casino
530 a_casino_id <- insert_xml_or_select c
532 -- Get a Maybe Double instead of the Maybe String that's in there.
533 let away_line = home_away_line c
535 -- Unconditionally update that casino's away team line with ours.
536 update [Ogl_Away_Line =. away_line] $ -- WHERE
537 Ogl_Odds_Casinos_Id ==. a_casino_id
539 -- Repeat all that for the home team.
540 forM_ (xml_team_casinos $ xml_home_team game) $ \c ->do
541 h_casino_id <- insert_xml_or_select c
542 let home_line = home_away_line c
543 update [Ogl_Home_Line =. home_line] $ -- WHERE
544 Ogl_Odds_Casinos_Id ==. h_casino_id
548 return ImportSucceeded
555 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
557 pickle_game_with_notes :: PU OddsGameWithNotes
558 pickle_game_with_notes =
559 xpWrap (from_pair, to_pair) $
561 (xpOption $ xpElem "League_Name" xpText)
562 (xpList $ xpElem "Notes" xpText)
565 from_pair = uncurryN OddsGameWithNotes
566 to_pair OddsGameWithNotes{..} = (league, notes, game)
569 -- | Pickler for an 'OddsGameCasinoXml'.
571 pickle_casino :: PU OddsGameCasinoXml
574 xpWrap (from_tuple, to_tuple) $
576 (xpAttr "ClientID" xpInt)
577 (xpAttr "Name" xpText)
580 from_tuple = uncurryN OddsGameCasinoXml
581 -- Use record wildcards to avoid unused field warnings.
582 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
587 -- | Pickler for an 'OddsGameTeamXml'.
589 pickle_home_team :: PU OddsGameTeamXml
592 xpWrap (from_tuple, to_tuple) $
594 (xpElem "HomeTeamID" xpText)
595 (xpElem "HomeRotationNumber" (xpOption xpInt))
596 (xpElem "HomeAbbr" xpText)
597 (xpElem "HomeTeamName" xpText)
598 (xpOption pickle_home_starter)
599 (xpList pickle_casino)
601 from_tuple = uncurryN OddsGameTeamXml
603 -- Use record wildcards to avoid unused field warnings.
604 to_tuple OddsGameTeamXml{..} = (xml_team_id,
605 xml_team_rotation_number,
612 -- | Portion of the 'OddsGameTeamStarterXml' pickler that is not
613 -- specific to the home/away teams.
615 pickle_starter :: PU OddsGameTeamStarterXml
617 xpWrap (from_tuple, to_tuple) $
618 xpPair (xpAttr "ID" xpInt) (xpOption xpText)
620 from_tuple = uncurry OddsGameTeamStarterXml
621 to_tuple OddsGameTeamStarterXml{..} = (xml_starter_id,
624 -- | Pickler for an home team 'OddsGameTeamStarterXml'
626 pickle_home_starter :: PU OddsGameTeamStarterXml
627 pickle_home_starter = xpElem "HStarter" $ pickle_starter
630 -- | Pickler for an away team 'OddsGameTeamStarterXml'
632 pickle_away_starter :: PU OddsGameTeamStarterXml
633 pickle_away_starter = xpElem "AStarter" $ pickle_starter
637 -- | Pickler for an 'OddsGameTeamXml'.
639 pickle_away_team :: PU OddsGameTeamXml
642 xpWrap (from_tuple, to_tuple) $
644 (xpElem "AwayTeamID" xpText)
645 (xpElem "AwayRotationNumber" (xpOption xpInt))
646 (xpElem "AwayAbbr" xpText)
647 (xpElem "AwayTeamName" xpText)
648 (xpOption pickle_away_starter)
649 (xpList pickle_casino)
651 from_tuple = uncurryN OddsGameTeamXml
653 -- Use record wildcards to avoid unused field warnings.
654 to_tuple OddsGameTeamXml{..} = (xml_team_id,
655 xml_team_rotation_number,
663 -- | Pickler for an 'OddsGameOverUnderXml'.
665 pickle_over_under :: PU OddsGameOverUnderXml
667 xpElem "Over_Under" $
668 xpWrap (to_newtype, from_newtype) $
671 from_newtype (OddsGameOverUnderXml cs) = cs
672 to_newtype = OddsGameOverUnderXml
675 -- | Pickler for an 'OddsGameXml'.
677 pickle_game :: PU OddsGameXml
680 xpWrap (from_tuple, to_tuple) $
682 (xpElem "GameID" xpInt)
683 (xpElem "Game_Date" xp_date_padded)
684 (xpElem "Game_Time" xp_time)
689 from_tuple = uncurryN OddsGameXml
690 -- Use record wildcards to avoid unused field warnings.
691 to_tuple OddsGameXml{..} = (xml_game_id,
699 -- | Pickler for the top-level 'Message'.
701 pickle_message :: PU Message
704 xpWrap (from_tuple, to_tuple) $
705 xp8Tuple (xpElem "XML_File_ID" xpInt)
706 (xpElem "heading" xpText)
707 (xpElem "category" xpText)
708 (xpElem "sport" xpText)
709 (xpElem "Title" xpText)
710 (xpElem "Line_Time" xpText)
711 (xpList pickle_game_with_notes)
712 (xpElem "time_stamp" xp_time_stamp)
714 from_tuple = uncurryN Message
715 to_tuple m = (xml_xml_file_id m,
721 xml_games_with_notes m,
729 -- | A list of all tests for this module.
731 odds_tests :: TestTree
735 [ test_on_delete_cascade,
736 test_pickle_of_unpickle_is_identity,
737 test_unpickle_succeeds ]
740 -- | If we unpickle something and then pickle it, we should wind up
741 -- with the same thing we started with. WARNING: success of this
742 -- test does not mean that unpickling succeeded.
744 test_pickle_of_unpickle_is_identity :: TestTree
745 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
746 [ check "pickle composed with unpickle is the identity"
747 "test/xml/Odds_XML.xml",
749 check "pickle composed with unpickle is the identity (non-int team_id)"
750 "test/xml/Odds_XML-noninteger-team-id.xml",
752 check "pickle composed with unpickle is the identity (positive(+) line)"
753 "test/xml/Odds_XML-positive-line.xml",
755 check "pickle composed with unpickle is the identity (large file)"
756 "test/xml/Odds_XML-largefile.xml",
758 check "pickle composed with unpickle is the identity (league name)"
759 "test/xml/Odds_XML-league-name.xml",
761 check "pickle composed with unpickle is the identity (missing starters)"
762 "test/xml/Odds_XML-missing-starters.xml" ]
764 check desc path = testCase desc $ do
765 (expected, actual) <- pickle_unpickle pickle_message path
769 -- | Make sure we can actually unpickle these things.
771 test_unpickle_succeeds :: TestTree
772 test_unpickle_succeeds = testGroup "unpickle tests"
773 [ check "unpickling succeeds"
774 "test/xml/Odds_XML.xml",
776 check "unpickling succeeds (non-int team_id)"
777 "test/xml/Odds_XML-noninteger-team-id.xml",
779 check "unpickling succeeds (positive(+) line)"
780 "test/xml/Odds_XML-positive-line.xml",
782 check "unpickling succeeds (large file)"
783 "test/xml/Odds_XML-largefile.xml",
785 check "unpickling succeeds (league name)"
786 "test/xml/Odds_XML-league-name.xml",
788 check "unpickling succeeds (missing starters)"
789 "test/xml/Odds_XML-missing-starters.xml" ]
791 check desc path = testCase desc $ do
792 actual <- unpickleable path pickle_message
797 -- | Make sure everything gets deleted when we delete the top-level
798 -- record. The casinos and teams should be left behind.
800 test_on_delete_cascade :: TestTree
801 test_on_delete_cascade = testGroup "cascading delete tests"
802 [ check "deleting odds deletes its children"
803 "test/xml/Odds_XML.xml"
804 13 -- 5 casinos, 8 teams
807 check "deleting odds deletes its children (non-int team_id)"
808 "test/xml/Odds_XML-noninteger-team-id.xml"
809 51 -- 5 casinos, 46 teams
812 check "deleting odds deleted its children (positive(+) line)"
813 "test/xml/Odds_XML-positive-line.xml"
814 17 -- 5 casinos, 12 teams
817 check "deleting odds deleted its children (large file)"
818 "test/xml/Odds_XML-largefile.xml"
819 189 -- 5 casinos, 184 teams
821 check "deleting odds deleted its children (league name)"
822 "test/xml/Odds_XML-league-name.xml"
823 35 -- 5 casinos, 30 teams
825 check "deleting odds deleted its children (missing starters)"
826 "test/xml/Odds_XML-missing-starters.xml"
827 7 -- 5 casinos, 2 teams
830 check desc path expected = testCase desc $ do
831 odds <- unsafe_unpickle path pickle_message
832 let a = undefined :: Team
833 let b = undefined :: Odds
834 let c = undefined :: OddsCasino
835 let d = undefined :: OddsGame
836 let e = undefined :: OddsGameLine
837 actual <- withSqliteConn ":memory:" $ runDbConn $ do
838 runMigration silentMigrationLogger $ do
846 count_a <- countAll a
847 count_b <- countAll b
848 count_c <- countAll c
849 count_d <- countAll d
850 count_e <- countAll e
851 return $ sum [count_a, count_b, count_c,