forked from itsMattShull/nines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webperfSubmit.php
141 lines (111 loc) · 5.36 KB
/
webperfSubmit.php
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
<?php
//connect to the database so we can check, edit, or insert data to our users table
$conn = new PDO("mysql:host={{HOSTNAME}};dbname={{DATABASE-NAME}}", {{USERNAME}}, "{{PASSWORD}}");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$json = json_decode(file_get_contents("php://input"), true);
$clientURL = $json['url'];
$sql= $conn->prepare('SELECT * FROM perf WHERE url=:url AND datetime >= DATE_ADD(NOW(), INTERVAL 0 MINUTE)');
$sql->bindParam(':url', $clientURL);
$sql->execute();
$count = $sql->rowCount();
if ($count == 0) {
function calculate_median($arr) {
sort($arr);
$count = count($arr); //total numbers in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
$clientBackend = $json['backend'];
$clientFrontend = $json['frontend'];
$clientTotal = $json['total'];
$ipAddress=$_SERVER['REMOTE_ADDR'];
$newjson = file_get_contents("http://ip-api.com/json/".$ipAddress);
$obj = json_decode($newjson);
$country=$obj->country;
$state=$obj->regionName;
$city=$obj->city;
$sql= $conn->prepare('INSERT INTO perf (backend, frontend, total, url, ipAddress, country, state, city) VALUES (:backend, :frontend, :total, :url, :ipAddress, :country, :state, :city)');
$sql->bindParam(':backend', $clientBackend);
$sql->bindParam(':frontend', $clientFrontend);
$sql->bindParam(':total', $clientTotal);
$sql->bindParam(':url', $clientURL);
$sql->bindParam(':ipAddress', $ipAddress);
$sql->bindParam(':country', $country);
$sql->bindParam(':state', $state);
$sql->bindParam(':city', $city);
$sql->execute();
$overallbackend = array();
$overallfrontend = array();
$overalltotal = array();
$sql= $conn->prepare('SELECT * FROM perf WHERE url=:url');
$sql->bindParam(':url', $clientURL);
$sql->execute();
while ($row=$sql->fetch()) {
$overallbackend[] = $row['backend'];
$overallfrontend[] = $row['frontend'];
$overalltotal[] = $row['total'];
}
$overallbackend = number_format((float)(calculate_median($overallbackend)), 2, '.', '');
$overallfrontend = number_format((float)(calculate_median($overallfrontend)), 2, '.', '');
$overalltotal = number_format((float)(calculate_median($overalltotal)), 2, '.', '');
$overall = array('backend' => $overallbackend, 'frontend' => $overallfrontend, 'total' => $overalltotal);
$sql= $conn->prepare('SELECT * FROM perf WHERE url=:url ORDER BY country');
$sql->bindParam(':url', $clientURL);
$sql->execute();
$rowCount=$sql->rowCount();
$countries = array();
$prevCountry = "";
$i=0;
$countrybackend = array();
$countryfrontend = array();
$countrytotal = array();
while ($row=$sql->fetch()) {
$dbCountry=$row['country'];
if ($i==0) {$prevCountry=$dbCountry;}
if (($prevCountry==$dbCountry)) {
$countrybackend[] = $row['backend'];
$countryfrontend[] = $row['frontend'];
$countrytotal[] = $row['total'];
}
elseif (($i++)==$rowCount) {
$countrybackend = number_format((float)(calculate_median($countrybackend)), 2, '.', '');
$countryfrontend = number_format((float)(calculate_median($countryfrontend)), 2, '.', '');
$countrytotal = number_format((float)(calculate_median($countrytotal)), 2, '.', '');
$countries[] = array($prevCountry => array('backend' => $countrybackend, 'frontend' => $countryfrontend, 'total' => $countrytotal));
$countrybackend = array();
$countryfrontend = array();
$countrytotal = array();
$countrybackend[] = $row['backend'];
$countryfrontend[] = $row['frontend'];
$countrytotal[] = $row['total'];
}
else {
$countrybackend = number_format((float)(calculate_median($countrybackend)), 2, '.', '');
$countryfrontend = number_format((float)(calculate_median($countryfrontend)), 2, '.', '');
$countrytotal = number_format((float)(calculate_median($countrytotal)), 2, '.', '');
$countries[] = array($prevCountry => array('backend' => $countrybackend, 'frontend' => $countryfrontend, 'total' => $countrytotal));
$countrybackend = array();
$countryfrontend = array();
$countrytotal = array();
$countrybackend[] = $row['backend'];
$countryfrontend[] = $row['frontend'];
$countrytotal[] = $row['total'];
}
$prevCountry=$dbCountry;
$i++;
}
$countrybackend = number_format((float)(calculate_median($countrybackend)), 2, '.', '');
$countryfrontend = number_format((float)(calculate_median($countryfrontend)), 2, '.', '');
$countrytotal = number_format((float)(calculate_median($countrytotal)), 2, '.', '');
$countries[] = array($prevCountry => array('backend' => $countrybackend, 'frontend' => $countryfrontend, 'total' => $countrytotal));
$sendjson = array('overall' => $overall, 'countries' => $countries);
echo json_encode($sendjson);
}
?>