-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurements_report.html
72 lines (61 loc) · 2.17 KB
/
measurements_report.html
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
<?php
require_once 'php/vendor/autoload.php';
require_once 'php/login.php';
//parse command line parameters
if (isset($_GET['duration'])) $duration=$_GET['duration'];
else $duration=1;
$timestamp = time() - ($duration * 24 * 60 * 60);
if (isset($_GET['domain'])) $match_clause=['measurement_domain' => $_GET['domain'], 'timestamp' => ['$gte' => $timestamp]];
else $match_clause=['measurement_domain' => 'google.com', 'timestamp' => ['$gte' => $timestamp]];
//open driver manager and geoip reader
$manager = new MongoDB\Driver\Manager(
$mongodbConnectionString,
[
'ssl' => TRUE,
'username' => $mongodbUsername,
'password' => $mongodbPassword,
'authSource' => $mongodbAuthSource
],
[
'ca_file' => $mongodbSslCaCert,
'pem_file' => $mongodbSslPem
]
);
$reader = new GeoIp2\Database\Reader('/usr/local/share/GeoLite2/GeoLite2-City.mmdb');
//query for measurements
$cursor = $manager->executeCommand(
'proddle',
new MongoDB\Driver\Command(
[
'aggregate' => 'measurements',
'pipeline' => [
['$match' => $match_clause],
['$sort' => ['timestamp' => -1]],
['$project' => ['headers' => 0]]
]
]
)
);
//geolocate vantage and effective url
$array = $cursor->toArray()[0];
$coordinates = [];
for ($i=0; $i < count($array->result); ++$i) {
$result = $array->result[$i];
$coordinate = (object) array();
if (isset($result->vantage_ip_address)) {
$coordinate->vantage = (object) array();
$record = $reader->city($result->vantage_ip_address);
$coordinate->vantage->latitude = $record->location->latitude;
$coordinate->vantage->longitude = $record->location->longitude;
}
if (isset($result->primary_ip_address)) {
$coordinate->domain = (object) array();
$record = $reader->city($result->primary_ip_address);
$coordinate->domain->latitude = $record->location->latitude;
$coordinate->domain->longitude = $record->location->longitude;
}
$coordinates[$i] = $coordinate;
}
$array->coordinates = $coordinates;
echo MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($array));
?>