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 contains
11 -- a root element \<message\> that contains a bunch of other
19 import Control.Monad ( forM_, join )
20 import Data.Tuple.Curry ( uncurryN )
21 import Database.Groundhog (
28 import Database.Groundhog.Core ( DefaultKey )
29 import Database.Groundhog.TH (
32 import Test.Tasty ( TestTree, testGroup )
33 import Test.Tasty.HUnit ( (@?=), testCase )
34 import Text.Read ( readMaybe )
35 import Text.XML.HXT.Core (
53 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
54 import TSN.XmlImport ( XmlImport(..) )
55 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
59 -- | The home/away lines are 'Double's, but the over/under lines are
60 -- textual. If we want to use one data type for both, we have to go
61 -- with a String and then attempt to 'read' a 'Double' later when we
62 -- go to insert the thing.
64 data OddsGameCasinoXml =
66 xml_casino_client_id :: Int,
67 xml_casino_name :: String,
68 xml_casino_line :: Maybe String }
72 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
73 -- priori textual (because it might be an over/under line).
75 home_away_line :: OddsGameCasinoXml -> Maybe Double
76 home_away_line = join . (fmap readMaybe) . xml_casino_line
78 -- | The casinos should have their own table, but the lines don't
79 -- belong in that table. (There should be another table joining the
80 -- casinos and the thing the lines are for together.)
82 -- We drop the 'Game' prefix because the Casinos really aren't
83 -- children of the games; the XML just makes it seem that way.
87 casino_client_id :: Int,
88 casino_name :: String }
91 instance FromXml OddsGameCasinoXml where
92 type Db OddsGameCasinoXml = OddsCasino
94 -- We don't need the key argument (from_xml_fk) since the XML type
95 -- contains more information in this case.
96 from_xml OddsGameCasinoXml{..} = OddsCasino
100 instance XmlImport OddsGameCasinoXml
103 data OddsGameHomeTeamXml =
104 OddsGameHomeTeamXml {
105 xml_home_team_id :: Int,
106 xml_home_rotation_number :: Int,
107 xml_home_abbr :: String,
108 xml_home_team_name :: String,
109 xml_home_casinos :: [OddsGameCasinoXml] }
112 instance FromXml OddsGameHomeTeamXml where
113 type Db OddsGameHomeTeamXml = OddsGameTeam
114 from_xml OddsGameHomeTeamXml{..} = OddsGameTeam
119 instance XmlImport OddsGameHomeTeamXml where
126 db_team_name :: String }
130 -- | Database mapping between games and their home/away teams.
131 data OddsGame_OddsGameTeam =
132 OddsGame_OddsGameTeam {
133 ogogt_odds_games_id :: DefaultKey OddsGame,
134 ogogt_away_team_id :: DefaultKey OddsGameTeam,
135 ogogt_home_team_id :: DefaultKey OddsGameTeam }
137 data OddsGameAwayTeamXml =
138 OddsGameAwayTeamXml {
139 xml_away_team_id :: Int,
140 xml_away_rotation_number :: Int,
141 xml_away_abbr :: String,
142 xml_away_team_name :: String,
143 xml_away_casinos :: [OddsGameCasinoXml] }
146 instance FromXml OddsGameAwayTeamXml where
147 type Db OddsGameAwayTeamXml = OddsGameTeam
148 from_xml OddsGameAwayTeamXml{..} = OddsGameTeam
153 instance XmlImport OddsGameAwayTeamXml where
155 -- | Can't use a newtype with Groundhog.
156 newtype OddsGameOverUnderXml =
157 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
160 -- | This database representation of the casino lines can't be
161 -- constructed from the one in the XML. The casinos within
162 -- Game>HomeTeam, Game>AwayTeam, and Game>Over_Under are all more or
163 -- less the same. We don't need a bajillion different tables to
164 -- store that, just one tying the casino/game pair to the three
167 -- The one small difference between the over/under casinos and the
168 -- home/away ones is that the home/away lines are all 'Double's, but
169 -- the over/under lines appear to be textual.
173 ogl_odds_games_id :: DefaultKey OddsGame,
174 ogl_odds_casinos_id :: DefaultKey OddsCasino,
175 ogl_over_under :: Maybe String,
176 ogl_away_line :: Maybe Double,
177 ogl_home_line :: Maybe Double }
182 db_game_date :: String, -- TODO
183 db_game_time :: String, -- TODO
184 db_game_away_team_rotation_number :: Int,
185 db_game_home_team_rotation_number :: Int }
186 deriving instance Eq OddsGame
187 deriving instance Show OddsGame
192 xml_game_date :: String, -- TODO
193 xml_game_time :: String, -- TODO
194 xml_game_away_team :: OddsGameAwayTeamXml,
195 xml_game_home_team :: OddsGameHomeTeamXml,
196 xml_game_over_under :: OddsGameOverUnderXml }
199 -- | Pseudo-field that lets us get the 'OddsCasino's out of
200 -- xml_game_over_under.
201 xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
202 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
205 instance FromXml OddsGameXml where
206 type Db OddsGameXml = OddsGame
207 from_xml OddsGameXml{..} = OddsGame
211 (xml_away_rotation_number xml_game_away_team)
212 (xml_home_rotation_number xml_game_home_team)
214 instance XmlImport OddsGameXml
222 db_line_time :: String }
225 -- | Map 'Odds' to their children 'OddsGame's.
228 oog_odds_id :: DefaultKey Odds,
229 oog_odds_games_id :: DefaultKey OddsGame }
231 -- | This is our best guess at what occurs in the Odds_XML
232 -- documents. It looks like each consecutive set of games can
233 -- optionally have some notes appear before it. Each "note" comes as
234 -- its own <Notes>...</Notes> element.
236 -- The notes are ignored completely in the database; we only bother
237 -- with them to ensure that we're (un)pickling correctly.
239 -- We can't group the notes with a "set" of 'OddsGame's, because that
240 -- leads to ambiguity in parsing. Since we're going to ignore the
241 -- notes anyway, we just stick them with an arbitrary game. C'est la
244 data OddsGameWithNotes =
247 game :: OddsGameXml }
250 -- | The XML representation of Odds.
253 xml_xml_file_id :: Int,
254 xml_heading :: String,
255 xml_category :: String,
258 xml_line_time :: String,
259 xml_games_with_notes :: [OddsGameWithNotes],
260 xml_time_stamp :: String }
263 -- | Pseudo-field that lets us get the 'OddsGame's out of
264 -- 'xml_games_with_notes'.
265 xml_games :: Message -> [OddsGameXml]
266 xml_games m = map game (xml_games_with_notes m)
269 instance FromXml Message where
270 type Db Message = Odds
272 -- We don't need the key argument (from_xml_fk) since the XML type
273 -- contains more information in this case.
274 from_xml (Message _ _ _ d e f _ _) =
277 instance XmlImport Message
281 -- * Groundhog database schema.
282 -- | This must come before the dbimport code.
284 mkPersist tsn_codegen_config [groundhog|
292 - name: unique_odds_casino
294 fields: [casino_client_id]
296 - entity: OddsGameTeam
297 dbName: odds_games_teams
301 - name: unique_odds_games_team
311 - name: unique_odds_game
315 - entity: OddsGameLine
316 dbName: odds_games_lines
318 - entity: Odds_OddsGame
319 dbName: odds__odds_games
321 - entity: OddsGame_OddsGameTeam
322 dbName: odds_games__odds_games_teams
325 instance DbImport Message where
328 migrate (undefined :: Odds)
329 migrate (undefined :: OddsCasino)
330 migrate (undefined :: OddsGameTeam)
331 migrate (undefined :: OddsGame)
332 migrate (undefined :: Odds_OddsGame)
333 migrate (undefined :: OddsGame_OddsGameTeam)
334 migrate (undefined :: OddsGameLine)
337 -- Insert the root "odds" element and acquire its primary key (id).
338 odds_id <- insert_xml m
340 -- Next, we insert the home and away teams. We do this before
341 -- inserting the game itself because the game has two foreign keys
342 -- pointing to odds_games_teams.
343 forM_ (xml_games m) $ \g -> do
344 game_id <- insert_xml_or_select g
345 -- Insert a record into odds__odds_game mapping this game
346 -- to its parent in the odds table.
347 insert_ (Odds_OddsGame odds_id game_id)
349 -- Next to insert the home and away teams.
350 away_team_id <- insert_xml_or_select (xml_game_away_team g)
351 home_team_id <- insert_xml_or_select (xml_game_home_team g)
353 -- Insert a record into odds_games__odds_games_teams
354 -- mapping the home/away teams to this game.
355 insert_ (OddsGame_OddsGameTeam game_id away_team_id home_team_id)
357 -- Finaly, we insert the lines. The over/under entries for this
358 -- game and the lines for the casinos all wind up in the same
359 -- table, odds_games_lines. We can insert the over/under entries
360 -- freely with empty away/home lines:
361 forM_ (xml_game_over_under_casinos g) $ \c -> do
362 -- Start by inderting the casino.
363 ou_casino_id <- insert_xml_or_select c
365 -- Now add the over/under entry with the casino's id.
366 let ogl = OddsGameLine
375 -- ...but then when we insert the home/away team lines, we
376 -- prefer to update the existing entry rather than overwrite it
377 -- or add a new record.
378 forM_ (xml_away_casinos $ xml_game_away_team g) $ \c ->do
379 -- insert, or more likely retrieve the existing, casino
380 a_casino_id <- insert_xml_or_select c
382 -- Get a Maybe Double instead of the Maybe String that's in there.
383 let away_line = home_away_line c
385 -- Unconditionally update that casino's away team line with ours.
386 update [Ogl_Away_Line =. away_line] $ -- WHERE
387 Ogl_Odds_Casinos_Id ==. a_casino_id
389 -- Repeat all that for the home team.
390 forM_ (xml_home_casinos $ xml_game_home_team g) $ \c ->do
391 h_casino_id <- insert_xml_or_select c
392 let home_line = home_away_line c
393 update [Ogl_Home_Line =. home_line] $ -- WHERE
394 Ogl_Odds_Casinos_Id ==. h_casino_id
398 return ImportSucceeded
400 pickle_game_with_notes :: PU OddsGameWithNotes
401 pickle_game_with_notes =
402 xpWrap (from_pair, to_pair) $
404 (xpList $ xpElem "Notes" xpText)
407 from_pair = uncurry OddsGameWithNotes
408 to_pair OddsGameWithNotes{..} = (notes, game)
412 pickle_casino :: PU OddsGameCasinoXml
415 xpWrap (from_tuple, to_tuple) $
417 (xpAttr "ClientID" xpInt)
418 (xpAttr "Name" xpText)
421 from_tuple = uncurryN OddsGameCasinoXml
422 -- Use record wildcards to avoid unused field warnings.
423 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
428 pickle_home_team :: PU OddsGameHomeTeamXml
431 xpWrap (from_tuple, to_tuple) $
433 (xpElem "HomeTeamID" xpInt)
434 (xpElem "HomeRotationNumber" xpInt)
435 (xpElem "HomeAbbr" xpText)
436 (xpElem "HomeTeamName" xpText)
437 (xpList pickle_casino)
439 from_tuple = uncurryN OddsGameHomeTeamXml
440 -- Use record wildcards to avoid unused field warnings.
441 to_tuple OddsGameHomeTeamXml{..} = (xml_home_team_id,
442 xml_home_rotation_number,
449 pickle_away_team :: PU OddsGameAwayTeamXml
452 xpWrap (from_tuple, to_tuple) $
454 (xpElem "AwayTeamID" xpInt)
455 (xpElem "AwayRotationNumber" xpInt)
456 (xpElem "AwayAbbr" xpText)
457 (xpElem "AwayTeamName" xpText)
458 (xpList pickle_casino)
460 from_tuple = uncurryN OddsGameAwayTeamXml
461 -- Use record wildcards to avoid unused field warnings.
462 to_tuple OddsGameAwayTeamXml{..} = (xml_away_team_id,
463 xml_away_rotation_number,
470 pickle_over_under :: PU OddsGameOverUnderXml
472 xpElem "Over_Under" $
473 xpWrap (to_newtype, from_newtype) $
476 from_newtype (OddsGameOverUnderXml cs) = cs
477 to_newtype = OddsGameOverUnderXml
481 pickle_game :: PU OddsGameXml
484 xpWrap (from_tuple, to_tuple) $
486 (xpElem "GameID" xpInt)
487 (xpElem "Game_Date" xpText)
488 (xpElem "Game_Time" xpText)
493 from_tuple = uncurryN OddsGameXml
494 -- Use record wildcards to avoid unused field warnings.
495 to_tuple OddsGameXml{..} = (xml_game_id,
503 pickle_message :: PU Message
506 xpWrap (from_tuple, to_tuple) $
507 xp8Tuple (xpElem "XML_File_ID" xpInt)
508 (xpElem "heading" xpText)
509 (xpElem "category" xpText)
510 (xpElem "sport" xpText)
511 (xpElem "Title" xpText)
512 (xpElem "Line_Time" xpText)
513 (xpList pickle_game_with_notes)
514 (xpElem "time_stamp" xpText)
516 from_tuple = uncurryN Message
517 to_tuple m = (xml_xml_file_id m,
523 xml_games_with_notes m,
529 odds_tests :: TestTree
533 [ test_pickle_of_unpickle_is_identity,
534 test_unpickle_succeeds ]
537 -- | Warning, succeess of this test does not mean that unpickling
539 test_pickle_of_unpickle_is_identity :: TestTree
540 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
541 [ check "pickle composed with unpickle is the identity"
542 "test/xml/Odds_XML.xml",
544 check "pickle composed with unpickle is the identity (positive(+) line)"
545 "test/xml/Odds_XML-positive-line.xml" ]
547 check desc path = testCase desc $ do
548 (expected, actual) <- pickle_unpickle pickle_message path
552 test_unpickle_succeeds :: TestTree
553 test_unpickle_succeeds = testGroup "unpickle tests"
554 [ check "unpickling succeeds"
555 "test/xml/Odds_XML.xml",
557 check "unpickling succeeds (positive(+) line)"
558 "test/xml/Odds_XML-positive-line.xml" ]
560 check desc path = testCase desc $ do
561 actual <- unpickleable path pickle_message