1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | Parse TSN XML for the DTD \"weatherxml.dtd\". Each document
9 -- contains a bunch of forecasts, which each contain zero or more
10 -- leagues, which in turn (each) contain a bunch of listings.
12 module TSN.XML.Weather (
19 -- * WARNING: these are private but exported to silence warnings
20 WeatherConstructor(..),
21 WeatherDetailedWeatherListingItemConstructor(..),
22 WeatherForecastConstructor(..),
23 WeatherForecastListingConstructor(..) )
27 import Control.Monad ( forM_ )
28 import Data.Time ( UTCTime )
29 import Data.Tuple.Curry ( uncurryN )
30 import Database.Groundhog (
36 silentMigrationLogger )
37 import Database.Groundhog.Core ( DefaultKey )
38 import Database.Groundhog.Generic ( runDbConn )
39 import Database.Groundhog.Sqlite ( withSqliteConn )
40 import Database.Groundhog.TH (
43 import Test.Tasty ( TestTree, testGroup )
44 import Test.Tasty.HUnit ( (@?=), testCase )
45 import Text.XML.HXT.Core (
74 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
75 import TSN.Picklers ( xp_datetime, xp_gamedate, xp_time_stamp )
76 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
89 -- | The DTD to which this module corresponds. Used to invoke dbimport.
92 dtd = "weatherxml.dtd"
99 -- * WeatherForecastListing/WeatherForecastListingXml
101 -- | XML representation of a weather forecast listing.
103 data WeatherForecastListingXml =
104 WeatherForecastListingXml {
106 xml_weather :: String }
110 -- | Database representation of a weather forecast listing. The
111 -- 'db_league_name' field should come from the containing \<league\>
112 -- element which is not stored in the database.
114 data WeatherForecastListing =
115 WeatherForecastListing {
116 db_weather_forecasts_id :: DefaultKey WeatherForecast,
117 db_league_name :: Maybe String,
119 db_weather :: String }
122 -- | We don't make 'WeatherForecastListingXml' an instance of
123 -- 'FromXmlFk' because it needs some additional information, namely
124 -- the league name from its containing \<league\> element.
126 -- When supplied with a forecast id and a league name, this will
127 -- turn an XML listing into a database one.
129 from_xml_fk_league :: DefaultKey WeatherForecast
131 -> WeatherForecastListingXml
132 -> WeatherForecastListing
133 from_xml_fk_league fk ln WeatherForecastListingXml{..} =
134 WeatherForecastListing {
135 db_weather_forecasts_id = fk,
137 db_teams = xml_teams,
138 db_weather = xml_weather }
143 -- | XML representation of a league, as they appear in the weather
144 -- documents. There is no associated database representation because
145 -- the league element really adds no information besides its own
146 -- (usually empty) name. The leagues contain listings, so we
147 -- associate the league name with each listing instead.
151 league_name :: Maybe String,
152 listings :: [WeatherForecastListingXml] }
156 -- * WeatherForecast/WeatherForecastXml
158 -- | Database representation of a weather forecast.
160 data WeatherForecast =
162 db_weather_id :: DefaultKey Weather,
163 db_game_date :: UTCTime }
166 -- | XML representation of a weather forecast.
168 data WeatherForecastXml =
170 xml_game_date :: UTCTime,
171 xml_leagues :: [WeatherLeague] }
175 instance ToDb WeatherForecastXml where
176 -- | The database representation of a 'WeatherForecastXml' is a
177 -- 'WeatherForecast'.
179 type Db WeatherForecastXml = WeatherForecast
182 instance Child WeatherForecastXml where
183 -- | The database type containing a 'WeatherForecastXml' is
185 type Parent WeatherForecastXml = Weather
188 instance FromXmlFk WeatherForecastXml where
190 -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we
191 -- add the foreign key to the containing 'Weather', and copy the
194 from_xml_fk fk WeatherForecastXml{..} =
197 db_game_date = xml_game_date }
200 -- | This allows us to call 'insert_xml' on an 'WeatherForecastXml'
201 -- without first converting it to the database representation.
203 instance XmlImportFk WeatherForecastXml
205 -- * WeatherDetailedWeatherXml
207 -- | XML Representation of a \<Detailed_Weather\>, which just contains
208 -- a bunch iof \<DW_Listing\>s. There is no associated database type
209 -- since these don't really contain any information.
211 data WeatherDetailedWeatherXml =
212 WeatherDetailedWeatherXml {
213 xml_detailed_listings :: [WeatherDetailedWeatherListingXml] }
217 -- * WeatherDetailedWeatherXml
219 -- | XML Representation of a \<DW_Listing\>. The sport and sport code
220 -- come as attributes, but then these just contain a bunch of
221 -- \<Item\>s. There is no associated database type since these don't
222 -- contain much information. The sport we already know from the
223 -- \<message\>, while the sport code is ignored since it's already
224 -- present in each \<Item\>s.
226 data WeatherDetailedWeatherListingXml =
227 WeatherDetailedWeatherListingXml {
228 xml_dtl_listing_sport :: String,
229 xml_dtl_listing_sport_code :: String,
230 xml_items :: [WeatherDetailedWeatherListingItemXml] }
233 -- * WeatherDetailedWeatherListingItem / WeatherDetailedWeatherListingItemXml
235 -- | Database representation of a detailed weather item. The away/home
236 -- teams don't use the representation in "TSN.Team" because all
237 -- we're given is a name, and a team id is required for "TSN.Team".
239 -- We also drop the sport name, because it's given in the parent
242 data WeatherDetailedWeatherListingItem =
243 WeatherDetailedWeatherListingItem {
244 db_dtl_weather_id :: DefaultKey Weather, -- ^ Avoid name collision by
245 -- using \"dtl\" prefix.
246 db_sport_code :: String,
248 db_dtl_game_date :: UTCTime, -- ^ Avoid name clash with \"dtl\" prefix
249 db_away_team :: String,
250 db_home_team :: String,
251 db_weather_type :: Int,
252 db_description :: String,
253 db_temp_adjust :: Maybe String,
254 db_temperature :: Int }
257 -- | XML representation of a detailed weather item. Same as the
258 -- database representation, only without the foreign key and the
259 -- sport name that comes from the containing listing.
260 data WeatherDetailedWeatherListingItemXml =
261 WeatherDetailedWeatherListingItemXml {
262 xml_sport_code :: String,
264 xml_dtl_game_date :: UTCTime,
265 xml_away_team :: String,
266 xml_home_team :: String,
267 xml_weather_type :: Int,
268 xml_description :: String,
269 xml_temp_adjust :: Maybe String,
270 xml_temperature :: Int }
274 instance ToDb WeatherDetailedWeatherListingItemXml where
275 -- | Our database analogue is a 'WeatherDetailedWeatherListingItem'.
276 type Db WeatherDetailedWeatherListingItemXml =
277 WeatherDetailedWeatherListingItem
279 instance Child WeatherDetailedWeatherListingItemXml where
280 -- | We skip two levels of containers and say that the items belong
281 -- to the top-level 'Weather'.
282 type Parent WeatherDetailedWeatherListingItemXml = Weather
284 instance FromXmlFk WeatherDetailedWeatherListingItemXml where
285 -- | To convert from the XML to database representation, we simply
286 -- add the foreign key (to Weather) and copy the rest of the fields.
287 from_xml_fk fk WeatherDetailedWeatherListingItemXml{..} =
288 WeatherDetailedWeatherListingItem {
289 db_dtl_weather_id = fk,
290 db_sport_code = xml_sport_code,
291 db_game_id = xml_game_id,
292 db_dtl_game_date = xml_dtl_game_date,
293 db_away_team = xml_away_team,
294 db_home_team = xml_home_team,
295 db_weather_type = xml_weather_type,
296 db_description = xml_description,
297 db_temp_adjust = xml_temp_adjust,
298 db_temperature = xml_temperature }
300 -- | This allows us to insert the XML representation directly without
301 -- having to do the manual XML -\> DB conversion.
303 instance XmlImportFk WeatherDetailedWeatherListingItemXml
307 -- | The database representation of a weather message. We don't
308 -- contain the forecasts or the detailed weather since those are
309 -- foreigned-keyed to us.
313 db_xml_file_id :: Int,
316 db_time_stamp :: UTCTime }
319 -- | The XML representation of a weather message.
323 xml_xml_file_id :: Int,
324 xml_heading :: String,
325 xml_category :: String,
328 xml_forecasts :: [WeatherForecastXml],
329 xml_detailed_weather :: Maybe WeatherDetailedWeatherXml,
330 xml_time_stamp :: UTCTime }
333 instance ToDb Message where
334 -- | The database representation of 'Message' is 'Weather'.
336 type Db Message = Weather
338 instance FromXml Message where
339 -- | To get a 'Weather' from a 'Message', we drop a bunch of
342 from_xml Message{..} =
344 db_xml_file_id = xml_xml_file_id,
345 db_sport = xml_sport,
346 db_title = xml_title,
347 db_time_stamp = xml_time_stamp }
349 -- | This allows us to insert the XML representation 'Message'
352 instance XmlImport Message
359 mkPersist tsn_codegen_config [groundhog|
364 - name: unique_weather
366 # Prevent multiple imports of the same message.
367 fields: [db_xml_file_id]
369 - entity: WeatherForecast
370 dbName: weather_forecasts
372 - name: WeatherForecast
374 - name: db_weather_id
378 - entity: WeatherForecastListing
379 dbName: weather_forecast_listings
381 - name: WeatherForecastListing
383 - name: db_weather_forecasts_id
387 # We rename the two fields that needed a "dtl" prefix to avoid a name clash.
388 - entity: WeatherDetailedWeatherListingItem
389 dbName: weather_detailed_items
391 - name: WeatherDetailedWeatherListingItem
393 - name: db_dtl_weather_id
397 - name: db_dtl_game_date
404 -- | There are two different types of documents that claim to be
405 -- \"weatherxml.dtd\". The first, more common type has listings
406 -- within forecasts. The second type has forecasts within
407 -- listings. Clearly we can't parse both of these using the same
410 -- For now we're simply punting on the issue and refusing to parse
411 -- the second type. This will check the given @xmltree@ to see if
412 -- there are any forecasts contained within listings. If there are,
413 -- then it's the second type that we don't know what to do with.
415 is_type1 :: XmlTree -> Bool
421 parse :: XmlTree -> [XmlTree]
422 parse = runLA $ hasName "/"
425 /> hasName "forecast"
427 elements = parse xmltree
430 -- | Some weatherxml documents even have the Home/Away teams in the
431 -- wrong order. We can't parse that! This next bit of voodoo detects
432 -- whether or not there are any \<HomeTeam\> elements that are
433 -- directly followed by sibling \<AwayTeam\> elements. This is the
434 -- opposite of the usual order.
436 teams_are_normal :: XmlTree -> Bool
437 teams_are_normal xmltree =
442 parse :: XmlTree -> [XmlTree]
443 parse = runLA $ hasName "/"
445 /> hasName "Detailed_Weather"
446 /> hasName "DW_Listing"
450 >>> filterAxis (hasName "HomeTeam")
451 >>> followingSiblingAxis
453 >>> hasName "AwayTeam"
455 elements = parse xmltree
458 instance DbImport Message where
461 migrate (undefined :: Weather)
462 migrate (undefined :: WeatherForecast)
463 migrate (undefined :: WeatherForecastListing)
464 migrate (undefined :: WeatherDetailedWeatherListingItem)
467 -- First we insert the top-level weather record.
468 weather_id <- insert_xml m
470 -- Next insert all of the forecasts, one at a time.
471 forM_ (xml_forecasts m) $ \forecast -> do
472 forecast_id <- insert_xml_fk weather_id forecast
474 -- With the forecast id in hand, loop through this forecast's
476 forM_ (xml_leagues forecast) $ \league -> do
477 -- Construct the function that converts an XML listing to a
479 let todb = from_xml_fk_league forecast_id (league_name league)
481 -- Now use it to convert all of the XML listings.
482 let db_listings = map todb (listings league)
484 -- And finally, insert those DB listings.
485 mapM_ insert_ db_listings
487 -- Now we do the detailed weather items.
488 case (xml_detailed_weather m) of
491 let detailed_listings = xml_detailed_listings dw
492 let items = concatMap xml_items detailed_listings
493 mapM_ (insert_xml_fk_ weather_id) items
495 return ImportSucceeded
502 -- | Pickler to convert a 'WeatherForecastListingXml' to/from XML.
504 pickle_listing :: PU WeatherForecastListingXml
507 xpWrap (from_pair, to_pair) $
509 (xpElem "teams" xpText)
510 (xpElem "weather" xpText)
512 from_pair = uncurry WeatherForecastListingXml
513 to_pair WeatherForecastListingXml{..} = (xml_teams, xml_weather)
516 -- | Pickler to convert a 'WeatherLeague' to/from XML.
518 pickle_league :: PU WeatherLeague
521 xpWrap (from_pair, to_pair) $
523 (xpAttr "name" $ xpOption xpText)
524 (xpList pickle_listing)
526 from_pair = uncurry WeatherLeague
527 to_pair WeatherLeague{..} = (league_name, listings)
530 -- | Pickler to convert a 'WeatherForecastXml' to/from XML.
532 pickle_forecast :: PU WeatherForecastXml
535 xpWrap (from_pair, to_pair) $
537 (xpAttr "gamedate" xp_gamedate)
538 (xpList pickle_league)
540 from_pair = uncurry WeatherForecastXml
541 to_pair WeatherForecastXml{..} = (xml_game_date,
546 -- | (Un)pickle a 'WeatherDetailedWeatherListingItemXml'.
548 pickle_item :: PU WeatherDetailedWeatherListingItemXml
551 xpWrap (from_tuple, to_tuple) $
552 xp9Tuple (xpElem "Sportcode" xpText)
553 (xpElem "GameID" xpInt)
554 (xpElem "Gamedate" xp_datetime)
555 (xpElem "AwayTeam" xpText)
556 (xpElem "HomeTeam" xpText)
557 (xpElem "WeatherType" xpInt)
558 (xpElem "Description" xpText)
559 (xpElem "TempAdjust" (xpOption xpText))
560 (xpElem "Temperature" xpInt)
562 from_tuple = uncurryN WeatherDetailedWeatherListingItemXml
563 to_tuple w = (xml_sport_code w,
574 -- | (Un)pickle a 'WeatherDetailedWeatherListingXml'.
576 pickle_dw_listing :: PU WeatherDetailedWeatherListingXml
578 xpElem "DW_Listing" $
579 xpWrap (from_tuple, to_tuple) $
580 xpTriple (xpAttr "SportCode" xpText)
581 (xpAttr "Sport" xpText)
584 from_tuple = uncurryN WeatherDetailedWeatherListingXml
585 to_tuple w = (xml_dtl_listing_sport w,
586 xml_dtl_listing_sport_code w,
590 -- | (Un)pickle a 'WeatherDetailedWeatherXml'
592 pickle_detailed_weather :: PU WeatherDetailedWeatherXml
593 pickle_detailed_weather =
594 xpElem "Detailed_Weather" $
595 xpWrap (WeatherDetailedWeatherXml, xml_detailed_listings)
596 (xpList pickle_dw_listing)
599 -- | Pickler to convert a 'Message' to/from XML.
601 pickle_message :: PU Message
604 xpWrap (from_tuple, to_tuple) $
606 (xpElem "XML_File_ID" xpInt)
607 (xpElem "heading" xpText)
608 (xpElem "category" xpText)
609 (xpElem "sport" xpText)
610 (xpElem "title" xpText)
611 (xpList pickle_forecast)
612 (xpOption pickle_detailed_weather)
613 (xpElem "time_stamp" xp_time_stamp)
615 from_tuple = uncurryN Message
616 to_tuple Message{..} = (xml_xml_file_id,
622 xml_detailed_weather,
629 weather_tests :: TestTree
633 [ test_on_delete_cascade,
634 test_pickle_of_unpickle_is_identity,
635 test_unpickle_succeeds,
636 test_types_detected_correctly,
637 test_normal_teams_detected_correctly ]
640 -- | If we unpickle something and then pickle it, we should wind up
641 -- with the same thing we started with. WARNING: success of this
642 -- test does not mean that unpickling succeeded.
644 test_pickle_of_unpickle_is_identity :: TestTree
645 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
646 [ check "pickle composed with unpickle is the identity"
647 "test/xml/weatherxml.xml",
649 check "pickle composed with unpickle is the identity (detailed)"
650 "test/xml/weatherxml-detailed.xml" ]
652 check desc path = testCase desc $ do
653 (expected, actual) <- pickle_unpickle pickle_message path
657 -- | Make sure we can actually unpickle these things.
659 test_unpickle_succeeds :: TestTree
660 test_unpickle_succeeds = testGroup "unpickle tests"
661 [ check "unpickling succeeds"
662 "test/xml/weatherxml.xml",
663 check "unpickling succeeds (detailed)"
664 "test/xml/weatherxml-detailed.xml" ]
666 check desc path = testCase desc $ do
667 actual <- unpickleable path pickle_message
672 -- | Make sure everything gets deleted when we delete the top-level
675 test_on_delete_cascade :: TestTree
676 test_on_delete_cascade = testGroup "cascading delete tests"
677 [ check "deleting weather deletes its children"
678 "test/xml/weatherxml.xml",
679 check "deleting weather deletes its children (detailed)"
680 "test/xml/weatherxml-detailed.xml" ]
682 check desc path = testCase desc $ do
683 weather <- unsafe_unpickle path pickle_message
684 let a = undefined :: Weather
685 let b = undefined :: WeatherForecast
686 let c = undefined :: WeatherForecastListing
687 let d = undefined :: WeatherDetailedWeatherListingItem
688 actual <- withSqliteConn ":memory:" $ runDbConn $ do
689 runMigration silentMigrationLogger $ do
694 _ <- dbimport weather
696 count_a <- countAll a
697 count_b <- countAll b
698 count_c <- countAll c
699 count_d <- countAll d
700 return $ count_a + count_b + count_c + count_d
705 -- | This is used in a few tests to extract an 'XmlTree' from a path.
707 unsafe_get_xmltree :: String -> IO XmlTree
708 unsafe_get_xmltree path =
709 fmap head $ runX $ readDocument parse_opts path
712 -- | We want to make sure type1 documents are detected as type1, and
713 -- type2 documents detected as type2..
715 test_types_detected_correctly :: TestTree
716 test_types_detected_correctly =
717 testGroup "weatherxml types detected correctly" $
718 [ check "test/xml/weatherxml.xml"
719 "first type detected correctly"
721 check "test/xml/weatherxml-detailed.xml"
722 "first type detected correctly (detailed)"
724 check "test/xml/weatherxml-type2.xml"
725 "second type detected correctly"
728 check path desc expected = testCase desc $ do
729 xmltree <- unsafe_get_xmltree path
730 let actual = is_type1 xmltree
734 -- | We want to make sure normal teams are detected as normal, and the
735 -- backwards ones are flagged as backwards.
737 test_normal_teams_detected_correctly :: TestTree
738 test_normal_teams_detected_correctly =
739 testGroup "team order is detected correctly" [
741 check "normal teams are detected correctly"
742 "test/xml/weatherxml.xml"
745 check "backwards teams are detected correctly"
746 "test/xml/weatherxml-backwards-teams.xml"
749 check desc path expected = testCase desc $ do
750 xmltree <- unsafe_get_xmltree path
751 let actual = teams_are_normal xmltree