forked from gw/decidekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·121 lines (106 loc) · 3.37 KB
/
app.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
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// load environment properties from a .env file for local development
require('dotenv').load({silent: true});
var express = require('express'),
app = express();
// Bootstrap application settings
require('./config/express')(app);
//integration with tradeoff analytics service
var tradeoffAnalyticsConfig = require('./config/tradeoff-analytics-config');
tradeoffAnalyticsConfig.setupToken(app, {//for dev purposes. in bluemix it is taken from VCAP.
url: process.env.TA_URL || 'https://gateway.watsonplatform.net/tradeoff-analytics/api/v1',
username: process.env.TA_USERNAME,
password: process.env.TA_PASSWORD,
version: 'v1'
});
app.get('/', function(req, res) {
res.render('index', {
ct: req._csrfToken,
GOOGLE_ANALYTICS_ID: process.env.GOOGLE_ANALYTICS_ID
});
});
app.get('/refresh', function(req, res) {
refreshData();
res.writeHead(200);
res.end();
});
app.get('/last_refresh', function(req, res) {
lastRefresh(function(time){
res.writeHead(200, { 'Content-Type': 'text/plain'});
res.write(time.toJSON());
res.end();
});
});
var FILE_RAW = 'config/edmunds/cars_raw.json';
var FILE_PROBLEM = './public/data/auto.json';
var edmunds = require('./config/edmunds/Edmunds');
var fs = require('fs');
var SECOND = 1000,
MINUTE= 60*SECOND,
HOUR = 60*MINUTE;
var MAX_TIME_BETWEEN_IMPORTS = 24*HOUR;
var TIME_BETWEEN_CHECKS = 1*HOUR;
var refreshing= false;
function checkForRefresh(){
lastRefresh(function(lastImportTime){
var duration = (new Date() - lastImportTime);
if(duration>MAX_TIME_BETWEEN_IMPORTS && !refreshing){
refreshData();
}
});
}
function refreshData(){
if(refreshing){
return;
}
var startTime = Date.now();
refreshing = true;
function onFailure(err){
console.log('import failed. \n'+ err);
refreshing = false;
}
try{
edmunds.importEdmunds(function(data){// brings the data from RAW file instead from API
fs.writeFile(FILE_RAW, JSON.stringify(data, null, 2));
// var data= JSON.parse(fs.readFileSync(FILE_RAW));
edmunds.mapEdmunds(data, function(problem){
fs.writeFile(FILE_PROBLEM, JSON.stringify(problem, null, 2));
refreshing = false;
var duration = (Date.now() - startTime),
m= Math.floor(duration/MINUTE),
s= Math.floor((duration-m*MINUTE)/SECOND);
console.log("Duration: "+m+"M:"+s+"s");
});
}, onFailure);
}catch(e){
onFailure(e);
}
//the server will retry in the next check interval;
}
setInterval(checkForRefresh, TIME_BETWEEN_CHECKS);
function lastRefresh(callback){
fs.stat(FILE_PROBLEM, function(err, stats){
if(stats){//file exist
callback(new Date(stats.mtime));
}else{
callback(new Date(0));
}
});
}
checkForRefresh();
module.exports = app;