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): ...@@ -32,7 +32,7 @@ class Host(object):
for v in problems_path: for v in problems_path:
problem = Problem(v) problem = Problem(v, self.config['solution_path'])
_key = problem.name _key = problem.name
self.problems[_key] = problem self.problems[_key] = problem
...@@ -148,11 +148,11 @@ class Worker(object): ...@@ -148,11 +148,11 @@ class Worker(object):
self.address = params['address'] self.address = params['address']
self.name = params['name'] self.name = params['name']
self.host = params['host'] self.host = params['host']
self.role = None self.role = params['role']
self.params = params self.params = params
self.status = 'Setting up' self.status = 'Setting up'
self.set_role(params['role']) self.configure()
def post(self, path, data): def post(self, path, data):
try: try:
...@@ -169,7 +169,7 @@ class Worker(object): ...@@ -169,7 +169,7 @@ class Worker(object):
response = None response = None
return response return response
def set_role(self, role): def configure(self):
r = self.post('role', self.params) r = self.post('role', self.params)
class Request(object): class Request(object):
...@@ -232,7 +232,7 @@ class Request(object): ...@@ -232,7 +232,7 @@ class Request(object):
worker_status = dict() worker_status = dict()
for v in all_workers: for v in all_workers:
if v in self.response: if v in self.response:
worker_status[v] = 'Processed' worker_status[v] = self.response[v]['status']
else: else:
worker_status[v] = 'Waiting for response' worker_status[v] = 'Waiting for response'
......
import datetime import datetime
import json
import os import os
import time import time
import uuid import uuid
class Problem(object): class Problem(object):
def __init__(self, path): def __init__(self, problem_path, solution_path):
self.path = path self.path = problem_path
self.name = '' self.name = ''
self.size = (0, 0) self.size = (0, 0)
self.block_num = 0 self.block_num = 0
self.problem = '' self.problem = ''
self.status = 'Ready' self.status = 'Ready'
self.solutions = dict() self.solutions = dict()
self.solution_path = solution_path
self._load_problem(path) self._load_problem(problem_path)
@property @property
def size_str(self): def size_str(self):
...@@ -61,6 +63,9 @@ class Problem(object): ...@@ -61,6 +63,9 @@ class Problem(object):
print(f'I: Put a solution: {solution_id}') print(f'I: Put a solution: {solution_id}')
self.solutions[solution_id] = solution self.solutions[solution_id] = solution
# Save solution
solution.save(self.solution_path)
def get_solutions(self): def get_solutions(self):
return self.solutions return self.solutions
...@@ -80,6 +85,25 @@ class Solution(object): ...@@ -80,6 +85,25 @@ class Solution(object):
def get_id(self): def get_id(self):
return self._id 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 @property
def timestamp_str(self): def timestamp_str(self):
dt = datetime.datetime.fromtimestamp( 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