def render_graph_data(self): self.init() selected = request.args.get("session") session = self.storage.get_selected_session(selected) left_axis = request.args.get("left_axis") right_axis = request.args.get("right_axis") colors = request.args.get("colors") if self.config.read("colors") != colors: self.config.write("colors", colors, flush=True) format = Format(session["version"] if session else None) data = [] if session: for item in self.storage.fetch_measurements(session["id"]): if left_axis in item: data.append({ "date": format.timestamp(item), "left": item[left_axis], "right": item[right_axis], }) return jsonify(data)
def render_graph_data(self): self.init() selected = request.args.get("name") name = self.storage.translate_selected_name(selected) left_axis = request.args.get("left_axis") right_axis = request.args.get("right_axis") data = { "labels": [], "left": { "data": [], }, "right": { "data": [], }, } format = Format() for item in self.storage.fetch_measurements(name): if left_axis in item: data["left"]["data"].append({ "t": format.timestamp(item), "y": item[left_axis], }) if right_axis in item: data["right"]["data"].append({ "t": format.timestamp(item), "y": item[right_axis], }) return jsonify(data)
def render_data(self): self.init() names, selected = self.prepare_selection() name = self.storage.translate_selected_name(selected) if request.args.get("export") == "": string = io.StringIO() writer = csv.writer(string) format = Format() names = [] for field in format.export_fields: names.append(format.field_name(field)) writer.writerow(names) for item in self.storage.fetch_measurements(name): values = [] for field in format.export_fields: if field == "time": values.append(format.time(item)) else: values.append(item[field]) writer.writerow(values) output = make_response(string.getvalue()) output.headers[ "Content-Disposition"] = "attachment; filename=" + name + ".csv" output.headers["Content-type"] = "text/csv" return output elif request.args.get("destroy") == "": self.storage.destroy_measurements(name) flash("Measurements with session name '" + name + "' were deleted", "danger") return redirect(request.path) page = request.args.get("page", 1, int) limit = 100 offset = limit * (page - 1) count = self.storage.fetch_measurements_count(name) pages = self.prepare_pages(name, page, limit, count) measurements = self.storage.fetch_measurements(name, limit, offset) return render_template( "data.html", names=names, selected=selected, measurements=measurements, page="data", pages=pages, )
def fill(self): variables = { "format": Format(), "url_for": self.url_for, "port": self.config.read("port", ""), "rate": str(self.config.read("rate", 1.0)), "name": self.config.read("name", arrow.now().format("YYYY-MM-DD")), } status = self.storage.fetch_status() variables["status"] = status.title() variables["connect_disabled"] = status != "disconnected" variables["connect_button"] = "Connect" if status == "disconnected" else "Disconnect" return variables
def update(self, data, version): format = Format(version) table = [] for name in format.table_fields: callback = getattr(format, name) table.append(callback(data)) graph = {} for name in format.graph_fields: if name == "timestamp": callback = getattr(format, name) value = callback(data) else: value = data[name] graph[name] = value graph = self.converter.convert(graph) if self.on_receive: if not self.buffer: self.buffer = [] data["timestamp"] = int(data["timestamp"]) self.buffer.append(data) execute = True if self.on_receive_interval: execute = False if not self.buffer_expiration or self.buffer_expiration <= time( ): execute = True self.buffer_expiration = time() + self.on_receive_interval if execute: payload = json.dumps(self.buffer) self.buffer = None payload_file = os.path.join( os.getcwd(), "on-receive-payload-%s.json") % time() with open(payload_file, "w") as file: file.write(payload) command = self.on_receive + " \"" + payload_file + "\"" subprocess.Popen(command, shell=True, env={}) self.emit("update", json.dumps({ "table": table, "graph": graph, }))
def update(self, data): format = Format() table = [] for name in format.table_fields: callback = getattr(format, name) table.append(callback(data)) graph = {} for name in format.graph_fields: if name == "timestamp": callback = getattr(format, name) value = callback(data) else: value = data[name] graph[name] = value self.emit("update", json.dumps({ "table": table, "graph": graph, }))
def render_graph(self): self.init() sessions, selected = self.prepare_selection() session = self.storage.get_selected_session(selected) format = Format(session["version"] if session else None) last_measurement = None if selected == "": last_measurement = self.storage.fetch_last_measurement() return render_template( "graph.html", format=format, sessions=sessions, selected=selected, item=last_measurement, left_axis="voltage", right_axis="current", colors=self.config.read("colors", "colorful"), page="graph" )
def render_data(self): self.init() sessions, selected = self.prepare_selection() session = self.storage.get_selected_session(selected) format = None measurements = [] pages = [] if session: format = Format(session["version"]) if request.args.get("export") == "": string = io.StringIO() writer = csv.writer(string) names = [] for field in format.export_fields: names.append(format.field_name(field)) writer.writerow(names) run_time_offset = None for item in self.storage.fetch_measurements(session["id"]): if run_time_offset is None and item["resistance"] < 9999.9: run_time_offset = item["timestamp"] rune_time = 0 if run_time_offset is not None: rune_time = round(item["timestamp"] - run_time_offset) values = [] for field in format.export_fields: if field == "time": values.append(format.time(item)) elif field == "run_time": remaining = rune_time hours = floor(remaining / 3600) remaining -= hours * 3600 minutes = floor(remaining / 60) remaining -= minutes * 60 seconds = remaining parts = [ hours, minutes, seconds, ] for index, value in enumerate(parts): parts[index] = str(value).zfill(2) values.append(":".join(parts)) elif field == "run_time_seconds": values.append(rune_time) else: values.append(item[field]) writer.writerow(values) output = make_response(string.getvalue()) output.headers["Content-Disposition"] = "attachment; filename=" + session["name"] + ".csv" output.headers["Content-type"] = "text/csv" return output elif request.args.get("destroy") == "": if selected == "": flash("Please select session first", "info") return redirect(request.path) self.storage.destroy_measurements(session["id"]) flash("Measurements with session name '" + session["name"] + "' were deleted", "danger") return redirect(request.path) page = request.args.get("page", 1, int) limit = 100 offset = limit * (page - 1) count = self.storage.fetch_measurements_count(session["id"]) pages = self.prepare_pages(session["id"], page, limit, count) measurements = self.storage.fetch_measurements(session["id"], limit, offset) return render_template( "data.html", format=format, sessions=sessions, selected=selected, measurements=measurements, pages=pages, page="data", )