diff --git a/app/__init__.py b/app/__init__.py index e03065e..92ffbe3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,16 +1,12 @@ -# project/__init__.py - +# app/__init__.py import os -from flask import Flask, render_template, request, session, send_from_directory, jsonify, url_for, flash, redirect, Response +from flask import Flask 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']) +# import mysql.connector + + +UPLOAD_FOLDER = os.path.join(os.getcwd(), 'app', 'docs') app = Flask('app', static_url_path='') app.config.update( @@ -23,129 +19,6 @@ 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('/') -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///') -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/') -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/') -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/') -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/', defaults={'page':1}) -@app.route('/api/cardsbydate//') -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/') -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) +from datalogger import views +from datalogger import api +from datalogger.models import * diff --git a/app/datalogger/datalogger.py b/app/datalogger/api.py similarity index 100% rename from app/datalogger/datalogger.py rename to app/datalogger/api.py diff --git a/app/datalogger/models.py b/app/datalogger/models.py index 081ae0c..fd0413b 100644 --- a/app/datalogger/models.py +++ b/app/datalogger/models.py @@ -122,31 +122,6 @@ class Tag(db.Model): "updated_on": self.updated_on } -def latest_to_obj(tup): - ob = {} - ob['id'] = tup[0] - ob['datetime'] = str(tup[1]) - ob['tag_id'] = str(tup[2]) - ob['tag_name'] = str(tup[3]) - ob['tag'] = str(tup[4]) - ob['value'] = tup[5] - ob['units'] = tup[6] - ob['tag_description'] = str(tup[7]) - ob['min_expected'] = tup[8] - ob['max_expected'] = tup[9] - return ob - -def tagsattime_to_obj(tup): - ob = {} - ob['_id'] = tup[0] - ob['value'] = tup[1] - ob['created_on'] = str(tup[2]) - ob['tag_name'] = str(tup[3]) - ob['min_expected'] = tup[4] - ob['max_expected'] = tup[5] - ob['units'] = tup[6] - return ob - class Tag_val(db.Model): __tablename__ = "tag_vals" diff --git a/app/datalogger/views.py b/app/datalogger/views.py new file mode 100644 index 0000000..0a53d5a --- /dev/null +++ b/app/datalogger/views.py @@ -0,0 +1,163 @@ + +import os +from flask import Flask, render_template, request, session, send_from_directory, jsonify, url_for, flash, redirect, Response +from werkzeug.utils import secure_filename +from sqlalchemy import and_ +from sqlalchemy.sql import func +from datetime import datetime + +from app import app, db +from app.datalogger.models import * + + +ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'zip']) + +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + +def latest_to_obj(tup): + ob = {} + ob['id'] = tup[0] + ob['datetime'] = str(tup[1]) + ob['tag_id'] = str(tup[2]) + ob['tag_name'] = str(tup[3]) + ob['tag'] = str(tup[4]) + ob['value'] = tup[5] + ob['units'] = tup[6] + ob['tag_description'] = str(tup[7]) + ob['min_expected'] = tup[8] + ob['max_expected'] = tup[9] + return ob + +def tagsattime_to_obj(tup): + ob = {} + ob['_id'] = tup[0] + ob['value'] = tup[1] + ob['created_on'] = str(tup[2]) + ob['tag_name'] = str(tup[3]) + ob['min_expected'] = tup[4] + ob['max_expected'] = tup[5] + ob['units'] = tup[6] + return ob + + +@app.route('/', defaults={'path': ''}) +@app.route('/') +def catch_all(path): + return app.send_static_file('index.html') + + +@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///') +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/') +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/') +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/') +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/', defaults={'page':1}) +@app.route('/api/cardsbydate//') +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/') +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)