Completed
Push — master ( 9e89e4...0d5964 )
by Kevin
14:20
created

host_stats.php ➔ hosts_by_os()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 28

Duplication

Lines 6
Ratio 21.43 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 0
dl 6
loc 28
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2018 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// show statistics of the host population
21
22
require_once("../inc/util.inc");
23
require_once("../inc/cache.inc");
24
25
$min_credit = .1;   // only count hosts with this much RAC
26
$total_rac = 0;
27
28
define("CACHE_PERIOD", 7*86400);
29
30
function show_type($type, $stats) {
31
    global $total_rac;
32
    $pct = $total_rac?number_format(100*$stats->rac/$total_rac, 4):0;
33
    row_array(array($type, $stats->nhosts, "$pct %"));
34
}
35
36
// a "stats" object consists of #hosts and RAC.  Compare the RAC.
37
//
38
function cmp_rac($stat1, $stat2) {
39
    if ($stat1->rac < $stat2->rac) return 1;
40
    if ($stat1->rac > $stat2->rac) return -1;
41
    return 0;
42
}
43
44
// sort an array whose values are stats objects
45
//
46
function sort_stats_by_rac($stats) {
47
    uasort($stats, 'cmp_rac');
48
    return $stats;
49
}
50
51
function get_stats($clause) {
52
    global $db, $min_credit;
53
    $results = $db->enum_general(
54
        'StdClass',
55
        "select count(*) as nhosts, sum(expavg_credit) as rac from host where expavg_credit>$min_credit and $clause"
56
    );
57
    return $results[0];
58
}
59
60
function hosts_win($os_names) {
61
    $total = new StdClass;
62
    $total->nhosts = 0;
63
    $total->rac = 0;
64
    $os_stats = array();
65 View Code Duplication
    foreach ($os_names as $os_name) {
66
        if (strstr($os_name, "Windows")) {
67
            $stats = get_stats("os_name='$os_name'");
68
            $os_stats[$os_name] = $stats;
69
            $total->nhosts += $stats->nhosts;
70
            $total->rac += $stats->rac;
71
        }
72
    }
73
    $os_stats = sort_stats_by_rac($os_stats);
74
    $os_stats['Windows total'] = $total;
75
    return $os_stats;
76
}
77
78
function hosts_darwin() {
79
    global $db, $min_credit;
80
    $results = $db->enum_general(
81
        'StdClass',
82
        "select distinct(os_version) as v from host where os_name='Darwin' and expavg_credit>$min_credit"
83
    );
84
    $vers = array();
85
    foreach ($results as $result) {
86
        $vers[] = $result->v;
87
    }
88
    $total = new StdClass;
89
    $total->nhosts = 0;
90
    $total->rac = 0;
91
    $os_stats = array();
92 View Code Duplication
    foreach ($vers as $ver) {
93
        $stats = get_stats(
94
            "os_name='Darwin' and os_version='$v'"
0 ignored issues
show
Bug introduced by
The variable $v does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
        );
96
        $os_stats[$ver] = $stats;
97
        $total->nhosts += $stats->nhosts;
98
        $total->rac += $stats->rac;
99
    }
100
    $os_stats = sort_stats_by_rac($os_stats);
101
    $os_stats['total'] = $total;
102
    return $os_stats;
103
}
104
105
function hosts_linux() {
106
    return get_stats("os_name like '%Linux%'");
107
}
108
109
function hosts_other($os_names) {
110
    $d = array();
111
    foreach ($os_names as $os_name) {
112
        if (strstr($os_name, 'Windows')) continue;
113
        if (strstr($os_name, 'Darwin')) continue;
114
        if (strstr($os_name, 'Linux')) continue;
115
        if (!$os_name) continue;
116
        $d[$os_name] = get_stats("os_name='$os_name'");
117
    }
118
    return $d;
119
}
120
121
function get_os_data() {
122
    global $db, $min_credit;
123
    $db = BoincDb::get();
124
    $names = $db->enum_general(
125
        'StdClass',
126
        "select distinct(os_name) from host where expavg_credit>$min_credit"
127
    );
128
    $os_names = array();
129
    foreach ($names as $n) {
130
        $os_names[] = $n->os_name;
131
    }
132
    $data = new StdClass;
133
    $data->total_rac = $db->sum('host', 'expavg_credit', '');
134
    $data->windows = hosts_win($os_names);
135
    $data->darwin = hosts_darwin();
136
    $data->linux = hosts_linux();
137
    $data->other = hosts_other($os_names);
138
    return $data;
139
}
140
141
function hosts_by_os() {
142
    global $db, $min_credit, $total_rac;
143
    $data = get_cached_data(CACHE_PERIOD, "os");
144 View Code Duplication
    if ($data) {
145
        $data = unserialize($data);
146
    } else {
147
        $data = get_os_data();
148
        set_cached_data(CACHE_PERIOD, serialize($data), "os");
149
    }
150
    $total_rac = $data->total_rac;
151
    page_head("Computer breakdown by operating system");
152
    echo "<p><a href=host_stats.php?boinc_version=1>View breakdown by BOINC version</a><p>\n";
153
    start_table("table-striped");
154
    row_heading_array(array("Operating system", "# of active computers", "% of recent credit"));
155
    //echo "total: $total_rac\n";
156
    foreach ($data->windows as $vers=>$stats) {
157
        show_type($vers, $stats);
158
    }
159
    foreach ($data->darwin as $vers=>$state) {
160
        show_type("Darwin $vers", $state);
161
    }
162
    show_type("Linux total", $data->linux);
163
    foreach ($data->other as $vers=>$stats) {
164
        show_type($vers, $stats);
165
    }
166
    end_table();
167
    page_tail();
168
}
169
170
function get_boinc_version_data() {
171
    global $db, $min_credit;
172
    $db = BoincDb::get();
173
    $results = $db->enum_general(
174
        'StdClass',
175
        "select distinct substring(serialnum, locate('BOINC', serialnum), 14) as boinc_vers from host where expavg_credit>$min_credit"
176
    );
177
    $boinc_versions = array();
178
    foreach ($results as $result) {
179
        $boinc_vers = substr($result->boinc_vers, 6);
180
        $boinc_vers = strstr($boinc_vers, "]", true);
181
        if ($boinc_vers) $boinc_versions[] = $boinc_vers;
182
    }
183
    $vers = array();
184
    foreach ($boinc_versions as $boinc_vers) {
185
        $vers[$boinc_vers] = get_stats("serialnum like '%$boinc_vers%'");
186
    }
187
    $vers = sort_stats_by_rac($vers);
188
    $data = new StdClass;
189
    $data->total_rac = $db->sum('host', 'expavg_credit', '');
190
    $data->vers = $vers;
191
    return $data;
192
}
193
194
function hosts_by_boinc_version() {
195
    global $db, $min_credit, $total_rac;
196
    $data = get_cached_data(CACHE_PERIOD, "boinc_version");
197 View Code Duplication
    if ($data) {
198
        $data = unserialize($data);
199
    } else {
200
        $data = get_boinc_version_data();
201
        set_cached_data(CACHE_PERIOD, serialize($data), "boinc_version");
202
    }
203
    $total_rac = $data->total_rac;
204
    page_head("Computer breakdown by BOINC version");
205
    echo "<p><a href=host_stats.php>View breakdown by operating system</a><p>\n";
206
    start_table("table-striped");
207
    row_heading_array(array("BOINC version", "# of active computers", "% of recent credit"));
208
    foreach ($data->vers as $vers=>$stats) {
209
        show_type($vers, $stats);
210
    }
211
    end_table();
212
    page_tail();
213
}
214
215
if (get_str("boinc_version", true)) {
216
    hosts_by_boinc_version();
217
} else {
218
    hosts_by_os();
219
}
220
221
?>
222