3
3
from celery import chain , shared_task
4
4
from django .conf import settings
5
5
6
- from apps .etl .extraction .sources .glide .extract import GlideExtraction
6
+ from apps .etl .extraction .sources .glide .extract import GlideExtraction , GlideQueryVars
7
7
from apps .etl .models import ExtractionData , HazardType
8
8
from apps .etl .transform .sources .glide import GlideTransformHandler
9
9
10
10
GLIDE_HAZARDS = [
11
- ( "EQ" , HazardType .EARTHQUAKE ) ,
12
- ( "TC" , HazardType .CYCLONE ) ,
13
- ( "FL" , HazardType .FLOOD ) ,
14
- ( "DR" , HazardType .DROUGHT ) ,
15
- ( "WF" , HazardType .WILDFIRE ) ,
16
- ( "VO" , HazardType .VOLCANO ) ,
17
- ( "TS" , HazardType .TSUNAMI ) ,
18
- ( "CW" , HazardType .COLDWAVE ) ,
19
- ( "EP" , HazardType .EPIDEMIC ) ,
20
- ( "EC" , HazardType .EXTRATROPICAL_CYCLONE ) ,
21
- ( "ET" , HazardType .EXTREME_TEMPERATURE ) ,
22
- ( "FR" , HazardType .FIRE ) ,
23
- ( "FF" , HazardType .FLASH_FLOOD ) ,
24
- ( "HT" , HazardType .HEAT_WAVE ) ,
25
- ( "IN" , HazardType .INSECT_INFESTATION ) ,
26
- ( "LS" , HazardType .LANDSLIDE ) ,
27
- ( "MS" , HazardType .MUD_SLIDE ) ,
28
- ( "ST" , HazardType .SEVERE_LOCAL_STROM ) ,
29
- ( "SL" , HazardType .SLIDE ) ,
30
- ( "AV" , HazardType .SNOW_AVALANCHE ) ,
31
- ( "SS" , HazardType .STORM ) ,
32
- ( "AC" , HazardType .TECH_DISASTER ) ,
33
- ( "TO" , HazardType .TORNADO ) ,
34
- ( "VW" , HazardType .VIOLENT_WIND ) ,
35
- ( "WV" , HazardType .WAVE_SURGE ) ,
11
+ HazardType .EARTHQUAKE ,
12
+ HazardType .FLOOD ,
13
+ HazardType .CYCLONE ,
14
+ HazardType .EPIDEMIC ,
15
+ HazardType .STORM ,
16
+ HazardType .DROUGHT ,
17
+ HazardType .TSUNAMI ,
18
+ HazardType .WILDFIRE ,
19
+ HazardType .VOLCANO ,
20
+ HazardType .COLDWAVE ,
21
+ HazardType .EXTRATROPICAL_CYCLONE ,
22
+ HazardType .EXTREME_TEMPERATURE ,
23
+ HazardType .FIRE ,
24
+ HazardType .FLASH_FLOOD ,
25
+ HazardType .HEAT_WAVE ,
26
+ HazardType .INSECT_INFESTATION ,
27
+ HazardType .LANDSLIDE ,
28
+ HazardType .MUD_SLIDE ,
29
+ HazardType .SEVERE_LOCAL_STROM ,
30
+ HazardType .SLIDE ,
31
+ HazardType .SNOW_AVALANCHE ,
32
+ HazardType .TECH_DISASTER ,
33
+ HazardType .TORNADO ,
34
+ HazardType .VIOLENT_WIND ,
35
+ HazardType .WAVE_SURGE ,
36
36
]
37
37
38
38
39
39
@shared_task
40
- def _ext_and_transform_glide_latest_data (hazard_type , hazard_type_str ):
40
+ def _ext_and_transform_glide_latest_data (hazard_type : HazardType ):
41
41
ext_object = (
42
42
ExtractionData .objects .filter (
43
43
source = ExtractionData .Source .GLIDE ,
@@ -48,31 +48,55 @@ def _ext_and_transform_glide_latest_data(hazard_type, hazard_type_str):
48
48
.order_by ("-created_at" )
49
49
.first ()
50
50
)
51
+
51
52
if ext_object :
52
53
from_date = ext_object .created_at .date ()
53
54
else :
54
55
from_date = datetime .strptime (settings .GLIDE_START_DATE , "%Y-%m-%d" ).date ()
55
56
56
57
to_date = datetime .today ().date ()
57
- url = f"{ settings .GLIDE_URL } /glide/jsonglideset.jsp?fromyear={ from_date .year } &frommonth={ from_date .month } &fromday={ from_date .day } &toyear={ to_date .year } &tomonth={ to_date .month } &today={ to_date .day } &events={ hazard_type } " # noqa: E501
58
58
59
- chain (GlideExtraction .task .s (url ), GlideTransformHandler .task .s ()).apply_async ()
59
+ # FIXME: Check if the date filters are inclusive
60
+ url = f"{ settings .GLIDE_URL } /glide/jsonglideset.jsp"
61
+ variables : GlideQueryVars = {
62
+ "fromyear" : from_date .year ,
63
+ "frommonth" : from_date .month ,
64
+ "fromday" : from_date .day ,
65
+ "toyear" : to_date .year ,
66
+ "tomonth" : to_date .month ,
67
+ "today" : to_date .day ,
68
+ "events" : hazard_type .value ,
69
+ }
70
+
71
+ chain (GlideExtraction .task .s (url , variables ), GlideTransformHandler .task .s ()).apply_async ()
60
72
61
73
62
74
@shared_task
63
- def _ext_and_transform_glide_historical_data (hazard_type , hazard_type_str ):
75
+ def _ext_and_transform_glide_historical_data (hazard_type : HazardType ):
64
76
to_date = datetime .today ().date ()
65
- url = f"{ settings .GLIDE_URL } /glide/jsonglideset.jsp?toyear={ to_date .year } &tomonth={ to_date .month } &to_date={ to_date .day } &events={ hazard_type } " # noqa: E501
66
- chain (GlideExtraction .task .s (url ), GlideTransformHandler .task .s ()).apply_async ()
77
+
78
+ # FIXME: Check if the date filters are inclusive
79
+ url = f"{ settings .GLIDE_URL } /glide/jsonglideset.jsp"
80
+ variables : GlideQueryVars = {
81
+ "fromyear" : None ,
82
+ "frommonth" : None ,
83
+ "fromday" : None ,
84
+ "toyear" : to_date .year ,
85
+ "tomonth" : to_date .month ,
86
+ "today" : to_date .day ,
87
+ "events" : hazard_type .value ,
88
+ }
89
+
90
+ chain (GlideExtraction .task .s (url , variables ), GlideTransformHandler .task .s ()).apply_async ()
67
91
68
92
69
93
@shared_task
70
94
def ext_and_transform_glide_latest_data ():
71
- for hazard_type , hazard_type_str in GLIDE_HAZARDS :
72
- _ext_and_transform_glide_latest_data (hazard_type , hazard_type_str )
95
+ for hazard_type in GLIDE_HAZARDS :
96
+ _ext_and_transform_glide_latest_data (hazard_type )
73
97
74
98
75
99
@shared_task
76
100
def ext_and_transform_glide_historical_data ():
77
- for hazard_type , hazard_type_str in GLIDE_HAZARDS :
78
- _ext_and_transform_glide_historical_data (hazard_type , hazard_type_str )
101
+ for hazard_type in GLIDE_HAZARDS :
102
+ _ext_and_transform_glide_historical_data (hazard_type )
0 commit comments