From 6499636b0a346005ebd53397de1302af856af904 Mon Sep 17 00:00:00 2001 From: Emrys Date: Tue, 5 Feb 2019 12:58:38 +0000 Subject: [PATCH] added download dropdown to view template added css to style dropdown added new api endpoint /download returned file as download (csv or json) passes all options from "CSV options" dropdowns to new api endpoint --- Lib/timezone-js | 1 + graph.css | 13 +++++ graph.js | 21 ++++++++ graph_controller.php | 119 ++++++++++++++++++++++++++++++++++++++++++- view.php | 40 +++++++++++++++ 5 files changed, 193 insertions(+), 1 deletion(-) create mode 160000 Lib/timezone-js diff --git a/Lib/timezone-js b/Lib/timezone-js new file mode 160000 index 0000000..c071c49 --- /dev/null +++ b/Lib/timezone-js @@ -0,0 +1 @@ +Subproject commit c071c498822c4aeacfe9159f058571e83dfb9acd diff --git a/graph.css b/graph.css index 796d1f4..6b11c9e 100644 --- a/graph.css +++ b/graph.css @@ -160,6 +160,19 @@ input.feed-tag-checkbox-left, input.feed-tag-checkbox-right{ #legend [data-legend-series] a{ outline: none; } + +#download-buttons form{ + margin: 0; + padding: 0; +} +#download-buttons form button { + width: 100%; + text-align: left; +} +.csvoptions{ + width: auto; +} + @media(max-width: 512px) { #legend .col { position: relative; left: 0; diff --git a/graph.js b/graph.js index a339a2a..9fb8987 100644 --- a/graph.js +++ b/graph.js @@ -915,6 +915,7 @@ function printcsv() var line = []; var lastvalue = []; var start_time = feedlist[0].data[0][0]; + var end_time = feedlist[feedlist.length-1].data[feedlist[feedlist.length-1].data.length-1][0]; var showName=false; var showTag=false; @@ -992,6 +993,26 @@ function printcsv() } } $("#csv").val(csvout); + + // populate download form + for (f in feedlist) { + var meta = feedlist[f]; + + $("[data-download]").each(function(i,elem){ + $form = $(this); + var path = $form.find('[data-path]').val(); + var action = $form.find('[data-action]').val(); + var format = $form.find('[data-format]').val(); + $form.attr('action', path + action + '.' + format); + $form.find('[name="ids"]').val(meta.id); + $form.find('[name="start"]').val(start_time); + $form.find('[name="end"]').val(end_time); + $form.find('[name="headers"]').val('names'); + $form.find('[name="timeformat"]').val(csvtimeformat); + $form.find('[name="interval"]').val(view.interval); + $form.find('[name="nullvalues"]').val(csvnullvalues); + }); + } } //---------------------------------------------------------------------------------------- diff --git a/graph_controller.php b/graph_controller.php index f20c342..a079ae3 100644 --- a/graph_controller.php +++ b/graph_controller.php @@ -5,7 +5,7 @@ function graph_controller() { - global $session,$route,$mysqli,$redis; + global $session,$route,$mysqli,$redis, $path; // Check if group module is installed $group = false; @@ -70,7 +70,124 @@ function graph_controller() else if ($group && $route->action=="groupgraph") { $result = view("Modules/graph/group_view.php", array("session" => $session["write"], 'group_support' => 1)); } + // Download data + else if ($route->action === 'download') { + $ids = explode(',', get('ids')); + $start = get('start'); + $end = get('end'); + $shownull = get('shownull'); + $headers = get('headers'); // tags|names|none + $format = get('timeformat'); // datestr|unix|seconds + $nullvalues = get('nullvalues'); // lastvalue|remove|show + $interval = get('interval'); + $skipmissing = get('skipmissing'); + $limitinterval = get('limitinterval'); + + // @todo: make use of new multi id data endpoint + $id = $ids[0]; + $url = sprintf($path. "/feed/data.json?id=%s&start=%s&end=%s&interval=%s&skipmissing=%s&limitinterval=%s", + $id, $start, $end, $interval, $skipmissing, $limitinterval + ); + $input_data = json_decode(file_get_contents($url),true); + $lastvalue = null; + // create array with correctly formatted values + $data = array(); + if (!isset($input_data['success'])) { + foreach($input_data as $row){ + list($time, $value) = $row; + $time /= 1000; + if ($format === 'datestr') { + $time = date('c', $time); + } elseif ($format === 'seconds') { + $time = $time - $start; + } + $add_data = true; + if (is_null($value)) { + if ($nullvalues !== 'remove') { + if ($nullvalues === 'show') { + $value = null; + } elseif ($nullvalues === 'lastvalue') { + $value = $lastvalue; + } + } elseif ($nullvalues === 'remove') { + // dont add data to output + $add_data = false; + } + } else { + $lastvalue = $value; + } + if ($add_data) $data[] = array($time, $value, $row[0]); + } + } + + // filename for download + $filename = "graph_data_" . date('Ymd') . '.' . $route->format; + header("Content-Disposition: attachment; filename=\"$filename\""); + + // @todo: multi id api endpoint for aget.json + $url2 = sprintf($path. "/feed/aget.json?id=%s", $id); + $meta = json_decode(file_get_contents($url2), true); + + // feed titles + if (isset($meta['success'])) { + $title = $meta['message']; + $unit = ''; + } else { + if ($headers === 'tags') { + $title = $meta['tag']; + } else { + $title = implode(':', array($meta['tag'], $meta['name'])); + } + $unit = $meta['unit']; + } + + if ($route->format === 'csv') { + if (isset($input_data['success'])) { + return 'Error: ' . $input_data['message']; + } + // build csv column headings + if ($headers!=='none') { + if ($format === 'datestr') { + $col_titles[] = '"Date-time string"'; + } elseif ($format === 'seconds') { + $col_titles[] = '"Seconds since start"'; + } else { + $col_titles[] = '"Unix timestamp"'; + } + } + $col_titles[] = '"' . $title . ' (' . $unit . ')"'; + // join column headings with comma + $lines[] = implode(',', $col_titles); + + // get csv data + foreach($data as $col){ + $lines[] = implode(',', array($col[0], $col[1])); + } + + // join all the lines with line break + return implode("\n", $lines); + + } elseif($route->format === 'json') { + if (isset($input_data['success'])) return $input_data; + + // return data as json object + // data returned in data property + $json['title'] = $title; + $json['name'] = $meta['name']; + $json['tag'] = $meta['tag']; + $json['id'] = $meta['id']; + $json['unit'] = $meta['unit']; + foreach($data as $val) { + $json['data'][] = array( + 'timestamp'=> $val[2], + 'formatted'=> $val[0], + 'value'=> $val[1] + ); + } + return array($json); + } + } else { $result = view("Modules/graph/view.php", array("session" => $session["write"])); } diff --git a/view.php b/view.php index 75190cf..7ae7e85 100644 --- a/view.php +++ b/view.php @@ -233,9 +233,49 @@ +
+ + + + + +
+
+