aboutsummaryrefslogtreecommitdiffstats
path: root/Report/Box.php
blob: 79408a017b1e2c33ba47a4761eee545ced051114 (plain)
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
<?php
/**
 * Abstract box class for reports.
 *
 * PHP version 5
 *
 * @category Dashboard
 * @package  Buildsystem
 * @author   Romain d'Alverny <rda@mageia.org>
 * @license  MIT License, see LICENSE.txt
 * @link     http://svnweb.mageia.org/svn/soft/dashboard/
*/

/**
*/
abstract class Report_Box
{
    /**
    */
    var $title = null;
    
    /**
    */
    var $_new_values = null;

    /**
    */
    var $working = false;

    /**
    */
    function __construct()
    {
        $this->_labels = array_merge(array('?' => '%s %s'),
            $this->_get_var_definitions());
    }
    
    /**
    */
    final public function run_report()
    {
        $path  = realpath(dirname(__FILE__));
        $files = glob(sprintf('%s/Box/*.php', $path));

        foreach ($files as $f) {
            if (substr(basename($f), 0, 7) != 'ignore_')
                include $f;
        }

        $reports = array();
        foreach (get_declared_classes() as $class) {
            if (is_subclass_of($class, 'Report_Box')) {
                echo $class, "\n";
                $c         = new $class;
                $reports[] = $c->render();
            }
        }

        file_put_contents('report.html', Report_HTML::render($reports));
    }

    /**
     * lowercase keys!
    */
    final public function update() 
    {
        $this->_get_new_values();
        $this->_db_save($this->_new_values);
    }
    
    /**
    */
    function render ()
    {
        $this->load();

        // associate key/values with string labels
        // compute global statuts
        // push
        $status     = count($this->_new_values);
        $status_max = count($this->_new_values);
        $links      = $this->_get_links();
        $values     = array();
    
        $this->_tmp_labels = $this->_labels;
        if (is_null($this->_new_values))
            $this->_new_values = array();

        foreach ($this->_tmp_labels as $k => $v) {
            //foreach ($this->_new_values as $k => $v) {
            if ($k == '?')
                continue;

            $v = $this->_render_value_gen($k);

            $status  -= $v['s'];
            $values[] = $v;
        }

        if (count($this->_tmp_labels) > 0) {
            foreach ($this->_tmp_labels as $k => $v) {
                if ($k == '?')
                    continue;

                $values[] = $this->_render_value_gen($k);
            }
        }
        
        echo $status / $status_max;

        return array(
            'title'  => $this->title,
            'status' => $status_max > 0 ? round($status / $status_max, 2) : 0,
            'values' => $values,
            'links'  => $links
        );
    }
    
    /**
    */
    final private function _render_value_gen($k = null)
    {
        if (array_key_exists($k, $this->_tmp_labels)) {

            if ($this->_tmp_labels[$k] == ':render') {
                $this->_cur_val = isset($this->_new_values[$k]) ? $this->_new_values[$k] : null;
                $v              = call_user_func(array($this, '_render_value_' . $k));
                unset($this->_cur_val);
            } else {
                $v = $this->_render_value_default($k, $this->_tmp_labels[$k]);
            }
        } else {
            $v = $this->_render_value_default($k); 
        }
        unset($this->_tmp_labels[$k]);

        return $v;
    }
    
    /**
    */
    final private function _render_value_default($k = null, $format = null)
    {
        $score     = 0;
        $class     = 'unk';
        $weight    = 1;
        $test_case = null;

        if (is_null($format)) {
            $format = '%s %s';
        } elseif (!is_string($format)) {
            $test_case = $format['t'];
            $format    = $format['l'];
            $weight    = isset($format['w']) ? $format['w'] : $weight;
        }

        $v = isset($this->_new_values[$k]) ? $this->_new_values[$k] : null;
        if (!is_null($v)) {
            if (!is_null($test_case)) {
                $test_case = sprintf('$evalres = ($v %s);', $test_case);
                eval($test_case);

                $class = $evalres === true ? 'ok' : 'failed';
            } else {
                $class = 'ok';
            }
        } else {
            $class = 'unk';
        }

        if ($class == 'failed')
            $score = -1;
        elseif ($class == 'ok')
            $score = 1;

        $score *= $weight;

        return array(
            't' => sprintf($format, $v, $k),
            'c' => $class,
            's' => $score
        );
    }
    
    /**
    */
    final public function load()
    {
        // go in table TABLE, load all most recent key/values
        //
        // TODO(rda)
        $this->_get_new_values();
        $this->_values = array();
    }
    
    /**
    */
    final private function _db_save($vals)
    {
        echo "Saving:\n";
        echo get_class($this),"\n";
        print_r($vals);
    }
    
    /**
    */
    final private function _get_new_values()
    {
        $values  = null;
        $methods = array();
        foreach (get_class_methods($this) as $m) {
            if (substr($m, 0, 7) == '_fetch_') {
                $methods[] = $m;
            }
        }
        if (count($methods) == 0) {
            echo '> Nothing to fetch.', "\n";
        } else {
            $values = array();
            foreach ($methods as $m) {
                echo sprintf("> Calling [%s]", $m), "\n";
                $values = array_merge($values, $this->$m());
            }
        }

        $this->_new_values = $values;
    }
    
    /**
    */
    function _get_links()
    {
        return null;
    }
}