-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonitoringAPIcall.gs
176 lines (159 loc) · 6.23 KB
/
MonitoringAPIcall.gs
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
function showSidebar() {
var html = HtmlService.createTemplateFromFile('HTMLsidebar')
html.message = "none";
html = html.evaluate().setTitle('Check Select');
SpreadsheetApp.getUi().showSidebar(html);
}
function getCheckDetails(form) {
var ui = SpreadsheetApp.getUi(); // Same variations.
//Check that API textbox is populated
var apikey = form.apikey;
apikey = apikey.trim();
if (apikey == ''){
ui.alert('Please enter your API key');
return 'end';
} else {
//if API key is populated, clear this sheet and pull new data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
sheet.clear();
//call API function to pull data
sheet.appendRow(['=ImportJSON("https:\/\/monitoring-api\.rigor\.com\/v2\/checks?type=real_browser&page=1&per_page=1000&api_key=' + apikey + '", "\/checks")']);
//copy the output as values
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1, 1, lastRow, lastColumn);
range.copyTo(range, {contentsOnly:true});
//unhide next section of Textbox.html in Textbox.html
}
}
//Check that API textbox is populated
function checkTestID(form){
var ui = SpreadsheetApp.getUi(); // Same variations.
var testid = form.testid;
testid = testid.trim();
if (testid == ''){
ui.alert('Please enter your Test ID');
return 'end';
}
}
//============================UNIT TESTING
function testgetLoadTimeData(){
var form = new Object();
form.servertime = true;
form.domloadtime = true;
form.apikey ='ra2Dv8QkIJI6YGc6sfJ';
form.testid = '41291';
form.timeframe = 'custom';
form.fromDate = '2017-07-01';
form.toDate = '2017-07-07';
getFormData(form);
}
//==================================
//==========================================================================================================
//determine which load times to pull
function getFormData(form){
if(form.servertime == "true"){
getLoadTimeData(form, "server_time");
}
if(form.startrendertime == "true"){
getLoadTimeData(form, "start_render");
}
if(form.domloadtime == "true"){
getLoadTimeData(form, "dom_load_time");
}
if(form.onloadtime == "true"){
getLoadTimeData(form, "onload_time");
}
if(form.visuallycompletetime == "true"){
getLoadTimeData(form, "visually_complete");
}
if(form.fullyloadedtime == "true"){
getLoadTimeData(form, "fully_loaded_time");
}
if(form.speedindex == "true"){
getLoadTimeData(form, "speed_index");
}
}
//Get Load Time data
function getLoadTimeData(form, metric){
var ui = SpreadsheetApp.getUi(); // Same variations.
//Get API Key
var apikey = form.apikey;
apikey = apikey.trim();
//Get Test ID
var testid = form.testid;
testid = testid.trim();
//Get timeframe
var timeframe = form.timeframe;
var timeframe2 = 'range=' + form.timeframe;
var dateDiff = 4;
if (timeframe2 == 'range=custom'){
//ui.alert(form.fromDate);
timeframe2 = 'from=' + form.fromDate + '&to=' + form.toDate;
var fromDate = Date.parse(form.fromDate);
var toDate = Date.parse(form.toDate);
var timeDiff = toDate - fromDate;
dateDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
}
//Get aggregation (median vs. none)
var aggregation;
aggregation = 'median_';
if(form.timeframe == 'last_hour' || form.timeframe == 'last_4_hours' || form.timeframe == 'last_8_hours' || form.timeframe == 'last_12_hours' || form.timeframe =='last_24_hours' || dateDiff < 2){
aggregation = '';
}
//Add new sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(metric + '_' + testid + '_' + timeframe + '_' + SpreadsheetApp.getActive().getSheets().length);
var sheet = ss.getActiveSheet();
//Call Rigor API for Server Time data
var json = JSON.parse(UrlFetchApp.fetch("https://monitoring-api.rigor.com/v2/checks/real_browsers/"+ testid +"/pages/data?" + timeframe2 + "&metrics[]=" + aggregation + metric + "&include_total=true&include_failures=true&api_key=" + apikey));
var data = [];
var currentRow = 2;
//create header
var headers = [['Check ID', 'Selected From Date', 'Selected To Date', 'Selected Range', 'Metrics Name', 'Metrics Unit', 'Page ID', 'Page URL', 'Page Hostname', 'Check From Date', 'Check To Date', 'Value']];
sheet.getRange(1, 1, 1, headers[0].length).setValues(headers);
for(var seriesI = 0; seriesI < json.series.length ; seriesI++){
//access page data, assign webpage info accordingly
var rowValues = [[]];
rowValues[0].push(json.check_id);
rowValues[0].push(json.from);
rowValues[0].push(json.to);
rowValues[0].push(json.range);
rowValues[0].push(json.metrics[0].name);
rowValues[0].push(json.metrics[0].unit);
//if it's the Total row
if(json.series[seriesI].total == true){
rowValues[0].push('TOTAL');
rowValues[0].push('TOTAL');
rowValues[0].push('TOTAL');
} else{
rowValues[0].push(json.series[seriesI].page.id);
rowValues[0].push(json.series[seriesI].page.url);
rowValues[0].push(json.series[seriesI].page.hostname);
}
var numToKeep = rowValues[0].length
for(var dataI = 0; dataI < json.series[seriesI].data.length ; dataI++){
rowValues[0].push(json.series[seriesI].data[dataI].from);
rowValues[0].push(json.series[seriesI].data[dataI].to);
rowValues[0].push(json.series[seriesI].data[dataI].value);
sheet.getRange(currentRow, 1, 1, rowValues[0].length).setValues(rowValues);
rowValues[0].splice(numToKeep, rowValues[0].length - numToKeep);
currentRow++;
}
}
}
//====================================================================================================================
function deleteSheetsGAS(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var ui = SpreadsheetApp.getUi();
if(sheets.length >= 1){
var response = ui.alert('Are you sure you want to delete all data sheets?', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
for (i = 1; i < sheets.length; i++) {
ss.deleteSheet(sheets[i]);
}
}
}
}