Coverage for yaptide/batch/result_sender.py: 0%
43 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
1import argparse
2import json
3import logging
4import signal
5import ssl
6from pathlib import Path
7from urllib import request
10def send_simulation_results(output_Path: Path, simulation_id: int, update_key: str, backend_url: str):
11 """Sends simulation results to backend"""
12 if not backend_url:
13 logging.error("Backend url not specified")
14 return
16 estimators = []
17 for filename in output_Path.iterdir():
18 if filename.suffix != ".json":
19 continue
20 with open(filename, "r") as json_file:
21 est_dict = json.load(json_file)
22 est_dict["name"] = filename.stem
23 estimators.append(est_dict)
25 dict_to_send = {
26 "simulation_id": simulation_id,
27 "update_key": update_key,
28 "estimators": estimators,
29 }
30 results_url = f"{backend_url}/results"
31 context = ssl.SSLContext()
33 req = request.Request(results_url,
34 json.dumps(dict_to_send).encode(),
35 {'Content-Type': 'application/json'},
36 method='POST')
38 try:
39 with request.urlopen(req, context=context) as res: # skipcq: BAN-B310
40 if res.getcode() != 202:
41 logging.warning("Sending update to %s failed", results_url)
42 except Exception as e: # skipcq: PYL-W0703
43 print(e)
46if __name__ == "__main__":
47 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
49 logging.basicConfig(
50 level=logging.INFO,
51 format="%(asctime)s %(levelname)s %(message)s",
52 handlers=[
53 logging.StreamHandler()
54 ]
55 )
56 parser = argparse.ArgumentParser()
57 parser.add_argument("--output_dir", type=str)
58 parser.add_argument("--sim_id", type=int)
59 parser.add_argument("--update_key", type=str)
60 parser.add_argument("--backend_url", type=str)
61 args = parser.parse_args()
62 logging.info("output_dir %s", args.output_dir)
63 logging.info("sim_id %s", args.sim_id)
64 logging.info("update_key %s", args.update_key)
65 logging.info("backend_url %s", args.backend_url)
67 send_simulation_results(
68 output_Path=Path(args.output_dir),
69 simulation_id=args.sim_id,
70 update_key=args.update_key,
71 backend_url=args.backend_url
72 )