1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
9 -- | Parse TSN XML for the DTD \"weatherxml.dtd\". Each document
10 -- contains a bunch of forecasts, which each contain zero or more
11 -- leagues, which in turn (each) contain a bunch of listings.
13 module TSN.XML.Weather (
20 -- * WARNING: these are private but exported to silence warnings
21 WeatherConstructor(..),
22 WeatherDetailedWeatherListingItemConstructor(..),
23 WeatherForecastConstructor(..),
24 WeatherForecastListingConstructor(..) )
28 import Control.Monad ( forM_ )
29 import Data.Time ( UTCTime )
30 import Data.Tuple.Curry ( uncurryN )
31 import qualified Data.Vector.HFixed as H ( HVector, cons, convert )
32 import Database.Groundhog (
37 import Database.Groundhog.Core ( DefaultKey )
38 import Database.Groundhog.Generic ( runDbConn, runMigrationSilent )
39 import Database.Groundhog.Sqlite ( withSqliteConn )
40 import Database.Groundhog.TH (
43 import qualified GHC.Generics as GHC ( Generic )
44 import Test.Tasty ( TestTree, testGroup )
45 import Test.Tasty.HUnit ( (@?=), testCase )
46 import Text.XML.HXT.Core (
73 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
74 import TSN.Picklers ( xp_datetime, xp_gamedate, xp_time_stamp )
75 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
88 -- | The DTD to which this module corresponds. Used to invoke dbimport.
91 dtd = "weatherxml.dtd"
98 -- * WeatherForecastListing/WeatherForecastListingXml
100 -- | XML representation of a weather forecast listing.
102 data WeatherForecastListingXml =
103 WeatherForecastListingXml {
105 xml_weather :: Maybe String }
106 deriving (Eq, GHC.Generic, Show)
109 -- | For 'H.convert'.
111 instance H.HVector WeatherForecastListingXml
114 -- | Database representation of a weather forecast listing. The
115 -- 'db_league_name' field should come from the containing \<league\>
116 -- element which is not stored in the database.
118 data WeatherForecastListing =
119 WeatherForecastListing {
120 db_weather_forecasts_id :: DefaultKey WeatherForecast,
121 db_league_name :: Maybe String,
123 db_weather :: Maybe String }
126 -- | We don't make 'WeatherForecastListingXml' an instance of
127 -- 'FromXmlFk' because it needs some additional information, namely
128 -- the league name from its containing \<league\> element.
130 -- When supplied with a forecast id and a league name, this will
131 -- turn an XML listing into a database one.
133 from_xml_fk_league :: DefaultKey WeatherForecast
135 -> WeatherForecastListingXml
136 -> WeatherForecastListing
137 from_xml_fk_league fk ln WeatherForecastListingXml{..} =
138 WeatherForecastListing {
139 db_weather_forecasts_id = fk,
141 db_teams = xml_teams,
142 db_weather = xml_weather }
147 -- | XML representation of a league, as they appear in the weather
148 -- documents. There is no associated database representation because
149 -- the league element really adds no information besides its own
150 -- (usually empty) name. The leagues contain listings, so we
151 -- associate the league name with each listing instead.
155 league_name :: Maybe String,
156 listings :: [WeatherForecastListingXml] }
160 -- * WeatherForecast/WeatherForecastXml
162 -- | Database representation of a weather forecast.
164 data WeatherForecast =
166 db_weather_id :: DefaultKey Weather,
167 db_game_date :: UTCTime }
170 -- | XML representation of a weather forecast.
172 data WeatherForecastXml =
174 xml_game_date :: UTCTime,
175 xml_leagues :: [WeatherLeague] }
176 deriving (Eq, GHC.Generic, Show)
179 -- | For 'H.convert'.
181 instance H.HVector WeatherForecastXml
184 instance ToDb WeatherForecastXml where
185 -- | The database representation of a 'WeatherForecastXml' is a
186 -- 'WeatherForecast'.
188 type Db WeatherForecastXml = WeatherForecast
191 instance Child WeatherForecastXml where
192 -- | The database type containing a 'WeatherForecastXml' is
194 type Parent WeatherForecastXml = Weather
197 instance FromXmlFk WeatherForecastXml where
199 -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we
200 -- add the foreign key to the containing 'Weather', and copy the
203 -- This is so short it's pointless to do it generically.
205 from_xml_fk fk WeatherForecastXml{..} =
208 db_game_date = xml_game_date }
211 -- | This allows us to call 'insert_xml' on an 'WeatherForecastXml'
212 -- without first converting it to the database representation.
214 instance XmlImportFk WeatherForecastXml
216 -- * WeatherDetailedWeatherXml
218 -- | XML Representation of a \<Detailed_Weather\>, which just contains
219 -- a bunch iof \<DW_Listing\>s. There is no associated database type
220 -- since these don't really contain any information.
222 data WeatherDetailedWeatherXml =
223 WeatherDetailedWeatherXml {
224 xml_detailed_listings :: [WeatherDetailedWeatherListingXml] }
228 -- * WeatherDetailedWeatherXml
230 -- | XML Representation of a \<DW_Listing\>. The sport and sport code
231 -- come as attributes, but then these just contain a bunch of
232 -- \<Item\>s. There is no associated database type since these don't
233 -- contain much information. The sport we already know from the
234 -- \<message\>, while the sport code is ignored since it's already
235 -- present in each \<Item\>s.
237 data WeatherDetailedWeatherListingXml =
238 WeatherDetailedWeatherListingXml
239 String -- xml_dtl_listing_sport, unused
240 String -- xml_dtl_listing_sport_code, unused
241 [WeatherDetailedWeatherListingItemXml] -- xml_items
242 deriving (Eq, GHC.Generic, Show)
245 -- | Accessor for the third field of WeatherDetailedWeatherListingXml.
246 -- We don't use field names to avoid the unused field warnings that
247 -- we'd otherwise get for the first two fields.
249 xml_items :: WeatherDetailedWeatherListingXml
250 -> [WeatherDetailedWeatherListingItemXml]
251 xml_items (WeatherDetailedWeatherListingXml _ _ items) = items
254 -- | For 'H.convert'.
256 instance H.HVector WeatherDetailedWeatherListingXml
259 -- * WeatherDetailedWeatherListingItem / WeatherDetailedWeatherListingItemXml
261 -- | Database representation of a detailed weather item. The away/home
262 -- teams don't use the representation in "TSN.Team" because all
263 -- we're given is a name, and a team id is required for "TSN.Team".
265 -- We also drop the sport name, because it's given in the parent
268 -- The leading underscores prevent unused field warnings.
270 data WeatherDetailedWeatherListingItem =
271 WeatherDetailedWeatherListingItem {
272 _db_dtl_weather_id :: DefaultKey Weather, -- ^ Avoid name collision by
273 -- using \"dtl\" prefix.
274 _db_sport_code :: String,
276 _db_dtl_game_date :: UTCTime, -- ^ Avoid name clash with \"dtl\" prefix
277 _db_away_team :: String,
278 _db_home_team :: String,
279 _db_weather_type :: Int,
280 _db_description :: Maybe String,
281 _db_temp_adjust :: Maybe String,
282 _db_temperature :: Int }
283 deriving ( GHC.Generic )
285 -- | For 'H.cons' and 'H.convert'.
287 instance H.HVector WeatherDetailedWeatherListingItem
290 -- | XML representation of a detailed weather item. Same as the
291 -- database representation, only without the foreign key and the
292 -- sport name that comes from the containing listing.
294 -- The leading underscores prevent unused field warnings.
296 data WeatherDetailedWeatherListingItemXml =
297 WeatherDetailedWeatherListingItemXml {
298 _xml_sport_code :: String,
300 _xml_dtl_game_date :: UTCTime,
301 _xml_away_team :: String,
302 _xml_home_team :: String,
303 _xml_weather_type :: Int,
304 _xml_description :: Maybe String,
305 _xml_temp_adjust :: Maybe String,
306 _xml_temperature :: Int }
307 deriving (Eq, GHC.Generic, Show)
310 -- | For 'H.convert'.
312 instance H.HVector WeatherDetailedWeatherListingItemXml
314 instance ToDb WeatherDetailedWeatherListingItemXml where
315 -- | Our database analogue is a 'WeatherDetailedWeatherListingItem'.
316 type Db WeatherDetailedWeatherListingItemXml =
317 WeatherDetailedWeatherListingItem
319 instance Child WeatherDetailedWeatherListingItemXml where
320 -- | We skip two levels of containers and say that the items belong
321 -- to the top-level 'Weather'.
322 type Parent WeatherDetailedWeatherListingItemXml = Weather
324 instance FromXmlFk WeatherDetailedWeatherListingItemXml where
325 -- | To convert from the XML to database representation, we simply
326 -- add the foreign key (to Weather) and copy the rest of the fields.
329 -- | This allows us to insert the XML representation directly without
330 -- having to do the manual XML -\> DB conversion.
332 instance XmlImportFk WeatherDetailedWeatherListingItemXml
336 -- | The database representation of a weather message. We don't
337 -- contain the forecasts or the detailed weather since those are
338 -- foreigned-keyed to us.
342 db_xml_file_id :: Int,
345 db_time_stamp :: UTCTime }
348 -- | The XML representation of a weather message.
352 xml_xml_file_id :: Int,
353 xml_heading :: String,
354 xml_category :: String,
357 xml_forecasts :: [WeatherForecastXml],
358 xml_detailed_weather :: Maybe WeatherDetailedWeatherXml,
359 xml_time_stamp :: UTCTime }
360 deriving (Eq, GHC.Generic, Show)
363 -- | For 'H.convert'.
365 instance H.HVector Message
368 instance ToDb Message where
369 -- | The database representation of 'Message' is 'Weather'.
371 type Db Message = Weather
373 instance FromXml Message where
374 -- | To get a 'Weather' from a 'Message', we drop a bunch of
377 from_xml Message{..} =
379 db_xml_file_id = xml_xml_file_id,
380 db_sport = xml_sport,
381 db_title = xml_title,
382 db_time_stamp = xml_time_stamp }
384 -- | This allows us to insert the XML representation 'Message'
387 instance XmlImport Message
394 mkPersist tsn_codegen_config [groundhog|
399 - name: unique_weather
401 # Prevent multiple imports of the same message.
402 fields: [db_xml_file_id]
404 - entity: WeatherForecast
405 dbName: weather_forecasts
407 - name: WeatherForecast
409 - name: db_weather_id
413 - entity: WeatherForecastListing
414 dbName: weather_forecast_listings
416 - name: WeatherForecastListing
418 - name: db_weather_forecasts_id
422 # We rename the two fields that needed a "dtl" prefix to avoid a name
424 - entity: WeatherDetailedWeatherListingItem
425 dbName: weather_detailed_items
427 - name: WeatherDetailedWeatherListingItem
429 - name: _db_dtl_weather_id
433 - name: _db_dtl_game_date
440 -- | There are two different types of documents that claim to be
441 -- \"weatherxml.dtd\". The first, more common type has listings
442 -- within forecasts. The second type has forecasts within
443 -- listings. Clearly we can't parse both of these using the same
446 -- For now we're simply punting on the issue and refusing to parse
447 -- the second type. This will check the given @xmltree@ to see if
448 -- there are any forecasts contained within listings. If there are,
449 -- then it's the second type that we don't know what to do with.
451 is_type1 :: XmlTree -> Bool
457 parse :: XmlTree -> [XmlTree]
458 parse = runLA $ hasName "/"
461 /> hasName "forecast"
463 elements = parse xmltree
466 -- | Some weatherxml documents even have the Home/Away teams in the
467 -- wrong order. We can't parse that! This next bit of voodoo detects
468 -- whether or not there are any \<HomeTeam\> elements that are
469 -- directly followed by sibling \<AwayTeam\> elements. This is the
470 -- opposite of the usual order.
472 teams_are_normal :: XmlTree -> Bool
473 teams_are_normal xmltree =
478 parse :: XmlTree -> [XmlTree]
479 parse = runLA $ hasName "/"
481 /> hasName "Detailed_Weather"
482 /> hasName "DW_Listing"
486 >>> filterAxis (hasName "HomeTeam")
487 >>> followingSiblingAxis
489 >>> hasName "AwayTeam"
491 elements = parse xmltree
494 instance DbImport Message where
497 migrate (undefined :: Weather)
498 migrate (undefined :: WeatherForecast)
499 migrate (undefined :: WeatherForecastListing)
500 migrate (undefined :: WeatherDetailedWeatherListingItem)
503 -- First we insert the top-level weather record.
504 weather_id <- insert_xml m
506 -- Next insert all of the forecasts, one at a time.
507 forM_ (xml_forecasts m) $ \forecast -> do
508 forecast_id <- insert_xml_fk weather_id forecast
510 -- With the forecast id in hand, loop through this forecast's
512 forM_ (xml_leagues forecast) $ \league -> do
513 -- Construct the function that converts an XML listing to a
515 let todb = from_xml_fk_league forecast_id (league_name league)
517 -- Now use it to convert all of the XML listings.
518 let db_listings = map todb (listings league)
520 -- And finally, insert those DB listings.
521 mapM_ insert_ db_listings
523 -- Now we do the detailed weather items.
524 case (xml_detailed_weather m) of
527 let detailed_listings = xml_detailed_listings dw
528 let items = concatMap xml_items detailed_listings
529 mapM_ (insert_xml_fk_ weather_id) items
531 return ImportSucceeded
538 -- | Pickler to convert a 'WeatherForecastListingXml' to/from XML.
540 pickle_listing :: PU WeatherForecastListingXml
543 xpWrap (from_pair, H.convert) $
545 (xpElem "teams" xpText)
546 (xpElem "weather" (xpOption xpText))
548 from_pair = uncurry WeatherForecastListingXml
552 -- | Pickler to convert a 'WeatherLeague' to/from XML.
554 pickle_league :: PU WeatherLeague
557 xpWrap (from_pair, to_pair) $
559 (xpAttr "name" $ xpOption xpText)
560 (xpList pickle_listing)
562 from_pair = uncurry WeatherLeague
563 to_pair WeatherLeague{..} = (league_name, listings)
566 -- | Pickler to convert a 'WeatherForecastXml' to/from XML.
568 pickle_forecast :: PU WeatherForecastXml
571 xpWrap (from_pair, to_pair) $
573 (xpAttr "gamedate" xp_gamedate)
574 (xpList pickle_league)
576 from_pair = uncurry WeatherForecastXml
577 to_pair WeatherForecastXml{..} = (xml_game_date,
582 -- | (Un)pickle a 'WeatherDetailedWeatherListingItemXml'.
584 pickle_item :: PU WeatherDetailedWeatherListingItemXml
587 xpWrap (from_tuple, H.convert) $
588 xp9Tuple (xpElem "Sportcode" xpText)
589 (xpElem "GameID" xpInt)
590 (xpElem "Gamedate" xp_datetime)
591 (xpElem "AwayTeam" xpText)
592 (xpElem "HomeTeam" xpText)
593 (xpElem "WeatherType" xpInt)
594 (xpElem "Description" (xpOption xpText))
595 (xpElem "TempAdjust" (xpOption xpText))
596 (xpElem "Temperature" xpInt)
598 from_tuple = uncurryN WeatherDetailedWeatherListingItemXml
602 -- | (Un)pickle a 'WeatherDetailedWeatherListingXml'.
604 pickle_dw_listing :: PU WeatherDetailedWeatherListingXml
606 xpElem "DW_Listing" $
607 xpWrap (from_tuple, H.convert) $
608 xpTriple (xpAttr "SportCode" xpText)
609 (xpAttr "Sport" xpText)
612 from_tuple = uncurryN WeatherDetailedWeatherListingXml
615 -- | (Un)pickle a 'WeatherDetailedWeatherXml'
617 pickle_detailed_weather :: PU WeatherDetailedWeatherXml
618 pickle_detailed_weather =
619 xpElem "Detailed_Weather" $
620 xpWrap (WeatherDetailedWeatherXml, xml_detailed_listings)
621 (xpList pickle_dw_listing)
624 -- | Pickler to convert a 'Message' to/from XML.
626 pickle_message :: PU Message
629 xpWrap (from_tuple, H.convert) $
631 (xpElem "XML_File_ID" xpInt)
632 (xpElem "heading" xpText)
633 (xpElem "category" xpText)
634 (xpElem "sport" xpText)
635 (xpElem "title" xpText)
636 (xpList pickle_forecast)
637 (xpOption pickle_detailed_weather)
638 (xpElem "time_stamp" xp_time_stamp)
640 from_tuple = uncurryN Message
646 weather_tests :: TestTree
650 [ test_on_delete_cascade,
651 test_pickle_of_unpickle_is_identity,
652 test_unpickle_succeeds,
653 test_types_detected_correctly,
654 test_normal_teams_detected_correctly ]
657 -- | If we unpickle something and then pickle it, we should wind up
658 -- with the same thing we started with. WARNING: success of this
659 -- test does not mean that unpickling succeeded.
661 test_pickle_of_unpickle_is_identity :: TestTree
662 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
663 [ check "pickle composed with unpickle is the identity"
664 "test/xml/weatherxml.xml",
666 check "pickle composed with unpickle is the identity (detailed)"
667 "test/xml/weatherxml-detailed.xml" ]
669 check desc path = testCase desc $ do
670 (expected, actual) <- pickle_unpickle pickle_message path
674 -- | Make sure we can actually unpickle these things.
676 test_unpickle_succeeds :: TestTree
677 test_unpickle_succeeds = testGroup "unpickle tests"
678 [ check "unpickling succeeds"
679 "test/xml/weatherxml.xml",
680 check "unpickling succeeds (detailed)"
681 "test/xml/weatherxml-detailed.xml",
682 check "unpickling succeeds (empty weather)"
683 "test/xml/weatherxml-empty-weather.xml"]
685 check desc path = testCase desc $ do
686 actual <- unpickleable path pickle_message
691 -- | Make sure everything gets deleted when we delete the top-level
694 test_on_delete_cascade :: TestTree
695 test_on_delete_cascade = testGroup "cascading delete tests"
696 [ check "deleting weather deletes its children"
697 "test/xml/weatherxml.xml",
698 check "deleting weather deletes its children (detailed)"
699 "test/xml/weatherxml-detailed.xml",
700 check "deleting weather deletes its children (empty weather)"
701 "test/xml/weatherxml-empty-weather.xml"]
703 check desc path = testCase desc $ do
704 weather <- unsafe_unpickle path pickle_message
705 let a = undefined :: Weather
706 let b = undefined :: WeatherForecast
707 let c = undefined :: WeatherForecastListing
708 let d = undefined :: WeatherDetailedWeatherListingItem
709 actual <- withSqliteConn ":memory:" $ runDbConn $ do
710 runMigrationSilent $ do
715 _ <- dbimport weather
717 count_a <- countAll a
718 count_b <- countAll b
719 count_c <- countAll c
720 count_d <- countAll d
721 return $ count_a + count_b + count_c + count_d
726 -- | We want to make sure type1 documents are detected as type1, and
727 -- type2 documents detected as type2..
729 test_types_detected_correctly :: TestTree
730 test_types_detected_correctly =
731 testGroup "weatherxml types detected correctly"
732 [ check "test/xml/weatherxml.xml"
733 "first type detected correctly"
735 check "test/xml/weatherxml-detailed.xml"
736 "first type detected correctly (detailed)"
738 check "test/xml/weatherxml-empty-weather.xml"
739 "first type detected correctly (empty weather)"
741 check "test/xml/weatherxml-type2.xml"
742 "second type detected correctly"
745 check path desc expected = testCase desc $ do
746 xmltree <- unsafe_read_document path
747 let actual = is_type1 xmltree
751 -- | We want to make sure normal teams are detected as normal, and the
752 -- backwards ones are flagged as backwards.
754 test_normal_teams_detected_correctly :: TestTree
755 test_normal_teams_detected_correctly =
756 testGroup "team order is detected correctly" [
758 check "normal teams are detected correctly"
759 "test/xml/weatherxml.xml"
762 check "backwards teams are detected correctly"
763 "test/xml/weatherxml-backwards-teams.xml"
766 check desc path expected = testCase desc $ do
767 xmltree <- unsafe_read_document path
768 let actual = teams_are_normal xmltree