Browse Source

upperase literals + server start bugfix

pull/1/head
Alexey Velikiy 6 years ago
parent
commit
1c473d129d
  1. 6
      analytics/analytic_unit_worker.py
  2. 8
      analytics/detectors/pattern_detector.py
  3. 21
      analytics/server.py
  4. 4
      server/src/controllers/analytics_controller.ts
  5. 6
      server/src/services/analytics_service.ts

6
analytics/analytic_unit_worker.py

@ -18,11 +18,11 @@ class AnalyticUnitWorker(object):
try: try:
type = task['type'] type = task['type']
analytic_unit_id = task['analyticUnitId'] analytic_unit_id = task['analyticUnitId']
if type == "predict": if type == "PREDICT":
last_prediction_time = task['lastPredictionTime'] last_prediction_time = task['lastPredictionTime']
pattern = task['pattern'] pattern = task['pattern']
result = await self.do_predict(analytic_unit_id, last_prediction_time, pattern) result = await self.do_predict(analytic_unit_id, last_prediction_time, pattern)
elif type == "learn": elif type == "LEARN":
segments = task['segments'] segments = task['segments']
pattern = task['pattern'] pattern = task['pattern']
result = await self.do_learn(analytic_unit_id, segments, pattern) result = await self.do_learn(analytic_unit_id, segments, pattern)
@ -38,7 +38,7 @@ class AnalyticUnitWorker(object):
# TODO: move result to a class which renders to json for messaging to analytics # TODO: move result to a class which renders to json for messaging to analytics
result = { result = {
'task': type, 'task': type,
'status': "failed", 'status': "FAILED",
'analyticUnitId': analytic_unit_id, 'analyticUnitId': analytic_unit_id,
'error': str(e) 'error': str(e)
} }

8
analytics/detectors/pattern_detector.py

@ -17,13 +17,13 @@ logger = logging.getLogger('analytic_toolset')
def resolve_model_by_pattern(pattern: str) -> models.Model: def resolve_model_by_pattern(pattern: str) -> models.Model:
if pattern == 'peak': if pattern == 'PEAK':
return models.PeaksModel() return models.PeaksModel()
if pattern == 'drop': if pattern == 'DROP':
return models.StepModel() return models.StepModel()
if pattern == 'jump': if pattern == 'JUMP':
return models.JumpModel() return models.JumpModel()
if pattern == 'custom': if pattern == 'CUSTOM':
return models.CustomModel() return models.CustomModel()
raise ValueError('Unknown pattern "%s"' % pattern) raise ValueError('Unknown pattern "%s"' % pattern)

21
analytics/server.py

@ -28,24 +28,24 @@ logging_handler.setFormatter(logging_formatter)
root.addHandler(logging_handler) root.addHandler(logging_handler)
async def handle_task(payload: str): async def handle_task(task: object):
try: try:
task = json.loads(payload)
logger.info("Command is OK") logger.info("Command is OK")
response_task_payload = json.dumps({ response_task_payload = {
'_taskId': task['_taskId'], '_taskId': task['_taskId'],
'task': task['type'], 'task': task['type'],
'analyticUnitId': task['analyticUnitId'], 'analyticUnitId': task['analyticUnitId'],
'status': "in progress" 'status': "IN_PROGRESS"
}) }
message = services.server_service.ServerMessage('TASK_RESULT', response_task_payload) message = services.server_service.ServerMessage('TASK_RESULT', response_task_payload)
await server_service.send_message(message) await server_service.send_message(message)
res = await worker.do_task(task) res = await worker.do_task(task)
res['_taskId'] = task['_taskId'] res['_taskId'] = task['_taskId']
await server_service.send_message(json.dumps(res)) await server_service.send_message(json.dumps(res))
except Exception as e: except Exception as e:
@ -53,10 +53,9 @@ async def handle_task(payload: str):
async def handle_message(message: services.ServerMessage): async def handle_message(message: services.ServerMessage):
payload = None payload = None
if message.payload is not None: if message.method == 'TASK':
payload = json.loads(message.payload) await handle_task(message.payload)
if message.method == 'task':
await handle_task(payload)
def init_services(): def init_services():
logger.info("Starting services...") logger.info("Starting services...")
@ -70,7 +69,7 @@ def init_services():
return server_service, data_service return server_service, data_service
async def app_loop(): async def app_loop():
server_service.handle_loop() await server_service.handle_loop()
# await asyncio.gather(server_service.handle_loop(), test_file_save()) # await asyncio.gather(server_service.handle_loop(), test_file_save())

4
server/src/controllers/analytics_controller.ts

@ -36,7 +36,7 @@ async function onMessage(message: AnalyticsMessage) {
let resolvedMethod = false; let resolvedMethod = false;
if(message.method === 'TASK_RESULT') { if(message.method === 'TASK_RESULT') {
onTaskResult(JSON.parse(message.payload)); onTaskResult(message.payload);
resolvedMethod = true; resolvedMethod = true;
} }
@ -85,7 +85,7 @@ async function runTask(task): Promise<any> {
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
let segments = getLabeledSegments(id); let segments = getLabeledSegments(id);
AnalyticUnit.setStatus(id, 'learning'); AnalyticUnit.setStatus(id, 'LEARNING');
let unit = AnalyticUnit.findById(id); let unit = AnalyticUnit.findById(id);
let pattern = unit.type; let pattern = unit.type;
let task = { let task = {

6
server/src/services/analytics_service.ts

@ -38,13 +38,13 @@ export class AnalyticsService {
this._init(); this._init();
} }
public async sendTask(msgObj: any): Promise<void> { public async sendTask(taskObj: any): Promise<void> {
if(!this._ready) { if(!this._ready) {
return Promise.reject("Analytics is not ready"); return Promise.reject("Analytics is not ready");
} }
let message = { let message = {
method: 'task', method: 'TASK',
payload: JSON.stringify(msgObj) payload: taskObj
} }
return this.sendMessage(message); return this.sendMessage(message);
} }

Loading…
Cancel
Save