-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetupApplication.js
267 lines (235 loc) · 8.16 KB
/
setupApplication.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Creates the flowthings.io application on startup
*/
var Promise = require('bluebird'),
flowthings = require('flowthings'),
http = require('http'),
util = require('util'),
moment = require('moment'),
restClient = require('./gamiRESTClient.js'),
fs = require('fs'),
path = require('path');
var gamificationProxySetup = false;
module.exports = function() {
return {
/**
* First time setup of the flowthings Flows, Tracks, Devices.
*/
setupApplication: function(res, creds, gamificationDetails, f) {
var dateFormat = "YYYYMMDD";
var todaysDate = moment().startOf('day');
// Create the IBM Gamification user
var path = "/service/plan/" + gamificationDetails.planName + "/user?key=" + gamificationDetails.key +
"&tenantId=" + gamificationDetails.tenantId;
var options = {
hostname: gamificationDetails.host,
path: path,
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
var req = http.request(options, function(response) {
createGamificationEvent(gamificationDetails);
var appDetails = {
account: creds.account,
gamificationHost: gamificationDetails.host,
gamificationKey: gamificationDetails.key,
gamificationTenantId: gamificationDetails.tenantId,
gamificationUserId: creds.account,
gamificationPlanName: gamificationDetails.planName
};
var api = flowthings.API(creds, {
transform: flowthings.promisify(Promise)
});
/*
* Create the base Flow
*/
console.log("Setting up Flow App");
api.flow.create({
path: util.format("/%s/coaster", creds.account)
})
/*
* Create a "Device" object. This will set up input and output endpoints,
* a secure token, and management / monitoring tools
*/
.then(function(flow) {
console.log("Created Root Flow");
return api.device.create({
displayName: "Coaster",
path: util.format("/%s/coaster", creds.account)
});
})
/*
* Get the restricted token string. We'll need that to read data
*/
.then(function(device) {
console.log("Created Device");
appDetails.deviceId = device.id;
return api.token.read(device.tokenId);
})
/*
* Create a Flow to store the total amount drunk, per day
*/
.then(function(token) {
console.log("Got Device Token");
appDetails.tokenString = token.tokenString;
return api.flow.create({
path: util.format("/%s/coaster/total_consumed", creds.account)
});
})
/*
* Create some sample data
*/
.then(function(flow) {
console.log("Created Total Flow");
appDetails.totalFlowId = flow.id;
var date = todaysDate.format(dateFormat);
return api.drop(appDetails.totalFlowId).create({
fhash: date,
elems: {
day: date,
total: 200,
previousTotal: 150
}
});
}).then(function(drop) {
todaysDate.subtract(1, 'day');
var date = todaysDate.format(dateFormat);
return api.drop(appDetails.totalFlowId).create({
fhash: date,
elems: {
day: date,
total: 450
}
});
}).then(function(drop) {
todaysDate.subtract(1, 'day');
var date = todaysDate.format(dateFormat);
return api.drop(appDetails.totalFlowId).create({
fhash: date,
elems: {
day: date,
total: 250
}
});
}).then(function(drop) {
todaysDate.subtract(1, 'day');
var date = todaysDate.format(dateFormat);
return api.drop(appDetails.totalFlowId).create({
fhash: date,
elems: {
day: date,
total: 150
}
});
})
/*
* Create a Flow to store the latest reading from the Coaster
*/
.then(function(drop) {
console.log("Created Sample Drops");
return api.flow.create({
path: util.format("/%s/coaster/latest", creds.account),
capacity: 1
});
})
/*
* Create the application processing logic (Track).
*/
.then(function(flow) {
console.log("Created Latest Flow");
appDetails.latestFlowId = flow.id;
res.render('track', appDetails, function(err, trackText) {
console.log("Got Track JS");
res.render('metadata', appDetails, function(err, metadataText) {
console.log("Got Track Metadata");
api.track.create({
metadata: JSON.parse(metadataText),
js: trackText,
source: util.format("/%s/coaster/default", creds.account)
}).then(function(track) {
console.log("Created Track");
appDetails.trackId = track.id;
// Saving all the config locally
fs.writeFile("appConfig.json", JSON.stringify(appDetails, null, '\t'), function(err) {
if (err) {
return console.log(err);
}
console.log("Saved!");
});
f(appDetails);
}).catch(function(err) {
console.log("Error creating Track: ", err);
});
});
});
})
.catch(function(err) {
console.log("Error creating Application:", err);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(JSON.stringify({
"uid": creds.account,
"firstName": gamificationDetails.firstName,
"lastName": gamificationDetails.lastName
}));
req.end();
},
getAppConfig: function(app, notreadyf, readyf) {
fs.exists('appConfig.json', function(exists) {
try {
var appDetails = require("./appConfig.json");
/**
* Set up IBM Gamification Proxy, once.
*/
if (!gamificationProxySetup) {
var restConf = restClient.config({
gamiHost: appDetails.gamificationHost,
tenantId: appDetails.gamificationTenantId,
planName: appDetails.gamificationPlanName,
key: appDetails.gamificationKey,
getLoginUid: function(req) {
return appDetails.account;
}
});
console.log("Setting up Gamification: " + JSON.stringify(restConf, null, '\t'));
app.get("/proxy/*", restClient.proxy(restConf));
app.post("/proxy/*", restClient.proxy(restConf));
gamificationProxySetup = true;
}
readyf(appDetails);
} catch (err){
notreadyf();
}
});
}
};
};
function createGamificationEvent(gamificationDetails){
console.log("Creating Gamification Event");
var path = "/service/plan/" + gamificationDetails.planName + "/event?key=" + gamificationDetails.key +
"&tenantId=" + gamificationDetails.tenantId;
var options = {
hostname: gamificationDetails.host,
path: path,
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
var req = http.request(options, function(response) {});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(JSON.stringify({
"name" : "dailytargethit",
"impacts" : [
{"target" : "vars.xp.value", "formula" : "vars.xp.value+1"}
]
}));
req.end();
}