-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_bucketStatics.py
50 lines (41 loc) · 1.59 KB
/
insert_bucketStatics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import django
django.setup()
from bucketStatistics.models import BucketStatistics
from bucketRecord.models import BucketRecord
BucketStatistics.objects.all().delete();
BucketRecords = BucketRecord.objects.all()
village_list = []
for R in BucketRecords:
county = R.county
town = R.town
village = R.village
if([county, town, village] not in village_list):
village_list.append([county, town, village])
for item in village_list:
county = item[0]
town = item[1]
village = item[2]
village_BucketRecords = BucketRecord.objects.filter(
county=county
).filter(
town=town
).filter(village=village)
investigate_dates = []
for record in village_BucketRecords:
if(record.investigate_date not in investigate_dates):
investigate_dates.append(record.investigate_date)
for date in investigate_dates:
date_of_village_BucketRecords = village_BucketRecords.filter(investigate_date=date)
total_egg_count = sum(r.egg_count for r in date_of_village_BucketRecords if r.egg_count > 0)
bucketes_has_egg = sum(r.egg_count > 0 for r in date_of_village_BucketRecords)
positive_rate = bucketes_has_egg / len(date_of_village_BucketRecords)
avg_egg_count = 10 * (total_egg_count / len(date_of_village_BucketRecords))
BucketStatistics(
investigate_date=date,
county=county,
town=town,
village=village,
total_egg_count=total_egg_count,
positive_rate=positive_rate,
avg_egg_count=avg_egg_count
).save()