Skip to content

Instantly share code, notes, and snippets.

@firemanxbr
Created July 2, 2019 14:45
Show Gist options
  • Save firemanxbr/f5bbcbccdbe7ed9597f78ceb906892bf to your computer and use it in GitHub Desktop.
Save firemanxbr/f5bbcbccdbe7ed9597f78ceb906892bf to your computer and use it in GitHub Desktop.
import os
import uuid
from flask import Flask
from flask import request
from flask import Response
import json
import numpy
import task
app = Flask(__name__)
@app.route('/run')
def run():
file_uri = request.args.get('fileUri')
assert file_uri is not None
uid = str(uuid.uuid4())
task.master.delay(file_uri, uid)
return uid
@app.route('/status/<uid>')
def status(uid):
log_file = '/svc/images/%s.log' % uid
if not os.path.isfile(log_file):
return Response('Log still not available.', mimetype='text/txt')
with open('/svc/images/%s.log' % uid) as f:
content = f.read()
return Response(content.encode(), mimetype='text/txt')
@app.route('/average/<uid>')
def average(uid):
log_file = '/svc/images/%s.average' % uid
if not os.path.isfile(log_file):
return Response('Log still not available.', mimetype='text/txt')
avg = None
msg = 'Images:\n'
with open('/svc/images/%s.average' % uid) as f:
for line in f:
data = json.loads(line)
if data['image'] not in msg:
msg += '%s - %s\n' % (data['image'], data['timestamp'])
if avg is not None:
avg = (avg + numpy.array(data['array'])) / 2
else:
avg = numpy.array(data['array'])
msg += 'Average:\n'
msg += str(avg)
return Response(msg, mimetype='text/txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment