/adei/trunk

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

« back to all changes in this revision

Viewing changes to classes/view.php

  • Committer: Suren A. Chilingaryan
  • Date: 2018-07-15 01:53:01 UTC
  • Revision ID: csa@suren.me-20180715015301-s17qbq19snb3wlr5
Adding generalized data functions to VIEW

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
abstract class VIEW implements VIEWInterface {
10
10
 var $req;
11
11
 var $options;
 
12
 
 
13
 var $max_points;
 
14
 var $min_width, $min_height;
 
15
 var $width, $height;
 
16
 
 
17
 var $caches;   // CacheSet
 
18
 var $ivl;      // CacheSet Interval
 
19
 
 
20
 const MERGE_START = 1;
 
21
 
 
22
 const ALLOW_SAME_CHANNEL = 1;
 
23
 const FORBID_MULTIPLE_GROUPS = 2;
 
24
 
 
25
 const GET_RAW_DATA = 1;
12
26
 
13
27
 function __construct(REQUEST $req  = NULL, $options) {
14
 
    $this->req = $req;
 
28
    $this->req = $req?$req:new REQUEST();
15
29
    $this->options = $options;
 
30
 
 
31
    $this->object = $this->req->GetProp("view_object", false);
 
32
 
 
33
    if ($this->object)
 
34
        $this->max_points = $this->GetOption('max_points', 500);
 
35
    else
 
36
        $this->max_points = $this->GetOption('max_points', 5000);
 
37
 
 
38
 
 
39
    $this->aggregation = $this->GetOption('aggregation', false);
 
40
    $this->min_width = $this->GetOption('min_width', 0);
 
41
    $this->min_height = $this->GetOption('min_height', 0);
 
42
 
 
43
    $this->DetectImageDimensions();
 
44
    $this->CreateCacheSet();
 
45
 }
 
46
 
 
47
 function GetProp($prop, $default = false) {
 
48
    if (!preg_match("/^view_/", $prop))
 
49
        $prop = "view_" . $prop;
 
50
    
 
51
    return $this->req->GetProp($prop, $default);
 
52
 }
 
53
 
 
54
 function GetOption($opt, $default = false) {
 
55
    if (isset($this->options[$opt])) return $this->options[$opt];
 
56
    return $default;
16
57
 }
17
58
 
18
59
 function IsApplicable() {
19
60
    return true;
20
61
 }
 
62
 
21
63
 function GetOptions() {
22
64
    return array();
23
65
 }
 
66
 
 
67
 function DetectImageDimensions() {
 
68
    if ($this->object) {
 
69
        $width = $this->req->GetProp($this->object . "_width", $this->min_width + 20) - 20;
 
70
        if ($width < $this->min_width) $width = $this->min_width;
 
71
        $height = $width - 40;//$this->req->GetProp($this->object . "_height", $this->min_height);
 
72
    } else {
 
73
        $width = $this->req->GetProp("page_width", $this->min_width + 5) - 5;
 
74
        $height = $this->req->GetProp("page_height", $this->min_height);
 
75
        if ($width < $this->min_width) $width = $this->min_width;
 
76
        if ($height < $this->min_height) $height = $this->min_height;
 
77
    }
 
78
    
 
79
    $this->width = $width;
 
80
    $this->height = $height;
 
81
 }
 
82
 
 
83
 function CreateCacheSet() {
 
84
    if (!$this->caches) {
 
85
        $req = $this->req->CreateDataRequest();
 
86
 
 
87
        $rdr = $req->CreateReader();
 
88
        $group = $rdr->CreateGroup();
 
89
        $caches = $rdr->CreateCacheSet($group, $mask);
 
90
        $iv = $caches->CreateInterval($req, true);
 
91
        
 
92
        $this->caches = $caches;
 
93
        $this->ivl = $iv;
 
94
    }
 
95
    
 
96
 }
 
97
 
 
98
 function ParseDataOptions($params, $ignore_missing = false) {
 
99
    $ids = array();    
 
100
 
 
101
    foreach ($params as $param) {
 
102
        $x = $this->GetProp($param, false);
 
103
        if ($x) {
 
104
            list($x_gid, $x_id) = explode(":", $x);
 
105
        } else if (!$ignore_missing) {
 
106
            throw new ADEIException(translate("Parameter $param is not set"));
 
107
        } 
 
108
        
 
109
        if (!is_array($ids[$x_gid]))
 
110
            $ids[$x_gid] = array();
 
111
        array_push($ids[$x_gid], $x_id);
 
112
    }
 
113
    
 
114
    return $ids;
 
115
 }
 
116
 
 
117
 function GetDataVariants($params, $flags = 0) {
 
118
    $this->CreateCacheSet();
 
119
 
 
120
    if (!is_array($params)) 
 
121
        $params = array($params);
 
122
        
 
123
    $result = array();
 
124
    for ($i = 0; $i < sizeof($params); $i++) {    
 
125
        $result[$i] = array();
 
126
    }
 
127
    
 
128
    $gid = 0;
 
129
    $fixed_gid = false;
 
130
    foreach ($this->caches as $key => $cachewrap) {
 
131
        for ($i = 0; $i < sizeof($params); $i++) {    
 
132
            array_push($result[$i], array("label" => $cachewrap->GetGroupTitle(), "disabled" => 1));
 
133
        }
 
134
 
 
135
        $id = 0;
 
136
        $list = $cachewrap->GetItemList();
 
137
        $values = array();
 
138
        foreach ($list as $id => $info) {
 
139
            $title = $info['name'];
 
140
            for ($i = 0; $i < sizeof($params); $i++) {    
 
141
                $selected_chan = false;
 
142
 
 
143
                $idx = array_search("$gid:$id", $values);
 
144
                if ($idx === false) {
 
145
                        // free we can assign
 
146
                    if (!isset($values[$i])) {
 
147
                        $setval = $this->GetProp($params[$i], false);
 
148
                        if ($setval) {
 
149
                                // What if it invalid value? It will be just left unselected.
 
150
                            $values[$i] = $setval;
 
151
                        } else {
 
152
                            $values[$i] ="$gid:$id";
 
153
                        }
 
154
                    }
 
155
                } else if ($idx < $i) {
 
156
                        // Already used before
 
157
                    if (($flags&VIEW::ALLOW_SAME_CHANNEL) == 0)
 
158
                        continue;
 
159
                } else {
 
160
                        // Even if it set for later input, we can use it here and override...
 
161
                        // If it is invalid, the earlier is also set to something valid/invalid anyway...
 
162
                }
 
163
 
 
164
                if ($i) {
 
165
                    if ($fixed_gid !== false) {
 
166
                        if ($gid != $fixed_gid)
 
167
                            continue;
 
168
                    }
 
169
                } else {
 
170
                    if (($flags&VIEW::FORBID_MULTIPLE_GROUPS)&&(isset($values[$i]))) {
 
171
                        list($fixed_gid, $tmp) = explode(":", $values[$i]);
 
172
                    }
 
173
                }
 
174
                
 
175
                array_push($result[$i], array("value" => "$gid:$id", "label" => "  $title"));
 
176
            }
 
177
            $id++;
 
178
        }
 
179
        $gid++;
 
180
    }
 
181
 
 
182
    return $result;
 
183
 }
 
184
 
 
185
 function GetDataOptions($params, $flags = 0) {
 
186
    $result = $this->GetDataVariants($params, $flags);
 
187
 
 
188
    if (!is_array($params)) 
 
189
        $params = array($params);
 
190
    
 
191
    $res = array();    
 
192
    for ($i = 0; $i < sizeof($params); $i++) {
 
193
        array_push($res, array("select" => array("label" => $params[$i], "id" => $params[$i], "options" => $result[$i])));
 
194
        array_push($res, array("xml"=>"<br/>"));
 
195
    }
 
196
    
 
197
    return $res;
 
198
 }
 
199
 
 
200
 function GetData($params, INTERVAL $iv = NULL, $flags = 0) {
 
201
    $this->CreateCacheSet();
 
202
    if (!$iv) $iv = $this->ivl;
 
203
 
 
204
    if (!is_array($params)) 
 
205
        $params = array($params);
 
206
 
 
207
    $ids = $this->ParseDataOptions($params);
 
208
 
 
209
    $window_size = $iv->GetWindowSize();
 
210
    $window_start = $iv->GetWindowStart();
 
211
    $window_end = $iv->GetWindowEnd();
 
212
 
 
213
    $rescfg = array(
 
214
        'limit' => $this->max_points,
 
215
    );
 
216
 
 
217
    $gid = 0;
 
218
    $gpos = 0;
 
219
    $data = array();
 
220
    foreach ($this->caches as $key => $cachewrap) {
 
221
        if (!isset($ids[$gid])) {
 
222
            $gid++;
 
223
            continue;
 
224
        }
 
225
 
 
226
 
 
227
 
 
228
        if ($flags&VIEW::GET_RAW_DATA) {
 
229
            $points = $cachewrap->GetAllPoints($this->ivl, $this->max_points);
 
230
            $operation_info = array("resolution" => 0);
 
231
        } else {
 
232
            $resolution = $cachewrap->GetResolution();
 
233
            if ($this->aggregation === false) {     
 
234
                $r = $resolution->Get($iv, $this->width);
 
235
                $size = $resolution->GetWindowSize($r);
 
236
            
 
237
                if (($size > 0)&&(($window_size / $size) > $this->max_points)) {
 
238
                    $new_r = $resolution->Larger($r);
 
239
                    if ($new_r !== false) $r = $new_r;
 
240
                }
 
241
            } else {
 
242
                $r = $resolution->GetByWindowSize($this->aggregation);
 
243
            }
 
244
            $rescfg['resolution'] = $r;
 
245
            $points = $cachewrap->GetIntervals($iv, $rescfg, CACHE::TRUNCATE_INTERVALS);
 
246
            $operation_info = $points->GetOperationInfo();
 
247
        }
 
248
        
 
249
 
 
250
        foreach ($ids[$gid] as $i => $id) {
 
251
            $info[$i] = $operation_info;
 
252
        }
 
253
 
 
254
        foreach($points as $t => $v) {
 
255
            foreach ($ids[$gid] as $i => $id) {
 
256
                if ($flags&VIEW::GET_RAW_DATA) $val = $v[$id];
 
257
                else $val = $v['mean'.$id];
 
258
 
 
259
                if (is_numeric($val)) {
 
260
                    if (!is_array($data[$t])) $data[$t] = array();
 
261
                    $data[$t][$gpos + $i] = $val;
 
262
                }
 
263
            }
 
264
        }
 
265
 
 
266
        $gid++;
 
267
        $gpos += sizeof($ids[$i]); 
 
268
    }
 
269
    
 
270
    return array("info" => $info, "data" => $data);
 
271
 }
 
272
 
 
273
 function MergeViewInfo(&$view, $info, $flags = 0) {
 
274
    $done = false;
 
275
    foreach ($view as &$v) {
 
276
        if (isset($v['info'])) {
 
277
            if ($flags&VIEW::MERGE_START)
 
278
                $v['info'] = array_merge($info, $v['info']);
 
279
            else
 
280
                $v['info'] = array_merge($v['info'], $info);
 
281
            
 
282
            $done = true;
 
283
            break;
 
284
        }
 
285
    }
 
286
    
 
287
    if (!$done) {
 
288
        array_push($view, array("info" => $info));
 
289
    }
 
290
    
 
291
    return $view;
 
292
 }
 
293
 
24
294
};
25
295
 
 
296
 
26
297
?>
 
 
b'\\ No newline at end of file'