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.
18 -- * WARNING: these are private but exported to silence warnings
19 OddsCasinoConstructor(..),
21 OddsGame_OddsGameTeamConstructor(..),
22 OddsGameConstructor(..),
23 OddsGameLineConstructor(..),
24 OddsGameTeamConstructor(..) )
28 import Control.Monad ( forM_, join )
29 import Data.Time ( UTCTime(..) )
30 import Data.Tuple.Curry ( uncurryN )
31 import Database.Groundhog (
39 silentMigrationLogger,
41 import Database.Groundhog.Core ( DefaultKey )
42 import Database.Groundhog.Generic ( runDbConn )
43 import Database.Groundhog.Sqlite ( withSqliteConn )
44 import Database.Groundhog.TH (
47 import Test.Tasty ( TestTree, testGroup )
48 import Test.Tasty.HUnit ( (@?=), testCase )
49 import Text.Read ( readMaybe )
50 import Text.XML.HXT.Core (
68 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
69 import TSN.Picklers ( xp_date, xp_time, xp_time_stamp )
70 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
84 -- * OddsGameCasino/OddsGameCasinoXml
87 -- | The casinos should have their own table, but the lines don't
88 -- belong in that table (there is a separate table for
89 -- 'OddsGameLine' which associates the two).
91 -- We drop the \"Game\" prefix because the casinos really aren't
92 -- children of the games; the XML just makes it seem that way.
96 casino_client_id :: Int,
97 casino_name :: String }
101 -- | The home/away lines are 'Double's, but the over/under lines are
102 -- textual. If we want to use one data type for both, we have to go
103 -- with a 'String' and then attempt to 'read' a 'Double' later when we
104 -- go to insert the thing.
106 data OddsGameCasinoXml =
108 xml_casino_client_id :: Int,
109 xml_casino_name :: String,
110 xml_casino_line :: Maybe String }
114 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
115 -- priori textual (because it might be an over/under line).
117 home_away_line :: OddsGameCasinoXml -> Maybe Double
118 home_away_line = join . (fmap readMaybe) . xml_casino_line
122 instance ToDb OddsGameCasinoXml where
123 -- | The database representation of an 'OddsGameCasinoXml' is an
126 type Db OddsGameCasinoXml = OddsCasino
129 instance FromXml OddsGameCasinoXml where
130 -- | We convert from XML to the database by dropping the line field.
132 from_xml OddsGameCasinoXml{..} =
134 casino_client_id = xml_casino_client_id,
135 casino_name = xml_casino_name }
138 -- | This allows us to insert the XML representation 'OddsGameCasinoXml'
141 instance XmlImport OddsGameCasinoXml
147 -- | The database representation of teams as they appear in odds
152 db_team_id :: String, -- ^ The home/away team IDs are
153 -- three characters but Postgres
154 -- imposes no performance penalty
155 -- on lengthless text fields, so
156 -- we ignore the probable upper
157 -- bound of three characters.
159 db_team_name :: String }
163 -- * OddsGameHomeTeam/OddsGameHomeTeamXml
165 -- | The XML representation of a \<HomeTeam\>, as found in \<Game\>s.
167 data OddsGameHomeTeamXml =
168 OddsGameHomeTeamXml {
169 xml_home_team_id :: String, -- ^ The home/away team IDs
170 -- are three characters but
171 -- Postgres imposes no
172 -- performance penalty on
173 -- lengthless text fields,
174 -- so we ignore the probable
175 -- upper bound of three
177 xml_home_rotation_number :: Int,
178 xml_home_abbr :: String,
179 xml_home_team_name :: String,
180 xml_home_casinos :: [OddsGameCasinoXml] }
183 instance ToDb OddsGameHomeTeamXml where
184 -- | The database representation of an 'OddsGameHomeTeamXml' is an
187 type Db OddsGameHomeTeamXml = OddsGameTeam
189 instance FromXml OddsGameHomeTeamXml where
190 -- | We convert from XML to the database by dropping the lines and
191 -- rotation number (which are specific to the games, not the teams
194 from_xml OddsGameHomeTeamXml{..} =
196 db_team_id = xml_home_team_id,
197 db_abbr = xml_home_abbr,
198 db_team_name = xml_home_team_name }
200 -- | This allows us to insert the XML representation
201 -- 'OddsGameHomeTeamXml' directly.
203 instance XmlImport OddsGameHomeTeamXml where
206 -- * OddsGameAwayTeam/OddsGameAwayTeamXml
208 -- | The XML representation of a \<AwayTeam\>, as found in \<Game\>s.
210 data OddsGameAwayTeamXml =
211 OddsGameAwayTeamXml {
212 xml_away_team_id :: String, -- ^ The home/away team IDs are
213 -- three characters but Postgres
214 -- imposes no performance penalty
215 -- on lengthless text fields, so
216 -- we ignore the probable upper
217 -- bound of three characters
218 xml_away_rotation_number :: Int,
219 xml_away_abbr :: String,
220 xml_away_team_name :: String,
221 xml_away_casinos :: [OddsGameCasinoXml] }
224 instance ToDb OddsGameAwayTeamXml where
225 -- | The database representation of an 'OddsGameAwayTeamXml' is an
228 type Db OddsGameAwayTeamXml = OddsGameTeam
230 instance FromXml OddsGameAwayTeamXml where
231 -- | We convert from XML to the database by dropping the lines and
232 -- rotation number (which are specific to the games, not the teams
235 from_xml OddsGameAwayTeamXml{..} = OddsGameTeam
240 -- | This allows us to insert the XML representation
241 -- 'OddsGameAwayTeamXml' directly.
243 instance XmlImport OddsGameAwayTeamXml where
246 -- * OddsGame_OddsGameTeam
248 -- | Database mapping between games and their home/away teams.
250 data OddsGame_OddsGameTeam =
251 OddsGame_OddsGameTeam {
252 ogogt_odds_games_id :: DefaultKey OddsGame,
253 ogogt_away_team_id :: DefaultKey OddsGameTeam,
254 ogogt_home_team_id :: DefaultKey OddsGameTeam }
257 -- * OddsGameOverUnderXml
259 -- | XML representation of the over/under. A wrapper around a bunch of
262 newtype OddsGameOverUnderXml =
263 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
269 -- | This database representation of the casino lines can't be
270 -- constructed from the one in the XML. The casinos within
271 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
272 -- less the same. We don't need a bajillion different tables to
273 -- store that, just one tying the casino/game pair to the three
276 -- The one small difference between the over/under casinos and the
277 -- home/away ones is that the home/away lines are all 'Double's, but
278 -- the over/under lines appear to be textual.
282 ogl_odds_games_id :: DefaultKey OddsGame,
283 ogl_odds_casinos_id :: DefaultKey OddsCasino,
284 ogl_over_under :: Maybe String,
285 ogl_away_line :: Maybe Double,
286 ogl_home_line :: Maybe Double }
289 -- * OddsGame/OddsGameXml
291 -- | Database representation of a game. We retain the rotation number
292 -- of the home/away teams, since those are specific to the game and
297 db_odds_id :: DefaultKey Odds,
299 db_game_time :: UTCTime, -- ^ Contains both the date and time.
300 db_game_away_team_rotation_number :: Int,
301 db_game_home_team_rotation_number :: Int }
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_game_away_team :: OddsGameAwayTeamXml,
312 xml_game_home_team :: OddsGameHomeTeamXml,
313 xml_game_over_under :: OddsGameOverUnderXml }
316 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
317 -- xml_game_over_under.
319 xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
320 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
323 instance ToDb OddsGameXml where
324 -- | The database representation of an 'OddsGameXml' is an
327 type Db OddsGameXml = OddsGame
329 instance FromXmlFk OddsGameXml where
330 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
331 -- the foreign key for 'OddsGame' points to an 'Odds'.
333 type Parent OddsGameXml = Odds
335 -- | To convert from the XML representation to the database one, we
336 -- drop the home/away teams and the casino lines, but retain the
337 -- home/away rotation numbers.
339 from_xml_fk fk OddsGameXml{..} =
342 db_game_id = xml_game_id,
344 db_game_time = UTCTime
345 (utctDay xml_game_date) -- Take the day part from one,
346 (utctDayTime xml_game_time), -- the time from the other.
348 db_game_away_team_rotation_number =
349 (xml_away_rotation_number xml_game_away_team),
351 db_game_home_team_rotation_number =
352 (xml_home_rotation_number xml_game_home_team) }
354 -- | This lets us insert the XML representation 'OddsGameXml' directly.
356 instance XmlImportFk OddsGameXml
359 -- * OddsGameWithNotes
361 -- | This is our best guess at what occurs in the Odds_XML
362 -- documents. It looks like each consecutive set of games can
363 -- optionally have some notes appear before it. Each \"note\" comes
364 -- as its own \<Notes\>...\</Notes\> element.
366 -- The notes are ignored completely in the database; we only bother
367 -- with them to ensure that we're (un)pickling correctly.
369 -- We can't group the notes with a \"set\" of 'OddsGame's, because
370 -- that leads to ambiguity in parsing. Since we're going to ignore
371 -- the notes anyway, we just stick them with an arbitrary
372 -- game. C'est la vie.
374 data OddsGameWithNotes =
377 game :: OddsGameXml }
383 -- | Database representation of a 'Message'.
387 db_xml_file_id :: Int,
390 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
391 -- because their timezones are ambiguous
392 -- (and the date is less than useful when
393 -- it might be off by an hour).
394 db_time_stamp :: UTCTime }
397 -- | The XML representation of 'Odds'.
401 xml_xml_file_id :: Int,
402 xml_heading :: String,
403 xml_category :: String,
406 xml_line_time :: String,
407 xml_games_with_notes :: [OddsGameWithNotes],
408 xml_time_stamp :: UTCTime }
411 -- | Pseudo-field that lets us get the 'OddsGame's out of
412 -- 'xml_games_with_notes'.
414 xml_games :: Message -> [OddsGameXml]
415 xml_games m = map game (xml_games_with_notes m)
418 instance ToDb Message where
419 -- | The database representation of a 'Message' is 'Odds'.
421 type Db Message = Odds
423 instance FromXml Message where
424 -- | To convert from the XML representation to the database one, we
425 -- just drop a bunch of fields.
427 from_xml Message{..} =
429 db_xml_file_id = xml_xml_file_id,
430 db_sport = xml_sport,
431 db_title = xml_title,
432 db_line_time = xml_line_time,
433 db_time_stamp = xml_time_stamp }
435 -- | This lets us insert the XML representation 'Message' directly.
437 instance XmlImport Message
444 -- Groundhog database schema. This must come before the DbImport
445 -- instance definition. Don't know why.
446 mkPersist tsn_codegen_config [groundhog|
453 # Prevent multiple imports of the same message.
454 fields: [db_xml_file_id]
461 - name: unique_odds_casino
463 fields: [casino_client_id]
465 - entity: OddsGameTeam
466 dbName: odds_games_teams
470 - name: unique_odds_games_team
484 - entity: OddsGameLine
485 dbName: odds_games_lines
489 - name: ogl_odds_games_id
492 - name: ogl_odds_casinos_id
496 - entity: OddsGame_OddsGameTeam
497 dbName: odds_games__odds_games_teams
499 - name: OddsGame_OddsGameTeam
501 - name: ogogt_odds_games_id
504 - name: ogogt_away_team_id
507 - name: ogogt_home_team_id
512 instance DbImport Message where
515 migrate (undefined :: Odds)
516 migrate (undefined :: OddsCasino)
517 migrate (undefined :: OddsGameTeam)
518 migrate (undefined :: OddsGame)
519 migrate (undefined :: OddsGame_OddsGameTeam)
520 migrate (undefined :: OddsGameLine)
523 -- Insert the root "odds" element and acquire its primary key (id).
524 odds_id <- insert_xml m
526 forM_ (xml_games m) $ \g -> do
527 -- Next, we insert the home and away teams. We do this before
528 -- inserting the game itself because the game has two foreign keys
529 -- pointing to odds_games_teams.
530 -- Next to insert the home and away teams.
531 away_team_id <- insert_xml_or_select (xml_game_away_team g)
532 home_team_id <- insert_xml_or_select (xml_game_home_team g)
534 -- Now insert the game, keyed to the "odds",
535 game_id <- insert_xml_fk odds_id g
537 -- Insert a record into odds_games__odds_games_teams mapping the
538 -- home/away teams to this game. Use the full record syntax
539 -- because the types would let us mix up the home/away teams.
540 insert_ OddsGame_OddsGameTeam {
541 ogogt_odds_games_id = game_id,
542 ogogt_away_team_id = away_team_id,
543 ogogt_home_team_id = home_team_id }
545 -- Finaly, we insert the lines. The over/under entries for this
546 -- game and the lines for the casinos all wind up in the same
547 -- table, odds_games_lines. We can insert the over/under entries
548 -- freely with empty away/home lines:
549 forM_ (xml_game_over_under_casinos g) $ \c -> do
550 -- Start by inderting the casino.
551 ou_casino_id <- insert_xml_or_select c
553 -- Now add the over/under entry with the casino's id.
554 let ogl = OddsGameLine {
555 ogl_odds_games_id = game_id,
556 ogl_odds_casinos_id = ou_casino_id,
557 ogl_over_under = (xml_casino_line c),
558 ogl_away_line = Nothing,
559 ogl_home_line = Nothing }
563 -- ...but then when we insert the home/away team lines, we
564 -- prefer to update the existing entry rather than overwrite it
565 -- or add a new record.
566 forM_ (xml_away_casinos $ xml_game_away_team g) $ \c -> do
567 -- insert, or more likely retrieve the existing, casino
568 a_casino_id <- insert_xml_or_select c
570 -- Get a Maybe Double instead of the Maybe String that's in there.
571 let away_line = home_away_line c
573 -- Unconditionally update that casino's away team line with ours.
574 update [Ogl_Away_Line =. away_line] $ -- WHERE
575 Ogl_Odds_Casinos_Id ==. a_casino_id
577 -- Repeat all that for the home team.
578 forM_ (xml_home_casinos $ xml_game_home_team g) $ \c ->do
579 h_casino_id <- insert_xml_or_select c
580 let home_line = home_away_line c
581 update [Ogl_Home_Line =. home_line] $ -- WHERE
582 Ogl_Odds_Casinos_Id ==. h_casino_id
586 return ImportSucceeded
593 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
595 pickle_game_with_notes :: PU OddsGameWithNotes
596 pickle_game_with_notes =
597 xpWrap (from_pair, to_pair) $
599 (xpList $ xpElem "Notes" xpText)
602 from_pair = uncurry OddsGameWithNotes
603 to_pair OddsGameWithNotes{..} = (notes, game)
606 -- | Pickler for an 'OddsGameCasinoXml'.
608 pickle_casino :: PU OddsGameCasinoXml
611 xpWrap (from_tuple, to_tuple) $
613 (xpAttr "ClientID" xpInt)
614 (xpAttr "Name" xpText)
617 from_tuple = uncurryN OddsGameCasinoXml
618 -- Use record wildcards to avoid unused field warnings.
619 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
624 -- | Pickler for an 'OddsGameHomeTeamXml'.
626 pickle_home_team :: PU OddsGameHomeTeamXml
629 xpWrap (from_tuple, to_tuple) $
631 (xpElem "HomeTeamID" xpText)
632 (xpElem "HomeRotationNumber" xpInt)
633 (xpElem "HomeAbbr" xpText)
634 (xpElem "HomeTeamName" xpText)
635 (xpList pickle_casino)
637 from_tuple = uncurryN OddsGameHomeTeamXml
638 -- Use record wildcards to avoid unused field warnings.
639 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
640 xml_home_rotation_number,
646 -- | Pickler for an 'OddsGameAwayTeamXml'.
648 pickle_away_team :: PU OddsGameAwayTeamXml
651 xpWrap (from_tuple, to_tuple) $
653 (xpElem "AwayTeamID" xpText)
654 (xpElem "AwayRotationNumber" xpInt)
655 (xpElem "AwayAbbr" xpText)
656 (xpElem "AwayTeamName" xpText)
657 (xpList pickle_casino)
659 from_tuple = uncurryN OddsGameAwayTeamXml
660 -- Use record wildcards to avoid unused field warnings.
661 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
662 xml_away_rotation_number,
669 -- | Pickler for an 'OddsGameOverUnderXml'.
671 pickle_over_under :: PU OddsGameOverUnderXml
673 xpElem "Over_Under" $
674 xpWrap (to_newtype, from_newtype) $
677 from_newtype (OddsGameOverUnderXml cs) = cs
678 to_newtype = OddsGameOverUnderXml
681 -- | Pickler for an 'OddsGameXml'.
683 pickle_game :: PU OddsGameXml
686 xpWrap (from_tuple, to_tuple) $
688 (xpElem "GameID" xpInt)
689 (xpElem "Game_Date" xp_date)
690 (xpElem "Game_Time" xp_time)
695 from_tuple = uncurryN OddsGameXml
696 -- Use record wildcards to avoid unused field warnings.
697 to_tuple OddsGameXml{..} = (xml_game_id,
705 -- | Pickler for the top-level 'Message'.
707 pickle_message :: PU Message
710 xpWrap (from_tuple, to_tuple) $
711 xp8Tuple (xpElem "XML_File_ID" xpInt)
712 (xpElem "heading" xpText)
713 (xpElem "category" xpText)
714 (xpElem "sport" xpText)
715 (xpElem "Title" xpText)
716 (xpElem "Line_Time" xpText)
717 (xpList pickle_game_with_notes)
718 (xpElem "time_stamp" xp_time_stamp)
720 from_tuple = uncurryN Message
721 to_tuple m = (xml_xml_file_id m,
727 xml_games_with_notes m,
735 -- | A list of all tests for this module.
737 odds_tests :: TestTree
741 [ test_on_delete_cascade,
742 test_pickle_of_unpickle_is_identity,
743 test_unpickle_succeeds ]
746 -- | If we unpickle something and then pickle it, we should wind up
747 -- with the same thing we started with. WARNING: success of this
748 -- test does not mean that unpickling succeeded.
750 test_pickle_of_unpickle_is_identity :: TestTree
751 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
752 [ check "pickle composed with unpickle is the identity"
753 "test/xml/Odds_XML.xml",
755 check "pickle composed with unpickle is the identity (non-int team_id)"
756 "test/xml/Odds_XML-noninteger-team-id.xml",
758 check "pickle composed with unpickle is the identity (positive(+) line)"
759 "test/xml/Odds_XML-positive-line.xml",
761 check "pickle composed with unpickle is the identity (large file)"
762 "test/xml/Odds_XML-largefile.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 desc path = testCase desc $ do
786 actual <- unpickleable path pickle_message
791 -- | Make sure everything gets deleted when we delete the top-level
794 test_on_delete_cascade :: TestTree
795 test_on_delete_cascade = testGroup "cascading delete tests"
796 [ check "deleting odds deletes its children"
797 "test/xml/Odds_XML.xml"
798 13 -- 5 casinos, 8 teams
801 check "deleting odds deletes its children (non-int team_id)"
802 "test/xml/Odds_XML-noninteger-team-id.xml"
803 51 -- 5 casinos, 46 teams
806 check "deleting odds deleted its children (positive(+) line)"
807 "test/xml/Odds_XML-positive-line.xml"
808 17 -- 5 casinos, 12 teams
811 check "deleting odds deleted its children (large file)"
812 "test/xml/Odds_XML-largefile.xml"
813 189 -- 5 casinos, 184 teams
816 check desc path expected = testCase desc $ do
817 odds <- unsafe_unpickle path pickle_message
818 let a = undefined :: Odds
819 let b = undefined :: OddsCasino
820 let c = undefined :: OddsGameTeam
821 let d = undefined :: OddsGame
822 let e = undefined :: OddsGame_OddsGameTeam
823 let f = undefined :: OddsGameLine
824 actual <- withSqliteConn ":memory:" $ runDbConn $ do
825 runMigration silentMigrationLogger $ do
833 -- No idea how 'delete' works, so do this instead.
834 executeRaw False "DELETE FROM odds;" []
835 count_a <- countAll a
836 count_b <- countAll b
837 count_c <- countAll c
838 count_d <- countAll d
839 count_e <- countAll e
840 count_f <- countAll f
841 return $ sum [count_a, count_b, count_c,
842 count_d, count_e, count_f ]