forked from urbanriskmap/cognicity-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reports.js
195 lines (168 loc) · 4.88 KB
/
Reports.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
'use strict';
// RSVP promises module
var RSVP = require('rsvp');
/**
* The Cognicity Reports data collection application.
* Uses data source plugins to retrieve data from external sources and store in the Cognicity Server
* database.
* @constructor
* @param {config} config Configuration object
* @param {object} pg Configured instance of pg object from pg module
* @param {object} logger Configured instance of logger object from Winston module
* @param {function} exitWithStatus Function to exit the process with the status code
*/
var Reports = function(
config,
pg,
logger,
exitWithStatus
){
this.config = config;
this.pg = pg;
this.logger = logger;
this.exitWithStatus = exitWithStatus;
};
Reports.prototype = {
/**
* Configuration object
* @type {config}
*/
config: null,
/**
* Configured instance of pg object from pg module
* @type {object}
*/
pg: null,
/**
* Configured instance of logger object from Winston module
* @type {object}
*/
logger: null,
/**
* Function to exit the process with the supplied status code
* @type {function}
*/
exitWithStatus: null,
/**
* The data sources used to retrieve data from external sources.
* @type {Array}
*/
_dataSources: [],
/**
* DB query success callback
* @callback DbQuerySuccess
* @param {object} result The 'pg' module result object on a successful query
*/
/**
* Execute the SQL against the database connection. Run the success callback on success if supplied.
* @param {object} config The pg config object for a parameterized query, e.g. {text:"select * from foo where a=$1", values:['bar']}
* @param {string} config.text The SQL query to execute
* @param {Array} config.values Parameterized values to use for the SQL query
* @param {DbQuerySuccess} success Callback function to execute on success.
*/
dbQuery: function(config, success){
var self = this;
self.logger.debug( "dbQuery: executing query: " + JSON.stringify(config) );
self.pg.connect(self.config.pg.conString, function(err, client, done){
if (err){
self.logger.error("dbQuery: " + JSON.stringify(config) + ", " + err);
done();
return;
}
client.query(config, function(err, result){
if (err){
self.logger.error("dbQuery: " + JSON.stringify(config) + ", " + err);
done();
return;
}
done();
self.logger.debug( "dbQuery: success: " + JSON.stringify(config) );
if (success) {
try {
success(result);
} catch(error) {
self.logger.error("dbQuery: Error in success callback: " + error.message + ", " + error.stack);
}
}
});
});
},
/**
* Verify that all necessary configuration has been supplied and is correct.
* Exit with error if not.
*/
validateConfig: function() {
var self = this;
// Fetch any validation promises from the data sources
var validationPromises = [];
self._dataSources.forEach( function(dataSource) {
if (dataSource.validateConfig) {
validationPromises.push( dataSource.validateConfig() );
}
});
// Execute all validation promises and check for any errors
RSVP.all( validationPromises )
.catch(function(err) {
self.logger.error("validateConfig: Error, " + err);
self.exitWithStatus(1);
});
},
/**
* Start collecting data.
* This will call start() on each data source.
*/
start: function() {
var self = this;
self.validateConfig();
self._dataSources.forEach( function(dataSource) {
dataSource.start();
});
},
/**
* Stop collecting data.
* This will call stop() on each data source.
*/
stop: function() {
var self = this;
// TODO Do we need to delay or check when these have finished?
self._dataSources.forEach( function(dataSource) {
if (dataSource.stop) {
dataSource.stop();
}
});
},
/**
* Enable caching mode, ask each data source to hold processing until DB is ready.
* This will call enableCacheMode() on each data source.
*/
enableCacheMode: function() {
var self = this;
self._dataSources.forEach( function(dataSource) {
if (dataSource.enableCacheMode) {
dataSource.enableCacheMode();
}
});
},
/**
* Disable caching mode, ask each data source to hold processing until DB is ready.
* This will call enableCacheMode() on each data source.
*/
disableCacheMode: function() {
var self = this;
self._dataSources.forEach( function(dataSource) {
if (dataSource.disableCacheMode) {
dataSource.disableCacheMode();
}
});
},
/**
* Add the supplied data source to the reports object's list of data sources.
* @param {BaseDataSource} Data source to add.
*/
addDataSource: function(dataSource) {
var self = this;
self._dataSources.push( dataSource );
}
};
// Export the Reports constructor
module.exports = Reports;