-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdataJSON.php
More file actions
150 lines (110 loc) · 4.29 KB
/
Copy pathdataJSON.php
File metadata and controls
150 lines (110 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/* Copyright 2012 Bryan Nielsen <bnielsen1965@gmail.com>
*
* This file is part of Simple SPC.
*
* Simple SPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Simple SPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Simple SPC. If not, see <http://www.gnu.org/licenses/>.
*
*/
include 'config.php';
$user = new User;
include HEADERS . 'operator_header.php';
$metric = new Metric;
$rule = new Rule;
$db = new DB;
if( isset($_REQUEST['action']) ) $action = $_REQUEST['action'];
else $action = 'ChartData';
$errorMessages = array();
$json = array();
switch($action) {
case 'ChartData':
if( isset($_REQUEST['Name']) ) {
// query the last 50 data points
$sql = "SELECT * FROM Measurements WHERE Metric_Name='" . $db->escapeString($_REQUEST['Name']) . "' " .
"ORDER BY Timestamp DESC LIMIT 0, 50";
$rs = $db->runQuery($sql);
if( $rs ) {
// reverse the array so we have ascending order
$rs = array_reverse($rs);
$metric = new Metric($_REQUEST['Name']);
// get control settings
$centerLine = $metric->getCenterline();
$UCL = $metric->getUCL();
$LCL = $metric->getLCL();
// make sure values are numbers if numeric
$centerLine = (is_numeric($centerLine) ? floatval($centerLine) : $centerLine);
$UCL = (is_numeric($UCL) ? floatval($UCL) : $UCL);
$LCL = (is_numeric($LCL) ? floatval($LCL) : $LCL);
$data = array();
// process each data row
$dataMax = 0;
$dataMin = 0;
$decimalPoints = $metric->getPrecision(); // 0;
foreach( $rs as $i => $row ) {
// convert timestamp string to time value
$ts = strtotime($row['Timestamp']);
$value = floatval($row['Value']);
// store data point in array
$data[] = array(date('Y-m-d h:i:sA', $ts), $value);
if( $value > $dataMax ) $dataMax = $value;
if( $value < $dataMin ) $dataMin = $value;
/*
$d = abs($value) - floor(abs($value));
if( $d > 0 ) {
$dparts = explode('.', strval($d));
if( isset($dparts[1]) ) $decimalPoints = strlen($dparts[1]);
}
*/
}
// determine start and end dates for line
$startdate = strtotime($rs[0]['Timestamp']);
$enddate = strtotime($rs[count($rs) - 1]['Timestamp']);
// create control lines
$center = array(array(date('Y-m-d h:i:sA', $startdate), $centerLine), array(date('Y-m-d h:i:sA', $enddate), $centerLine));
$json['centerLine'] = $center;
if( is_numeric($UCL) ) $json['UCL'] = array(array(date('Y-m-d h:i:sA', $startdate), $UCL), array(date('Y-m-d h:i:sA', $enddate), $UCL));
if( is_numeric($LCL) ) $json['LCL'] = array(array(date('Y-m-d h:i:sA', $startdate), $LCL), array(date('Y-m-d h:i:sA', $enddate), $LCL));
$json['data'] = $data;
$json['range'] = abs($dataMax - $dataMin);
$json['decimalPoints'] = $decimalPoints;
// run SPC rule checks
$rule = new Rule;
$spcResult = $rule->runSPC($_REQUEST['Name']);
if( $spcResult->violationData && count($spcResult->violationData) > 0 ) {
$violationData = array();
foreach( $spcResult->violationData as $i => $row ) {
// convert timestamp string to time value
$ts = strtotime($row['Timestamp']);
$value = floatval($row['Value']);
// store data point in array
$violationData[] = array(date('Y-m-d h:i:sA', $ts), $value);
}
$json['violationData'] = $violationData;
}
if( $spcResult->violationMessages && count($spcResult->violationMessages) > 0 ) {
$json['violationMessages'] = $spcResult->violationMessages;
}
$json['spc'] = $spcResult;
$json['description'] = $metric->getDescription();
}
}
// exit();
}
$json['errorMessages'] = $errorMessages;
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
//header('Content-type: application/json');
echo json_encode($json);
exit();
?>