-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathfunc.py
More file actions
57 lines (48 loc) · 1.79 KB
/
func.py
File metadata and controls
57 lines (48 loc) · 1.79 KB
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
51
52
53
54
55
56
57
import logging
import datetime
import os
from flask import jsonify
from parliament import Context
import minio
def main(context: Context):
logging.getLogger().setLevel(logging.INFO)
begin = datetime.datetime.now() # Initialize begin outside the try block
event = context.request.json
logging.info(f"Received event: {event}")
request_id = context.request.headers.get('X-Request-ID')
try:
from function import function
# Update the timestamp after extracting JSON data
begin = datetime.datetime.now()
# Pass the extracted JSON data to the handler function
ret = function.handler(event)
end = datetime.datetime.now()
logging.info("Function result: {}".format(ret))
log_data = {"result": ret["result"]}
if "measurement" in ret:
log_data["measurement"] = ret["measurement"]
results_time = (end - begin) / datetime.timedelta(microseconds=1)
is_cold = False
fname = "cold_run"
if not os.path.exists(fname):
is_cold = True
open(fname, "a").close()
return {
"request_id": request_id,
"begin": begin.strftime("%s.%f"),
"end": end.strftime("%s.%f"),
"results_time": results_time,
"is_cold": is_cold,
"result": log_data,
}
except Exception as e:
end = datetime.datetime.now()
results_time = (end - begin) / datetime.timedelta(microseconds=1)
logging.error(f"Error - invocation failed! Reason: {e}")
return {
"request_id": request_id,
"begin": begin.strftime("%s.%f"),
"end": end.strftime("%s.%f"),
"results_time": results_time,
"result": f"Error - invocation failed! Reason: {e}",
}