Files
www-POC/app/__init__.py

152 lines
6.4 KiB
Python

# project/__init__.py
import os
from flask import Flask, render_template, request, session, send_from_directory, jsonify, url_for, flash, redirect, Response
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import secure_filename
from sqlalchemy import and_
from sqlalchemy.sql import func
# import mysql.connector
from datetime import datetime
UPLOAD_FOLDER = '/root/tag-server/flask/app/docs'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'zip'])
app = Flask('app', static_url_path='')
app.config.update(
DEBUG=True,
# SQLALCHEMY_DATABASE_URI='mysql+mysqlconnector://website:henrypump@127.0.0.1/poconsole'
SQLALCHEMY_DATABASE_URI='sqlite:///../database.db',
)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.secret_key = 'henry_pump'
db = SQLAlchemy(app)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
return app.send_static_file('index.html')
from .datalogger import datalogger
from .datalogger.models import *
@app.route('/api/latest')
def get_latest_tag_vals():
res = db.engine.execute('SELECT v1._id as id, v1.created_on as dtime, t._id as t_id, t.name as name, t.tag as tag, v1.value as value, t.units as units, t.description as description, t.min_expected as min_expected, t.max_expected as max_expected FROM tag_vals v1 INNER JOIN tags t ON t._id = v1.tag_id WHERE v1._id = (SELECT v2._id FROM tag_vals v2 WHERE v2.tag_id = v1.tag_id ORDER BY v2._id DESC LIMIT 1) ORDER BY t._id')
lat = res.fetchall()
latest_tags = list(map(latest_to_obj, lat))
return jsonify(latest_tags)
@app.route('/api/valuesbetween/<string:ids>/<string:start>/<string:end>')
def get_tag_vals_between(ids, start, end):
ids = ids.split(',')
res = Tag_val.query.filter(and_(Tag_val.tag_id.in_(ids), Tag_val.created_on > start, Tag_val.created_on <= end)).all()
return jsonify([i.serialize for i in res])
@app.route('/api/multipletags/<string:ids>')
def get_multiple_tags(ids):
ids = ids.split(',')
res = Tag.query.filter(Tag._id.in_(ids)).all()
return jsonify([i.serialize for i in res])
@app.route('/doc/upload', methods=['POST'])
def upload_file():
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect("/#/docs")
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect("/#/docs")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
d = Doc(name=filename)
db.session.add(d)
db.session.commit()
return redirect(url_for('uploaded_file',
filename=filename))
return redirect("/#/docs")
@app.route('/docs/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/csv/all')
def get_csv_all():
csv_string = "datetime,"
all_tags = [i.serialize for i in Tag.query.all()]
all_tag_names = [x['name'] for x in all_tags]
for x in all_tag_names:
csv_string += "{},".format(x)
csv_string += "\n"
all_vals = [i.serialize for i in Tag_val.query.all()]
val_objs = [{'value': x['value'], 'tag_name': x['tag']['name'], 'datetime': x['created_on']} for x in all_vals]
for v in val_objs:
tag_ind = all_tag_names.index(v['tag_name'])
csv_string += "{},".format(v['datetime']) + "," * tag_ind + "{},".format(v['value']) + "," * (len(all_tag_names) - tag_ind) + "\n"
return Response(
csv_string,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=datadump.csv"})
@app.route('/csv/<string:ids>')
def get_csv_selected(ids):
csv_string = "datetime,"
all_tags = [i.serialize for i in Tag.query.filter(Tag._id.in_(ids)).all()]
all_tag_names = [x['name'] for x in all_tags]
for x in all_tag_names:
csv_string += "{},".format(x)
csv_string += "\n"
all_vals = [i.serialize for i in Tag_val.query.filter(Tag_val.tag_id.in_(ids)).all()]
val_objs = [{'value': x['value'], 'tag_name': x['tag']['name'], 'datetime': x['created_on']} for x in all_vals]
for v in val_objs:
tag_ind = all_tag_names.index(v['tag_name'])
csv_string += "{},".format(v['datetime']) + "," * tag_ind + "{},".format(v['value']) + "," * (len(all_tag_names) - tag_ind) + "\n"
return Response(
csv_string,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=datadump{}.csv".format(ids.replace(",","-"))})
@app.route('/api/card_dates')
def get_card_dates():
# res = session.query(func.DATE(Card.created_on)).distinct()
query = 'SELECT DISTINCT(DATE("created_on", "localtime")) FROM cards'
res = db.engine.execute(query).fetchall()
return jsonify([str(i[0]) for i in res])
# return jsonify([i.serialize for i in res])
@app.route('/api/cardsbydate/<string:datepar>', defaults={'page':1})
@app.route('/api/cardsbydate/<string:datepar>/<int:page>')
def get_cardsbydate(datepar, page):
res = Card.query.with_entities(Card._id, Card.stroke_number, Card.stroke_type, Card.created_on).filter(func.date(Card.created_on, 'localtime') == datepar).paginate(page=page,per_page=20, error_out=False)
# Mon, 14 Nov 2016 19:46:09 GMT
return jsonify({
'cards':[{'_id': i[0], 'stroke_number': i[1], 'stroke_type': i[2], 'created_on': i[3]} for i in res.items],
'num_pages':res.pages, 'per_page': res.per_page, 'total':res.total})
@app.route('/api/tagsattime/<string:datetime>')
def get_tags_at_time(datetime):
query = "SELECT v._id, v.value, v.created_on, t.name, t.max_expected, t.min_expected, t.units FROM (SELECT tag_id, MAX(created_on) created_on FROM tag_vals WHERE created_on <= '{}' GROUP BY tag_id) v0 JOIN tag_vals v ON v0.tag_id = v.tag_id AND v0.created_on = v.created_on JOIN tags t ON t._id = v.tag_id".format(datetime)
res = db.engine.execute(query)
tag_data = res.fetchall()
tag_data_list = list(map(tagsattime_to_obj, tag_data))
return jsonify(tag_data_list)