Commit e88e9f9c authored by Kento HASEGAWA's avatar Kento HASEGAWA

Add support for saving received solutions

parent bfd3e10c
......@@ -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'
......
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
......@@ -80,6 +85,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):
dt = datetime.datetime.fromtimestamp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment