Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Potential trends #36

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ Martin, Arthur, Will, Rupert, Ivan, Charlie
]
}
```

`case_trend.json`
```
{"time": 1585692855,
"fsa": {"M9C": {"2020-03-31": 6,
.
.
.
"2020-03-17": 0},
"V6R": {"2020-03-31": 2},
"M4P": {"2020-03-31": 0},
"M9A": {"2020-03-31": 0}
}
}
```

### Setting up Firebase Storage for the first time

Expand Down
2 changes: 1 addition & 1 deletion appengine/form_data/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def root():
if not ('X-Appengine-Cron' in request.headers and request.headers['X-Appengine-Cron'] == 'true'):
return Response(status=403)
try:
main()
main(True)
return Response(status=200)
except Exception as e:
print(e)
Expand Down
48 changes: 42 additions & 6 deletions appengine/form_data/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

GCS_BUCKET = os.environ['GCS_BUCKET']
UPLOAD_FILE = 'form_data.json'
UPLOAD_CASE_TREND = 'case_trend.json'
DS_NAMESPACE = os.environ['DS_NAMESPACE']
DS_KIND = 'form-user'

Expand All @@ -24,8 +25,16 @@ def upload_blob(bucket, data_string, destination_blob_name):
)
)

# Downloads a blob from the bucket as a string
def download_blob(bucket_name, source_blob_name):
storage_client = storage.Client()

bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
s = blob.download_as_string()
return s

def main():
def main(get_all):
"""
Processes the info in the datastore into
"""
Expand All @@ -34,17 +43,28 @@ def main():

storage_client = storage.Client()
bucket = storage_client.bucket(GCS_BUCKET)

map_data = {'time': floor(datetime.datetime.utcnow().timestamp()), 'max': 0, 'fsa': {}}

query = datastore_client.query(kind=DS_KIND)
total_responses = 0

if get_all:
time = 0
case_data = {'time': 0, 'fsa': {}}
map_data = {'time': floor(datetime.datetime.utcnow().timestamp()), 'max': 0, 'fsa': {}}
else:
case_data = json.loads(download_blob(GCS_BUCKET, UPLOAD_CASE_TREND))
map_data = json.loads(download_blob(GCS_BUCKET, UPLOAD_FILE))
time = min(map_data['time']*1000, case_data['time']*1000)
query.add_filter('timestamp', '>', time)

max_timestamp = time
for entity in query.fetch():
try:
# make this get the latest form data...
postcode = entity['form_responses']['postalCode'].upper()
pot = 1 if entity['probable'] else 0
risk = 1 if entity['at_risk'] else 0
timestamp = entity['timestamp'] / 1000
date = datetime.date.fromtimestamp(timestamp).strftime("%Y-%m-%d")
except KeyError as e:
continue
total_responses += 1
Expand All @@ -57,8 +77,24 @@ def main():
map_data['fsa'][postcode] = {'number_reports': 1, 'pot': pot, 'risk': risk}
map_data['max'] = max(map_data['max'],
map_data['fsa'][postcode]['pot'] + 2 * map_data['fsa'][postcode]['risk'])

if timestamp > max_timestamp:
max_timestamp = timestamp

if postcode in case_data['fsa']:
if date in case_data['fsa'][postcode]:
case_data['fsa'][postcode][date] += pot
else:
case_data['fsa'][postcode][date] = pot
else:
case_data['fsa'][postcode] = {date: pot}

map_data['time'] = floor(max_timestamp)
map_data['total_responses'] = total_responses
case_data['time'] = floor(max_timestamp)

json_str = json.dumps(map_data)
case_json = json.dumps(case_data)
map_json = json.dumps(map_data)

upload_blob(bucket, json_str, UPLOAD_FILE)
upload_blob(bucket, map_json, UPLOAD_FILE)
upload_blob(bucket, case_json, UPLOAD_CASE_TREND)