-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSpeedTestViewApp.js
72 lines (65 loc) · 3.11 KB
/
SpeedTestViewApp.js
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
angular.module('SpeedTestViewApp', ['SpeedTestViewModule', 'ngDialog'])
.service('SpeedTestService', function(){
var settings = httpGet('./settings.txt?'+Date.now());
this.Settings = settings.status == 200 ? JSON.parse(settings.responseText) : {};
this.Translations = this.Settings.translations || {};
var result = httpGet('./speedtest.txt?'+Date.now());
if (result.status == 200) {
var speedResults = JSON.parse(result.responseText);
this.SpeedTestResults = SortRuleResultsByImpact(speedResults);
} else {
alert('Failed to get speed test data!');
}
this.GetKeys = GetKeys;
function SortRuleResultsByImpact(speedResults) {
for (var resultsKey in speedResults) {
if (speedResults[resultsKey] != null) {
// profiles mobile/desktop
for (var profileKey in speedResults[resultsKey]) {
// websiteKey example 'http://www.google.com/'
for (var websiteKey in speedResults[resultsKey][profileKey]) {
var sortedRules = {};
// if object is not from google speed insights
if (speedResults[resultsKey][profileKey][websiteKey].formattedResults !== undefined) {
// ruleKey example 'AvoidLandingPageRedirects'
var ruleKeys = GetKeys(speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults);
for (var j = 0; j < ruleKeys.length; j++) {
var max = undefined;
for (var i = 0; i < ruleKeys.length; i++) {
if (max === undefined || speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults[ruleKeys[i]] !== undefined && max.value !== undefined
&& max.value.ruleImpact <= speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults[ruleKeys[i]].ruleImpact) {
max = { key: ruleKeys[i], value: speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults[ruleKeys[i]] };
}
};
sortedRules[max.key] = max.value;
delete speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults[max.key];
}
speedResults[resultsKey][profileKey][websiteKey].formattedResults.ruleResults = sortedRules;
}
}
}
}
}
return speedResults;
}
function GetKeys(object) {
var keys = [];
for (var key in object) {
keys.push(key);
}
return keys;
};
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp;
}
})
.config([function(){
// console.log('Configuration');
}])
.run([function(){
// console.log('Run');
}]);