-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathMysqlTimeSeries.php
913 lines (817 loc) · 32 KB
/
MysqlTimeSeries.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
<?php
include_once dirname(__FILE__) . '/shared_helper.php';
class MysqlTimeSeries implements engine_methods
{
protected $dir = '';
protected $generic = true;
protected $prefix = "feed_";
protected $mysqli;
protected $redis;
protected $log;
private $writebuffer = array();
/**
* Constructor.
*
* @api
*/
public function __construct($mysqli, $redis=false, $settings=array()) {
$this->log = new EmonLogger(__FILE__);
if (isset($settings['datadir'])) {
$this->dir = $settings['datadir'];
}
if (isset($settings['database'])) {
$database = $settings['database'];
if (isset($settings['server']) && isset($settings['port'])) {
$server = $settings['server'];
$port = $settings['port'];
}
else {
global $server, $port;
}
if (isset($settings['username']) && isset($settings['password'])) {
$username = $settings['username'];
$password = $settings['password'];
}
else {
global $username, $password;
}
$this->mysqli = @new mysqli($server,$username,$password,$database,$port);
if ($this->mysqli->connect_error) {
$this->log->error("Can't connect to database:". $mysqli->connect_error);
$this->mysqli = $mysqli;
}
}
else {
$this->mysqli = $mysqli;
}
$this->redis = $redis;
if (isset($settings['prefix'])) {
$this->prefix = $settings['prefix'];
}
if (isset($settings['generic'])) {
$this->generic = $settings['generic'];
}
}
// #### \/ Below are required methods
/**
* Create feed
*
* @param integer $feedid The id of the feed to be created
* @param array $options for the engine
*/
public function create($feedid, $options)
{
$result = $this->create_meta($feedid, $options);
if (is_string($result)) {
return $result;
}
$table = $this->get_table(intval($feedid));
$name = $table['name'];
$type = $table['type'];
$this->mysqli->query("CREATE TABLE $name (time INT UNSIGNED NOT NULL, data $type, UNIQUE (time)) ENGINE=MYISAM");
return true;
}
/**
* Delete feed
*
* @param integer $feedid The id of the feed to be created
*/
public function delete($feedid)
{
$meta = $this->dir."$feedid.meta";
if (file_exists($meta)) {
unlink($meta);
}
$table = $this->get_table_name(intval($feedid));
$this->mysqli->query("DROP TABLE $table");
}
/**
* Gets engine metadata
*
* @param integer $feedid The id of the feed to be created
*/
public function get_meta($feedid)
{
$feedid = (int) $feedid;
if (!$this->generic) {
$meta = $this->read_meta($feedid);
}
else {
$meta = new stdClass();
$meta->id = $feedid;
$meta->table_name = ($this->prefix ?: "") . trim($feedid);
$meta->value_type = "FLOAT NOT NULL";
$meta->start_time = 0;
$meta->end_time = 0;
}
$result = $this->mysqli->query("SELECT COUNT(*) FROM ".$meta->table_name);
if ($result && $row = $result->fetch_array()) {
$meta->npoints = (int) $row[0];
} else {
$meta->npoints = -1;
}
if ($meta->start_time == 0) {
$table = $this->get_table_name($feedid);
// Get first and last datapoint of feed
$sql = "SELECT DISTINCT time, data FROM $table WHERE ("
." time = (SELECT min(time) FROM $table )"
."OR time = (SELECT max(time) FROM $table )"
.")";
if ($result = $this->mysqli->query($sql)) {
$range = $result->fetch_all(MYSQLI_ASSOC);
if (isset($range[0])) {
$meta->start_time = (int) $range[0]['time'];
}
if (isset($range[1])) {
$meta->end_time = (int) $range[1]['time'];
}
$meta->interval = 0;
if ($meta->npoints>0) {
$meta->interval = floor(($meta->end_time - $meta->start_time) / $meta->npoints);
}
if (!$this->generic) {
$this->write_meta($feedid, $meta);
}
}
}
return $meta;
}
/**
* Returns engine occupied size in bytes
*
* @param integer $feedid The id of the feed to be created
*/
public function get_feed_size($feedid)
{
$table = $this->get_table_name(intval($feedid));
$result = $this->mysqli->query("SHOW TABLE STATUS LIKE '$table'");
$row = $result->fetch_array();
return $row['Data_length']+$row['Index_length'];
}
/**
* Adds a data point to the feed
*
* @param integer $feedid 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
* @param array $padding_mode optional padding mode argument
* $feedname, $time and $value are all typecased in feed->insert and feed->update
*/
public function post($feedid, $time, $value, $padding_mode=null)
{
$time = intval($time);
$value = floatval($value);
$table = $this->get_table_name(intval($feedid));
$this->mysqli->query("INSERT INTO $table (time,data) VALUES ('$time','$value') ON DUPLICATE KEY UPDATE data=VALUES(data)");
}
/**
* Get array with last time and value from a feed
*
* @param integer $feedid The id of the feed
*/
public function lastvalue($feedid)
{
$table = $this->get_table_name(intval($feedid));
$result = $this->mysqli->query("SELECT time, data FROM $table ORDER BY time Desc LIMIT 1");
if ($result && $row = $result->fetch_array()) {
if ($row['data'] !== null) $row['data'] = (float) $row['data'];
return array('time'=>(int)$row['time'], 'value'=>$row['data']);
} else {
return false;
}
}
/**
* @param integer $feedid 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 $interval output data point interval
* @param integer $average enabled/disable averaging
* @param string $timezone a name for a php timezone eg. "Europe/London"
* @param string $timeformat csv datetime format e.g: unix timestamp, excel, iso8601 (NOT CURRENTLY SUPPORTED IN MYSQL)
* @param integer $csv pipe output as csv (NOT CURRENTLY SUPPORTED IN MYSQL)
* @param integer $skipmissing skip null datapoints
* @param integer $limitinterval limit interval to feed interval
* @return void or array
*/
public function get_data_combined($feedid,$start,$end,$interval,$average=0,$timezone="UTC",$timeformat="unix",$csv=false,$skipmissing=0,$limitinterval=1)
{
if (in_array($interval,array("daily","weekly","monthly","annual"))) {
return $this->get_data_DMY($feedid, $start, $end, $interval, $average, $timezone, $timeformat, $csv, $skipmissing);
} else {
if (!$average) {
return $this->get_data($feedid, $start, $end, $interval, $timezone, $timeformat, $csv, $skipmissing, $limitinterval);
} else {
return $this->get_average($feedid, $start, $end, $interval, $timezone, $timeformat, $csv, $skipmissing);
}
}
}
/**
* Return the data for the given timerange - cf shared_helper.php
*
* @param integer $limitinterval not implemented
*
*/
public function get_data($feedid, $start, $end, $interval, $timezone, $timeformat, $csv, $skipmissing, $limitinterval)
{
$feedid = (int) $feedid;
$start = (int) $start;
$end = (int) $end;
$interval = (int) $interval;
$skipmissing = (int) $skipmissing;
// Interval should not be less than 1 second
if ($interval < 1) $interval = 1;
// Set time to start
$time = $start;
$table = $this->get_table_name($feedid);
$stmt = $this->mysqli->prepare("SELECT time, data FROM $table WHERE time BETWEEN ? AND ? ORDER BY time ASC LIMIT 1");
$t = $start; $tb = 0;
$stmt->bind_param("ii", $div_start, $div_end);
$stmt->bind_result($data_time, $data_value);
if ($csv) {
global $settings;
require_once "Modules/feed/engine/shared_helper.php";
$helperclass = new SharedHelper($settings['feed']);
$helperclass->set_time_format($timezone,$timeformat);
$helperclass->csv_header($feedid);
} else {
$data = array();
}
while($time<=$end)
{
// Start time of interval/division
$div_start = $time;
$timestamp = $div_start;
// calculate start of next interval
$div_end = $time + $interval;
$value = null;
$stmt->execute();
if ($stmt->fetch() && $data_value !== null) {
if ($limitinterval==2) {
$timestamp = $data_time;
}
$value = (float) $data_value;
}
if ($value!==null || $skipmissing===0) {
// Write as csv or array
if ($csv) {
$helperclass->csv_write($timestamp,$value);
} else {
$data[] = array($timestamp,$value);
}
}
// Advance position
$time = $div_end;
}
if ($csv) {
$helperclass->csv_close();
exit;
} else {
return $data;
}
}
/**
* Return the averaged data over interval for the given timerange. The returned timestamp denotes the intervals start time. Averaging is performed over all values from time to time+interval.
*
* @param integer $feedid 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 $interval The number os seconds for each data point to return (used by some engines)
*/
public function get_average($feedid, $start, $end, $interval, $timezone, $timeformat, $csv, $skipmissing)
{
$feedid = (int) $feedid;
$start = (int) $start;
$end = (int) $end;
$interval= (int) $interval;
$skipmissing = (int) $skipmissing;
// Minimum interval
if ($interval < 1) $interval = 1;
$table = $this->get_table_name($feedid);
// 1. Create associative array of time => values
$data_assoc = array();
$sql = "SELECT time, AVG(data) AS data_avg FROM $table WHERE time >= $start AND time < $end GROUP BY FLOOR(time/$interval)";
$result = $this->mysqli->query($sql);
if ($result) {
while($row = $result->fetch_array()) {
$time = floor((int)$row['time']/$interval)*$interval;
$data_assoc[$time] = (float) $row['data_avg'];
}
}
// 2. Assing values to correct output format
// returns null if output does not exist for that timestamp
// allowing for easier cross feed comparison e.g in csv view
if ($csv) {
global $settings;
require_once "Modules/feed/engine/shared_helper.php";
$helperclass = new SharedHelper($settings['feed']);
$helperclass->set_time_format($timezone,$timeformat);
$helperclass->csv_header($feedid);
} else {
$data = array();
}
$time = $start;
while($time<=$end)
{
$value = null;
if (isset($data_assoc[$time])) {
$value = $data_assoc[$time];
}
// Write as csv or array
if ($value!==null || $skipmissing===0) {
if ($csv) {
$helperclass->csv_write($time,$value);
} else {
$data[] = array($time,$value);
}
}
$time += $interval;
}
if ($csv) {
$helperclass->csv_close();
exit;
} else {
return $data;
}
}
/**
* Return datapoints for intervals in the given timerange.
*
* @param integer $feedid 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 string $interval The name of the interval. Possible values are: daily, weekly, monthly, annual
* @param string $timezone The time zone to which the intervals refer
*/
public function get_data_DMY($feedid, $start, $end, $interval, $average, $timezone, $timeformat, $csv, $skipmissing)
{
if (!in_array($interval,array("daily","weekly","monthly","annual"))) return false;
$feedid = (int) $feedid;
$start = (int) $start;
$end = (int) $end;
$average = (int) $average;
$skipmissing = (int) $skipmissing;
$table = $this->get_table_name($feedid);
$meta = $this->get_meta($feedid);
if (!$start_time = $meta->start_time) return false;
if (!$end_time = $meta->end_time) return false;
if ($timezone===0) $timezone = "UTC";
$date = new DateTime();
$date->setTimezone(new DateTimeZone($timezone));
$date->setTimestamp($start);
$date->modify("midnight");
$modify = "+1 day";
if ($interval=="weekly") {
$date->modify("this monday");
$modify = "+1 week";
} elseif ($interval=="monthly") {
$date->modify("first day of this month");
$modify = "+1 month";
} elseif ($interval=="annual") {
$date->modify("first day of january this year");
$modify = "+1 year";
}
// Set time to start
$time = $date->getTimestamp();
if ($csv) {
global $settings;
require_once "Modules/feed/engine/shared_helper.php";
$helperclass = new SharedHelper($settings['feed']);
$helperclass->set_time_format($timezone,$timeformat);
$helperclass->csv_header($feedid);
} else {
$data = array();
}
while($time<=$end)
{
// Start time of interval/division
$div_start = $time;
// calculate start of next interval
$date->modify($modify);
$div_end = $date->getTimestamp();
$value = null;
if ($average) {
$sql = "SELECT AVG(data) AS dp FROM $table WHERE time >= $div_start AND time < $div_end";
if ($result = $this->mysqli->query($sql)) {
if ($dp = $result->fetch_array()) {
if ($dp['dp'] !== null) $value = (float) $dp['dp'];
}
}
} else {
// Limit DB requests to available datapoints in feed
if ($start_time < $time && $time < $end_time) {
// get datapoint using interpolation if necessary
$dp = $this->get_datapoint_interpolated($feedid, $time);
$value = $dp[1];
}
}
// Write as csv or array
if ($value!==null || $skipmissing===0) {
if ($csv) {
$helperclass->csv_write($div_start,$value);
} else {
$data[] = array($div_start,$value);
}
}
// Advance position
$time = $div_end;
}
if ($csv) {
$helperclass->csv_close();
exit;
} else {
return $data;
}
}
public function get_data_DMY_time_of_day($feedid, $start, $end, $mode, $timezone, $split)
{
if (!in_array($mode,array("daily","weekly","monthly","annual"))) return false;
$feedid = (int) $feedid;
$start = (int) $start;
$end = (int) $end;
$table = $this->get_table_name($feedid);
$data = array();
$split = json_decode($split);
if (gettype($split) != "array") return false;
/* SP Increase to 48 points to allow a days worth of half hour readings */
if (count($split) > 48) return false;
// Set interval based on timezone
$date = new DateTime();
if ($timezone === 0) $timezone = "UTC";
$date->setTimezone(new DateTimeZone($timezone));
$date->setTimestamp($start);
$date->modify("midnight");
$increment="+1 day";
if ($mode=="weekly") { $date->modify("this monday"); $increment="+1 week"; }
if ($mode=="monthly") { $date->modify("first day of this month"); $increment="+1 month"; }
if ($mode=="annual") { $date->modify("first day of January this year"); $increment="+1 year"; }
// Get first and last datapoint of feed
$sql = "SELECT DISTINCT time, data FROM $table WHERE ("
." time = (SELECT min(time) FROM $table )"
."OR time = (SELECT max(time) FROM $table )"
.")";
$result = $this->mysqli->query($sql);
if($result) {
$range = $result->fetch_all(MYSQLI_ASSOC);
if (count($range) < 2) {
return array('success'=>false, 'message'=>"Feed $feedid does not contain enough datapoints yet");
}
} else {
return false;
}
// Iterate intervals
$n = 0;
while($n < 10000) // max iterations
{
$time = $date->getTimestamp();
if ($time > $end) break;
$split_values = array();
foreach ($split as $splitpoint) {
//Fix issue with rounding to nearest 30 minutes
$split_offset = (int) (((float)$splitpoint) * 3600.0);
$split_time = $time+$split_offset;
// Limit DB requests to available datapoints in feed
if ($range[0]['time'] < $time && $time < $range[1]['time']) {
// get datapoint using interpolation if necessary
$result = $this->get_datapoint_interpolated($feedid, $split_time);
$value = $result[1];
}
elseif($time >= $range[1]['time']) {
// return latest feed value
$value = (float) $range[1]['data'];
break;
}
else {
$value = null;
}
$split_values[] = $value;
}
$data[] = array($time, $split_values);
$date->modify($increment);
$n++;
}
return $data;
}
public function export($feedid, $start)
{
// Feed id and start time of feed to export
$feedid = intval($feedid);
$start = intval($start) - 1;
// Open database etc here
// Extend timeout limit from 30s to 2mins
set_time_limit (120);
// Regulate mysql and apache load.
$block_size = 400;
$sleep = 80000;
$table = $this->get_table_name($feedid);
$file = $table.'.csv';
// 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: text/csv");
header("Content-Disposition: attachment; filename={$file}");
header("Expires: 0");
header("Pragma: no-cache");
// Write to output stream
$fh = @fopen( 'php://output', 'w' );
// Load new feed blocks until there is no more data
$data_available = 1;
while ($data_available) {
// 1) Load a block
$result = $this->mysqli->query("SELECT * FROM $table WHERE time>$start
ORDER BY time Asc Limit $block_size");
$data_available = 0;
while($row = $result->fetch_array())
{
// Write block as csv to output stream
if (!isset($row['data2'])) {
fputcsv($fh, array($row['time'],$row['data']));
}
else {
fputcsv($fh, array($row['time'],$row['data'],$row['data2']));
}
// Set new start time so that we read the next block along
$start = $row['time'];
$data_available = 1;
}
// 2) Sleep for a bit
usleep($sleep);
}
fclose($fh);
exit;
}
public function clear($feedid) {
$feedid = filter_var($feedid, FILTER_SANITIZE_NUMBER_INT);
$table = $this->get_table_name($feedid);
$sql = "TRUNCATE TABLE $table";
if (!$this->mysqli->query($sql)) {
return array('success'=>false,'message'=>"0 rows deleted");
} else {
return array('success'=>true,'message'=>"All database rows deleted");
}
}
public function trim($feedid, $start){
$feedid = filter_var ($feedid, FILTER_SANITIZE_NUMBER_INT);
$start = filter_var ($start, FILTER_SANITIZE_NUMBER_INT);
$table = $this->get_table_name($feedid);
$stmt = $this->mysqli->prepare("DELETE FROM $table WHERE time < ?");
if (!$stmt) return array('success'=>false,'message'=>"Error accessing database");
if (!$stmt->bind_param("i", $start)) return array('success'=>false,'message'=>"Error passing parameters to database");
if (!$stmt->execute()) return array('success'=>false,'message'=>"Error executing commands on database");
$affected_rows = $stmt->affected_rows;
if ($affected_rows > 0) {
return array('success'=>true,'message'=>"$affected_rows rows deleted");
} else {
return array('success'=>false,'message'=>"0 rows deleted");
}
}
// #### /\ Above are required methods
// #### \/ Below are buffer write methods
// Insert data in post buffer
public function post_bulk_prepare($feedid,$time,$value,$padding_mode=null)
{
$this->writebuffer[(int)$feedid][] = array((int)$time,$value);
//$this->log->info("post_bulk_prepare() $feedid, $time, $value, $padding_mode");
}
// Saves post buffer to mysql feed_table, performing bulk inserts instead of an insert for each point
public function post_bulk_save()
{
$stepcnt = 1048576/30; // Data points to save in each insert command limit is max_allowed_packet = 1Mb default ~20-30bytes are used for each data point
foreach ($this->writebuffer as $feedid=>$data) {
$table = $this->get_table_name($feedid);
$cnt = count($data);
if ($cnt > 0) {
$p = 0; // point
while ($p < $cnt) {
$sql_values="";
$s=0; // data point step
while ($s < $stepcnt) {
if (isset($data[$p][0]) && isset($data[$p][1])) {
$sql_values .= "(".$data[$p][0].",".$data[$p][1]."),";
}
$s++;
$p++;
if ($p >= $cnt) break;
}
if ($sql_values!="") {
$this->log->info("post_bulk_save() " . "INSERT INTO $table (`time`,`data`) VALUES " . substr($sql_values,0,-1) . " ON DUPLICATE KEY UPDATE data=VALUES(data)");
$this->mysqli->query("INSERT INTO $table (`time`,`data`) VALUES " . substr($sql_values,0,-1) . " ON DUPLICATE KEY UPDATE data=VALUES(data)");
}
}
}
}
$this->writebuffer = array(); // clear buffer
}
// #### \/ Below engine specific public methods
public function delete_data_point($feedid,$time)
{
$feedid = intval($feedid);
$time = intval($time);
$table = $this->get_table_name($feedid);
$this->mysqli->query("DELETE FROM $table where `time` = '$time' LIMIT 1");
}
public function delete_data_range($feedid,$start,$end)
{
$feedid = intval($feedid);
$start = (int) $start;
$end = (int) $end;
$table = $this->get_table_name($feedid);
$this->mysqli->query("DELETE FROM $table where `time` >= '$start' AND `time`<= '$end'");
return true;
}
public function get_table_name($feedid)
{
$feedid = intval($feedid);
$name = "";
if ($this->prefix) {
$name .= $this->prefix;
}
if ($this->generic) {
$name .= "".trim($feedid);
}
else {
$name .= $this->get_table($feedid, "name");
}
return $name;
}
public function get_table($feedid, $field=null)
{
if ($this->generic) {
$table = array(
"name" => $this->get_table_name($feedid),
"type" => "FLOAT NOT NULL"
);
} elseif ($this->redis && $this->redis->exists("feed:$feedid:table")) {
if (!empty($field)) {
return $this->redis->hget("feed:$feedid:table", $field);
} else {
return $this->redis->get("feed:$feedid:table");
}
}
else {
$meta = $this->get_meta($feedid);
$name = $meta->table_name;
$type = $meta->value_type;
if (!$meta->value_empty) {
$type .= " NOT NULL";
}
$table = array(
"name" => $name,
"type" => $type
);
if ($this->redis) {
$this->redis->hMSet("feed:$feedid:table", $table);
}
}
if (!empty($field)) {
return $table[$field];
}
return $table;
}
// #### \/ Bellow are engine private methods
private function create_meta($feedid, $options)
{
// Check to ensure ne existing feed will be overridden
if (empty($this->dir) || !is_dir($this->dir) || !is_writable($this->dir)) {
return true;
} elseif (file_exists($this->dir.$feedid.".meta")) {
$result = "Unable to create MySQL already existing meta file '".$this->dir.$feedid.".meta'";
$this->log->error($result);
return $result;
}
if ($this->generic) {
$name = ($this->prefix ?: "") . trim($feedid);
$type = "FLOAT";
$empty = false;
} else {
$name = "";
if ($this->prefix) {
$name .= $this->prefix;
}
if (empty($options["name"])) {
$name .= "" . trim($feedid);
}
else {
$name .= preg_replace('/[^\p{N}\p{L}\_]/u', '_', $options['name']);
}
$type = !empty($options['type']) ? $options['type'] : "FLOAT";
$empty = isset($options['empty']) && boolval($options['empty']);
}
// Set initial feed meta data
$meta = new stdClass();
$meta->table_name = $name;
$meta->value_type = $type;
$meta->value_empty = $empty;
$meta->start_time = 0;
// Save meta data
$result = $this->write_meta($feedid, $meta);
if ($result !== true) {
return $result;
}
if (!file_exists($this->dir.$feedid.".meta")) {
$this->log->error("Creating MySQL meta data failed. Unable to find file '".$this->dir.$feedid.".meta'");
return $result;
}
return $meta;
}
private function read_meta($feedid)
{
$file = "$feedid.meta";
if (!file_exists($this->dir.$file)) {
$error = "meta file does not exist '".$this->dir.$file."'";
$this->log->warn("read_meta() ".$error);
throw new Exception($error);
}
$meta_file = parse_ini_file($this->dir.$file, false, INI_SCANNER_TYPED);
$meta = new stdClass();
foreach ($meta_file as $key => $value) {
$meta->$key = $value;
}
return $meta;
}
private function write_meta($feedid, $meta)
{
$file = $feedid.".meta";
if (!is_dir($this->dir) || !is_writable($this->dir) ||
(is_file($this->dir.$file) and !is_writable($this->dir.$file))) {
$result = "unable to write meta data file: ".$this->dir.$file;
$this->log->error("write_meta() ".$result);
return $result;
}
$meta_file = @fopen($this->dir.$file, 'w');
if (!$meta_file) {
$error = error_get_last();
$result = "could not write meta data file ".$error['message'];
$this->log->error("write_meta() ".$result);
return $result;
}
if (!flock($meta_file, LOCK_EX)) {
$result = "meta data file '".$this->dir.$file."' is locked by another process";
$this->log->error("write_meta() ".$result);
fclose($meta_file);
return $result;
}
foreach ($meta as $key => $value) {
if (is_bool($value)) $value = $value ? 'true' : false;
fwrite($meta_file, $key.'='.$value.PHP_EOL);
}
fclose($meta_file);
return true;
}
/**
* Return datapoint for requested timestamp. If feed does not contain a datapoint for requested timestamp, the value is calculated using linear interpolation.
*
* @param integer $feedid The id of the feed to fetch from
* @param integer $time The unix timestamp in ms of the requested datapoint
*/
private function get_datapoint_interpolated($feedid, $time)
{
$feedid = (int) $feedid;
$time = (int) $time;
$table = $this->get_table_name($feedid);
$data = array();
// Search for previous and next datapoint
$sql = "SELECT time, data FROM $table WHERE ("
." time = IFNULL( (SELECT max(time) FROM $table where time <= $time), 0) "
."OR time = IFNULL( (SELECT min(time) FROM $table where time > $time), 0) "
.")";
$result = $this->mysqli->query($sql);
if($result) {
$dp = $result->fetch_all(MYSQLI_ASSOC);
if (count($dp) == 2) {
if ($dp[0]['time'] == $time) {
// Datapoint to given timestamp found
$data = array($time , (float) $dp[0]['data']);
}
else {
// No datapoint to given timestamp found. Datapoint will be interpolated
$delta_t = $dp[1]['time'] - $dp[0]['time'];
$delta_data = $dp[1]['data'] - $dp[0]['data'];
if ($delta_t != 0){
// Linear interpolation
$value = $dp[0]['data'] + ($delta_data / $delta_t) * ($time - $dp[0]['time']);
$data = array($time , (float) $value);
}
}
}
else {
// only one datapoint found, interpolation not possible.
$data = array($time , null);
}
}
return $data;
}
/**
* Used for testing
*
*/
public function print_all($id) {
$table = $this->get_table_name($id);
$result = $this->mysqli->query("SELECT time, data FROM $table");
$n = 0;
while($row = $result->fetch_object()) {
print $n." ".$row->time." ".$row->data."\n";
$n++;
}
}
}