4
4
"""
5
5
6
6
# third party
7
+ import json
7
8
import mysql .connector
8
9
import numpy as np
10
+ from math import ceil
9
11
10
12
# first party
11
13
import delphi .operations .secrets as secrets
12
14
13
- from math import ceil
14
15
15
16
16
17
class CovidcastRow ():
@@ -543,7 +544,13 @@ def update_timeseries_direction_updated_timestamp(
543
544
def get_covidcast_meta (self ):
544
545
"""Compute and return metadata on all non-WIP COVIDcast signals."""
545
546
546
- sql = '''
547
+ meta = []
548
+
549
+ sql = 'SELECT `source`, `signal` FROM covidcast GROUP BY `source`, `signal` ORDER BY `source` ASC, `signal` ASC;'
550
+ self ._cursor .execute (sql )
551
+ for source , signal in [ss for ss in self ._cursor ]: #NOTE: this obfuscation protects the integrity of the cursor; using the cursor as a generator will cause contention w/ subsequent queries
552
+
553
+ sql = '''
547
554
SELECT
548
555
t.`source` AS `data_source`,
549
556
t.`signal`,
@@ -574,39 +581,38 @@ def get_covidcast_meta(self):
574
581
`geo_value`
575
582
FROM
576
583
`covidcast`
584
+ WHERE
585
+ `source` = %s AND
586
+ `signal` = %s
577
587
GROUP BY
578
588
`time_value`,
579
589
`time_type`,
580
590
`geo_type`,
581
- `source`,
582
- `signal`,
583
591
`geo_value`
584
592
) x
585
593
ON
586
594
x.`max_issue` = t.`issue` AND
587
595
x.`time_type` = t.`time_type` AND
588
596
x.`time_value` = t.`time_value` AND
589
- x.`source` = t.`source` AND
590
- x.`signal` = t.`signal` AND
591
597
x.`geo_type` = t.`geo_type` AND
592
598
x.`geo_value` = t.`geo_value`
593
599
WHERE
594
- NOT t.`is_wip`
600
+ NOT t.`is_wip` AND
601
+ t.`source` = %s AND
602
+ t.`signal` = %s
595
603
GROUP BY
596
- t.`source`,
597
- t.`signal`,
598
604
t.`time_type`,
599
605
t.`geo_type`
600
606
ORDER BY
601
- t.`source` ASC,
602
- t.`signal` ASC,
603
607
t.`time_type` ASC,
604
608
t.`geo_type` ASC
605
- '''
606
- self ._cursor .execute (sql )
607
- return list (dict (zip (self ._cursor .column_names ,x )) for x in self ._cursor )
609
+ '''
610
+ self ._cursor .execute (sql , ( source , signal , source , signal ) )
611
+ meta . extend ( list (dict (zip (self ._cursor .column_names ,x )) for x in self ._cursor ) )
608
612
609
- def update_covidcast_meta_cache (self , epidata_json ):
613
+ return meta
614
+
615
+ def update_covidcast_meta_cache (self , metadata ):
610
616
"""Updates the `covidcast_meta_cache` table."""
611
617
612
618
sql = '''
@@ -616,5 +622,21 @@ def update_covidcast_meta_cache(self, epidata_json):
616
622
`timestamp` = UNIX_TIMESTAMP(NOW()),
617
623
`epidata` = %s
618
624
'''
625
+ epidata_json = json .dumps (metadata )
619
626
620
627
self ._cursor .execute (sql , (epidata_json ,))
628
+
629
+ def retrieve_covidcast_meta_cache (self ):
630
+ sql = '''
631
+ SELECT `epidata`
632
+ FROM `covidcast_meta_cache`
633
+ ORDER BY `timestamp` DESC
634
+ LIMIT 1;
635
+ '''
636
+ self ._cursor .execute (sql )
637
+ cache_json = self ._cursor .fetchone ()[0 ]
638
+ cache = json .loads (cache_json )
639
+ cache_hash = {}
640
+ for entry in cache :
641
+ cache_hash [(entry ['data_source' ], entry ['signal' ], entry ['time_type' ], entry ['geo_type' ])] = entry
642
+ return cache_hash
0 commit comments