-
Notifications
You must be signed in to change notification settings - Fork 3
/
rules.php
395 lines (319 loc) · 10.3 KB
/
rules.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/* Copyright 2012 Bryan Nielsen <[email protected]>
*
* 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 . 'engineer_header.php';
if( !empty($_REQUEST['metricName']) ) $metricName = $_REQUEST['metricName'];
else $metricName = '';
$rule = new Rule;
// AJAX action request
if( isset($_REQUEST['action']) ) {
//$rule = new Rule;
$errorMessages = array();
$json = array();
$success = FALSE;
switch( $_REQUEST['action'] ) {
case 'createRule':
// make sure type is available
if( $rule->typeAvailable($_REQUEST['Type'], $_REQUEST['MetricName']) ) {
// make sure not in demo mode
if( !DEMO_MODE ) {
// attempt to create
if( $rule->createRule($_REQUEST['Type'], $_REQUEST['MetricName'], 'READY', $_REQUEST['ViolationStatus']) ) {
// successfully created
$success = TRUE;
}
else $errorMessages[] = 'Error occurred creating rule';
}
else {
$errorMessages[] = 'Cannot create rule in demo mode';
}
}
else $errorMessages[] = 'Type not available';
break;
case 'deleteRule':
// make sure not in demo mode
if( !DEMO_MODE ) {
// attempt to delete
if( $rule->deleteRule($_REQUEST['Type'], $_REQUEST['MetricName']) ) {
// successfully deleted
$success = TRUE;
}
else $errorMessages[] = 'Error occurred deleting rule';
}
else {
$errorMessages[] = 'Cannot delete rule in demo mode';
}
break;
case 'updateRule':
// make sure not in demo mode
if( !DEMO_MODE ) {
// if type and old type do not match then update
if( $_REQUEST['oldType'] != $_REQUEST['Type'] ) {
// make sure type is available
if( $rule->typeAvailable($_REQUEST['Type'], $_REQUEST['MetricName']) ) {
// try changing type
if( $rule->changeType($_REQUEST['oldType'], $_REQUEST['Type'], $_REQUEST['MetricName']) === FALSE ) {
$errorMessages[] = 'Error changing rule type';
break;
}
else $success = TRUE;
}
else {
$errorMessages[] = 'Type not available';
}
}
if( $rule->updateRule($_REQUEST['Type'], $_REQUEST['MetricName'], $_REQUEST['ViolationStatus']) === FALSE ) {
$errorMessages[] = 'Error updating rule';
break;
}
$success = TRUE;
}
else {
$errorMessages[] = 'Cannot update rule in demo mode';
}
break;
case 'getRuleList':
// get count
$count = $rule->getRuleCount($_REQUEST['MetricName']);
// if have rules then create a list
if( !empty($count) ) {
// start with an empty array
$list = array();
// determine the limit for the number of names in the list
$limit = (!empty($_REQUEST['limit']) ? intval($_REQUEST['limit']) : 3);
// determine the offset into the table for pagination
$offset = (!empty($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0);
// get the list
$list = $rule->getRuleList($_REQUEST['MetricName'], $offset, $limit);
// note any errors or store the list in the JSON array
if( $list === FALSE ) $errorMessages[] = 'Error getting rule list';
else if( is_array($list) ) $json['ruleList'] = $list;
// store the count
$json['ruleCount'] = $count;
// store the limit that was used
$json['limit'] = $limit;
// store the pagination offset that was used
$json['offset'] = $offset;
}
break;
default:
// the AJAX action request is unknown
$errorMessages[] = 'Action unknown';
break;
}
// send JSON
$json['errorMessages'] = $errorMessages;
$json['success'] = $success;
// make sure json is not cached
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
echo json_encode($json);
// script must end after echoing out JSON
exit();
}
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<title>Rules</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
var listLimit = 10;
var listOffset = 0;
var ruleList = null;
var ruleCount = 0;
$(document).ready(function () {
// save user form submission action
$('form#save_rule').submit(function () {
// make the save AJAX call
callJSON(
{
'action': $('input[name="action"]').val(),
'MetricName': $('input[name="MetricName"]').val(),
'oldType': $('input[name="oldType"]').val(),
'Type': $('select[name="Type"]').val(),
'ViolationStatus': $('select[name="ViolationStatus"]').val()
},
function() {
if( this.success ) {
// clear the form
clearForm();
getRuleList(listOffset);
}
}
);
return false;
});
// enable save user button now that page is loaded
$('input[name="save_rule"]').removeAttr('disabled');
// load list
getRuleList();
});
function clearForm() {
// clear the form
$('form#save_rule').not(':checkbox').each(function() { this.reset(); });
$('form#save_rule').find(':checkbox').each(function() { $(this).removeAttr('checked'); });
// set action to create
$('form#save_rule').find('input[name="action"]').val('createRule');
$('form#save_rule').removeClass('updating');
}
function deleteRule(ruleIndex) {
// get the type from the list
deleteRuleType = ruleList[ruleIndex].Type;
// make the delete AJAX call
callJSON(
{
'action': 'deleteRule',
'Type': deleteRuleType,
'MetricName': '<?php echo $metricName; ?>'
},
function() {
// reload the list
getRuleList(listOffset);
}
);
}
function editRule(ruleIndex) {
// clear the form
clearForm();
// set action to update
$('form#save_rule').find('input[name="action"]').val('updateRule');
// set the name in the form using the selected
$('form#save_rule').find('input[name="oldType"]').val(ruleList[ruleIndex].Type);
$('form#save_rule').find('select[name="Type"]').val(ruleList[ruleIndex].Type);
$('form#save_rule').find('select[name="ViolationStatus"]').val(ruleList[ruleIndex].Violation_Status);
$('form#save_rule').addClass('updating');
}
function getRuleList(newOffset) {
// if not specified then use the previous pagination offset, otherwise set the current to the specified offset
if( typeof(newOffset) == 'undefined' ) newOffset = listOffset;
else listOffset = newOffset;
// make the AJAX call to get the paginated list
callJSON(
{
'action': 'getRuleList',
'MetricName': '<?php echo $metricName; ?>',
'offset': listOffset,
'limit': listLimit
},
function () {
// if a list was returned then save results and show the list
if( this.ruleList ) {
ruleList = this.ruleList;
ruleCount = this.ruleCount;
}
else {
ruleList = null;
}
showRuleList();
}
);
}
function showRuleList() {
// if empty list then display empty list
if( !ruleList || ruleList.length == 0 ) {
$('#rule_list').html('No rules');
return;
}
// construct pagination controls if needed
controlsHTML = '<li>';
if( listOffset > 0 ) {
if( listOffset - listLimit < 0 ) controlsHTML += '<a href="" onclick="getMetricList(0); return false;">Previous</a>';
else controlsHTML += '<a href="" onclick="getRuleList(' + (listOffset - listLimit) + '); return false;">Previous</a>';
}
if( listOffset + listLimit < ruleCount ) controlsHTML += '<a href="" onclick="getRuleList(' + (listOffset + listLimit) + '); return false;">Next</a>';
controlsHTML += '</li>';
// construct the unordered list
listHTML = '<ul>';
listHTML += controlsHTML;
for(i = 0; i < ruleList.length; i++) {
listHTML += '<li class="' + (i%2 > 0 ? 'odd' : 'even') + '">';
listHTML += '<label>' + ruleList[i].Type_Description + '</label>';
listHTML += '<div style="float: right;">';
listHTML += '<a href="" onclick="editRule(' + i + '); return false;">Edit</a>';
listHTML += ' <a href="" onclick="deleteRule(' + i + '); return false;">Delete</a>';
listHTML += '</div>';
listHTML += '</li>';
}
listHTML += controlsHTML;
listHTML += '</ul>';
// show the new list
$('#rule_list').html(listHTML);
}
function callJSON(calldata, callback) {
$.getJSON(
'rules.php', calldata, function(response) {
if( response.errorMessages.length > 0 ) {
errMsg = '';
for(i = 0; i < response.errorMessages.length; i++) errMsg += response.errorMessages[i] + "\n";
alert(errMsg);
}
callback.call(response);
}
);
}
</script>
</head>
<body>
<?php include 'fragments/menu.php'; ?>
<div id="content">
<form method="post" id="save_rule" onsubmit="return false;">
<input type="hidden" name="action" value="createRule">
<input type="hidden" name="MetricName" value="<?php echo $metricName; ?>">
<input type="hidden" name="oldType" value="">
<table>
<tr>
<td>Metric Name: </td>
<td><?php echo $metricName; ?></td>
<td></td>
</tr>
<tr>
<td>Rule Type: </td>
<td>
<select name="Type">
<?php echo $rule->ruleTypeOptions(); ?>
</select>
</td>
<td> <a class="helptip" href="#"><img src="images/helptip.png"><span>Select a rule that you want applied to this chart.</span></a></td>
</tr>
<tr>
<td>Rule Violation Status: </td>
<td><select name="ViolationStatus">
<option value="HOLD">HOLD</option>
<option value="WARN">WARN</option>
</select></td>
<td> <a class="helptip" href="#"><img src="images/helptip.png"><span>Select the new status value to apply if this rule is violated.</span></a></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save Rule"> <input type="button" value="Cancel" onclick="clearForm(); return false;"></td>
<td></td>
</tr>
</table>
</form>
<div style="float:left; width: 30em; clear: left;" id="rule_list"></div>
</div>
<?php include 'fragments/footer.php'; ?>
</body>
</html>