-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
223 lines (199 loc) · 7.06 KB
/
server.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
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
var express = require('express');
var app = express();
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var request = require('request');
var google = require('googleapis');
var moment = require('moment');
var readProperties = require('./services/readPropertiesService.js')();
// configuration
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended': 'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.use(methodOverride());
// routes =================================
app.get("/api/getSummary", function(req, res){
var options = {
url: "https://app.beckon.com/rest/a/QA/datasummary/basic",
method: "GET",
headers: {
Cookie: readProperties["com.beckon.jSessionId"],
"X-XSRF-TOKEN": readProperties["com.beckon.xsrfToken"]
}
};
request(options, function(error, response, body){
if(error){
console.log(error);
}
res.send(body);
});
});
app.get("/api/dashboards", function(req, res){
var options = {
url: "https://app.beckon.com/rest/a/QA/dashboard/list",
method: "GET",
headers: {
Cookie: readProperties["com.beckon.jSessionId"],
"X-XSRF-TOKEN": readProperties["com.beckon.xsrfToken"],
"Content-Type": "application/json",
"Accept": "application/json, text/plain, */*"
}
};
request(options, function(error, response, body){
if(error){
console.log(error);
}
res.send(body);
});
});
app.get("/api/scorecards", function(req, res){
var options = {
url: "https://app.beckon.com/rest/a/QA/scorecards",
method: "GET",
headers: {
Cookie: readProperties["com.beckon.jSessionId"],
"X-XSRF-TOKEN": readProperties["com.beckon.xsrfToken"],
"Content-Type": "application/json",
"Accept": "application/json, text/plain, */*"
}
};
request(options, function(error, response, body){
if(error){
console.log(error);
}
res.send(body);
});
});
app.get("/googleAnalytics", function (req, res) {
var newUsers, usageData, mostPopularDayOfWeek;
getNewUsers(function (err, data) {
getMostPopularDayOfWeek(function (err, data) {
getUsageStats(function (err, data) {
if (err) {
return console.log('An error occurred', err);
}
var usersWithUsage = data.rows,
totalHoursSpent = data.totalsForAllResults["ga:sessionDuration"];
usersWithUsage.sort(function (a, b) {
return b[1] - a[1];
});
var nonSupportUsers = usersWithUsage.filter( function (user) {
return user[0] !== "[email protected]";
});
usageData = {
topUsers: nonSupportUsers,
totalHoursSpent: totalHoursSpent
};
return res.send({
newUsers: newUsers,
mostAvidUsers: usageData.topUsers,
totalHoursSpent: usageData.totalHoursSpent,
mostPopularDayOfWeek: mostPopularDayOfWeek
});
});
if (err) {
return console.log('An error occurred', err);
}
var usageDates = data.rows;
var dayTallies = [
{day: "Monday", time: 0},
{day: "Tuesday", time: 0},
{day: "Wednesday", time: 0},
{day: "Thursday", time: 0},
{day: "Friday", time: 0},
{day: "Saturday", time: 0},
{day: "Sunday", time: 0}
];
for (var i = 0; i < usageDates.length; i++ ) {
var day = moment(usageDates[i][0], "YYYYMMDD").format('dddd');
for (var j = 0; j < dayTallies.length; j++ ) {
if (dayTallies[j].day === day) {
dayTallies[j].time += parseInt(usageDates[i][1]);
}
}
}
dayTallies.sort(function (a, b) {
return b.time - a.time;
});
mostPopularDayOfWeek = dayTallies;
});
if (err) {
return console.log('An error occurred', err);
}
return newUsers = data.totalResults;
});
});
// google analytics ==============================================
var OAuth2Client = google.auth.OAuth2,
analytics = google.analytics('v3'),
CLIENT_ID = readProperties["CLIENT_ID"],
CLIENT_SECRET = readProperties["CLIENT_SECRET"],
REDIRECT_URL = 'http://localhost:7060/oauthcallback',
oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
// retrieve an access token
function getAccessToken () {
// generate consent page url
var url = oauth2Client.generateAuthUrl({
access_type: 'offline', // will return a refresh token
scope: 'https://www.googleapis.com/auth/analytics' // can be a space-delimited string or an array of scopes
});
console.log('Visit the url: ', url);
}
getAccessToken();
// GA calls
function getNewUsers(callback) {
analytics.data.ga.get({
auth: oauth2Client,
'ids': 'ga:59525543',
"start-date": "2016-03-02",
"end-date": "2017-03-02",
'metrics': 'ga:newUsers',
'dimensions': 'ga:dimension2',
'filters':'ga:dimension1==QA'
}, callback);
}
function getMostPopularDayOfWeek(callback) {
analytics.data.ga.get({
auth: oauth2Client,
'ids': 'ga:59525543',
"start-date": "2016-03-02",
"end-date": "2017-03-02",
'metrics': 'ga:sessionDuration',
'dimensions': 'ga:date',
'filters':'ga:dimension1==QA'
}, callback);
}
function getUsageStats(callback) {
analytics.data.ga.get({
auth: oauth2Client,
'ids': 'ga:59525543',
"start-date": "2016-03-02",
"end-date": "2017-03-02",
'metrics': 'ga:sessionDuration',
'dimensions': 'ga:dimension2',
'filters':'ga:dimension1==QA'
}, callback);
}
//called after successfully authenticating
app.get("/oauthcallback", function (req, res) {
if (req.query) {
oauth2Client.getToken(req.query.code, function (err, tokens) {
if (err) {
return console.log(err);
}
// set tokens to the client
// TODO: tokens should be set by OAuth2 client.
oauth2Client.setCredentials(tokens);
});
}
res.sendFile(__dirname + '/public/index.html');
});
// application -------------------------------------
app.get("/home", function(req,res){
res.sendFile(__dirname + '/public/index.html');
});
app.listen(7060);
console.log('App listening on port 7060');