fix point thing

This commit is contained in:
Michaela 2021-04-20 18:06:21 +10:00
parent 77673e06b1
commit c7323d81af

View File

@ -9,6 +9,7 @@ import json
import os import os
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
import sys, traceback import sys, traceback
import re
HOST = os.getenv("ES") HOST = os.getenv("ES")
# get current sondes, filter by date, location # get current sondes, filter by date, location
@ -195,9 +196,6 @@ def datanew(event, context):
duration_query = "1hour" duration_query = "1hour"
requested_time = datetime.now(timezone.utc) requested_time = datetime.now(timezone.utc)
if event["queryStringParameters"]["type"] != "positions":
raise ValueError
max_positions = ( max_positions = (
int(event["queryStringParameters"]["max_positions"]) int(event["queryStringParameters"]["max_positions"])
if "max_positions" in event["queryStringParameters"] if "max_positions" in event["queryStringParameters"]
@ -206,22 +204,40 @@ def datanew(event, context):
if event["queryStringParameters"]["mode"] in durations: if event["queryStringParameters"]["mode"] in durations:
duration_query = event["queryStringParameters"]["mode"] duration_query = event["queryStringParameters"]["mode"]
(duration, interval) = durations[duration_query]
elif event["queryStringParameters"]["mode"] == "single":
duration = 259200
interval = 1
else: else:
return f"Duration must be either {', '.join(durations.keys())}" return f"Duration must be either {', '.join(durations.keys())}"
(duration, interval) = durations[duration_query]
if "vehicles" in event["queryStringParameters"] and ( if "vehicles" in event["queryStringParameters"] and (
event["queryStringParameters"]["vehicles"] != "RS_*;*chase" event["queryStringParameters"]["vehicles"] != "RS_*;*chase"
and event["queryStringParameters"]["vehicles"] != "" and event["queryStringParameters"]["vehicles"] != ""
): ):
interval = 1 interval = 1
if event["queryStringParameters"]["position_id"] != "0": if event["queryStringParameters"]["position_id"] != "0":
if event["queryStringParameters"]["mode"] == "single":
position_id = event["queryStringParameters"]["position_id"]
matches = re.search("(.+)-(\d{4}-\d{2}-\d{2}\w\d{2}:\d{2}:\d{2}.\d+)Z",position_id).groups()
matched_time = matches[1].replace("Z", "+00:00")
matched_vehicle = matches[0]
requested_time = datetime.fromisoformat(matched_time)
lt = requested_time
gte = requested_time
else:
requested_time = datetime.fromisoformat( requested_time = datetime.fromisoformat(
event["queryStringParameters"]["position_id"].replace("Z", "+00:00") event["queryStringParameters"]["position_id"].replace("Z", "+00:00")
) )
lt = datetime.now(timezone.utc) lt = datetime.now(timezone.utc)
gte = requested_time gte = requested_time
elif event["queryStringParameters"]["mode"] == "single":
return f"Single requires a position id specified"
else: else:
lt = datetime.now(timezone.utc) lt = datetime.now(timezone.utc)
gte = datetime.now(timezone.utc) - timedelta(0, duration) gte = datetime.now(timezone.utc) - timedelta(0, duration)
@ -246,7 +262,7 @@ def datanew(event, context):
"aggs": { "aggs": {
"1": { "1": {
"top_hits": { "top_hits": {
"size": 5, "size": 8,
"sort": [{"datetime": {"order": "desc"}}], "sort": [{"datetime": {"order": "desc"}}],
} }
} }
@ -261,7 +277,7 @@ def datanew(event, context):
{"match_all": {}}, {"match_all": {}},
{ {
"range": { "range": {
"datetime": {"gte": gte.isoformat(), "lt": lt.isoformat()} "datetime": {"gte": gte.isoformat(), "lte": lt.isoformat()}
} }
}, },
], ],
@ -281,6 +297,14 @@ def datanew(event, context):
} }
} }
) )
if event["queryStringParameters"]["mode"] == "single":
payload["query"]["bool"]["filter"].append(
{
"match_phrase": {
"serial": matched_vehicle
}
}
)
results = es_request(payload, path, "POST") results = es_request(payload, path, "POST")
@ -580,11 +604,8 @@ if __name__ == "__main__":
datanew( datanew(
{ {
"queryStringParameters": { "queryStringParameters": {
"type" : "positions", "mode": "single",
"mode": "6hours", "position_id": "912-2-12555-2021-04-20T06:47:59.000000Z"
"position_id": "0",
"chase_only": "false",
"vehicles": "17008547"
} }
}, },
{}, {},