def get(self, start, end): data_db = factory.data_db raw_col = factory.raw_col trans_col = factory.trans_col try: auth_headers = flask.request.headers.get("Authorization") method, token = auth_headers.split(" ") checked = factory.auth_db["token"].find({"token": token}) if checked.count(True) > 0: factory.auth_db["token"].update_one( {"token": token}, { "$inc": { "expir": 604800000 } } ) raw_records = data_db[raw_col].find({ "$and": [{"timestamp": {"$gte": start}}, {"timestamp": {"$lte": end}}]} ) trans_records = data_db[trans_col].find({ "$and": [{"timestamp": {"$gte": start}}, {"timestamp": {"$lte": end}}]} ) def generator(raw_records, trans_records): header = ["timestamp"] for sensor in raw_records[0]["sensors"]: header.append(sensor) for channel in trans_records[0]["channel"]: header.append(channel) yield ",".join(header) + "\n" for raw_json, trans_json in zip(raw_records, trans_records): row = [ datetime.datetime.fromtimestamp(raw_json["timestamp"] / 1000).strftime("%Y-%m-%d %H:%M:%S")] for sensor in raw_json["sensors"]: row.append(str(raw_json["sensors"][sensor]["value"])) for channel in trans_json["channel"]: row.append(str(trans_json["channel"][channel]["value"])) yield ",".join(row) + "\n" resp = Response(generator(raw_records=raw_records, trans_records=trans_records)) resp.headers["Content-Length"] = resp.calculate_content_length() resp.headers["Access-Control-Allow-Origin"] = "*" resp.headers["Content-Disposition"] = "attachment; filename=export.csv" resp.headers["Content-Type"] = "text/csv; charset=utf-8" return resp except Exception, err: resp_data = { "err": "True", "message": str(err), "result": "" } resp = flask.make_response(dumps(resp_data)) resp.headers.extend({ "Access-Control-Allow-Origin": "*" }) return resp
def get(self, start, end): data_db = factory.data_db raw_col = factory.raw_col trans_col = factory.trans_col try: auth_headers = flask.request.headers.get("Authorization") method, token = auth_headers.split(" ") checked = factory.auth_db["token"].find({"token": token}) if checked.count(True) > 0: factory.auth_db["token"].update_one( {"token": token}, {"$inc": { "expir": 604800000 }}) raw_records = data_db[raw_col].find({ "$and": [{ "timestamp": { "$gte": start } }, { "timestamp": { "$lte": end } }] }) trans_records = data_db[trans_col].find({ "$and": [{ "timestamp": { "$gte": start } }, { "timestamp": { "$lte": end } }] }) def generator(raw_records, trans_records): header = ["timestamp"] for sensor in raw_records[0]["sensors"]: header.append(sensor) for channel in trans_records[0]["channel"]: header.append(channel) yield ",".join(header) + "\n" for raw_json, trans_json in zip(raw_records, trans_records): row = [ datetime.datetime.fromtimestamp( raw_json["timestamp"] / 1000).strftime("%Y-%m-%d %H:%M:%S") ] for sensor in raw_json["sensors"]: row.append( str(raw_json["sensors"][sensor]["value"])) for channel in trans_json["channel"]: row.append( str(trans_json["channel"][channel]["value"])) yield ",".join(row) + "\n" resp = Response( generator(raw_records=raw_records, trans_records=trans_records)) resp.headers["Content-Length"] = resp.calculate_content_length( ) resp.headers["Access-Control-Allow-Origin"] = "*" resp.headers[ "Content-Disposition"] = "attachment; filename=export.csv" resp.headers["Content-Type"] = "text/csv; charset=utf-8" return resp except Exception, err: resp_data = {"err": "True", "message": str(err), "result": ""} resp = flask.make_response(dumps(resp_data)) resp.headers.extend({"Access-Control-Allow-Origin": "*"}) return resp