/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/reader.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
interface READERInterface {
 
4
    public function __construct(&$props);
 
5
 
 
6
    public function GetDatabaseList($flags = 0);
 
7
    public function GetGroupList($flags = 0);
 
8
    public function GetGroupInfo(LOGGROUP &$grp = NULL, $flags = 0);
 
9
    public function GetItemList(LOGGROUP &$grp = NULL, MASK &$mask = NULL, $flags = 0);
 
10
    public function GetExperimentList($flags = 0);
 
11
    public function GetMaskList(LOGGROUP &$grp = NULL, $flags = 0);
 
12
    
 
13
    public function CreateGroup(array &$ginfo = NULL, $flags = 0);
 
14
    public function CreateInterval(LOGGROUP &$group = NULL, array &$iinfo = NULL, $flags = 0);
 
15
    public function CreateMask(LOGGROUP &$grp = NULL, array &$minfo = NULL, $flags = 0);
 
16
    
 
17
    public function GetGroups();
 
18
    public function GetGroupSize(LOGGROUP &$grp = NULL);
 
19
    
 
20
    public function ImportUnixTime($unix_time);
 
21
    public function ExportUnixTime($db_time);
 
22
 
 
23
    public function HaveData(LOGGROUP &$grp = NULL, $from = 0, $to = 0);
 
24
    public function GetData(LOGGROUP &$grp = NULL, $from = 0, $to = 0);
 
25
    public function PushData(LOGGROUP &$grp, $time, $data);
 
26
    public function RemoveData(LOGGROUP &$grp, $time);
 
27
    
 
28
    public function Backup(&$binfo);
 
29
}
 
30
 
 
31
abstract class READER implements READERInterface {
 
32
 var $req;
 
33
 var $server;
 
34
 var $opts;
 
35
 
 
36
 var $srvid, $dbname;
 
37
 var $start_date;
 
38
 var $end_date;
 
39
 
 
40
 var $time_format;
 
41
 var $time_zone;
 
42
 var $time_module;
 
43
 
 
44
 var $gmt_timezone;
 
45
 
 
46
 var $group_class;
 
47
 
 
48
 const LIST_ALL = 1;
 
49
 const NEED_INFO = 2;
 
50
 const NEED_COUNT = 4;
 
51
 const NEED_ITEMINFO = 8;
 
52
 
 
53
 const BACKUP_FULL = 1;
 
54
 
 
55
 function __construct(&$props) {
 
56
    if ($props instanceof REQUEST) {
 
57
        $this->req = &$props;
 
58
        $this->server = $this->req->GetServerInfo();
 
59
        
 
60
        $this->srvid = $props->props['db_server'];
 
61
    } else {
 
62
        $this->req = false;
 
63
        $this->server = &$props;
 
64
 
 
65
        $this->srvid = false;
 
66
    }
 
67
    
 
68
    if (isset($this->server['database']))
 
69
        $this->dbname = $this->server['database'];
 
70
    else 
 
71
        $this->dbname = false;
 
72
        
 
73
    $this->time_format = "U.u";
 
74
    $this->time_zone = new DateTimeZone("GMT");
 
75
 
 
76
    $this->gmt_timezone = new DateTimeZone("GMT");
 
77
 
 
78
    $this->opts = new OPTIONS($props);
 
79
 
 
80
    $date_limit = $this->opts->Get('date_limit');
 
81
    if ($date_limit) {
 
82
        if (is_array($date_limit)) {
 
83
            if (is_int($date_limit[0])) $this->start_date = $date_limit[0];
 
84
            else $this->start_date = strtotime($date_limit[0]);
 
85
            if (is_int($date_limit[1])) $this->end_date = $date_limit[1];
 
86
            else $this->end_date = strtotime($date_limit[1]);
 
87
        } else {
 
88
            if (is_int($date_limit)) $this->start_date = $date_limit;
 
89
            else $this->start_date = strtotime($date_limit);
 
90
            $this->end_date = false;
 
91
        }
 
92
    } else {
 
93
        $this->start_date = false;
 
94
        $this->end_date = false;
 
95
    }
 
96
 
 
97
    $time_module = $this->opts->Get('time_module');
 
98
    if ($time_module) {
 
99
        if (!include_once("time/" . strtolower($time_module) . '.php')) {
 
100
            throw new ADEIException(translate("Unsupported time module is configured: \"%s\"", $time_module));
 
101
        }
 
102
        
 
103
        $time_options = $this->opts->Get('time_options');
 
104
        $cl = strtoupper($time_module);
 
105
        $this->time_module = new $cl($this, $time_options);
 
106
    } else $this->time_module = false;
 
107
    
 
108
    $group_class = false;
 
109
 }
 
110
 
 
111
 function ImportTime(DateTime $dt) {
 
112
    if ($this->time_module) return $this->time_module->ImportTime($dt);
 
113
    
 
114
    $dt->setTimezone($this->time_zone);
 
115
    return $dt->format($this->time_format);
 
116
 }
 
117
 
 
118
 function ExportTime($db_time) {
 
119
    if ($this->time_module) return $this->time_module->ExportTime($db_time);
 
120
 
 
121
    if ($this->time_zone) {
 
122
        $ctz = date_default_timezone_get();
 
123
        date_default_timezone_set($this->time_zone->getName());
 
124
        $dt = new DateTime($db_time);
 
125
        date_default_timezone_set($ctz);
 
126
 
 
127
//      $dt->setTimezone($this->gmt_timezone);
 
128
        return $dt;
 
129
    }
 
130
    return new DateTime($db_time);
 
131
 }
 
132
 
 
133
 function ImportUnixTime($unix_time) {
 
134
    $itime = (int)floor($unix_time);
 
135
 
 
136
    if ($itime==$unix_time) {
 
137
            // Time zone should be specified, esle "e" output is invalid
 
138
        return $this->ImportTime(new DateTime("@$itime", $this->gmt_timezone));
 
139
    } else {
 
140
        if (is_float($unix_time)) $subsec = strchr(sprintf("%F", $unix_time), '.');
 
141
        else $subsec = strchr($unix_time, '.');
 
142
        
 
143
            // DS: Due to the bug in PHP (precision is limited to 10ns)
 
144
        if (strlen($subsec) > 9) $subsec = substr($subsec, 0, 9);
 
145
        return $this->ImportTime(new DateTime(strftime("%Y-%m-%dT%H:%M:%S", $itime) . $subsec, $this->gmt_timezone));
 
146
//      return $this->ImportTime(new DateTime(strftime("%Y/%m/%d %H:%M:%S", $itime) . $subsec, $this->gmt_timezone));
 
147
    }
 
148
 }
 
149
 
 
150
 function ExportUnixTime($db_time) {
 
151
    $dt = $this->ExportTime($db_time);
 
152
    return $dt->format("U.u");
 
153
 }
 
154
 
 
155
 function GetDatabaseList($flags = 0) {
 
156
    if ($this->req)
 
157
        $server = $this->req->GetServerConfig();
 
158
    else
 
159
        throw new ADEIException(translate("The data source server is not specified"));
 
160
 
 
161
    $dblist = array();
 
162
    foreach ($server['database'] as $name) {
 
163
        $dblist[$name] = array(
 
164
            'name' => $name
 
165
        );
 
166
    }
 
167
    
 
168
    return $dblist;
 
169
 }
 
170
 
 
171
 function GetGroupList($flags = 0) {
 
172
    return $this->GetGroupInfo($gr=NULL, $flags);
 
173
 }
 
174
 
 
175
 function GetExperimentList($flags = 0) {
 
176
    return array();
 
177
 }
 
178
 
 
179
 function GetMaskList(LOGGROUP &$grp = NULL, $flags = 0) {
 
180
    return array();
 
181
 }
 
182
 
 
183
 function CheckGroup(LOGGROUP &$grp = NULL) {
 
184
    if ($grp) {
 
185
        if (($this->group_class)&&(!$grp instanceof $this->group_class))
 
186
            throw Exception(translate("Invalid LOGGROUP supplied"));
 
187
    } else {
 
188
        $grinfo = $this->req->GetGroupInfo();
 
189
        $grp = $this->CreateGroup($grinfo);
 
190
    }
 
191
    return $grp;
 
192
 }
 
193
 
 
194
 function CreateGroup(array &$ginfo = NULL, $flags = 0) {
 
195
    if (!$ginfo) $ginfo = $this->req->GetGroupInfo();
 
196
 
 
197
    if ($this->group_class) return new $this->group_class($ginfo, $this, $flags);
 
198
    return new LOGGROUP($ginfo, $this, $flags);
 
199
 }
 
200
 
 
201
 function CreateMask(LOGGROUP &$grp = NULL, array &$minfo = NULL, $flags = 0) {
 
202
    if (!$minfo) {
 
203
        if ($this->req instanceof GROUPRequest)
 
204
            $minfo = $this->req->GetMaskInfo();
 
205
    }
 
206
    //$grp = $this->CheckGroup($grp);
 
207
    
 
208
    return new MASK($minfo);
 
209
 }
 
210
 
 
211
 function CreateInterval(LOGGROUP &$grp = NULL, array &$iinfo = NULL, $flags = 0) {
 
212
    if (!$iinfo) {
 
213
        if ($this->req instanceof DATARequest)
 
214
            $iinfo = $this->req->GetIntervalInfo();
 
215
    }
 
216
    $grp = $this->CheckGroup($grp);
 
217
    
 
218
    $ivl = new INTERVAL($iinfo, $this, $grp, $flags);
 
219
    
 
220
    if ((($this->start_date)&&($ivl->GetWindowStart() < $this->start_date))||(($this->end_date)&&($ivl->GetWindowEnd() > $this->end_date))) {
 
221
        $ivl->Limit($this->start_date, $this->end_date);
 
222
    }
 
223
    
 
224
    return $ivl;
 
225
 }
 
226
 
 
227
 
 
228
 function GetGroups() {
 
229
    $groups = $this->GetGroupList(); 
 
230
 
 
231
    $list = array();
 
232
    foreach (array_keys($groups) as $group) {
 
233
        $ginfo = array("db_group" => $group);
 
234
        array_push($list, $this->CreateGroup($ginfo));
 
235
    }
 
236
 
 
237
    return $list;
 
238
 }
 
239
 
 
240
 function GetGroupSize(LOGGROUP &$grp = NULL) {
 
241
    return sizeof($this->GetItemList($grp));
 
242
 }
 
243
 
 
244
 function HaveData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
 
245
    return true;
 
246
 }
 
247
 
 
248
 function PushData(LOGGROUP &$grp, $time, $data) {
 
249
    throw new ADEIException(get_class($this) . ". PushData is not implemented");
 
250
 }
 
251
 
 
252
 function RemoveData(LOGGROUP &$grp, $time) {
 
253
    throw new ADEIException(get_class($this) . ". RemoveData is not implemented");
 
254
 }
 
255
    
 
256
 /* DS: check for invalid columns number */
 
257
 function Clean(&$lg, $from = 0, $to = 0) {
 
258
    $data = $this->GetData($lg, $from, $to);
 
259
    foreach ($data as $t => $value) {
 
260
        if (sizeof($value)==0) {
 
261
            $this->RemoveData($lg, $t);
 
262
        }
 
263
        
 
264
        $check = true;
 
265
        foreach ($value as $v) {
 
266
            if ($v) {
 
267
                $check = false;
 
268
                break;
 
269
            }
 
270
        }
 
271
        
 
272
        if ($check) {
 
273
            $this->RemoveData($lg, $t);
 
274
        }
 
275
    }
 
276
 }
 
277
 
 
278
 function Backup(&$binfo) {
 
279
    throw new ADEIException(get_class($this) . ". Backup is not implemented");
 
280
 }
 
281
 
 
282
 function Export(DATAHandler &$h = NULL, LOGGROUP &$grp = NULL, MASK &$mask = NULL, $from = 0, $to = 0, $opts = 0) {
 
283
    if (!$h) $h = new CSVHandler();
 
284
    $grp = $this->CheckGroup($grp);
 
285
 
 
286
    $names = $this->GetItemList($grp);
 
287
    $data = $this->GetData($grp, $from, $to);
 
288
 
 
289
    $h->Start();
 
290
    $h->HeaderStart();
 
291
    $h->TimeHeader();
 
292
    $columns = 0;
 
293
    foreach ($names as $name) {
 
294
        $h->DataHeader($name["name"], $columns);
 
295
        $columns++;
 
296
    }
 
297
    $h->HeaderEnd();
 
298
    
 
299
 
 
300
    foreach ($data as $time => $row) {
 
301
        if (sizeof($row) != $columns) continue;
 
302
        
 
303
        $h->VectorStart();
 
304
        $h->TimeValue($time);
 
305
 
 
306
        foreach ($row as $i => &$column) {
 
307
            $h->DataValue($column, $i);
 
308
        }
 
309
        $h->VectorEnd();
 
310
    }
 
311
 
 
312
    $h->End();
 
313
 }
 
314
 
 
315
 function ExportCSV(STRINGHandler &$h = NULL, LOGGROUP &$grp = NULL, MASK &$mask = NULL, $from = 0, $to = 0, $opts = 0) {
 
316
    return $this->Export(new CSVHandler($h), $grp, $msk, $from, $to, $opts);
 
317
 }
 
318
 
 
319
/*
 
320
 function ExportCSV(STRINGHandler &$h = NULL, LOGGROUP &$grp = NULL, $from = 0, $to = 0, $opts = 0) {
 
321
    if (!$h) $h = new STRINGStreamHandler();
 
322
    $grp = $this->CheckGroup($grp);
 
323
 
 
324
    $subseconds = !$this->opts->Get('ignore_subsecond');
 
325
 
 
326
    $names = $this->GetItemList($grp);
 
327
    $data = $this->GetData($grp, $from, $to);
 
328
 
 
329
    $columns = 0;
 
330
    $h->Process("Date/Time");
 
331
    foreach ($names as $name) {
 
332
        $h->Process(", " . preg_replace("/,/", " ", $name["name"]));
 
333
        $columns++;
 
334
    }
 
335
    $h->Process("\r\n");
 
336
 
 
337
    $saved_tz = date_default_timezone_get ();
 
338
    date_default_timezone_set("UTC");
 
339
    foreach ($data as $time => $row) {
 
340
        if (sizeof($row) != $columns) continue;
 
341
        
 
342
        if ($subseconds) {
 
343
            $subsec = strchr(sprintf("%.6F", $time), '.');
 
344
            $h->Process(date("d-M-y H:i:s", $time) . $subsec);
 
345
        } else 
 
346
            $h->Process(date("d-M-y H:i:s", $time));
 
347
        foreach ($row as $column) {
 
348
            $h->Process(", " . $column);
 
349
        }
 
350
        $h->Process("\r\n");
 
351
    }
 
352
    date_default_timezone_set($saved_tz);
 
353
 }
 
354
*/
 
355
}
 
356
 
 
357
 
 
358
 
 
359
class NULLReader implements READERInterface {
 
360
    function __construct(&$props) {}
 
361
 
 
362
    function GetDatabaseList($flags = 0) {
 
363
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
364
    }
 
365
 
 
366
    function GetGroupList($flags = 0) {
 
367
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
368
    }
 
369
    
 
370
    function GetGroupInfo(LOGGROUP &$grp = NULL, $flags = 0) {
 
371
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
372
    }
 
373
    
 
374
    function GetItemList(LOGGROUP &$grp = NULL, MASK &$mask = NULL, $flags = 0) {
 
375
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
376
    }
 
377
    
 
378
    function GetExperimentList($flags = 0) {
 
379
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
380
    }
 
381
    
 
382
    function GetMaskList(LOGGROUP &$grp = NULL, $flags = 0) {
 
383
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
384
    }
 
385
    
 
386
    function CreateGroup(array &$ginfo = NULL, $flags = 0) {
 
387
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
388
    }
 
389
    
 
390
    function CreateInterval(LOGGROUP &$group = NULL, array &$iinfo = NULL, $flags = 0) {
 
391
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
392
    }
 
393
    
 
394
    function CreateMask(LOGGROUP &$grp = NULL, array &$minfo = NULL, $flags = 0) {
 
395
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
396
    }
 
397
    
 
398
    function GetGroups() {
 
399
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
400
    }
 
401
    
 
402
    function GetGroupSize(LOGGROUP &$grp = NULL) {
 
403
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
404
    }
 
405
    
 
406
    function ImportUnixTime($unix_time) {
 
407
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
408
    }
 
409
    
 
410
    function ExportUnixTime($db_time) {
 
411
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
412
    }
 
413
 
 
414
    function HaveData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
 
415
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
416
    }
 
417
    
 
418
    function GetData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
 
419
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
420
    }
 
421
    
 
422
    function PushData(LOGGROUP &$grp, $time, $data) {
 
423
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
424
    }
 
425
    
 
426
    function RemoveData(LOGGROUP &$grp, $time) {
 
427
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
428
    }
 
429
    
 
430
    function Backup(&$binfo) {
 
431
        throw new ADEIException(get_class($this) . ". " . __METHOD__ . " is not implemented");
 
432
    }
 
433
}
 
434
 
 
435
?>
 
 
b'\\ No newline at end of file'