Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit a4252d6

Browse files
committed
Stats table & begin Graph
1 parent 6701f30 commit a4252d6

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

php/core/Stats.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,50 @@ public function __construct( Template $temp, Login $login ) {
2626
$cmd = $this->paramsToCmd();
2727
if( !empty($cmd)){
2828
$data = new TTTStats($cmd, API::getStorageDir($this->login->getGroup()));
29+
$this->displayContent($data->getAllResults());
30+
}
31+
}
32+
}
2933

30-
$this->temp->setContent('GRAPH', PHP_EOL . print_r($data->getAllResults(), true));
34+
private function displayContent(array $data) : void {
35+
$this->temp->setContent('COMBIDATA', json_encode($data['combi']));
36+
$this->temp->setContent('TABLEA', $this->arrayToTable($data['main']));
37+
if(isset($data['today'])){
38+
$this->temp->setContent('TABLEB', $this->arrayToTable($data['today']) );
39+
}
40+
}
41+
42+
private function arrayToTable(array $data) : string {
43+
$table = "<table class=\"accounttable\">";
44+
$head = false;
45+
$lastCat = '';
46+
foreach($data as $row){
47+
if(!$head){
48+
$table .= "<tr>";
49+
foreach($row as $col => $val){
50+
$table .= "<th>". $col ."</th>";
51+
}
52+
$table .= "</tr>";
53+
$head = true;
54+
}
55+
$table .= "<tr>";
56+
foreach($row as $key => $val){
57+
if($key === 'Category'){
58+
if( $val === $lastCat ){
59+
$table .= "<td></td>";
60+
}
61+
else{
62+
$table .= "<td>". $val ."</td>";
63+
}
64+
$lastCat = $val;
65+
}
66+
else{
67+
$table .= "<td>". $val ."</td>";
68+
}
3169
}
70+
$table .= "</tr>";
3271
}
72+
return $table . "</table>";
3373
}
3474

3575
private function paramsToCmd() : array {
@@ -53,6 +93,12 @@ private function paramsToCmd() : array {
5393
$cmd[] = $_POST["range-from"];
5494
$cmd[] = $_POST["range-to"];
5595
}
96+
else if( isset($_POST["range-from"]) && preg_match( TTTStatsData::DATE_PREG, $_POST["range-from"]) === 1 ){
97+
$cmd[] = $_POST["range-from"];
98+
}
99+
else if( isset($_POST["range-to"]) && preg_match( TTTStatsData::DATE_PREG, $_POST["range-to"]) === 1){
100+
$cmd[] = $_POST["range-to"];
101+
}
56102
else{
57103
$error = true;
58104
}
@@ -85,6 +131,7 @@ private function paramsToCmd() : array {
85131
return array();
86132
}
87133
else{
134+
$this->temp->setContent('CMD', 'ttt s ' . implode(' ', $cmd));
88135
return $cmd;
89136
}
90137
}
@@ -104,6 +151,20 @@ private function setUpHtml(){
104151
}
105152
$this->temp->setMultipleContent('Devices', $ds);
106153
}
154+
155+
$this->temp->setContent('GRAPHES', json_encode(
156+
array_values(array_map(
157+
function ($s) {
158+
return substr($s, 0, -3);
159+
},
160+
array_filter(
161+
scandir(__DIR__ . '/../load/graphs/'),
162+
function ($s) {
163+
return strlen($s) > 4 && substr($s, -3) === '.js';
164+
}
165+
)
166+
))
167+
));
107168
}
108169

109170
}

php/core/templates/stats.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
"%%NAME%%" : ""
99
}
1010
},
11-
"%%GRAPH%%" : ""
11+
"%%TABLEA%%" : "",
12+
"%%TABLEB%%" : "",
13+
"%%COMBIDATA%%" : "[]",
14+
"%%CMD%%" : "",
15+
"%%GRAPHES%%" : ""
1216
}

php/core/templates/stats_en.html

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,39 @@ <h3>Select data</h3>
4848
}
4949
});
5050
</script>
51+
<h3>Tables</h3>
52+
<p><code>%%CMD%%</code></p>
53+
54+
<div style="margin-bottom: 10px;">
55+
%%TABLEA%%
56+
</div>
57+
<div>
58+
%%TABLEB%%
59+
</div>
5160

5261
<h3>Graph</h3>
62+
<select id="graphselect">
63+
64+
</select>
65+
<button id="displaygraph">Display Graph</button>
66+
67+
<div id="maingraph"></div>
68+
<script>
69+
var combiData = %%COMBIDATA%%;
70+
var graphes = %%GRAPHES%%;
71+
var loadUrl = "%%SERVERURL%%/load/graphs/";
72+
73+
$.ajaxSetup({cache: true});
74+
graphes.forEach( (g) => {
75+
$("select#graphselect").append('<option value="'+g+'">'+g+'</option>');
76+
});
5377

54-
<pre>
55-
%%GRAPH%%
56-
</pre>
78+
$("button#displaygraph").click( () => {
79+
var url = loadUrl + $("select#graphselect").val() + '.js';
80+
$.getScript( url, () => {
81+
if( typeof createGraph === "function" && combiData.length > 0 ){
82+
createGraph(combiData, "div#maingraph");
83+
}
84+
});
85+
});
86+
</script>

php/load/graphs/simple.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function createGraph(data, selector){
2+
/**
3+
* The DOM has jQuery as $ and the ChartJS library.
4+
* The data can be found in data and selector is a css-selector
5+
* where the graph should appear.
6+
*/
7+
8+
console.log('simple', data, selector);
9+
}

php/load/graphs/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function createGraph(data, selector){
2+
/**
3+
* The DOM has jQuery as $ and the ChartJS library.
4+
* The data can be found in data and selector is a css-selector
5+
* where the graph should appear.
6+
*/
7+
8+
console.log('test', data, selector);
9+
}

0 commit comments

Comments
 (0)