-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommon.php
339 lines (302 loc) · 10.8 KB
/
common.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
<?php
class PostProcess_common
{
public $dir;
public function __construct($dir)
{
$this->dir = $dir;
}
public function description()
{
return array(
"name" => "common",
"description" => "Common functions for postprocessing",
"settings" => array()
);
}
public function process($processitem)
{
return false;
}
public function validate($processitem)
{
$settings = $this->description()["settings"];
foreach ($settings as $key=>$setting) {
if (!isset($processitem->{$key})) {
return array("success" => false, "message" => "setting ".$key." not set\n");
}
if ($setting["type"] == "feed" || $setting["type"] == "newfeed") {
$feedid = (int) $processitem->{$key};
if (!file_exists($this->dir.$feedid.".meta")) {
if (isset($setting["optional"]) && $setting["optional"]) {
continue;
}
return array("success" => false, "message" => "setting: $key, feed: $feedid.meta does not exist\n");
}
// check if feed is readable
if (!is_readable($this->dir.$feedid.".meta")) {
return array("success" => false, "message" => "setting: $key, feed: $feedid.meta is not readable\n");
}
// check if feed is writable
if ($setting["type"] == "newfeed" && !is_writable($this->dir.$feedid.".meta")) {
return array("success" => false, "message" => "setting: $key, feed: $feedid.meta is not writable\n");
}
}
if ($setting['type']=="value") {
$value = (float) 1*$processitem->$key;
if ($value!=$processitem->$key) {
return array("success"=>false,"message"=>"invalid value");
}
}
if ($setting['type']=="interval") {
// check if interval is valid
if (is_numeric($processitem->key)) {
$processitem->key = (int) $processitem->key;
}
if (!in_array($processitem->key, array('original',10,30,60,1800,3600))) {
return array("success"=>false,"message"=>"invalid interval");
}
}
if ($setting['type']=="timezone") {
if (!$datetimezone = new DateTimeZone($processitem->{$key})) {
return array("success"=>false,"message"=>"invalid timezone");
}
}
if ($setting['type']=="formula") {
}
}
return array("success" => true);
}
}
function updatetimevalue($id,$time,$value){
global $redis;
$redis->hMset("feed:$id", array('value' => $value, 'time' => $time));
}
function getmeta($dir, $id)
{
if (!file_exists($dir . $id . ".meta")) {
print "input file $id.meta does not exist\n";
return false;
}
$meta = new stdClass();
$metafile = fopen($dir . $id . ".meta", 'rb');
fseek($metafile, 8);
$tmp = unpack("I", fread($metafile, 4));
$meta->interval = $tmp[1];
$tmp = unpack("I", fread($metafile, 4));
$meta->start_time = $tmp[1];
fclose($metafile);
clearstatcache($dir . $id . ".dat");
$npoints = floor(filesize($dir . $id . ".dat") / 4.0);
$meta->npoints = $npoints;
$meta->end_time = $meta->start_time + ($meta->npoints * $meta->interval);
return $meta;
}
function createmeta($dir, $id, $meta)
{
$metafile = fopen($dir . $id . ".meta", 'wb');
fwrite($metafile, pack("I", 0));
fwrite($metafile, pack("I", 0));
fwrite($metafile, pack("I", $meta->interval));
fwrite($metafile, pack("I", $meta->start_time));
fclose($metafile);
}
//compute meta datas of different feeds intended for a preprocessing
function compute_meta()
{
$numargs = func_num_args();
$arg_list = func_get_args();
$all_intervals = [];
$all_start_times = [];
$all_ending_times = [];
$meta = new stdClass();
for ($i = 0; $i < $numargs; $i++) {
$all_intervals[] = $arg_list[$i]->interval;
$all_start_times[] = $arg_list[$i]->start_time;
$all_ending_times[] = $arg_list[$i]->start_time + $arg_list[$i]->npoints * $arg_list[$i]->interval;
}
$meta->interval = max($all_intervals);
$meta->start_time = floor(max($all_start_times) / $meta->interval) * $meta->interval;
$meta->writing_end_time = min($all_ending_times);
//print("intervals.....");print_r($all_intervals);
//print("start_times....");print_r($all_start_times);
//print("ending_times.....");print_r($all_ending_times);
print("NOTICE : output interval=$meta->interval, start=$meta->start_time, end=$meta->writing_end_time \n");
return $meta;
}
/*
format an array for the formula engine
$b is the result of a preg_match on a formula chunk with the regexp /($Xop)?($Xnbr)?($Xf)?/
$Xop : regexp for operator
$Xnbr : regexp for float or int value
$Xf : regexp for a feed
cf process basic_formula for more details on $Xop,$Xnbr,$Xf
ouputs a formula element as a 3 elements vector :
0 -> type of data ie "feed" ou "value"
1 -> operator
2 -> value or feed number
*/
function ftoa($b)
{
$c = [];
//print_r($b);
if (sizeof($b) == 4) {
$c[0] = "feed";
$c[2] = intval(substr($b[3], 1));
} else {
$c[0] = "value";
$c[2] = $b[2];
}
$c[1] = $b[1];
if (!$c[2]) $c[2] = 1;
if (!$c[1]) $c[1] = '+';
return $c;
}
/*
output of a basic formula for a specified time
the formula is described by the $elements array > a third dimensionnal array > datas are on level 2
for a given level 1, we operate multiplication/division within level 2 elements and give a sign to the result
the final result is the addition of the whole
a formula element is a 3 elements vector :
0 -> type of data ie "feed" ou "value"
1 -> operator
2 -> value or feed number
feeds_meta : array of metas such as produced by getmeta
*/
function bfo($elements, $feeds_meta, $feeds_dat, $time)
{
$s = [];
foreach ($elements as $element) {
$values = [];
foreach ($element as $e) {
$value = NAN;
if ($e[0] == "feed") {
$pos = floor(($time - $feeds_meta[$e[2]]->start_time) / $feeds_meta[$e[2]]->interval);
if ($pos >= 0 && $pos < $feeds_meta[$e[2]]->npoints) {
fseek($feeds_dat[$e[2]], $pos * 4);
$tmp = unpack("f", fread($feeds_dat[$e[2]], 4));
$value = $tmp[1];
}
}
if ($e[0] == "value") $value = $e[2];
if (!is_nan($value) && $value != 0) {
if ($e[1] == "/") $value = 1 / $value;
if ($e[1] == "-") $value = -$value;
}
$values[] = $value;
}
if (!in_array(NAN, $values)) {
$s[] = array_product($values);
} else $s[] = NAN;
}
if (!in_array(NAN, $s)) {
$sum = array_sum($s);
} else $sum = NAN;
return $sum;
}
class ModelHelper
{
private $dir;
private $params;
private $fh = array();
private $buffer = array();
public $meta = array();
public $value = array();
public $start_time = false;
public $end_time = false;
public function __construct($dir, $params)
{
$this->dir = $dir;
$this->params = $params;
}
private function load($key, $mode)
{
// check for valid key
if (!isset($this->params->$key)) return false;
$feedid = $this->params->$key;
// load meta
if (!$meta = getmeta($this->dir, $feedid)) return false;
$this->meta[$key] = $meta;
if (!$this->fh[$key] = @fopen($this->dir . $feedid . ".dat", $mode)) {
echo "ERROR: could not open " . $this->dir . $feedid . ".dat\n";
return false;
}
// Fetch last value
$this->value[$key] = 0.0;
if ($meta->npoints > 0) {
fseek($this->fh[$key], ($meta->npoints - 1) * 4);
$tmp = unpack("f", fread($this->fh[$key], 4));
if (!is_nan($tmp[1])) $this->value[$key] = 1 * $tmp[1];
}
return true;
}
public function input($key)
{
$result = $this->load($key, 'rb');
if ($this->start_time === false) $this->start_time = $this->meta[$key]->start_time;
else if ($this->meta[$key]->start_time > $this->start_time) $this->start_time = $this->meta[$key]->start_time;
if ($this->end_time === false) $this->end_time = $this->meta[$key]->end_time;
else if ($this->meta[$key]->end_time < $this->end_time) $this->end_time = $this->meta[$key]->end_time;
return $result;
}
public function output($key)
{
if (!$this->load($key, 'c+')) return false;
$this->buffer[$key] = "";
return true;
}
public function read($key, $value)
{
$tmp = unpack("f", fread($this->fh[$key], 4));
if ($tmp[1] != null && !is_nan($tmp[1])) $value = $tmp[1];
return $value;
}
public function seek_to_time($time)
{
foreach (array_keys($this->fh) as $key) {
$pos = floor(($time - $this->meta[$key]->start_time) / $this->meta[$key]->interval);
fseek($this->fh[$key], $pos * 4);
}
}
public function seek_to_time_inputs($time)
{
foreach (array_keys($this->fh) as $key) {
if (!isset($this->buffer[$key])) {
$pos = floor(($time - $this->meta[$key]->start_time) / $this->meta[$key]->interval);
fseek($this->fh[$key], $pos * 4);
}
}
}
public function write($key, $value)
{
$this->buffer[$key] .= pack("f", $value);
$this->value[$key] = $value;
}
public function set_output_meta($start_time, $interval)
{
foreach (array_keys($this->buffer) as $key) {
$this->meta[$key]->start_time = $start_time;
$this->meta[$key]->interval = $interval;
if ($this->meta[$key]->end_time == 0) $this->meta[$key]->end_time = $start_time;
}
}
public function save_all()
{
$total_size = 0;
foreach (array_keys($this->buffer) as $key) {
$size = strlen($this->buffer[$key]);
if ($size > 0) {
$feedid = $this->params->$key;
// Write meta data
createmeta($this->dir, $feedid, $this->meta[$key]);
// Write data
fwrite($this->fh[$key], $this->buffer[$key]);
fclose($this->fh[$key]);
$total_size += $size;
// Update feed last time and value
updatetimevalue($feedid, time(), $this->value[$key]);
}
}
return $total_size;
}
}