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
11 -- contains a root element \<message\> that contains a bunch of
12 -- other... disorganized... information.
19 -- * WARNING: these are private but exported to silence warnings
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 (
40 silentMigrationLogger,
42 import Database.Groundhog.Core ( DefaultKey )
43 import Database.Groundhog.Generic ( runDbConn )
44 import Database.Groundhog.Sqlite ( withSqliteConn )
45 import Database.Groundhog.TH (
48 import Test.Tasty ( TestTree, testGroup )
49 import Test.Tasty.HUnit ( (@?=), testCase )
50 import Text.Read ( readMaybe )
51 import Text.XML.HXT.Core (
69 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
70 import TSN.Picklers ( xp_date, xp_time, xp_time_stamp )
71 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
81 -- | The DTD to which this module corresponds. Used to invoke dbimport.
91 -- * OddsGameCasino/OddsGameCasinoXml
94 -- | The casinos should have their own table, but the lines don't
95 -- belong in that table (there is a separate table for
96 -- 'OddsGameLine' which associates the two).
98 -- We drop the \"Game\" prefix because the casinos really aren't
99 -- children of the games; the XML just makes it seem that way.
103 casino_client_id :: Int,
104 casino_name :: String }
108 -- | The home/away lines are 'Double's, but the over/under lines are
109 -- textual. If we want to use one data type for both, we have to go
110 -- with a 'String' and then attempt to 'read' a 'Double' later when we
111 -- go to insert the thing.
113 data OddsGameCasinoXml =
115 xml_casino_client_id :: Int,
116 xml_casino_name :: String,
117 xml_casino_line :: Maybe String }
121 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
122 -- priori textual (because it might be an over/under line).
124 home_away_line :: OddsGameCasinoXml -> Maybe Double
125 home_away_line = join . (fmap readMaybe) . xml_casino_line
129 instance ToDb OddsGameCasinoXml where
130 -- | The database representation of an 'OddsGameCasinoXml' is an
133 type Db OddsGameCasinoXml = OddsCasino
136 instance FromXml OddsGameCasinoXml where
137 -- | We convert from XML to the database by dropping the line field.
139 from_xml OddsGameCasinoXml{..} =
141 casino_client_id = xml_casino_client_id,
142 casino_name = xml_casino_name }
145 -- | This allows us to insert the XML representation 'OddsGameCasinoXml'
148 instance XmlImport OddsGameCasinoXml
154 -- | The database representation of teams as they appear in odds
159 db_team_id :: String, -- ^ The home/away team IDs are
160 -- three characters but Postgres
161 -- imposes no performance penalty
162 -- on lengthless text fields, so
163 -- we ignore the probable upper
164 -- bound of three characters.
166 db_team_name :: String }
170 -- * OddsGameHomeTeam/OddsGameHomeTeamXml
172 -- | The XML representation of a \<HomeTeam\>, as found in \<Game\>s.
174 data OddsGameHomeTeamXml =
175 OddsGameHomeTeamXml {
176 xml_home_team_id :: String, -- ^ The home/away team IDs
177 -- are three characters but
178 -- Postgres imposes no
179 -- performance penalty on
180 -- lengthless text fields,
181 -- so we ignore the probable
182 -- upper bound of three
184 xml_home_rotation_number :: Int,
185 xml_home_abbr :: String,
186 xml_home_team_name :: String,
187 xml_home_casinos :: [OddsGameCasinoXml] }
190 instance ToDb OddsGameHomeTeamXml where
191 -- | The database representation of an 'OddsGameHomeTeamXml' is an
194 type Db OddsGameHomeTeamXml = OddsGameTeam
196 instance FromXml OddsGameHomeTeamXml where
197 -- | We convert from XML to the database by dropping the lines and
198 -- rotation number (which are specific to the games, not the teams
201 from_xml OddsGameHomeTeamXml{..} =
203 db_team_id = xml_home_team_id,
204 db_abbr = xml_home_abbr,
205 db_team_name = xml_home_team_name }
207 -- | This allows us to insert the XML representation
208 -- 'OddsGameHomeTeamXml' directly.
210 instance XmlImport OddsGameHomeTeamXml where
213 -- * OddsGameAwayTeam/OddsGameAwayTeamXml
215 -- | The XML representation of a \<AwayTeam\>, as found in \<Game\>s.
217 data OddsGameAwayTeamXml =
218 OddsGameAwayTeamXml {
219 xml_away_team_id :: String, -- ^ The home/away team IDs are
220 -- three characters but Postgres
221 -- imposes no performance penalty
222 -- on lengthless text fields, so
223 -- we ignore the probable upper
224 -- bound of three characters
225 xml_away_rotation_number :: Int,
226 xml_away_abbr :: String,
227 xml_away_team_name :: String,
228 xml_away_casinos :: [OddsGameCasinoXml] }
231 instance ToDb OddsGameAwayTeamXml where
232 -- | The database representation of an 'OddsGameAwayTeamXml' is an
235 type Db OddsGameAwayTeamXml = OddsGameTeam
237 instance FromXml OddsGameAwayTeamXml where
238 -- | We convert from XML to the database by dropping the lines and
239 -- rotation number (which are specific to the games, not the teams
242 from_xml OddsGameAwayTeamXml{..} = OddsGameTeam
247 -- | This allows us to insert the XML representation
248 -- 'OddsGameAwayTeamXml' directly.
250 instance XmlImport OddsGameAwayTeamXml where
253 -- * OddsGame_OddsGameTeam
255 -- | Database mapping between games and their home/away teams.
257 data OddsGame_OddsGameTeam =
258 OddsGame_OddsGameTeam {
259 ogogt_odds_games_id :: DefaultKey OddsGame,
260 ogogt_away_team_id :: DefaultKey OddsGameTeam,
261 ogogt_home_team_id :: DefaultKey OddsGameTeam }
264 -- * OddsGameOverUnderXml
266 -- | XML representation of the over/under. A wrapper around a bunch of
269 newtype OddsGameOverUnderXml =
270 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
276 -- | This database representation of the casino lines can't be
277 -- constructed from the one in the XML. The casinos within
278 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
279 -- less the same. We don't need a bajillion different tables to
280 -- store that, just one tying the casino/game pair to the three
283 -- The one small difference between the over/under casinos and the
284 -- home/away ones is that the home/away lines are all 'Double's, but
285 -- the over/under lines appear to be textual.
289 ogl_odds_games_id :: DefaultKey OddsGame,
290 ogl_odds_casinos_id :: DefaultKey OddsCasino,
291 ogl_over_under :: Maybe String,
292 ogl_away_line :: Maybe Double,
293 ogl_home_line :: Maybe Double }
296 -- * OddsGame/OddsGameXml
298 -- | Database representation of a game. We retain the rotation number
299 -- of the home/away teams, since those are specific to the game and
304 db_odds_id :: DefaultKey Odds,
306 db_game_time :: UTCTime, -- ^ Contains both the date and time.
307 db_game_away_team_rotation_number :: Int,
308 db_game_home_team_rotation_number :: Int }
311 -- | XML representation of an 'OddsGame'.
316 xml_game_date :: UTCTime, -- ^ Contains only the date
317 xml_game_time :: UTCTime, -- ^ Contains only the time
318 xml_game_away_team :: OddsGameAwayTeamXml,
319 xml_game_home_team :: OddsGameHomeTeamXml,
320 xml_game_over_under :: OddsGameOverUnderXml }
323 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
324 -- xml_game_over_under.
326 xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
327 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
330 instance ToDb OddsGameXml where
331 -- | The database representation of an 'OddsGameXml' is an
334 type Db OddsGameXml = OddsGame
336 instance FromXmlFk OddsGameXml where
337 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
338 -- the foreign key for 'OddsGame' points to an 'Odds'.
340 type Parent OddsGameXml = Odds
342 -- | To convert from the XML representation to the database one, we
343 -- drop the home/away teams and the casino lines, but retain the
344 -- home/away rotation numbers.
346 from_xml_fk fk OddsGameXml{..} =
349 db_game_id = xml_game_id,
351 db_game_time = UTCTime
352 (utctDay xml_game_date) -- Take the day part from one,
353 (utctDayTime xml_game_time), -- the time from the other.
355 db_game_away_team_rotation_number =
356 (xml_away_rotation_number xml_game_away_team),
358 db_game_home_team_rotation_number =
359 (xml_home_rotation_number xml_game_home_team) }
361 -- | This lets us insert the XML representation 'OddsGameXml' directly.
363 instance XmlImportFk OddsGameXml
366 -- * OddsGameWithNotes
368 -- | This is our best guess at what occurs in the Odds_XML
369 -- documents. It looks like each consecutive set of games can
370 -- optionally have some notes appear before it. Each \"note\" comes
371 -- as its own \<Notes\>...\</Notes\> element.
373 -- The notes are ignored completely in the database; we only bother
374 -- with them to ensure that we're (un)pickling correctly.
376 -- We can't group the notes with a \"set\" of 'OddsGame's, because
377 -- that leads to ambiguity in parsing. Since we're going to ignore
378 -- the notes anyway, we just stick them with an arbitrary
379 -- game. C'est la vie.
381 data OddsGameWithNotes =
384 game :: OddsGameXml }
390 -- | Database representation of a 'Message'.
394 db_xml_file_id :: Int,
397 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
398 -- because their timezones are ambiguous
399 -- (and the date is less than useful when
400 -- it might be off by an hour).
401 db_time_stamp :: UTCTime }
404 -- | The XML representation of 'Odds'.
408 xml_xml_file_id :: Int,
409 xml_heading :: String,
410 xml_category :: String,
413 xml_line_time :: String,
414 xml_games_with_notes :: [OddsGameWithNotes],
415 xml_time_stamp :: UTCTime }
418 -- | Pseudo-field that lets us get the 'OddsGame's out of
419 -- 'xml_games_with_notes'.
421 xml_games :: Message -> [OddsGameXml]
422 xml_games m = map game (xml_games_with_notes m)
425 instance ToDb Message where
426 -- | The database representation of a 'Message' is 'Odds'.
428 type Db Message = Odds
430 instance FromXml Message where
431 -- | To convert from the XML representation to the database one, we
432 -- just drop a bunch of fields.
434 from_xml Message{..} =
436 db_xml_file_id = xml_xml_file_id,
437 db_sport = xml_sport,
438 db_title = xml_title,
439 db_line_time = xml_line_time,
440 db_time_stamp = xml_time_stamp }
442 -- | This lets us insert the XML representation 'Message' directly.
444 instance XmlImport Message
451 -- Groundhog database schema. This must come before the DbImport
452 -- instance definition. Don't know why.
453 mkPersist tsn_codegen_config [groundhog|
460 # Prevent multiple imports of the same message.
461 fields: [db_xml_file_id]
468 - name: unique_odds_casino
470 fields: [casino_client_id]
472 - entity: OddsGameTeam
473 dbName: odds_games_teams
477 - name: unique_odds_games_team
491 - entity: OddsGameLine
492 dbName: odds_games_lines
496 - name: ogl_odds_games_id
499 - name: ogl_odds_casinos_id
503 - entity: OddsGame_OddsGameTeam
504 dbName: odds_games__odds_games_teams
506 - name: OddsGame_OddsGameTeam
508 - name: ogogt_odds_games_id
511 - name: ogogt_away_team_id
514 - name: ogogt_home_team_id
519 instance DbImport Message where
522 migrate (undefined :: Odds)
523 migrate (undefined :: OddsCasino)
524 migrate (undefined :: OddsGameTeam)
525 migrate (undefined :: OddsGame)
526 migrate (undefined :: OddsGame_OddsGameTeam)
527 migrate (undefined :: OddsGameLine)
530 -- Insert the root "odds" element and acquire its primary key (id).
531 odds_id <- insert_xml m
533 forM_ (xml_games m) $ \g -> do
534 -- Next, we insert the home and away teams. We do this before
535 -- inserting the game itself because the game has two foreign keys
536 -- pointing to odds_games_teams.
537 -- Next to insert the home and away teams.
538 away_team_id <- insert_xml_or_select (xml_game_away_team g)
539 home_team_id <- insert_xml_or_select (xml_game_home_team g)
541 -- Now insert the game, keyed to the "odds",
542 game_id <- insert_xml_fk odds_id g
544 -- Insert a record into odds_games__odds_games_teams mapping the
545 -- home/away teams to this game. Use the full record syntax
546 -- because the types would let us mix up the home/away teams.
547 insert_ OddsGame_OddsGameTeam {
548 ogogt_odds_games_id = game_id,
549 ogogt_away_team_id = away_team_id,
550 ogogt_home_team_id = home_team_id }
552 -- Finaly, we insert the lines. The over/under entries for this
553 -- game and the lines for the casinos all wind up in the same
554 -- table, odds_games_lines. We can insert the over/under entries
555 -- freely with empty away/home lines:
556 forM_ (xml_game_over_under_casinos g) $ \c -> do
557 -- Start by inderting the casino.
558 ou_casino_id <- insert_xml_or_select c
560 -- Now add the over/under entry with the casino's id.
561 let ogl = OddsGameLine {
562 ogl_odds_games_id = game_id,
563 ogl_odds_casinos_id = ou_casino_id,
564 ogl_over_under = (xml_casino_line c),
565 ogl_away_line = Nothing,
566 ogl_home_line = Nothing }
570 -- ...but then when we insert the home/away team lines, we
571 -- prefer to update the existing entry rather than overwrite it
572 -- or add a new record.
573 forM_ (xml_away_casinos $ xml_game_away_team g) $ \c -> do
574 -- insert, or more likely retrieve the existing, casino
575 a_casino_id <- insert_xml_or_select c
577 -- Get a Maybe Double instead of the Maybe String that's in there.
578 let away_line = home_away_line c
580 -- Unconditionally update that casino's away team line with ours.
581 update [Ogl_Away_Line =. away_line] $ -- WHERE
582 Ogl_Odds_Casinos_Id ==. a_casino_id
584 -- Repeat all that for the home team.
585 forM_ (xml_home_casinos $ xml_game_home_team g) $ \c ->do
586 h_casino_id <- insert_xml_or_select c
587 let home_line = home_away_line c
588 update [Ogl_Home_Line =. home_line] $ -- WHERE
589 Ogl_Odds_Casinos_Id ==. h_casino_id
593 return ImportSucceeded
600 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
602 pickle_game_with_notes :: PU OddsGameWithNotes
603 pickle_game_with_notes =
604 xpWrap (from_pair, to_pair) $
606 (xpList $ xpElem "Notes" xpText)
609 from_pair = uncurry OddsGameWithNotes
610 to_pair OddsGameWithNotes{..} = (notes, game)
613 -- | Pickler for an 'OddsGameCasinoXml'.
615 pickle_casino :: PU OddsGameCasinoXml
618 xpWrap (from_tuple, to_tuple) $
620 (xpAttr "ClientID" xpInt)
621 (xpAttr "Name" xpText)
624 from_tuple = uncurryN OddsGameCasinoXml
625 -- Use record wildcards to avoid unused field warnings.
626 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
631 -- | Pickler for an 'OddsGameHomeTeamXml'.
633 pickle_home_team :: PU OddsGameHomeTeamXml
636 xpWrap (from_tuple, to_tuple) $
638 (xpElem "HomeTeamID" xpText)
639 (xpElem "HomeRotationNumber" xpInt)
640 (xpElem "HomeAbbr" xpText)
641 (xpElem "HomeTeamName" xpText)
642 (xpList pickle_casino)
644 from_tuple = uncurryN OddsGameHomeTeamXml
645 -- Use record wildcards to avoid unused field warnings.
646 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
647 xml_home_rotation_number,
653 -- | Pickler for an 'OddsGameAwayTeamXml'.
655 pickle_away_team :: PU OddsGameAwayTeamXml
658 xpWrap (from_tuple, to_tuple) $
660 (xpElem "AwayTeamID" xpText)
661 (xpElem "AwayRotationNumber" xpInt)
662 (xpElem "AwayAbbr" xpText)
663 (xpElem "AwayTeamName" xpText)
664 (xpList pickle_casino)
666 from_tuple = uncurryN OddsGameAwayTeamXml
667 -- Use record wildcards to avoid unused field warnings.
668 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
669 xml_away_rotation_number,
676 -- | Pickler for an 'OddsGameOverUnderXml'.
678 pickle_over_under :: PU OddsGameOverUnderXml
680 xpElem "Over_Under" $
681 xpWrap (to_newtype, from_newtype) $
684 from_newtype (OddsGameOverUnderXml cs) = cs
685 to_newtype = OddsGameOverUnderXml
688 -- | Pickler for an 'OddsGameXml'.
690 pickle_game :: PU OddsGameXml
693 xpWrap (from_tuple, to_tuple) $
695 (xpElem "GameID" xpInt)
696 (xpElem "Game_Date" xp_date)
697 (xpElem "Game_Time" xp_time)
702 from_tuple = uncurryN OddsGameXml
703 -- Use record wildcards to avoid unused field warnings.
704 to_tuple OddsGameXml{..} = (xml_game_id,
712 -- | Pickler for the top-level 'Message'.
714 pickle_message :: PU Message
717 xpWrap (from_tuple, to_tuple) $
718 xp8Tuple (xpElem "XML_File_ID" xpInt)
719 (xpElem "heading" xpText)
720 (xpElem "category" xpText)
721 (xpElem "sport" xpText)
722 (xpElem "Title" xpText)
723 (xpElem "Line_Time" xpText)
724 (xpList pickle_game_with_notes)
725 (xpElem "time_stamp" xp_time_stamp)
727 from_tuple = uncurryN Message
728 to_tuple m = (xml_xml_file_id m,
734 xml_games_with_notes m,
742 -- | A list of all tests for this module.
744 odds_tests :: TestTree
748 [ test_on_delete_cascade,
749 test_pickle_of_unpickle_is_identity,
750 test_unpickle_succeeds ]
753 -- | If we unpickle something and then pickle it, we should wind up
754 -- with the same thing we started with. WARNING: success of this
755 -- test does not mean that unpickling succeeded.
757 test_pickle_of_unpickle_is_identity :: TestTree
758 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
759 [ check "pickle composed with unpickle is the identity"
760 "test/xml/Odds_XML.xml",
762 check "pickle composed with unpickle is the identity (non-int team_id)"
763 "test/xml/Odds_XML-noninteger-team-id.xml",
765 check "pickle composed with unpickle is the identity (positive(+) line)"
766 "test/xml/Odds_XML-positive-line.xml",
768 check "pickle composed with unpickle is the identity (large file)"
769 "test/xml/Odds_XML-largefile.xml" ]
771 check desc path = testCase desc $ do
772 (expected, actual) <- pickle_unpickle pickle_message path
776 -- | Make sure we can actually unpickle these things.
778 test_unpickle_succeeds :: TestTree
779 test_unpickle_succeeds = testGroup "unpickle tests"
780 [ check "unpickling succeeds"
781 "test/xml/Odds_XML.xml",
783 check "unpickling succeeds (non-int team_id)"
784 "test/xml/Odds_XML-noninteger-team-id.xml",
786 check "unpickling succeeds (positive(+) line)"
787 "test/xml/Odds_XML-positive-line.xml",
789 check "unpickling succeeds (large file)"
790 "test/xml/Odds_XML-largefile.xml" ]
792 check desc path = testCase desc $ do
793 actual <- unpickleable path pickle_message
798 -- | Make sure everything gets deleted when we delete the top-level
801 test_on_delete_cascade :: TestTree
802 test_on_delete_cascade = testGroup "cascading delete tests"
803 [ check "deleting odds deletes its children"
804 "test/xml/Odds_XML.xml"
805 13 -- 5 casinos, 8 teams
808 check "deleting odds deletes its children (non-int team_id)"
809 "test/xml/Odds_XML-noninteger-team-id.xml"
810 51 -- 5 casinos, 46 teams
813 check "deleting odds deleted its children (positive(+) line)"
814 "test/xml/Odds_XML-positive-line.xml"
815 17 -- 5 casinos, 12 teams
818 check "deleting odds deleted its children (large file)"
819 "test/xml/Odds_XML-largefile.xml"
820 189 -- 5 casinos, 184 teams
823 check desc path expected = testCase desc $ do
824 odds <- unsafe_unpickle path pickle_message
825 let a = undefined :: Odds
826 let b = undefined :: OddsCasino
827 let c = undefined :: OddsGameTeam
828 let d = undefined :: OddsGame
829 let e = undefined :: OddsGame_OddsGameTeam
830 let f = undefined :: OddsGameLine
831 actual <- withSqliteConn ":memory:" $ runDbConn $ do
832 runMigration silentMigrationLogger $ do
841 count_a <- countAll a
842 count_b <- countAll b
843 count_c <- countAll c
844 count_d <- countAll d
845 count_e <- countAll e
846 count_f <- countAll f
847 return $ sum [count_a, count_b, count_c,
848 count_d, count_e, count_f ]