Skip to content

Commit

Permalink
fix: 366 - (vin decoding jobs)/resolver fixes/changes (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim738745 authored Jul 11, 2024
1 parent b4a3e74 commit 9c94c12
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
12 changes: 7 additions & 5 deletions django/api/services/decoded_vin_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def save_decoded_data(
uploaded_vin_records,
vins_to_insert,
decoded_records_to_update_map,
vins_to_decoded_record_ids_map,
service_name,
decoded_data,
):
Expand All @@ -34,10 +34,12 @@ def save_decoded_data(
decoded_records_to_insert.append(
decoded_vin_model(vin=vin, data=decoded_datum)
)
elif vin in decoded_records_to_update_map:
decoded_record_to_update = decoded_records_to_update_map.get(vin)
decoded_record_to_update.update_timestamp = timezone.now()
decoded_record_to_update.data = decoded_datum
elif vin in vins_to_decoded_record_ids_map:
decoded_record_to_update = decoded_vin_model(
id=vins_to_decoded_record_ids_map[vin],
update_timestamp=timezone.now(),
data=decoded_datum,
)
decoded_records_to_update.append(decoded_record_to_update)
elif vin in failed_vins:
set_decode_successful(service_name, uploaded_record, False)
Expand Down
3 changes: 2 additions & 1 deletion django/api/services/resolvers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dns.resolver import Resolver
from email_validator import caching_resolver


def get_google_resolver():
resolver = Resolver()
resolver.nameservers = ["8.8.8.8"]
return resolver
return caching_resolver(dns_resolver=resolver)
9 changes: 9 additions & 0 deletions django/api/utilities/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ def get_map(key_name, objects):
key = getattr(object, key_name)
result[key] = object
return result


def get_unified_map(key_name, value_name, maps):
result = {}
for map in maps:
key = map.get(key_name)
value = map.get(value_name)
result[key] = value
return result
2 changes: 1 addition & 1 deletion django/workers/external_apis/vinpower.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def batch_decode(uploaded_vin_records):
vins.append(record.vin)
headers = {"content-type": "application/json"}
response = requests.get(url, data=json.dumps(vins), headers=headers)
response.raise_for_status
response.raise_for_status()

data = response.json()
for vin in vins:
Expand Down
2 changes: 1 addition & 1 deletion django/workers/external_apis/vpic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def batch_decode(uploaded_vin_records):

body = {"format": "json", "data": request_data}
response = requests.post(url, data=body)
response.raise_for_status
response.raise_for_status()
data = response.json()["Results"]
decoded_vins_map = {}
for record in data:
Expand Down
14 changes: 9 additions & 5 deletions django/workers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from api.models.uploaded_vins_file import UploadedVinsFile
from api.models.uploaded_vin_record import UploadedVinRecord
from api.constants.decoder import get_service
from api.utilities.generic import get_map
from api.utilities.generic import get_unified_map
from api.services.decoded_vin_record import save_decoded_data
from api.services.uploaded_vin_record import parse_and_save
from django.db import transaction
Expand Down Expand Up @@ -61,10 +61,14 @@ def batch_decode_vins(service_name, batch_size=50):
for uploaded_record in uploaded_vin_records:
uploaded_vins.add(uploaded_record.vin)
vins_to_update = set()
decoded_records_to_update_map = get_map(
"vin", decoded_vin_model.objects.filter(vin__in=uploaded_vins)
vins_to_decoded_record_ids_map = get_unified_map(
"vin",
"id",
decoded_vin_model.objects.only("id", "vin")
.filter(vin__in=uploaded_vins)
.values(),
)
for decoded_vin in decoded_records_to_update_map:
for decoded_vin in vins_to_decoded_record_ids_map:
vins_to_update.add(decoded_vin)
vins_to_insert = uploaded_vins.difference(vins_to_update)

Expand All @@ -74,7 +78,7 @@ def batch_decode_vins(service_name, batch_size=50):
save_decoded_data(
uploaded_vin_records,
vins_to_insert,
decoded_records_to_update_map,
vins_to_decoded_record_ids_map,
service_name,
decoded_data,
)

0 comments on commit 9c94c12

Please sign in to comment.