forked from cmu-delphi/delphi-epidata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
1501 lines (1459 loc) · 68.9 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
===============
=== Purpose ===
===============
An API for DELPHI's epidemiological data.
Documentation and sample code are on GitHub:
https://github.com/cmu-delphi/delphi-epidata
See also:
- fluview_update.py
- gft_update.py
- twitter_update.py
- wiki.py
- taiwan_update.py
- submission_loader.py
- ght_update.py
- sensor_update.py
- nowcast.py
- cdc_extract.py
- flusurv_update.py
- quidel_update.py
- norostat_update.py
- README.md
*/
// secrets
require_once('/var/www/html/secrets.php');
// helpers
require_once(__DIR__ . '/api_helpers.php');
// passwords
$AUTH = array(
'twitter' => Secrets::$api['twitter'],
'ght' => Secrets::$api['ght'],
'fluview' => Secrets::$api['fluview'],
'cdc' => Secrets::$api['cdc'],
'sensors' => Secrets::$api['sensors'],
'sensor_subsets' => Secrets::$api['sensor_subsets'],
'quidel' => Secrets::$api['quidel'],
'norostat' => Secrets::$api['norostat'],
'afhsb' => Secrets::$api['afhsb']
);
// begin sensor query authentication configuration
// A multimap of sensor names to the "granular" auth tokens that can be used to access them; excludes the "global" sensor auth key that works for all sensors:
$GRANULAR_SENSOR_AUTH_TOKENS = array(
'twtr' => array($AUTH['sensor_subsets']['twtr_sensor']),
'gft' => array($AUTH['sensor_subsets']['gft_sensor']),
'ght' => array($AUTH['sensor_subsets']['ght_sensors']),
'ghtj' => array($AUTH['sensor_subsets']['ght_sensors']),
'cdc' => array($AUTH['sensor_subsets']['cdc_sensor']),
'quid' => array($AUTH['sensor_subsets']['quid_sensor']),
'wiki' => array($AUTH['sensor_subsets']['wiki_sensor']),
);
// A set of sensors that do not require an auth key to access:
$OPEN_SENSORS = array(
'sar3',
'epic',
'arch',
);
// Limits on the number of effective auth token equality checks performed per sensor query; generate auth tokens with appropriate levels of entropy according to the limits below:
$MAX_GLOBAL_AUTH_CHECKS_PER_SENSOR_QUERY = 1; // (but imagine is larger to futureproof)
$MAX_GRANULAR_AUTH_CHECKS_PER_SENSOR_QUERY = 30; // (but imagine is larger to futureproof)
// A (currently redundant) limit on the number of auth tokens that can be provided:
$MAX_AUTH_KEYS_PROVIDED_PER_SENSOR_QUERY = 1;
// end sensor query authentication configuration
// result limit, ~10 years of daily data
$MAX_RESULTS = 3650;
// queries the `fluview` and `fluview_imputed` tables
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
// $authorized: determines whether private data (i.e. `fluview_imputed`) is
// included in the result
function get_fluview($epiweeks, $regions, $issues, $lag, $authorized) {
$epidata = array();
// public data
$table = '`fluview` fv';
$fields = "fv.`release_date`, fv.`issue`, fv.`epiweek`, fv.`region`, fv.`lag`, fv.`num_ili`, fv.`num_patients`, fv.`num_providers`, fv.`wili`, fv.`ili`, fv.`num_age_0`, fv.`num_age_1`, fv.`num_age_2`, fv.`num_age_3`, fv.`num_age_4`, fv.`num_age_5`";
_get_fluview_by_table($epidata, $epiweeks, $regions, $issues, $lag, $table, $fields);
if(!$authorized) {
// Make a special exception for New York. It is a (weighted) sum of two
// constituent locations -- "ny_minus_jfk" and "jfk" -- both of which are
// publicly available.
if(in_array('ny', array_map('strtolower', $regions))) {
$regions = array('ny');
$authorized = true;
}
}
if($authorized) {
// private data (no release date, no age groups, and wili is equal to ili)
$table = '`fluview_imputed` fv';
$fields = "NULL `release_date`, fv.`issue`, fv.`epiweek`, fv.`region`, fv.`lag`, fv.`num_ili`, fv.`num_patients`, fv.`num_providers`, fv.`ili` `wili`, fv.`ili`, NULL `num_age_0`, NULL `num_age_1`, NULL `num_age_2`, NULL `num_age_3`, NULL `num_age_4`, NULL `num_age_5`";
_get_fluview_by_table($epidata, $epiweeks, $regions, $issues, $lag, $table, $fields);
}
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// a helper function to query `fluview` and `fluview_imputed` individually
// parameters
function _get_fluview_by_table(&$epidata, $epiweeks, $regions, $issues, $lag, $table, $fields) {
// basic query info
$order = "fv.`epiweek` ASC, fv.`region` ASC, fv.`issue` ASC";
// build the epiweek filter
$condition_epiweek = filter_integers('fv.`epiweek`', $epiweeks);
// build the region filter
$condition_region = filter_strings('fv.`region`', $regions);
if($issues !== null) {
// build the issue filter
$condition_issue = filter_integers('fv.`issue`', $issues);
// final query using specific issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if($lag !== null) {
// build the lag filter
$condition_lag = "(fv.`lag` = {$lag})";
// final query using lagged issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// final query using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = fv.`issue` AND x.`epiweek` = fv.`epiweek` AND x.`region` = fv.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$fields_string = array('release_date', 'region');
$fields_int = array('issue', 'epiweek', 'lag', 'num_ili', 'num_patients', 'num_providers', 'num_age_0', 'num_age_1', 'num_age_2', 'num_age_3', 'num_age_4', 'num_age_5');
$fields_float = array('wili', 'ili');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
}
// queries the `fluview_clinical` table
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_fluview_clinical($epiweeks, $regions, $issues, $lag) {
// store the results in an array
$epidata = array();
// set up for query
$table = "`fluview_clinical` fvc";
// $fields = 'fvc.`release_date`, fvc.`issue`, fvc.`epiweek`, fvc.`region`, fvc.`lag`, fvc.`total_specimens`, fvc.`total_a_h1n1`, fvc.`total_a_h3`, fvc.`total_a_h3n2v`, fvc.`total_a_no_sub`, fvc.`total_b`, fvc.`total_b_vic`, fvc.`total_b_yam`';
$fields = "fvc.`release_date`, fvc.`issue`, fvc.`epiweek`, fvc.`region`, fvc.`lag`, fvc.`total_specimens`, fvc.`total_a`, fvc.`total_b`, fvc.`percent_positive`, fvc.`percent_a`, fvc.`percent_b`";
$order = "fvc.`epiweek` ASC, fvc.`region` ASC, fvc.`issue` ASC";
// create conditions
$condition_epiweek = filter_integers("fvc.`epiweek`", $epiweeks);
$condition_region = filter_strings("fvc.`region`", $regions);
if ($issues !== null) {
// using specific issues
$condition_issue = filter_integers("fvc.`issue`", $issues);
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if ($lag !== null) {
// using lagged issues
$condition_lag = '(fvc.`lag` = {$lag})';
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = fvc.`issue` AND x.`epiweek` = fvc.`epiweek` AND x.`region` = fvc.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$fields_string = array('release_date', 'region');
$fields_float = array('percent_positive', 'percent_a', 'percent_b');
$fields_int = array('issue', 'epiweek', 'lag', 'total_specimens', 'total_a', 'total_b');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the result, if any
return count($epidata) === 0 ? null : $epidata;
}
// queries the `flusurv` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of locations names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_flusurv($epiweeks, $locations, $issues, $lag) {
// basic query info
$table = '`flusurv` fs';
$fields = "fs.`release_date`, fs.`issue`, fs.`epiweek`, fs.`location`, fs.`lag`, fs.`rate_age_0`, fs.`rate_age_1`, fs.`rate_age_2`, fs.`rate_age_3`, fs.`rate_age_4`, fs.`rate_overall`";
$order = "fs.`epiweek` ASC, fs.`location` ASC, fs.`issue` ASC";
// build the epiweek filter
$condition_epiweek = filter_integers('fs.`epiweek`', $epiweeks);
// build the location filter
$condition_location = filter_strings('fs.`location`', $locations);
if($issues !== null) {
// build the issue filter
$condition_issue = filter_integers('fs.`issue`', $issues);
// final query using specific issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) AND ({$condition_issue}) ORDER BY {$order}";
} else if($lag !== null) {
// build the lag filter
$condition_lag = "(fs.`lag` = {$lag})";
// final query using lagged issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// final query using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `location` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) GROUP BY `epiweek`, `location`) x";
$condition = "x.`max_issue` = fs.`issue` AND x.`epiweek` = fs.`epiweek` AND x.`location` = fs.`location`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$epidata = array();
$fields_string = array('release_date', 'location');
$fields_int = array('issue', 'epiweek', 'lag');
$fields_float = array('rate_age_0', 'rate_age_1', 'rate_age_2', 'rate_age_3', 'rate_age_4', 'rate_overall');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `paho_dengue` table
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_paho_dengue($epiweeks, $regions, $issues, $lag) {
// store the results in an array
$epidata = array();
// set up for query
$table = "`paho_dengue` pd";
$fields = "pd.`release_date`, pd.`issue`, pd.`epiweek`, pd.`region`, pd.`lag`, pd.`total_pop`, pd.`serotype`, pd.`num_dengue`, pd.`incidence_rate`, pd.`num_severe`, pd.`num_deaths`";
$order = "pd.`epiweek` ASC, pd.`region` ASC, pd.`issue` ASC";
// create conditions
$condition_epiweek = filter_integers("pd.`epiweek`", $epiweeks);
$condition_region = filter_strings("pd.`region`", $regions);
if ($issues !== null) {
// using specific issues
$condition_issue = filter_integers("pd.`issue`", $issues);
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if ($lag !== null) {
// using lagged issues
$condition_lag = '(pd.`lag` = {$lag})';
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = pd.`issue` AND x.`epiweek` = pd.`epiweek` AND x.`region` = pd.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$fields_string = array('release_date', 'region', 'serotype');
$fields_float = array('incidence_rate');
$fields_int = array('issue', 'epiweek', 'lag', 'total_pop', 'num_dengue', 'num_severe', 'num_deaths');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the result, if any
return count($epidata) === 0 ? null : $epidata;
}
// queries the `ecdc_ili` table
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_ecdc_ili($epiweeks, $regions, $issues, $lag) {
// store the results in an array
$epidata = array();
// set up for query
$table = "`ecdc_ili` ec";
$fields = "ec.`release_date`, ec.`issue`, ec.`epiweek`, ec.`region`, ec.`lag`, ec.`incidence_rate`";
$order = "ec.`epiweek` ASC, ec.`region` ASC, ec.`issue` ASC";
// create conditions
$condition_epiweek = filter_integers("ec.`epiweek`", $epiweeks);
$condition_region = filter_strings("ec.`region`", $regions);
if ($issues !== null) {
// using specific issues
$condition_issue = filter_integers("ec.`issue`", $issues);
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if ($lag !== null) {
// using lagged issues
$condition_lag = '(ec.`lag` = {$lag})';
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = ec.`issue` AND x.`epiweek` = ec.`epiweek` AND x.`region` = ec.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$fields_string = array('release_date', 'region');
$fields_float = array('incidence_rate');
$fields_int = array('issue', 'epiweek', 'lag');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the result, if any
return count($epidata) === 0 ? null : $epidata;
}
// queries the `kcdc_ili` table
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_kcdc_ili($epiweeks, $regions, $issues, $lag) {
// store the results in an array
$epidata = array();
// set up for query
$table = "`kcdc_ili` kc";
$fields = "kc.`release_date`, kc.`issue`, kc.`epiweek`, kc.`region`, kc.`lag`, kc.`ili`";
$order = "kc.`epiweek` ASC, kc.`region` ASC, kc.`issue` ASC";
// create conditions
$condition_epiweek = filter_integers("kc.`epiweek`", $epiweeks);
$condition_region = filter_strings("kc.`region`", $regions);
if ($issues !== null) {
// using specific issues
$condition_issue = filter_integers("kc.`issue`", $issues);
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if ($lag !== null) {
// using lagged issues
$condition_lag = '(kc.`lag` = {$lag})';
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = kc.`issue` AND x.`epiweek` = kc.`epiweek` AND x.`region` = kc.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$fields_string = array('release_date', 'region');
$fields_float = array('ili');
$fields_int = array('issue', 'epiweek', 'lag');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the result, if any
return count($epidata) === 0 ? null : $epidata;
}
// queries the `gft` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of location names
function get_gft($epiweeks, $locations) {
// basic query info
$table = '`gft` g';
$fields = "g.`epiweek`, g.`location`, g.`num`";
$order = "g.`epiweek` ASC, g.`location` ASC";
// build the epiweek filter
$condition_epiweek = filter_integers('g.`epiweek`', $epiweeks);
// build the location filter
$condition_location = filter_strings('g.`location`', $locations);
// final query using specific issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, array('location'), array('epiweek', 'num'), null);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `ght` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of location names
// $query (required): search query or topic ID
function get_ght($epiweeks, $locations, $query) {
// basic query info
$table = '`ght` g';
$fields = "g.`epiweek`, g.`location`, g.`value`";
$order = "g.`epiweek` ASC, g.`location` ASC";
// build the epiweek filter
$condition_epiweek = filter_integers('g.`epiweek`', $epiweeks);
// build the location filter
$condition_location = filter_strings('g.`location`', $locations);
// build the query filter
$condition_query = filter_strings('g.`query`', array($query));
// final query using specific issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) AND ({$condition_query}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, array('location'), array('epiweek'), array('value'));
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `twitter` table
// $locations (required): array of location names
// $dates (required): array of date or epiweek values/ranges
// $resolution (required): either 'daily' or 'weekly'
function get_twitter($locations, $dates, $resolution) {
global $dbh;
// basic query info
$table = '`twitter` t';
// build the date filter and set field names
$fields_string = array('location');
$fields_int = array('num', 'total');
$fields_float = array('percent');
if($resolution === 'daily') {
$date_field = 't.`date`';
$date_name = 'date';
$condition_date = filter_dates($date_field, $dates);
array_push($fields_string, $date_name);
} else {
$date_field = 'yearweek(t.`date`, 6)';
$date_name = 'epiweek';
$condition_date = filter_integers($date_field, $dates);
array_push($fields_int, $date_name);
}
$fields = "{$date_field} `{$date_name}`, sum(t.`num`) `num`, sum(t.`total`) `total`, round(100 * sum(t.`num`) / sum(t.`total`), 8) `percent`";
// for consistency (some rows have low `total`, or `num` > `total`), filter out 2% of rows with highest `percent`
$condition_filter = 't.`num` / t.`total` <= 0.019';
// split locations into national/regional/state
$regions = array();
$states = array();
foreach($locations as $location) {
$location = strtolower($location);
if(in_array($location, array('nat', 'hhs1', 'hhs2', 'hhs3', 'hhs4', 'hhs5', 'hhs6', 'hhs7', 'hhs8', 'hhs9', 'hhs10', 'cen1', 'cen2', 'cen3', 'cen4', 'cen5', 'cen6', 'cen7', 'cen8', 'cen9'))) {
array_push($regions, $location);
} else {
array_push($states, $location);
}
}
// initialize the epidata array
$epidata = array();
// query each region type individually (the data is stored by state, so getting regional data requires some extra processing)
foreach($regions as $region) {
$region = mysqli_real_escape_string($dbh, $region);
if($region === 'nat') {
// final query for U.S. National
$query = "SELECT {$fields}, '{$region}' `location` FROM {$table} WHERE ({$condition_filter}) AND ({$condition_date}) GROUP BY {$date_field} ORDER BY {$date_field} ASC";
} else {
// build the location filter
$condition_location = "`state` IN (" . get_region_states($region) . ")";
// final query for HHS Regions
$query = "SELECT {$fields}, '{$region}' `location` FROM {$table} WHERE ({$condition_filter}) AND ({$condition_date}) AND ({$condition_location}) GROUP BY {$date_field} ORDER BY {$date_field} ASC";
}
// append query results to the epidata array
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
}
// query all states together
if(count($states) !== 0) {
// build the location filter
$condition_location = filter_strings('t.`state`', $states);
// final query for states
$query = "SELECT {$fields}, t.`state` `location` FROM {$table} WHERE ({$condition_filter}) AND ({$condition_date}) AND ({$condition_location}) GROUP BY {$date_field}, t.`state` ORDER BY {$date_field} ASC, t.`state` ASC";
// append query results to the epidata array
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
}
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `wiki` table
// $articles (required): array of article titles
// $language (required): specify the language of articles we want to retrieve
// $dates (required): array of date or epiweek values/ranges
// $resolution (required): either 'daily' or 'weekly'
// $hours (optional): array of hour values/ranges
// if present, $hours determines which counts are used within each day; otherwise all counts are used
// for example, if hours=[4], then only the 4 AM (UTC) stream is returned
function get_wiki($articles, $language, $dates, $resolution, $hours) {
// required for `mysqli_real_escape_string`
global $dbh;
$language = mysqli_real_escape_string($dbh, $language);
// basic query info
// in a few rare instances (~6 total), `total` is unreasonably high; something glitched somewhere, just ignore it
// $table = '`wiki` w JOIN (SELECT * FROM `wiki_meta` WHERE `total` < 100000000) m ON m.`datetime` = w.`datetime`';
// We select rows by language and then the problem is converted to the original one, and the rest of code can be same
$table = "( SELECT * FROM `wiki` WHERE `language` = '$language' ) w JOIN (SELECT * FROM `wiki_meta` WHERE `total` < 100000000 AND `language` = '$language' ) m ON m.`datetime` = w.`datetime`";
// build the date filter and set field names
$fields_string = array('article');
$fields_int = array('count', 'total', 'hour');
$fields_float = array('value');
if($resolution === 'daily') {
$date_field = 'm.`date`';
$date_name = 'date';
$condition_date = filter_dates($date_field, $dates);
array_push($fields_string, $date_name);
} else {
$date_field = 'm.`epiweek`';
$date_name = 'epiweek';
$condition_date = filter_integers($date_field, $dates);
array_push($fields_int, $date_name);
}
$fields = "{$date_field} `{$date_name}`, w.`article`, sum(w.`count`) `count`, sum(m.`total`) `total`, round(sum(w.`count`) / (sum(m.`total`) * 1e-6), 8) `value`";
// build the article filter
$condition_article = filter_strings('w.`article`', $articles);
if($hours !== null) {
// filter by specific hours
$condition_hour = filter_integers('hour(m.`datetime`)', $hours);
// final query, only taking counts from specific hours of the day
$query = "SELECT {$fields}, hour(m.`datetime`) `hour` FROM {$table} WHERE ({$condition_date}) AND ({$condition_article}) AND ({$condition_hour}) GROUP BY {$date_field}, w.`article`, hour(m.`datetime`) ORDER BY {$date_field} ASC, w.`article` ASC, hour(m.`datetime`) ASC";
} else {
// final query, summing over all hours of the day
$query = "SELECT {$fields}, -1 `hour` FROM {$table} WHERE ({$condition_date}) AND ({$condition_article}) GROUP BY {$date_field}, w.`article` ORDER BY {$date_field} ASC, w.`article` ASC";
}
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `quidel` table
// $locations (required): array of location names
// $epiweeks (required): array of epiweek values/ranges
function get_quidel($locations, $epiweeks) {
// basic query info
$table = '`quidel` q';
$fields = "q.`location`, q.`epiweek`, q.`value`";
$order = "q.`epiweek` ASC, q.`location` ASC";
// data type of each field
$fields_string = array('location');
$fields_int = array('epiweek');
$fields_float = array('value');
// build the location filter
$condition_location = filter_strings('q.`location`', $locations);
// build the epiweek filter
$condition_epiweek = filter_integers('q.`epiweek`', $epiweeks);
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_location}) AND ({$condition_epiweek}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `norostat_point` table
// $locations (required): single location value (str listing included states)
// $epiweeks (required): array of epiweek values/ranges
function get_norostat($location, $epiweeks) {
// todo add release/issue args
//
// build the filters:
$condition_location = filter_strings('`norostat_raw_datatable_location_pool`.`location`', [$location]);
$condition_epiweek = filter_integers('`latest`.`epiweek`', $epiweeks);
// get the data from the database
$epidata = array();
// (exclude "location" from output to reduce size & ugliness of result,
// transfer bandwidth required; it would just be a repeated echo of the input
// $location)
$fields_string = array('release_date');
$fields_int = array('epiweek', 'value');
$query = "
SELECT `latest`.`release_date`, `latest`.`epiweek`, `latest`.`new_value` AS `value`
FROM `norostat_point_diffs` AS `latest`
LEFT JOIN `norostat_raw_datatable_location_pool` USING (`location_id`)
LEFT JOIN (
SELECT * FROM `norostat_point_diffs`
) `later`
ON `latest`.`location_id` = `later`.`location_id` AND
`latest`.`epiweek` = `later`.`epiweek` AND
(`latest`.`release_date`, `latest`.`parse_time`) <
(`later`.`release_date`, `later`.`parse_time`) ANDou z z
`later`.`new_value` IS NOT NULL
WHERE ({$condition_location}) AND
({$condition_epiweek}) AND
`later`.`parse_time` IS NULL AND
`latest`.`new_value` IS NOT NULL
";
// xxx may reorder epiweeks
execute_query($query, $epidata, $fields_string, $fields_int, null);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `afhsb_00to13` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of location names
// $flu_types (required): array of flu types
function get_afhsb($locations, $epiweeks, $flu_types) {
global $dbh;
$epidata = array();
// split locations into national/regional/state
$location_dict = array("hhs" => array(), "cen" => array(),
"state" => array(), "country" => array());
foreach($locations as $location) {
$location = strtolower($location);
if(substr($location, 0, 3) === "hhs") {
array_push($location_dict["hhs"], $location);
} elseif (substr($location, 0, 3) === "cen") {
array_push($location_dict["cen"], $location);
} elseif (strlen($location) === 3) {
array_push($location_dict["country"], $location);
} elseif (strlen($location) === 2) {
array_push($location_dict["state"], $location);
}
}
// split flu types into disjoint/subset
$disjoint_flus = array();
$subset_flus = array();
foreach($flu_types as $flu_type) {
if(in_array($flu_type, array('flu1','flu2-flu1','flu3-flu2','ili-flu3'))) {
array_push($disjoint_flus, $flu_type);
} elseif(in_array($flu_type, array('flu2','flu3','ili'))) {
array_push($subset_flus, $flu_type);
}
}
foreach($location_dict as $location_type=>$locs) {
if(!empty($locs)) {
_get_afhsb_by_table($epidata, $location_type, $epiweeks, $locs, $disjoint_flus, $subset_flus);
}
}
return count($epidata) === 0 ? null : $epidata;
}
// A helper function to query afhsb tables
function _get_afhsb_by_table(&$epidata, $location_type, $epiweeks, $locations, $disjoint_flus, $subset_flus) {
// basic query info
$table = (in_array($location_type, array("hhs", "cen"))) ? "afhsb_00to13_region" : "afhsb_00to13_state";
$fields = "`epiweek`, `{$location_type}` `location`, sum(`visit_sum`) `visit_sum`";
$group = '`epiweek`, `location`';
$order = "`epiweek` ASC, `location` ASC";
$fields_string = array('location', 'flu_type');
$fields_int = array('epiweek', 'visit_sum');
// build the epiweek filter
$condition_epiweek = filter_integers('`epiweek`', $epiweeks);
// build the location filter
$condition_location = filter_strings($location_type, $locations);
// subset flu types: flu2, flu3, ili
$flu_mapping = array('flu2' => array('flu1','flu2-flu1'),
'flu3' => array('flu1','flu2-flu1','flu3-flu2'),
'ili' => array('flu1','flu2-flu1','flu3-flu2','ili-flu3'));
foreach($subset_flus as $subset_flu) {
$condition_flu = filter_strings('`flu_type`', $flu_mapping[$subset_flu]);
$query = "SELECT {$fields}, '{$subset_flu}' `flu_type` FROM {$table}
WHERE ({$condition_epiweek}) AND ({$condition_location}) AND ({$condition_flu})
GROUP BY {$group} ORDER BY {$order}";
execute_query($query, $epidata, $fields_string, $fields_int, null);
}
// disjoint flu types: flu1, flu2-flu1, flu3-flu2, ili-flu3
if(!empty($disjoint_flus)){
$condition_flu = filter_strings('`flu_type`', $disjoint_flus);
$query = "SELECT {$fields}, `flu_type` FROM {$table}
WHERE ({$condition_epiweek}) AND ({$condition_location}) AND ({$condition_flu})
GROUP BY {$group},`flu_type` ORDER BY {$order},`flu_type`";
execute_query($query, $epidata, $fields_string, $fields_int, null);
}
}
// queries the `nidss_flu` table
// $epiweeks (required): array of epiweek values/ranges
// $regions (required): array of region names
// $issues (optional): array of epiweek values/ranges
// overrides $lag
// default: most recent issue
// $lag (optional): number of weeks between each epiweek and its issue
// overridden by $issues
// default: most recent issue
function get_nidss_flu($epiweeks, $regions, $issues, $lag) {
// basic query info
$table = '`nidss_flu` nf';
$fields = "nf.`release_date`, nf.`issue`, nf.`epiweek`, nf.`region`, nf.`lag`, nf.`visits`, nf.`ili`";
$order = "nf.`epiweek` ASC, nf.`region` ASC, nf.`issue` ASC";
// build the epiweek filter
$condition_epiweek = filter_integers('nf.`epiweek`', $epiweeks);
// build the region filter
$condition_region = filter_strings('nf.`region`', $regions);
if($issues !== null) {
// build the issue filter
$condition_issue = filter_integers('nf.`issue`', $issues);
// final query using specific issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_issue}) ORDER BY {$order}";
} else if($lag !== null) {
// build the lag filter
$condition_lag = "(nf.`lag` = {$lag})";
// final query using lagged issues
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) AND ({$condition_lag}) ORDER BY {$order}";
} else {
// final query using most recent issues
$subquery = "(SELECT max(`issue`) `max_issue`, `epiweek`, `region` FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_region}) GROUP BY `epiweek`, `region`) x";
$condition = "x.`max_issue` = nf.`issue` AND x.`epiweek` = nf.`epiweek` AND x.`region` = nf.`region`";
$query = "SELECT {$fields} FROM {$table} JOIN {$subquery} ON {$condition} ORDER BY {$order}";
}
// get the data from the database
$epidata = array();
$fields_string = array('release_date', 'region');
$fields_int = array('issue', 'epiweek', 'lag', 'visits');
$fields_float = array('ili');
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `nidss_dengue` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of region and/or location names
function get_nidss_dengue($epiweeks, $locations) {
global $dbh;
// build the epiweek filter
$condition_epiweek = filter_integers('nd.`epiweek`', $epiweeks);
// get the data from the database
$epidata = array();
$fields_string = array('location');
$fields_int = array('epiweek', 'count');
foreach($locations as $location) {
$location = mysqli_real_escape_string($dbh, $location);
$query = "
SELECT
nd2.`epiweek`, nd2.`location`, count(1) `num_locations`, sum(nd2.`count`) `count`
FROM (
SELECT
nd1.`epiweek`, CASE WHEN q.`query` = nd1.`location` THEN nd1.`location` WHEN q.`query` = nd1.`region` THEN nd1.`region` ELSE nd1.`nat` END `location`, nd1.`count`
FROM (
SELECT
`epiweek`, `location`, `region`, 'nationwide' `nat`, `count`
FROM
`nidss_dengue` nd
WHERE {$condition_epiweek}
) nd1
JOIN (
SELECT
'{$location}' `query`
) q
ON
q.`query` IN (nd1.`location`, nd1.`region`, nd1.`nat`)
) nd2
GROUP BY
nd2.`epiweek`, nd2.`location`
";
execute_query($query, $epidata, $fields_string, $fields_int, null);
}
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `forecasts` table
// $system (required): system name
// $epiweek (required): epiweek on which the forecast was made
function get_forecast($system, $epiweek) {
global $dbh;
// get the data from the database
$system = mysqli_real_escape_string($dbh, $system);
$query = "SELECT `system`, `epiweek`, `json` FROM `forecasts` WHERE `system` = '{$system}' AND `epiweek` = {$epiweek}";
$epidata = array();
$fields_string = array('system', 'json');
$fields_int = array('epiweek');
execute_query($query, $epidata, $fields_string, $fields_int, null);
// parse forecast data
if(count($epidata) === 1 && array_key_exists('json', $epidata[0])) {
$epidata[0]['forecast'] = json_decode($epidata[0]['json']);
unset($epidata[0]['json']);
}
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `cdc_extract` table
// $epiweeks (required): array of epiweek values/ranges
// $locations (required): array of location names
function get_cdc($epiweeks, $locations) {
global $dbh;
// basic query info
$table = '`cdc_extract` c';
$group = "c.`epiweek`";
$order = "c.`epiweek` ASC";
$fields_string = array('location');
$fields_int = array('epiweek', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'total');
// build the epiweek filter
$condition_epiweek = filter_integers('c.`epiweek`', $epiweeks);
// split locations into national/regional/state
$regions = array();
$states = array();
foreach($locations as $location) {
$location = strtolower($location);
if(in_array($location, array('nat', 'hhs1', 'hhs2', 'hhs3', 'hhs4', 'hhs5', 'hhs6', 'hhs7', 'hhs8', 'hhs9', 'hhs10', 'cen1', 'cen2', 'cen3', 'cen4', 'cen5', 'cen6', 'cen7', 'cen8', 'cen9'))) {
array_push($regions, $location);
} else {
array_push($states, $location);
}
}
// initialize the epidata array
$epidata = array();
// query each region type individually (the data is stored by state, so getting regional data requires some extra processing)
foreach($regions as $region) {
$region = mysqli_real_escape_string($dbh, $region);
$fields = "'{$region}' `location`, c.`epiweek`, sum(c.`num1`) `num1`, sum(c.`num2`) `num2`, sum(c.`num3`) `num3`, sum(c.`num4`) `num4`, sum(c.`num5`) `num5`, sum(c.`num6`) `num6`, sum(c.`num7`) `num7`, sum(c.`num8`) `num8`, sum(c.`total`) `total`";
if($region === 'nat') {
// final query for U.S. National
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) GROUP BY {$group} ORDER BY {$order}";
} else {
// build the location filter
$condition_location = "`state` IN (" . get_region_states($region) . ")";
// final query for HHS Regions
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) GROUP BY {$group} ORDER BY {$order}";
}
// append query results to the epidata array
execute_query($query, $epidata, $fields_string, $fields_int, null);
}
// query all states together
if(count($states) !== 0) {
$fields = "c.`state` `location`, c.`epiweek`, c.`num1`, c.`num2`, c.`num3`, c.`num4`, c.`num5`, c.`num6`, c.`num7`, c.`num8`, c.`total`";
// build the location filter
$condition_location = filter_strings('c.`state`', $states);
// final query for states
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_epiweek}) AND ({$condition_location}) ORDER BY {$order}, c.`state` ASC";
// append query results to the epidata array
execute_query($query, $epidata, $fields_string, $fields_int, null);
}
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `sensors` table
// $names (required): array of sensor names
// $locations (required): array of location names
// $epiweeks (required): array of epiweek values/ranges
function get_sensors($names, $locations, $epiweeks) {
// basic query info
$table = '`sensors` s';
$fields = "s.`name`, s.`location`, s.`epiweek`, s.`value`";
$order = "s.`epiweek` ASC, s.`name` ASC, s.`location` ASC";
// data type of each field
$fields_string = array('name', 'location');
$fields_int = array('epiweek');
$fields_float = array('value');
// build the name filter
$condition_name = filter_strings('s.`name`', $names);
// build the location filter
$condition_location = filter_strings('s.`location`', $locations);
// build the epiweek filter
$condition_epiweek = filter_integers('s.`epiweek`', $epiweeks);
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_name}) AND ({$condition_location}) AND ({$condition_epiweek}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `dengue_sensors` table
// $names (required): array of sensor names
// $locations (required): array of location names
// $epiweeks (required): array of epiweek values/ranges
function get_dengue_sensors($names, $locations, $epiweeks) {
// basic query info
$table = '`dengue_sensors` s';
$fields = "s.`name`, s.`location`, s.`epiweek`, s.`value`";
$order = "s.`epiweek` ASC, s.`name` ASC, s.`location` ASC";
// data type of each field
$fields_string = array('name', 'location');
$fields_int = array('epiweek');
$fields_float = array('value');
// build the name filter
$condition_name = filter_strings('s.`name`', $names);
// build the location filter
$condition_location = filter_strings('s.`location`', $locations);
// build the epiweek filter
$condition_epiweek = filter_integers('s.`epiweek`', $epiweeks);
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_name}) AND ({$condition_location}) AND ({$condition_epiweek}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `nowcasts` table
// $locations (required): array of location names
// $epiweeks (required): array of epiweek values/ranges
function get_nowcast($locations, $epiweeks) {
// basic query info
$table = '`nowcasts` n';
$fields = "n.`location`, n.`epiweek`, n.`value`, n.`std`";
$order = "n.`epiweek` ASC, n.`location` ASC";
// data type of each field
$fields_string = array('location');
$fields_int = array('epiweek');
$fields_float = array('value', 'std');
// build the location filter
$condition_location = filter_strings('n.`location`', $locations);
// build the epiweek filter
$condition_epiweek = filter_integers('n.`epiweek`', $epiweeks);
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_location}) AND ({$condition_epiweek}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `dengue_nowcasts` table
// $locations (required): array of location names
// $epiweeks (required): array of epiweek values/ranges
function get_dengue_nowcast($locations, $epiweeks) {
// basic query info
$table = '`dengue_nowcasts` n';
$fields = "n.`location`, n.`epiweek`, n.`value`, n.`std`";
$order = "n.`epiweek` ASC, n.`location` ASC";
// data type of each field
$fields_string = array('location');
$fields_int = array('epiweek');
$fields_float = array('value', 'std');
// build the location filter
$condition_location = filter_strings('n.`location`', $locations);
// build the epiweek filter
$condition_epiweek = filter_integers('n.`epiweek`', $epiweeks);
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_location}) AND ({$condition_epiweek}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `covidcast` table.
// $source (required): name of upstream data souce
// $signal (required): name of signal derived from upstream data
// $time_type (required): temporal resolution (e.g. day, week)
// $geo_type (required): spatial resolution (e.g. county, msa, state)
// $time_values (required): array of time values/ranges
// $geo_value (required): location identifier or `*` as a wildcard for all
// locations (specific to `$geo_type`)
function get_covidcast($source, $signal, $time_type, $geo_type, $time_values, $geo_value) {
// required for `mysqli_real_escape_string`
global $dbh;
$source = mysqli_real_escape_string($dbh, $source);
$signal = mysqli_real_escape_string($dbh, $signal);
$time_type = mysqli_real_escape_string($dbh, $time_type);
$geo_type = mysqli_real_escape_string($dbh, $geo_type);
$geo_value = mysqli_real_escape_string($dbh, $geo_value);
// basic query info
$table = '`covidcast` t';
$fields = "t.`time_value`, t.`geo_value`, t.`value`, t.`stderr`, t.`sample_size`, t.`direction`";
$order = "t.`time_value` ASC, t.`geo_value` ASC";
// data type of each field
$fields_string = array('geo_value');
$fields_int = array('time_value', 'direction');
$fields_float = array('value', 'stderr', 'sample_size');
// build the source, signal, time, and location (type and id) filters
$condition_source = "t.`source` = '{$source}'";
$condition_signal = "t.`signal` = '{$signal}'";
$condition_time_type = "t.`time_type` = '{$time_type}'";
$condition_geo_type = "t.`geo_type` = '{$geo_type}'";
$condition_time_value = filter_integers('t.`time_value`', $time_values);
if ($geo_value === '*') {
// the wildcard query should return data for all locations in `geo_type`
$condition_geo_value = 'TRUE';
} else {
// return data for a particular location
$condition_geo_value = "t.`geo_value` = '{$geo_value}'";
}
// the query
$query = "SELECT {$fields} FROM {$table} WHERE ({$condition_source}) AND ({$condition_signal}) AND ({$condition_time_type}) AND ({$condition_geo_type}) AND ({$condition_time_value}) AND ({$condition_geo_value}) ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `covidcast` table for metadata only.
function get_covidcast_meta() {
// basic query info
$table = '`covidcast` t';
$fields = "t.`source` AS `data_source`, t.`signal`, t.`time_type`, t.`geo_type`, MIN(t.`time_value`) AS `min_time`, MAX(t.`time_value`) AS `max_time`, COUNT(DISTINCT `geo_value`) AS `num_locations`, MIN(`value`) AS `min_value`, MAX(`value`) AS `max_value`, AVG(`value`) AS `mean_value`, STD(`value`) AS `stdev_value`, MAX(`timestamp1`) AS `last_update`";
$condition_wip = "t.`signal` NOT LIKE 'wip_%'";
$group = "t.`source`, t.`signal`, t.`time_type`, t.`geo_type`";
$order = "t.`source` ASC, t.`signal` ASC, t.`time_type` ASC, t.`geo_type` ASC";
// data type of each field
$fields_string = array('data_source', 'signal', 'time_type', 'geo_type');
$fields_int = array('min_time', 'max_time', 'num_locations', 'last_update');
$fields_float = array('min_value', 'max_value', 'mean_value', 'stdev_value');
// the query
$query = "SELECT {$fields} FROM {$table} WHERE {$condition_wip} GROUP BY {$group} ORDER BY {$order}";
// get the data from the database
$epidata = array();
execute_query($query, $epidata, $fields_string, $fields_int, $fields_float);
// return the data
return count($epidata) === 0 ? null : $epidata;
}
// queries the `covidcast` table for metadata only. attempts to used the cached
// version, falling back to `get_covidcast_meta` otherwise.
function get_covidcast_meta_cached() {
// only use the cache if it's less than ~1 hour old (75 minutes max to allow
// for some wiggle room)
$max_age = 75 * 60;
// basic query info