commit
82f1a57150
@ -984,6 +984,10 @@ class AccuracyViewSet(viewsets.ViewSet):
|
|||||||
}
|
}
|
||||||
for k, v in file_result.items():
|
for k, v in file_result.items():
|
||||||
if k in sample_result.keys():
|
if k in sample_result.keys():
|
||||||
|
if isinstance(v, str) and v == '':
|
||||||
|
v = None
|
||||||
|
if k == "purchase_date" and v is not None:
|
||||||
|
v = v.split("")
|
||||||
if not isinstance(sample_result[k], list):
|
if not isinstance(sample_result[k], list):
|
||||||
sample_result[k] = v
|
sample_result[k] = v
|
||||||
elif v:
|
elif v:
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from tqdm import tqdm
|
||||||
|
from fwd_api.models import SubscriptionRequestFile, SubscriptionRequest
|
||||||
|
from fwd_api.exception.exceptions import InvalidException
|
||||||
|
from fwd_api.utils.accuracy import predict_result_to_ready
|
||||||
|
import traceback
|
||||||
|
import copy
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
KEY = "imei_number"
|
||||||
|
VALUE = "None"
|
||||||
|
EXPECTED_VALUE = []
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Refactor database for image level'
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
# Add your command-line arguments here
|
||||||
|
parser.add_argument('start', type=str, help='start date, sample: 2023-01-02T00:00:00+0700')
|
||||||
|
parser.add_argument('end', type=str, help='end date, sample: 2023-01-03T00:00:00+0700')
|
||||||
|
|
||||||
|
def process_request(self, request, result):
|
||||||
|
if len(request.request_id.split(".")[0].split("_")) < 2:
|
||||||
|
return
|
||||||
|
images = SubscriptionRequestFile.objects.filter(request=request)
|
||||||
|
if not request.predict_result:
|
||||||
|
# self.stdout.write(self.style.WARNING(f"Key predict_result not found in {request.request_id}"))
|
||||||
|
return
|
||||||
|
if request.predict_result.get("status", 200) != 200:
|
||||||
|
# self.stdout.write(self.style.WARNING(f"Not a sucess request {request.request_id}"))
|
||||||
|
return
|
||||||
|
|
||||||
|
for i, image in enumerate(images):
|
||||||
|
try:
|
||||||
|
if isinstance(image.reviewed_result, dict):
|
||||||
|
# print(f"[INFO]: dict ... {request.predict_result}")
|
||||||
|
_imeis = image.reviewed_result.get(KEY, [])
|
||||||
|
if _imeis is None:
|
||||||
|
continue
|
||||||
|
if len(_imeis) >= 1:
|
||||||
|
sub_imei = _imeis[0]
|
||||||
|
if sub_imei is None or VALUE in sub_imei or isinstance(sub_imei, list):
|
||||||
|
expected_value = copy.deepcopy(str(image.reviewed_result[KEY]))
|
||||||
|
expected_value = expected_value.replace("[", "").replace("]", "").replace(VALUE, "").replace('"', '').replace("'", "")
|
||||||
|
if expected_value == "":
|
||||||
|
expected_value = []
|
||||||
|
else:
|
||||||
|
expected_value = [expected_value]
|
||||||
|
image.reviewed_result[KEY] = expected_value
|
||||||
|
image.save()
|
||||||
|
result['total'] += 1
|
||||||
|
print(f"{image.reviewed_result[KEY]} -> {expected_value}")
|
||||||
|
except Exception as e:
|
||||||
|
self.stdout.write(self.style.ERROR(f"Request: {request.request_id} failed with {e}"))
|
||||||
|
print(traceback.format_exc())
|
||||||
|
result['failed'] += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
start = options['start']
|
||||||
|
end = options['end']
|
||||||
|
result = {'total':0,
|
||||||
|
'failed':0}
|
||||||
|
if start or end:
|
||||||
|
try:
|
||||||
|
start_date = timezone.datetime.strptime(start, '%Y-%m-%dT%H:%M:%S%z') # We care only about day precision only
|
||||||
|
end_date = timezone.datetime.strptime(end, '%Y-%m-%dT%H:%M:%S%z')
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[INFO]: start: {start}")
|
||||||
|
print(f"[INFO]: end: {end}")
|
||||||
|
raise InvalidException(excArgs="Date format")
|
||||||
|
subcription_iter = SubscriptionRequest.objects.filter(created_at__range=(start_date, end_date))
|
||||||
|
else:
|
||||||
|
subcription_iter = SubscriptionRequest.objects.all()
|
||||||
|
|
||||||
|
# file = open("modified.txt", "w")
|
||||||
|
for request in tqdm(subcription_iter.iterator()):
|
||||||
|
self.process_request(request, result)
|
||||||
|
# file.close()
|
||||||
|
self.stdout.write(self.style.SUCCESS('Sample Django management command executed successfully! total/failed: {}/{}'.format(result['total'], result['failed'])))
|
Loading…
Reference in New Issue
Block a user