-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathphptimestore_to_phpfina.php
211 lines (177 loc) · 7.82 KB
/
phptimestore_to_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
<?php
/*
PHPTIMESTORE/TIMESTORE to PHPFINA conversion script
How it works:
PHPTIMESTORE stores its bottom layer in the same format as PHPFina.
This script copies the bottom layer and reads the start_time and
interval from the PHPTIMESTORE meta file in order to create the phpfina feed.
Script last checked: 18th of June 2016
*/
if (posix_geteuid()!=0) {
print "Please run this script with sudo\n";
die;
}
print "---------------------------------------------------\n";
print "PHPTIMESTORE/TIMESTORE to PHPFINA conversion script\n";
print "---------------------------------------------------\n";
define('EMONCMS_EXEC', 1);
// Select emoncms directory
$setup = stdin("Is your setup a standard emonpi or emonbase? (y/n): ");
if ($setup=="y" || $setup=="Y") {
$emoncmsdir = "/var/www/emoncms";
} else {
$emoncmsdir = stdin("Please enter root directory of your emoncms installation (i.e /var/www/emoncms): ");
}
chdir($emoncmsdir);
if (!file_exists("process_settings.php")) {
echo "ERROR: This is not a valid emoncms directory, please retry\n"; die;
}
require "process_settings.php";
$mysqli = @new mysqli(
$settings["sql"]["server"],
$settings["sql"]["username"],
$settings["sql"]["password"],
$settings["sql"]["database"],
$settings["sql"]["port"]
);
if ( $mysqli->connect_error ) {
echo "Can't connect to database, please verify credentials/configuration in settings.php<br />";
if ( $display_errors ) {
echo "Error message: <b>" . $mysqli->connect_error . "</b>";
}
die();
}
if ($settings['redis']['enabled']) {
$redis = new Redis();
// older emoncms versions do not have redis server name and port defined, use defaults
if (empty($settings['redis']['host'])) $settings['redis']['host'] = "localhost";
if (empty($settings['redis']['port'])) $settings['redis']['port'] = "6379";
$connected = $redis->connect($settings['redis']['host'], $settings['redis']['port']);
if (!$connected) { echo "Can't connect to redis at ".$settings['redis']['host'].":".$settings['redis']['port']." , it may be that redis-server is not installed or started see readme for redis installation"; die; }
if (!empty($settings['redis']['prefix'])) $redis->setOption(Redis::OPT_PREFIX, $settings['redis']['prefix']);
if (!empty($settings['redis']['auth'])) {
if (!$redis->auth($settings['redis']['auth'])) {
echo "Can't connect to redis at ".$settings['redis']['host'].", autentication failed"; die;
}
}
if (!empty($settings['redis']['dbnum'])) {
$redis->select($settings['redis']['dbnum']);
}
} else {
$redis = false;
}
// Either use default phpfina data directories
// or use user specified directory from emoncms/settings.php
$sourcedir = "/var/lib/timestore/";
if (isset($settings["feed"]["timestore"]["datadir"])) $sourcedir = $settings["feed"]["timestore"]["datadir"];
$targetdir = "/var/lib/phpfina/";
if (isset($settings["feed"]["phpfina"])) $targetedir = $settings["feed"]["phpfina"]["datadir"];
// Find all timestore and PHPTimestore feeds
$result = $mysqli->query("SELECT * FROM feeds WHERE `engine`=1 OR `engine`=4");
// Quick check at this point so that conversion can be aborted
if ($result->num_rows>0) {
print "There are ".$result->num_rows." feeds to convert, would you like to continue? (y/n): ";
} else {
print "There are no feeds to convert\n";
die;
}
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'y') exit;
$phptimestorefeeds = array();
// For each PHPFINA feed
while($row = $result->fetch_array())
{
print $row['id']." ".$row['name']."\n";
$id = $row['id'];
$sourcefile = $sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_0_.dat";
$new_or_overwrite = (int) stdin("- Create a new feed or replace? (enter 1:new, 2:replace) ");
if ($new_or_overwrite==1) {
$userid = $row['userid'];
$new_feed = $row['name']." (new)";
$datatype = DataType::REALTIME;
$setengine = Engine::PHPFINA;
$mysqli->query("INSERT INTO feeds (userid,name,datatype,public,engine) VALUES ('$userid','$new_feed','$datatype',false,'$setengine')");
$outid = $mysqli->insert_id;
if ($redis) {
$redis->sAdd("user:feeds:$userid", $outid);
$redis->hMSet("feed:$outid",array('id'=>$outid,'userid'=>$userid,'name'=>$new_feed,'datatype'=>$datatype,'tag'=>"",'public'=>false,'size'=>0,'engine'=>$setengine));
}
} else {
$outid = $id;
}
$targetfile = $targetdir.$outid.".dat";
//-----------------------------------
// META FILE COPY
//-----------------------------------
// 1. Read PHPTimestore meta file
$meta = new stdClass();
if (!$metafile = @fopen($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT).".tsdb", 'rb')) {
print "- error opening phptimestore meta file to read\n";
die;
}
fseek($metafile,(8+8+4+4));
$tmp = unpack("I",fread($metafile,8));
$meta->start_time = $tmp[1];
$tmp = unpack("I",fread($metafile,4));
$meta->interval = $tmp[1];
fclose($metafile);
// 2. Write PHPFina meta file
if (file_exists($targetdir.$outid.".meta")) {
print $targetdir.$outid.".meta already exists?\n";
die;
}
if (!$metafile = @fopen($targetdir.$outid.".meta", 'wb')) {
print "- error opening phpfina meta file to write\n";
die;
}
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);
print "- metafile created: start_time=".$meta->start_time.", interval=".$meta->interval[0]."\n";
//-----------------------------------
// DATA FILE COPY
//-----------------------------------
print "- cp $sourcefile $targetfile\n";
exec("cp $sourcefile $targetfile");
// Confirm that copy is the same size
$s1 = filesize($sourcefile);
$s2 = filesize($targetfile);
if ($s1==$s2) {
print "- $id phptimestore to phpfina complete\n";
$mysqli->query("UPDATE feeds SET `engine`=5 WHERE `id`='$outid'");
$redis->hSet("feed:".$outid,"engine",5);
exec("chown --reference=".$targetdir." ".$targetdir.$outid.".meta");
exec("chown --reference=".$targetdir." ".$targetdir.$outid.".dat");
// Register feeds to delete
$phptimestorefeeds[] = $id;
} else {
print "- copy not exact $s1 $s2\n";
}
}
if ($outid==$id) {
print "---------------------------------------------------\n";
print "Delete phptimestore data files from $sourcedir? (y/n): ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line)!='y') die;
foreach ($phptimestorefeeds as $id) {
print "Deleting feed $id\n";
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT).".tsdb")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT).".tsdb");
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_0_.dat")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_0_.dat");
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_1_.dat")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_1_.dat");
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_2_.dat")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_2_.dat");
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_3_.dat")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_3_.dat");
if (file_exists($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_4_.dat")) unlink($sourcedir.str_pad($id, 16, '0', STR_PAD_LEFT)."_4_.dat");
}
}
function stdin($prompt = null){
if($prompt){
echo $prompt;
}
$fp = fopen("php://stdin","r");
$line = rtrim(fgets($fp, 1024));
return $line;
}