|
1 | | -from django.core.management.base import BaseCommand, CommandError |
| 1 | +from django.core.management.base import BaseCommand |
2 | 2 | from tigaserver_app.models import CoverageAreaMonth, Fix, Report |
3 | | -from tigaserver_app.views import get_latest_reports_qs, lat_function, lat_function_m0, lat_function_y0 |
4 | 3 | import pytz |
5 | 4 | from datetime import datetime |
6 | 5 | from django.conf import settings |
7 | 6 | from django.db.models import Q |
8 | 7 |
|
9 | 8 |
|
| 9 | +def lon_function(this_lon, these_lons, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 10 | + n_fixes = len([l for l in these_lons if l == this_lon]) |
| 11 | + if CoverageAreaMonth.objects.filter(lat=this_lat[0], lon=this_lon[0], year=this_lat[1],month=this_lat[2]).count() > 0: |
| 12 | + this_coverage_area = CoverageAreaMonth.objects.get(lat=this_lat[0], lon=this_lon[0], year=this_lat[1],month=this_lat[2]) |
| 13 | + this_coverage_area.n_fixes += n_fixes |
| 14 | + else: |
| 15 | + this_coverage_area = CoverageAreaMonth(lat=this_lat[0], lon=this_lon[0], year=this_lat[1],month=this_lat[2], n_fixes=n_fixes) |
| 16 | + if fix_list and fix_list.count() > 0: |
| 17 | + this_coverage_area.latest_fix_id = fix_list.order_by('id').last().id |
| 18 | + else: |
| 19 | + this_coverage_area.latest_fix_id = latest_fix_id |
| 20 | + if report_list and report_list.count() > 0: |
| 21 | + this_coverage_area.latest_report_server_upload_time = report_list.order_by('server_upload_time').last().server_upload_time |
| 22 | + else: |
| 23 | + this_coverage_area.latest_report_server_upload_time = latest_report_server_upload_time |
| 24 | + this_coverage_area.save() |
| 25 | + |
| 26 | + |
| 27 | +def lon_function_m0(this_lon, these_lons_m0, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 28 | + n_fixes = len([l for l in these_lons_m0 if l == this_lon]) |
| 29 | + if CoverageAreaMonth.objects.filter(lat=this_lat[0], lon=this_lon[0], year=this_lat[1], month=0).count() > 0: |
| 30 | + this_coverage_area = CoverageAreaMonth.objects.get(lat=this_lat[0], lon=this_lon[0], year=this_lat[1], month=0) |
| 31 | + this_coverage_area.n_fixes += n_fixes |
| 32 | + else: |
| 33 | + this_coverage_area = CoverageAreaMonth(lat=this_lat[0], lon=this_lon[0], year=this_lat[1], month=0, n_fixes=n_fixes) |
| 34 | + if fix_list and fix_list.count() > 0: |
| 35 | + this_coverage_area.latest_fix_id = fix_list.order_by('id').last().id |
| 36 | + else: |
| 37 | + this_coverage_area.latest_fix_id = latest_fix_id |
| 38 | + if report_list and report_list.count() > 0: |
| 39 | + this_coverage_area.latest_report_server_upload_time = report_list.order_by('server_upload_time').last().server_upload_time |
| 40 | + else: |
| 41 | + this_coverage_area.latest_report_server_upload_time = latest_report_server_upload_time |
| 42 | + this_coverage_area.save() |
| 43 | + |
| 44 | + |
| 45 | +def lon_function_y0(this_lon, these_lons_y0, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 46 | + n_fixes = len([l for l in these_lons_y0 if l == this_lon]) |
| 47 | + if CoverageAreaMonth.objects.filter(lat=this_lat[0], lon=this_lon[0], month=this_lat[1], year=0).count() > 0: |
| 48 | + this_coverage_area = CoverageAreaMonth.objects.get(lat=this_lat[0], lon=this_lon[0], month=this_lat[1], year=0) |
| 49 | + this_coverage_area.n_fixes += n_fixes |
| 50 | + else: |
| 51 | + this_coverage_area = CoverageAreaMonth(lat=this_lat[0], lon=this_lon[0], month=this_lat[1], year=0, n_fixes=n_fixes) |
| 52 | + if fix_list and fix_list.count() > 0: |
| 53 | + this_coverage_area.latest_fix_id = fix_list.order_by('id').last().id |
| 54 | + else: |
| 55 | + this_coverage_area.latest_fix_id = latest_fix_id |
| 56 | + if report_list and report_list.count() > 0: |
| 57 | + this_coverage_area.latest_report_server_upload_time = report_list.order_by('server_upload_time').last().server_upload_time |
| 58 | + else: |
| 59 | + this_coverage_area.latest_report_server_upload_time = latest_report_server_upload_time |
| 60 | + this_coverage_area.save() |
| 61 | + |
| 62 | + |
| 63 | +def lat_function(this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 64 | + these_lons = [(f.masked_lon, f.fix_time.year, f.fix_time.month) for f in fix_list if (f.masked_lat == this_lat[0] and f.fix_time.year == this_lat[1] and f.fix_time.month == this_lat[2])] + [(r.masked_lon, r.creation_time.year, r.creation_time.month) for r in report_list if (r.masked_lat is not None and r.masked_lat == this_lat and r.creation_time.year == this_lat[1] and r.creation_time.month == this_lat[2])] |
| 65 | + unique_lons = set(these_lons) |
| 66 | + [lon_function(this_lon, these_lons, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time) for this_lon in unique_lons] |
| 67 | + |
| 68 | + |
| 69 | +def lat_function_m0(this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 70 | + these_lons_m0 = [(f.masked_lon, f.fix_time.year) for f in fix_list if (f.masked_lat == this_lat[0] and f.fix_time.year == this_lat[1])] + [(r.masked_lon, r.creation_time.year) for r in report_list if (r.masked_lat is not None and r.masked_lat == this_lat and r.creation_time.year == this_lat[1])] |
| 71 | + unique_lons_m0 = set(these_lons_m0) |
| 72 | + [lon_function_m0(this_lon, these_lons_m0, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time) for this_lon in unique_lons_m0] |
| 73 | + |
| 74 | + |
| 75 | +def lat_function_y0(this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time): |
| 76 | + these_lons_y0 = [(f.masked_lon, f.fix_time.month) for f in fix_list if (f.masked_lat == this_lat[0] and f.fix_time.month == this_lat[1])] + [(r.masked_lon, r.creation_time.month) for r in report_list if (r.masked_lat is not None and r.masked_lat == this_lat and r.creation_time.month == this_lat[1])] |
| 77 | + unique_lons_y0 = set(these_lons_y0) |
| 78 | + [lon_function_y0(this_lon, these_lons_y0, this_lat, fix_list, latest_fix_id, report_list, latest_report_server_upload_time) for this_lon in unique_lons_y0] |
| 79 | + |
| 80 | + |
10 | 81 | class Command(BaseCommand): |
11 | 82 | args = '' |
12 | 83 | help = 'Updates coverage month model data based on background location and report location data' |
13 | 84 |
|
14 | 85 | def handle(self, *args, **options): |
15 | 86 | updated = False |
16 | | - if CoverageAreaMonth.objects.all().count() > 0: |
17 | | - latest_report_server_upload_time = CoverageAreaMonth.objects.order_by('latest_report_server_upload_time').last().latest_report_server_upload_time |
| 87 | + |
| 88 | + last_coverage = CoverageAreaMonth.objects.all().order_by('latest_report_server_upload_time').last() |
| 89 | + if last_coverage: |
| 90 | + latest_report_server_upload_time = last_coverage.latest_report_server_upload_time |
18 | 91 | latest_fix_id = CoverageAreaMonth.objects.order_by('latest_fix_id').last().latest_fix_id |
19 | 92 | else: |
20 | 93 | latest_report_server_upload_time = pytz.utc.localize(datetime(1970, 1, 1)) |
21 | 94 | latest_fix_id = 0 |
22 | | - if CoverageAreaMonth.objects.all().count() == 0 or latest_report_server_upload_time < Report.objects.order_by('server_upload_time').last().server_upload_time or latest_fix_id < Fix.objects.order_by('id').last().id: |
23 | | - report_list = get_latest_reports_qs(Report.objects.exclude(hide=True).filter(Q(package_name='Tigatrapp', creation_time__gte=settings.IOS_START_TIME) | Q(package_name='ceab.movelab.tigatrapp', package_version__gt=3)).filter(server_upload_time__gt=latest_report_server_upload_time)) |
| 95 | + |
| 96 | + last_report = Report.objects.order_by('server_upload_time').last() |
| 97 | + last_fix = Fix.objects.order_by('id').last() |
| 98 | + if not last_coverage or latest_report_server_upload_time < last_report.server_upload_time or latest_fix_id < last_fix.id: |
| 99 | + report_list = Report.objects.exclude(hide=True).filter( |
| 100 | + Q(package_name='Tigatrapp', creation_time__gte=settings.IOS_START_TIME) |
| 101 | + | Q(package_name='ceab.movelab.tigatrapp', package_version__gt=3) |
| 102 | + ).filter(server_upload_time__gt=latest_report_server_upload_time) |
24 | 103 | fix_list = Fix.objects.filter(fix_time__gt=settings.START_TIME, id__gt=latest_fix_id) |
| 104 | + |
25 | 105 | full_lat_list = [(f.masked_lat, f.fix_time.year, f.fix_time.month) for f in fix_list] + [(r.masked_lat, r.creation_time.year, r.creation_time.month) for r in report_list if r.masked_lat is not None] |
26 | 106 | full_lat_list_m0 = [(f.masked_lat, f.fix_time.year) for f in fix_list] + [(r.masked_lat, r.creation_time.year) for r in report_list if r.masked_lat is not None] |
27 | 107 | full_lat_list_y0 = [(f.masked_lat, f.fix_time.month) for f in fix_list] + [(r.masked_lat, r.creation_time.month) for r in report_list if r.masked_lat is not None] |
|
0 commit comments