-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathPHPFina.php
516 lines (406 loc) · 16.1 KB
/
PHPFina.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
// This timeseries engine implements:
// Fixed Interval No Averaging
class PHPFina
{
private $dir = "/var/lib/phpfina/";
private $log;
/**
* Constructor.
*
* @api
*/
public function __construct($settings)
{
if (isset($settings['datadir'])) $this->dir = $settings['datadir'];
$this->log = new EmonLogger(__FILE__);
}
/**
* Create feed
*
* @param integer $id The id of the feed to be created
*/
public function create($id,$options)
{
$interval = (int) $options['interval'];
if ($interval<5) $interval = 5;
// Check to ensure we dont overwrite an existing feed
if (!$meta = $this->get_meta($id))
{
// Set initial feed meta data
$meta = new stdClass();
$meta->interval = $interval;
$meta->start_time = 0;
$meta->npoints = 0;
// Save meta data
$this->create_meta($id,$meta);
$fh = @fopen($this->dir.$id.".dat", 'c+');
if (!$fh) {
$this->log->warn("PHPFina:create could not create data file id=$id");
return false;
}
fclose($fh);
}
$feedname = "$id.meta";
if (file_exists($this->dir.$feedname)) {
return true;
} else {
$this->log->warn("PHPFina:create failed to create feed id=$id");
return false;
}
}
/**
* Adds a data point to the feed
*
* @param integer $id The id of the feed to add to
* @param integer $time The unix timestamp of the data point, in seconds
* @param float $value The value of the data point
*/
public function post($id,$timestamp,$value)
{
$this->log->info("PHPFina:post post id=$id timestamp=$timestamp value=$value");
$id = (int) $id;
$timestamp = (int) $timestamp;
$value = (float) $value;
$now = time();
$start = $now-(3600*24*365*5); // 5 years in past
$end = $now+(3600*48); // 48 hours in future
if ($timestamp<$start || $timestamp>$end) {
$this->log->warn("PHPFina:post timestamp out of range");
return false;
}
// If meta data file does not exist then exit
if (!$meta = $this->get_meta($id)) {
$this->log->warn("PHPFina:post failed to fetch meta id=$id");
return false;
}
// Calculate interval that this datapoint belongs too
$timestamp = floor($timestamp / $meta->interval) * $meta->interval;
// If this is a new feed (npoints == 0) then set the start time to the current datapoint
if ($meta->npoints == 0 && $meta->start_time==0) {
$meta->start_time = $timestamp;
$this->create_meta($id,$meta);
}
if ($timestamp < $meta->start_time) {
$this->log->warn("PHPFina:post timestamp older than feed start time id=$id");
return false; // in the past
}
// Calculate position in base data file of datapoint
$pos = floor(($timestamp - $meta->start_time) / $meta->interval);
$last_pos = $meta->npoints - 1;
// if ($pos<=$last_pos) {
// return false;
// }
$fh = fopen($this->dir.$id.".dat", 'c+');
if (!$fh) {
$this->log->warn("PHPFina:post could not open data file id=$id");
return false;
}
// Write padding
$padding = ($pos - $last_pos)-1;
if ($padding>0) {
if ($this->write_padding($fh,$meta->npoints,$padding)===false)
{
// Npadding returned false = max block size was exeeded
$this->log->warn("PHPFina:post padding max block size exeeded id=$id");
return false;
}
} else {
//$this->log->warn("PHPFINA padding less than 0 id=$id");
//return false;
}
// Write new datapoint
fseek($fh,4*$pos);
if (!is_nan($value)) fwrite($fh,pack("f",$value)); else fwrite($fh,pack("f",NAN));
// Close file
fclose($fh);
return $value;
}
/**
* Updates a data point in the feed
*
* @param integer $id The id of the feed to add to
* @param integer $time The unix timestamp of the data point, in seconds
* @param float $value The value of the data point
*/
public function update($id,$timestamp,$value)
{
return $this->post($id,$timestamp,$value);
}
/**
* Return the data for the given timerange
*
* @param integer $id The id of the feed to fetch from
* @param integer $start The unix timestamp in ms of the start of the data range
* @param integer $end The unix timestamp in ms of the end of the data range
* @param integer $dp The number of data points to return (used by some engines)
*/
public function get_data($id,$start,$end,$outinterval)
{
$id = intval($id);
$start = intval($start/1000);
$end = intval($end/1000);
$outinterval= (int) $outinterval;
// If meta data file does not exist then exit
if (!$meta = $this->get_meta($id)) return false;
if ($outinterval<$meta->interval) $outinterval = $meta->interval;
$dp = ceil(($end - $start) / $outinterval);
$end = $start + ($dp * $outinterval);
// $dpratio = $outinterval / $meta->interval;
if ($dp<1) return false;
// The number of datapoints in the query range:
$dp_in_range = ($end - $start) / $meta->interval;
// Divided by the number we need gives the number of datapoints to skip
// i.e if we want 1000 datapoints out of 100,000 then we need to get one
// datapoints every 100 datapoints.
$skipsize = round($dp_in_range / $dp);
if ($skipsize<1) $skipsize = 1;
// Calculate the starting datapoint position in the timestore file
if ($start>$meta->start_time){
$startpos = ceil(($start - $meta->start_time) / $meta->interval);
} else {
$start = ceil($meta->start_time / $outinterval) * $outinterval;
$startpos = ceil(($start - $meta->start_time) / $meta->interval);
}
$data = array();
$time = 0; $i = 0;
// The datapoints are selected within a loop that runs until we reach a
// datapoint that is beyond the end of our query range
$fh = fopen($this->dir.$id.".dat", 'rb');
while($time<=$end)
{
// $position steps forward by skipsize every loop
$pos = ($startpos + ($i * $skipsize));
// Exit the loop if the position is beyond the end of the file
if ($pos > $meta->npoints-1) break;
// read from the file
fseek($fh,$pos*4);
$val = unpack("f",fread($fh,4));
// calculate the datapoint time
$time = $meta->start_time + $pos * $meta->interval;
// add to the data array if its not a nan value
if (!is_nan($val[1])) $data[] = array($time*1000,$val[1]);
$i++;
}
return $data;
}
/**
* Get the last value from a feed
*
* @param integer $id The id of the feed
*/
public function lastvalue($id)
{
$id = (int) $id;
// If meta data file does not exist then exit
if (!$meta = $this->get_meta($id)) return false;
if ($meta->npoints>0)
{
$fh = fopen($this->dir.$id.".dat", 'rb');
$size = $meta->npoints*4;
fseek($fh,$size-4);
$d = fread($fh,4);
fclose($fh);
$val = unpack("f",$d);
$time = date("Y-n-j H:i:s", $meta->start_time + $meta->interval * $meta->npoints);
return array('time'=>$time, 'value'=>$val[1]);
}
else
{
return array('time'=>0, 'value'=>0);
}
}
public function export($id,$start)
{
$id = (int) $id;
$start = (int) $start;
$feedname = $id.".dat";
// If meta data file does not exist then exit
if (!$meta = $this->get_meta($id)) {
$this->log->warn("PHPFina:post failed to fetch meta id=$id");
return false;
}
// There is no need for the browser to cache the output
header("Cache-Control: no-cache, no-store, must-revalidate");
// Tell the browser to handle output as a csv file to be downloaded
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename={$feedname}");
header("Expires: 0");
header("Pragma: no-cache");
// Write to output stream
$fh = @fopen( 'php://output', 'w' );
$primary = fopen($this->dir.$feedname, 'rb');
$primarysize = filesize($this->dir.$feedname);
$localsize = $start;
$localsize = intval($localsize / 4) * 4;
if ($localsize<0) $localsize = 0;
// Get the first point which will be updated rather than appended
if ($localsize>=4) $localsize = $localsize - 4;
fseek($primary,$localsize);
$left_to_read = $primarysize - $localsize;
if ($left_to_read>0){
do
{
if ($left_to_read>8192) $readsize = 8192; else $readsize = $left_to_read;
$left_to_read -= $readsize;
$data = fread($primary,$readsize);
fwrite($fh,$data);
}
while ($left_to_read>0);
}
fclose($primary);
fclose($fh);
exit;
}
public function delete($id)
{
if (!$meta = $this->get_meta($id)) return false;
unlink($this->dir.$id.".meta");
unlink($this->dir.$id.".dat");
}
public function get_feed_size($id)
{
if (!$meta = $this->get_meta($id)) return false;
return (filesize($this->dir.$id.".meta") + filesize($this->dir.$id.".dat"));
}
public function get_meta($id)
{
$id = (int) $id;
$feedname = "$id.meta";
if (!file_exists($this->dir.$feedname)) {
$this->log->warn("PHPFina:get_meta meta file does not exist id=$id");
return false;
}
$meta = new stdClass();
$metafile = fopen($this->dir.$feedname, '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($this->dir.$id.".dat");
$filesize = filesize($this->dir.$id.".dat");
$meta->npoints = floor($filesize / 4.0);
if ($meta->start_time>0 && $meta->npoints==0) {
$this->log->warn("PHPFina:get_meta start_time already defined but npoints is 0");
return false;
}
return $meta;
}
private function create_meta($id,$meta)
{
$id = (int) $id;
$feedname = "$id.meta";
$metafile = fopen($this->dir.$feedname, 'wb');
if (!$metafile) {
$this->log->warn("PHPFina:create_meta could not open meta data file id=".$id);
return false;
}
if (!flock($metafile, LOCK_EX)) {
$this->log->warn("PHPFina:create_meta meta file id=".$id." is locked by another process");
fclose($metafile);
return false;
}
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);
}
private function write_padding($fh,$npoints,$npadding)
{
$tsdb_max_padding_block = 1024 * 1024;
// Padding amount too large
if ($npadding>$tsdb_max_padding_block*2) {
return false;
}
// Maximum points per block
$pointsperblock = $tsdb_max_padding_block / 4; // 262144
// If needed is less than max set to padding needed:
if ($npadding < $pointsperblock) $pointsperblock = $npadding;
// Fill padding buffer
$buf = '';
for ($n = 0; $n < $pointsperblock; $n++) {
$buf .= pack("f",NAN);
}
fseek($fh,4*$npoints);
do {
if ($npadding < $pointsperblock)
{
$pointsperblock = $npadding;
$buf = '';
for ($n = 0; $n < $pointsperblock; $n++) {
$buf .= pack("f",NAN);
}
}
fwrite($fh, $buf);
$npadding -= $pointsperblock;
} while ($npadding);
}
public function csv_export($id,$start,$end,$outinterval)
{
global $csv_decimal_places;
global $csv_decimal_place_separator;
global $csv_field_separator;
$id = intval($id);
$start = intval($start);
$end = intval($end);
$outinterval= (int) $outinterval;
// If meta data file does not exist then exit
if (!$meta = $this->get_meta($id)) return false;
if ($outinterval<$meta->interval) $outinterval = $meta->interval;
$dp = ceil(($end - $start) / $outinterval);
$end = $start + ($dp * $outinterval);
// $dpratio = $outinterval / $meta->interval;
if ($dp<1) return false;
// The number of datapoints in the query range:
$dp_in_range = ($end - $start) / $meta->interval;
// Divided by the number we need gives the number of datapoints to skip
// i.e if we want 1000 datapoints out of 100,000 then we need to get one
// datapoints every 100 datapoints.
$skipsize = round($dp_in_range / $dp);
if ($skipsize<1) $skipsize = 1;
// Calculate the starting datapoint position in the timestore file
if ($start>$meta->start_time){
$startpos = ceil(($start - $meta->start_time) / $meta->interval);
} else {
$start = ceil($meta->start_time / $outinterval) * $outinterval;
$startpos = ceil(($start - $meta->start_time) / $meta->interval);
}
$data = array();
$time = 0; $i = 0;
// There is no need for the browser to cache the output
header("Cache-Control: no-cache, no-store, must-revalidate");
// Tell the browser to handle output as a csv file to be downloaded
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
$filename = $id.".csv";
header("Content-Disposition: attachment; filename={$filename}");
header("Expires: 0");
header("Pragma: no-cache");
// Write to output stream
$exportfh = @fopen( 'php://output', 'w' );
// The datapoints are selected within a loop that runs until we reach a
// datapoint that is beyond the end of our query range
$fh = fopen($this->dir.$id.".dat", 'rb');
while($time<=$end)
{
// $position steps forward by skipsize every loop
$pos = ($startpos + ($i * $skipsize));
// Exit the loop if the position is beyond the end of the file
if ($pos > $meta->npoints-1) break;
// read from the file
fseek($fh,$pos*4);
$val = unpack("f",fread($fh,4));
// calculate the datapoint time
$time = $meta->start_time + $pos * $meta->interval;
// add to the data array if its not a nan value
if (!is_nan($val[1])) fwrite($exportfh, $time.$csv_field_separator.number_format($val[1],$csv_decimal_places,$csv_decimal_place_separator,'')."\n");
$i++;
}
fclose($exportfh);
exit;
}
}