From e88e9f9ccb34971919b780ae8b3eac331fc42d1f Mon Sep 17 00:00:00 2001 From: Kento HASEGAWA Date: Fri, 19 Jul 2019 22:30:33 +0900 Subject: [PATCH] Add support for saving received solutions --- roles/host.py | 10 +++++----- utils/data.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/roles/host.py b/roles/host.py index 05d928b..eabfc42 100644 --- a/roles/host.py +++ b/roles/host.py @@ -32,7 +32,7 @@ class Host(object): for v in problems_path: - problem = Problem(v) + problem = Problem(v, self.config['solution_path']) _key = problem.name self.problems[_key] = problem @@ -148,11 +148,11 @@ class Worker(object): self.address = params['address'] self.name = params['name'] self.host = params['host'] - self.role = None + self.role = params['role'] self.params = params self.status = 'Setting up' - self.set_role(params['role']) + self.configure() def post(self, path, data): try: @@ -169,7 +169,7 @@ class Worker(object): response = None return response - def set_role(self, role): + def configure(self): r = self.post('role', self.params) class Request(object): @@ -232,7 +232,7 @@ class Request(object): worker_status = dict() for v in all_workers: if v in self.response: - worker_status[v] = 'Processed' + worker_status[v] = self.response[v]['status'] else: worker_status[v] = 'Waiting for response' diff --git a/utils/data.py b/utils/data.py index a371b6d..feaad10 100644 --- a/utils/data.py +++ b/utils/data.py @@ -1,21 +1,23 @@ import datetime +import json import os import time import uuid class Problem(object): - def __init__(self, path): + def __init__(self, problem_path, solution_path): - self.path = path + self.path = problem_path self.name = '' self.size = (0, 0) self.block_num = 0 self.problem = '' self.status = 'Ready' self.solutions = dict() + self.solution_path = solution_path - self._load_problem(path) + self._load_problem(problem_path) @property def size_str(self): @@ -61,6 +63,9 @@ class Problem(object): print(f'I: Put a solution: {solution_id}') self.solutions[solution_id] = solution + # Save solution + solution.save(self.solution_path) + def get_solutions(self): return self.solutions @@ -79,6 +84,25 @@ class Solution(object): def get_id(self): return self._id + + def get_dict(self): + return { + 'id': self._id, + 'timestamp': self.timestamp, + 'request_id': self.request_id, + 'worker': self.worker, + 'elapsed_time': self.elapsed_time, + 'problem': self.problem, + 'solution': self.solution + } + + def save(self, basedir): + outdir = f"{basedir}/{self.problem}" + if not os.path.exists(outdir): + os.mkdir(outdir) + outpath = f"{outdir}/{self.request_id}-{self.worker}.json".replace(":", ".") + with open(outpath, 'w') as fp: + json.dump(self.get_dict(), fp, indent=4) @property def timestamp_str(self): -- 2.22.0