renamed to loft service to be more clear, added solution file generation naming via regex, few qol things

This commit is contained in:
Josh Burman 2021-10-29 00:10:30 +00:00
parent 3a42d83b76
commit d13574ac08
4 changed files with 20 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import csv import csv
import io import io
import re
def items_csv_to_dict(items_csv_reader): def items_csv_to_dict(items_csv_reader):
items = [] items = []
@ -15,6 +16,8 @@ def items_csv_to_dict(items_csv_reader):
for key, col in enumerate(headers): for key, col in enumerate(headers):
if key == 0: if key == 0:
item[col] = row[key] item[col] = row[key]
elif col == 'b_param':
item[col] = row[key]
elif key > 1: elif key > 1:
item['attributes'].append({ item['attributes'].append({
'id': col, 'id': col,
@ -43,3 +46,6 @@ def solution_to_file(buffer, total_form_items, forms):
buff2 = io.BytesIO(buffer.getvalue().encode()) buff2 = io.BytesIO(buffer.getvalue().encode())
return buff2 return buff2
def key_to_uuid(key):
return re.split("_", key)[0]

View File

@ -1,11 +1,14 @@
import os import os
import sys
from services.loft import Loft from services.loft_service import LoftService
from helpers import aws_helper from helpers import aws_helper
from sqs_listener import SqsListener
from sqs_listener.daemon import Daemon
print("Starting Solver Service (v0.3.1)...") print("Starting Solver Service (v0.3.1)...")
# listen to the solver queue # # listen to the solver queue
while True: while True:
msg = aws_helper.receive_message(os.environ['SOLVER_SQS_INGEST_QUEUE']) msg = aws_helper.receive_message(os.environ['SOLVER_SQS_INGEST_QUEUE'])
@ -14,7 +17,7 @@ while True:
# for now the solver service only supports Loft types # for now the solver service only supports Loft types
# this is here to allow us to create an extensible way to # this is here to allow us to create an extensible way to
# gather/manage/process data based on the particular needs # gather/manage/process data based on the particular needs
service = Loft(message) service = LoftService(message)
service.process() service.process()
# delete message once process is complete # delete message once process is complete

View File

@ -6,3 +6,4 @@ from models.attribute import Attribute
class Item(BaseModel): class Item(BaseModel):
id: int id: int
attributes: List[Attribute] attributes: List[Attribute]
b_param: int

View File

@ -2,7 +2,6 @@ import os
import json import json
import random import random
import io import io
import time
from helpers import aws_helper, tar_helper, csv_helper, service_helper from helpers import aws_helper, tar_helper, csv_helper, service_helper
@ -12,7 +11,7 @@ from models.form import Form
from services.base import Base from services.base import Base
class Loft(Base): class LoftService(Base):
def process(self): def process(self):
self.solver_run = SolverRun.parse_obj(self.retreive_attributes_from_message()) self.solver_run = SolverRun.parse_obj(self.retreive_attributes_from_message())
self.solution = self.generate_solution() self.solution = self.generate_solution()
@ -20,17 +19,17 @@ class Loft(Base):
def retreive_attributes_from_message(self): def retreive_attributes_from_message(self):
# get s3 object # get s3 object
key = aws_helper.get_key_from_message(self.source) self.key = aws_helper.get_key_from_message(self.source)
s3_object = aws_helper.get_object(key, os.environ['SOLVER_INGEST_BUCKET']) s3_object = aws_helper.get_object(self.key, os.environ['SOLVER_INGEST_BUCKET'])
# convert to tar # convert to tar
tar = tar_helper.raw_to_tar(s3_object) self.tar = tar_helper.raw_to_tar(s3_object)
# get attributes file and convert to dict # get attributes file and convert to dict
attributes = json.loads(tar_helper.extract_file_from_tar(tar, 'solver_run_attributes.json').read()) attributes = json.loads(tar_helper.extract_file_from_tar(self.tar , 'solver_run_attributes.json').read())
# get items file and convert to dict # get items file and convert to dict
items_csv = tar_helper.extract_file_from_tar(tar, 'items.csv') items_csv = tar_helper.extract_file_from_tar(self.tar , 'items.csv')
items_csv_reader = csv_helper.file_stream_reader(items_csv) items_csv_reader = csv_helper.file_stream_reader(items_csv)
# add items to attributes dict # add items to attributes dict
@ -60,4 +59,4 @@ class Loft(Base):
solution_file = service_helper.solution_to_file(buffer, self.solver_run.total_form_items, self.solution.forms) solution_file = service_helper.solution_to_file(buffer, self.solver_run.total_form_items, self.solution.forms)
# upload generated file to s3 and return result # upload generated file to s3 and return result
return aws_helper.file_stream_upload(solution_file, f'test_solution_{time.time()}.csv', os.environ['MEASURE_PROCESSED_BUCKET']) return aws_helper.file_stream_upload(solution_file, f'{service_helper.key_to_uuid(self.key)}.csv', os.environ['MEASURE_PROCESSED_BUCKET'])