summaryrefslogtreecommitdiff
path: root/mirrors.php
blob: 8ba3cde1995cfe340c7ded2b8acf1c49c11aaae2 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
require_once "init.php";

require_once BASE . "/lib/mysql.php";
require_once BASE . "/lib/style.php";
require_once BASE . "/lib/format.php";

$options = explode('?', $_SERVER['REQUEST_URI'], 2);

$uri_parts = explode('/', $options[0]);
if (count($options) == 2)
  $options = $options[1];
else
  $options = '';
if ($uri_parts[count($uri_parts)-1] == '')
  array_pop($uri_parts);

if ($uri_parts[0] != '' || $uri_parts[1] != 'mirrors')
  throw_http_error(422, 'Unprocessable Entity');

array_splice(
  $uri_parts,
  0, 2
);

if (count($uri_parts) == 0) { // index

  $cutoff = 3600;
  
  $sorts = array(
    "server" => array(
      "title" => "server",
      "label" => "Server",
      "mysql" => "`url`"
    ),
    "country" => array(
      "title" => "country",
      "label" => "Country",
      "mysql" => "`l_ms`.`country_code`"
    ),
    "isos" => array(
      "title" => "wether isos are available",
      "label" => "ISOs",
      "mysql" => "`l_ms`.`isos`"
    ),
    "protocols" => array(
      "title" => "available protocols",
      "label" => "Protocols",
      "mysql" => "`protocols`"
    )
  );

  $query =
    "SELECT " .
    "GROUP_CONCAT(DISTINCT `l_ms`.`protocol`) AS `protocols`," .
    "SUBSTRING(`l_ms`.`url`,LENGTH(`l_ms`.`protocol`)+4) AS `url`," .
    "`l_ms`.`country`," .
    "`l_ms`.`country_code`," .
    "`l_ms`.`isos`," .
    "`l_ms`.`ipv4`," .
    "`l_ms`.`ipv6`" .
    " FROM (" .
      "SELECT " .
      "`mirror_statuses`.`url`," .
      "MAX(`mirror_statuses`.`start`) AS `start`" .
      " FROM `mirror_statuses`" .
      " WHERE `mirror_statuses`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff .
      " GROUP BY `mirror_statuses`.`url`" .
    ") AS `ls`" .
    " JOIN `mirror_statuses` AS `l_ms`" .
    " ON `ls`.`url`=`l_ms`.`url`" .
    " AND `ls`.`start`=`l_ms`.`start`" .
    " GROUP BY `url`" .
    " ORDER BY ";

  if (array_key_exists("sort", $_GET)) {
    if (isset($sorts[$_GET["sort"]]["mysql"]))
      $query .= $sorts[$_GET["sort"]]["mysql"] . ",";
    elseif (isset($sorts[substr($_GET["sort"],1)]["mysql"]))
      $query .= $sorts[substr($_GET["sort"],1)]["mysql"] . " DESC,";
  }

  $query .= "`url`";

  $result = mysql_run_query(
    $query
  );

  $last_check = 0;
  $max_count = 0;

  while($row = $result->fetch_assoc())
    $rows[] = $row;

  print_header("Mirror Overview");

?>
      <div id="dev-mirrorlist" class="box">
        <h2>Mirror Overview</h2>
        <table class="results">
          <thead>
            <tr>
<?php
    foreach ($sorts as $get => $sort) {
      print "              <th>\n";
      print "                <a href=\"?";
      print substr(str_replace(
        "&sort=".$_GET["sort"]."&",
        "&",
        "&".$_SERVER["QUERY_STRING"]."&"
      ),1)."sort=";
      if ($_GET["sort"] == $get)
        print "-";
      print $get."\" title=\"Sort package by ".$sort["title"]."\">".$sort["label"]."</a>\n";
      print "              </th>\n";
    }
?>
            </tr>
          </thead>
          <tbody>
<?php

  $oddity = "odd";
  if (isset($rows)) {
    foreach ($rows as $row) {
      print "            <tr class=\"" . $oddity ."\">\n";
      print "              <td>\n";
      print "                " . $row["url"] . "\n";
      print "              </td>\n";
      print "              <td class=\"country\">\n";
      print "                <span class=\"fam-flag fam-flag-" . $row["country_code"] . "\" title=\"" . $row["country"] . "\">\n";
      print "                </span>\n";
      print "                  " . $row["country"] . "\n";
      print "              </td>\n";
      print "              <td>\n";
      if ($row["isos"])
        print "                Yes\n";
      else
        print "                No\n";
      print "              </td>\n";
      print "              <td class=\"wrap\">\n";
      print "                " . $row["protocols"] . "\n";
      print "              </td>\n";
      print "            </tr>\n";
      if ($oddity == "odd")
        $oddity = "even";
      else
        $oddity = "odd";
    }
  } else {
    print "There is no data, as no mirror checks run in the last " . $cutoff . " seconds.";
  }

?>
          </tbody>
        </table>
      </div>
<?php

    print_footer();
    die();

} // index

if ($uri_parts[0] == 'status') {

  $cutoff = 86400;

  $result = mysql_run_query(
    "SELECT " .
    "`l_ms`.`protocol`," .
    "`l_ms`.`url`," .
    "`l_ms`.`country`," .
    "`l_ms`.`country_code`," .
    "`l_ms`.`last_sync`," .
    "`l_ms`.`start`," .
    "AVG(IF(`a_ms`.`active`,(`a_ms`.`start`-`a_ms`.`last_sync`)/3600,NULL)) AS `delay`," .
    "AVG(IF(`a_ms`.`active`,(`a_ms`.`stop`-`a_ms`.`start`)/3600,NULL)) AS `duration_avg`," .
    "STD(IF(`a_ms`.`active`,(`a_ms`.`stop`-`a_ms`.`start`)/3600,NULL)) AS `duration_stddev`," .
    "`l_ms`.`isos`," .
    "`l_ms`.`ipv4`," .
    "`l_ms`.`ipv6`," .
    "`l_ms`.`active`," .
    "(`l_ms`.`active` AND (`l_ms`.`start` > UNIX_TIMESTAMP(NOW()) - 3600)) AS `recently_active`," .
    "AVG(IF(`a_ms`.`active`,1,0)) AS `completion_pct`," .
    "COUNT(1) AS `count`" .
    " FROM (" .
      "SELECT " .
      "`mirror_statuses`.`url`," .
      "MAX(`mirror_statuses`.`start`) AS `start`" .
      " FROM `mirror_statuses`" .
      " WHERE `mirror_statuses`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff .
      " GROUP BY `mirror_statuses`.`url`" .
    ") AS `ls`" .
    " JOIN `mirror_statuses` AS `l_ms`" .
    " ON `ls`.`url`=`l_ms`.`url`" .
    " AND `ls`.`start`=`l_ms`.`start`" .
    " JOIN `mirror_statuses` AS `a_ms`" .
    " ON `a_ms`.`url`=`l_ms`.`url`" .
    " AND `a_ms`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff .
    " GROUP BY `l_ms`.`id`"
  );

  $last_check = 0;
  $max_count = 0;

  while($row = $result->fetch_assoc()) {
    foreach (array(
      "start",
      "delay",
      "duration_avg",
      "duration_stddev",
      "completion_pct",
      "count",
      "isos",
      "ipv4",
      "ipv6",
      "active",
      "recently_active"
    ) as $key)
      $row[$key] = floatval($row[$key]);
    $row["last_sync"] = gmdate("Y-m-d\TH:i:s\Z", $row["last_sync"]);
    if ($row["completion_pct"] == 0)
      $row["score"] = 0;
    else
      $row["score"] =
        ($row["delay"] + $row["duration_avg"] + $row["duration_stddev"]) / $row["completion_pct"];
    $urls[] = $row;
    $last_check = max ($row["start"], $last_check);
    $max_count = max ($row["count"], $max_count);
  }

  $content = array(
    "cutoff" => $cutoff,
    "check_frequency" => (($max_count!=0)?($cutoff/$max_count):0),
    "num_checks" => $max_count,
    "last_check" => gmdate("Y-m-d\TH:i:s.v\Z",$last_check), //"2018-06-15T07:25:06.741Z",
  //  "version" => 3,
    "urls" => (isset($urls)?$urls:[])
  );

  if (count($uri_parts) == 1) { // human readable
    print_header("Mirror Status");
    print <<<EOF
    <div id="dev-mirrorlist" class="box">
      <h2>Mirror Status</h2>
EOF;
    if ($max_count==0) {
      print "No results as there was no mirror check running in the last " . $cutoff . " seconds.";
    } else {
      print <<<EOF
        <p>
          This page reports the status of all known, public, and active ArchLinux32 mirrors.
          All data on this page reflects the status of the mirrors within the last $cutoff seconds.
          All listed times are UTC. The check script runs on a regular basis and polls for
          the lastsync file in the root of our repository layout. This file is regularly
          updated on the central repository, so checking the value within allows one
          to see if the mirror has synced recently. This page contains several pieces 
          of information about each mirror.
        </p>
        <p>
          The last mirror check ran at ${content['last_check']} UTC.
          Checks have ran ${content['num_checks']} times in the last
          $cutoff seconds at an average interval of ${content['check_frequency']} seconds.
        </p>
        <p>
          This data is also available in <a href="/mirrors/status/json/">JSON</a>
          or <a href="/mirrors/status/tsv/">TSV</a> format. The units of all
          time/duration values are in seconds; date/time values are UTC.
        </p>
        <h3>Successfully Syncing Mirrors</h3>
        <table class="results">
          <thead>
            <tr>
              <th>Mirror URL</th>
              <th>Protocol</td>
              <th>Country</th>
              <th>Completion</th>
              <th>Delay</th>
              <th>Last Sync</th>
EOF;
      print <<<EOF
            </tr>
          </thead>
          <tbody>
EOF;
        $oddity = "odd";
        if (isset($content['urls'])) {
          foreach ($content['urls'] as $row) {
            print "            <tr class=\"" . $oddity ."\">\n";
            print "              <td>\n";
            print "                " . $row["url"] . "\n";
            print "              </td>\n";
            print "              <td>\n";
            print "                " . $row["protocol"] . "\n";
            print "              </td>\n";
            print "              <td class=\"country\">\n";
            print "                <span class=\"fam-flag fam-flag-" . $row["country_code"] . "\" title=\"" . $row["country"] . "\">\n";
            print "                </span>\n";
            print "                  " . $row["country"] . "\n";
            print "              </td>\n";
            print "              <td>\n";
            print "                " . $row["completion_pct"] . "\n";
            print "              </td>\n";
            print "              <td>\n";
            print "                " . $row["delay"] . "\n";
            print "              </td>\n";
            print "              <td>\n";
            print "                " . $row["last_sync"] . "\n";
            print "              </td>\n";
            print "            </tr>\n";
            if ($oddity == "odd")
              $oddity = "even";
            else
              $oddity = "odd";
          }
        }
        print <<<EOF
          </tbody>
        </table>
EOF;
    print <<<EOF
    </div>
EOF;
    }
    exit( );
  } // human readable

  if (!export_as_requested(
    array(
      "json" => $content,
      "tsv" => $urls
    ),
    $uri_parts[1]
  )) {
    throw_http_error(
      406,
      'Not Acceptable',
      implode(
        "<br>\n",
        array_merge(
          array(
            'Unknown output format.',
            'Accepted:'
          ),
          array_map(
            function($type){
              return '<a href="/mirrors/status/' . $type . '/">' . $type . '</a>';
            },
            array('json', 'tsv')
          )
        )
      )
    );
  }
  die();
} // status

throw_http_error(501, "Not Implemented");