33# standard library
44import argparse
55import sys
6+ import time
67
78# first party
89from delphi .epidata .acquisition .covidcast .database import Database
10+ from delphi .epidata .acquisition .covidcast .logger import get_structured_logger
911from delphi .epidata .client .delphi_epidata import Epidata
10-
12+ from delphi . operations import secrets
1113
1214def get_argument_parser ():
1315 """Define command line arguments."""
1416
15- # there are no flags, but --help will still work
16- return argparse .ArgumentParser ()
17+ parser = argparse .ArgumentParser ()
18+ parser .add_argument ("--log_file" , help = "filename for log output" )
19+ return parser
1720
1821
1922def main (args , epidata_impl = Epidata , database_impl = Database ):
2023 """Update the covidcast metadata cache.
2124
2225 `args`: parsed command-line arguments
2326 """
27+
28+ secrets .db .host = 'delphi_database_epidata'
29+ secrets .db .epi = ('user' , 'pass' )
30+
31+ logger = get_structured_logger ("metadata_cache_updater" , filename = args .log_file )
32+ start_time = time .time ()
2433 database = database_impl ()
2534 database .connect ()
2635
2736 # fetch metadata
2837 try :
38+ metadata_calculation_start_time = time .time ()
2939 metadata = database .get_covidcast_meta ()
40+ metadata_calculation_interval_in_seconds = metadata_calculation_start_time - start_time
3041 except :
3142 # clean up before failing
3243 database .disconnect (True )
@@ -35,7 +46,7 @@ def main(args, epidata_impl=Epidata, database_impl=Database):
3546 args = ("success" ,1 )
3647 if len (metadata )== 0 :
3748 args = ("no results" ,- 2 )
38-
49+
3950 print ('covidcast_meta result: %s (code %d)' % args )
4051
4152 if args [- 1 ] != 1 :
@@ -44,13 +55,19 @@ def main(args, epidata_impl=Epidata, database_impl=Database):
4455
4556 # update the cache
4657 try :
58+ metadata_update_start_time = time .time ()
4759 database .update_covidcast_meta_cache (metadata )
60+ metadata_update_interval_in_seconds = time .time () - metadata_update_start_time
4861 print ('successfully cached epidata' )
4962 finally :
5063 # no catch block so that an exception above will cause the program to
5164 # fail after the following cleanup
5265 database .disconnect (True )
5366
67+ logger .info ("Generated and updated covidcast metadata" ,
68+ metadata_calculation_interval_in_seconds = round (metadata_calculation_interval_in_seconds , 2 ),
69+ metadata_update_interval_in_seconds = round (metadata_update_interval_in_seconds , 2 ),
70+ total_runtime_in_seconds = round (time .time () - start_time , 2 ))
5471 return True
5572
5673
0 commit comments