Skip to content

Commit

Permalink
Remove duplicate codes
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Aug 10, 2024
1 parent 0c935e7 commit 35045b5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 378 deletions.
191 changes: 38 additions & 153 deletions imminent/management/commands/create_pdc_daily.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
import datetime
import logging

import requests
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone
from sentry_sdk.crons import monitor

from common.models import HazardType
from common.utils import logging_response_context
from imminent.models import Pdc
from risk_module.sentry import SentryMonitor

from .create_pdc_data import Command as CreatePdcDataCommand

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Import Active Hazards"

def parse_timestamp(self, timestamp):
# NOTE: all timestamp are in millisecond and with timezone `utc`
return timezone.make_aware(datetime.datetime.utcfromtimestamp(int(timestamp) / 1000))
def save_pdc_data(self, hazard_type: HazardType, data):
pdc_updated_at = CreatePdcDataCommand.parse_timestamp(data["last_Update"])

# XXX: This was only done for WILDFIRE before??
existing_qs = Pdc.objects.filter(
uuid=data["uuid"],
hazard_type=hazard_type,
pdc_updated_at=pdc_updated_at,
)
if existing_qs.exists():
return

pdc_data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": CreatePdcDataCommand.parse_timestamp(data["start_Date"]),
"end_date": CreatePdcDataCommand.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": CreatePdcDataCommand.parse_timestamp(data["create_Date"]),
"pdc_updated_at": pdc_updated_at,
# XXX: Severity was not saved here compare to create_pdc_data
}
Pdc.objects.get_or_create(**pdc_data)

@monitor(monitor_slug=SentryMonitor.CREATE_PDC_DAILY)
def handle(self, *args, **options):
def handle(self, **_):
# NOTE: Use the search hazard api for the information download
# make sure to use filter the data
url = "https://sentry.pdc.org/hp_srv/services/hazards/t/json/get_active_hazards"
Expand All @@ -34,157 +59,17 @@ def handle(self, *args, **options):
"Error querying PDC data",
extra=logging_response_context(response),
)
# TODO return?
return

response_data = response.json()
for data in response_data:
# NOTE: Filter the active hazard only
# Update the hazard if it has expired
hazard_type = data["type_ID"]
hazard_status = data["status"]

if hazard_status == "E":
pdcs = Pdc.objects.filter(uuid=data["uuid"])
for pdc in pdcs:
pdc.status = Pdc.Status.EXPIRED
pdc.save(update_fields=["status"])
if hazard_status == "A":
if hazard_type == "FLOOD":
hazard_type = HazardType.FLOOD
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "CYCLONE":
hazard_type = HazardType.CYCLONE
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "STORM":
hazard_type = HazardType.STORM
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "DROUGHT":
hazard_type = HazardType.DROUGHT
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "WIND":
hazard_type = HazardType.WIND
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "TSUNAMI":
hazard_type = HazardType.TSUNAMI
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "EARTHQUAKE":
hazard_type = HazardType.EARTHQUAKE
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
elif hazard_type == "WILDFIRE":
hazard_type = HazardType.WILDFIRE
pdc_updated_at = self.parse_timestamp(data["last_Update"])
if Pdc.objects.filter(uuid=data["uuid"], hazard_type=hazard_type, pdc_updated_at=pdc_updated_at).exists():
continue
else:
data = {
"hazard_id": data["hazard_ID"],
"hazard_name": data["hazard_Name"],
"latitude": data["latitude"],
"longitude": data["longitude"],
"description": data["description"],
"hazard_type": hazard_type,
"uuid": data["uuid"],
"start_date": self.parse_timestamp(data["start_Date"]),
"end_date": self.parse_timestamp(data["end_Date"]),
"status": Pdc.Status.ACTIVE,
"pdc_created_at": self.parse_timestamp(data["create_Date"]),
"pdc_updated_at": self.parse_timestamp(data["last_Update"]),
}
Pdc.objects.get_or_create(**data)
Pdc.objects.filter(uuid=data["uuid"]).update(status=Pdc.Status.EXPIRED)

elif hazard_status == "A":
if hazard_type := CreatePdcDataCommand.HAZARD_TYPE_MAP.get(data["type_ID"].upper()):
self.save_pdc_data(hazard_type, data)
Loading

0 comments on commit 35045b5

Please sign in to comment.