-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·372 lines (339 loc) · 11.8 KB
/
cli.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env node
/**
* CLI App Entry Point.
*/
/**
* Module dependencies.
*/
const tangerine = require('commander');
const chalk = require('chalk');
/**
* Local Dependencies.
*/
const createColumnHeaders = require('./controllers/assessment').createColumnHeaders;
const processAssessmentResult = require('./controllers/result').generateResult;
const createWorkflowHeaders = require('./controllers/workflow').createWorkflowHeaders;
const processWorkflowResult = require('./controllers/trip').processWorkflowResult;
const generateCSV = require('./controllers/generate_csv').generateCSV;
const dbQuery = require('./utils/dbQuery');
const dbConfig = require('./config');
/******************************************
* HELPER FUNCTIONS FOR GENERATING AND *
* SAVING HEADERS AND RESULTS *
******************************************
*/
/**
* This function creates and saves assessment headers.
*
* @param {Array} data - database documents to be processed.
*
* @returns {Object} - couchDB save response
*/
async function generateAssessmentHeaders(data) {
let response;
for (item of data) {
let assessmentId = item.doc.assessmentId;
let generatedHeaders = await createColumnHeaders(item.doc, 0, dbConfig.base_db);
response = await dbQuery.saveHeaders(generatedHeaders, assessmentId, dbConfig.result_db);
console.log(response);
}
return response;
}
/**
* This function creates and saves workflow headers.
*
* @param {Array} data - database documents to be processed.
*
* @returns {Object} - couchDB save response
*/
async function generateworkflowHeaders(data) {
let response;
for (item of data) {
let workflowId = item.id;
let generatedWorkflowHeaders = await createWorkflowHeaders(item.doc, dbConfig.base_db);
response = await dbQuery.saveHeaders(generatedWorkflowHeaders, workflowId, dbConfig.result_db);
console.log(response);
}
return response;
}
/**
* This function creates and saves assessment results.
*
* @param {Array} data - database documents to be processed.
*
* @returns {Object} - couchDB save response
*/
async function generateAssessmentResult(data) {
let response;
for (item of data) {
let docId = item.assessmentId || item.curriculumId;
let ref = item._id;
let processedResult = await processAssessmentResult(docId, 0, dbConfig.base_db);
response = await dbQuery.saveResult(processedResult, ref, dbConfig.result_db);
console.log(response);
}
return response;
}
/**
* This function creates and saves workflow results.
*
* @param {Array} data - database documents to be processed.
*
* @returns {Object} - couchDB save response
*/
async function generateWorkflowResult(data) {
let response;
for (item of data) {
let resultDoc = [{ doc: item }];
if (!item.tripId) {
let docId = item.assessmentId || item.curriculumId;
let assessmentResults = await processResult(resultDoc);
response = await dbQuery.saveResult(assessmentResults, resultDbUrl);
console.log(response);
} else {
let processedResult = await processWorkflowResult(resultDoc);
response = await dbQuery.saveResult(processedResult, resultDbUrl);
console.log(response);
}
}
return response;
}
/**********************
* CLI APPLICATION *
**********************
*/
/**
* This part retrieves all assessments in the database.
* It is executed when the command `tangerine-reporting assessments` is run.
*/
tangerine
.version('0.1.0')
.command('assessments')
.description('Retrieves all assessments in the database')
.action(async() => {
const db = dbConfig.base_db;
console.log(await dbQuery.getAllAssessment(db));
console.log(chalk.green('✓ Successfully retrieve all assessments'));
});
/**
* This part retrieves all workflow documents in the database.
* It is executed when the command `tangerine-reporting workflows` is run.
*/
tangerine
.version('0.1.0')
.command('workflows')
.description('Retrieves all workflows in the database')
.action(async() => {
const db = dbConfig.base_db;
console.log(await dbQuery.getAllWorkflow(db));
console.log(chalk.green('✓ Successfully retrieve all workflows'));
});
/**
* This part retrieves all result documents in the database.
* It is executed when the command `tangerine-reporting results` is run.
*/
tangerine
.version('0.1.0')
.command('results')
.description('Retrieves all results in the database')
.action(async() => {
const db = dbConfig.base_db;
console.log(await dbQuery.getAllResult(db));
console.log(chalk.green('✓ Successfully retrieve all results'));
});
/**
* This part generates an assessment header.
* It is executed when the command `tangerine-reporting assessment-header <assessment_id>` is run.
*
* @param {string} id - assessment id is required.
*/
tangerine
.version('0.1.0')
.command('assessment-header <id>')
.description('generate header for an assessment')
.action((id) => {
dbQuery.retrieveDoc(id, dbConfig.base_db)
.then(async(data) => {
const docId = data.assessmentId || data.curriculumId;
const colHeaders = await createColumnHeaders(data, 0, dbConfig.base_db);
const saveResponse = await dbQuery.saveHeaders(colHeaders, docId, dbConfig.result_db);
console.log(saveResponse);
console.log(chalk.green('✓ Successfully generate and save assessment header'));
})
.catch((err) => Error(err));
});
/**
* This part processes an assessment result.
* It is executed when the command `tangerine-reporting assessment-result <assessment_id>` is run.
*
* @param {string} id - assessment id is required.
*/
tangerine
.version('0.1.0')
.command('assessment-result <id>')
.description('process result for an assessment')
.action((id) => {
dbQuery.retrieveDoc(id, dbConfig.base_db)
.then(async(data) => {
let resultDoc = { doc: data };
const result = processAssessmentResult(resultDoc);
const saveResponse = await dbQuery.saveResult(result, dbConfig.result_db);
console.log(saveResponse);
console.log(chalk.green('✓ Successfully process and save assessment result'));
})
.catch((err) => Error(err));
});
/**
* This part generates a workflow header.
* It is executed when the command `tangerine-reporting workflow-header <workflow_id>` is run.
*
* @param {string} id - workflow id is required.
*/
tangerine
.version('0.1.0')
.command('workflow-header <id>')
.description('generate headers for a workflow')
.action((id) => {
dbQuery.retrieveDoc(id, dbConfig.base_db)
.then(async(doc) => {
let colHeaders = await createWorkflowHeaders(doc, dbConfig.base_db);
const saveResponse = await dbQuery.saveHeaders(colHeaders, id, dbConfig.result_db);
console.log(saveResponse);
console.log(chalk.green('✓ Successfully generate and save workflow header'));
})
.catch((err) => Error(err));
});
/**
* This part processes a workflow result.
* It is executed when the command `tangerine-reporting workflow-result <workflow_id>` is run.
*
* @param {string} id - workflow id is required.
*/
tangerine
.version('0.1.0')
.command('workflow-result <id>')
.description('process result for a workflow')
.action(function(id) {
dbQuery.getResults(id, dbConfig.base_db)
.then(async(data) => {
const result = processWorkflowResult(data);
const saveResponse = await dbQuery.saveResult(result, dbConfig.result_db);
console.log(saveResponse);
console.log(chalk.green('✓ Successfully process and save workflow result'));
})
.catch((err) => Error(err));
});
/**
* This part processes headers or results based on the flag passed to it.
*
* It is executed when the command `tangerine-reporting create-all [flags]` is run.
* The various flags are required for this to execute.
* E.g. run `tangerine-reporting create-all -w` => to generate all workflow headers in the database
*/
tangerine
.version('0.1.0')
.command('create-all')
.description('create headers or results based on the collection type')
.option('-a', '--assessment', 'create all assessment headers')
.option('-r', '--result', 'process all assessment results')
.option('-w', '--workflow', 'create all workflow headers')
.option('-t', '--workflow-result', 'process all workflow results')
.action((options) => {
if (!options.A && !options.R && !options.W && !options.T) {
console.log(chalk.red('Please select a flag either "-a", "-r", "-t", or "-w" flag along with your command. \n'));
}
if (options.A) {
dbQuery.getAllAssessment(dbConfig.base_db)
.then(async(data) => {
await generateAssessmentHeaders(data);
console.log(chalk.green('✓ Successfully generate and save all assessment headers'));
}).catch((err) => Error(err));
}
if (options.W) {
dbQuery.getAllWorkflow(dbConfig.base_db)
.then(async(data) => {
await generateworkflowHeaders(data);
console.log(chalk.green('✓ Successfully generate and save all workflow headers'));
}).catch((err) => Error(err));
}
if (options.R) {
dbQuery.getAllResult(dbConfig.base_db)
.then(async(data) => {
await generateAssessmentResult(data);
console.log(chalk.green('✓ Successfully processe and save all assessment results'));
}).catch((err) => Error(err));
}
if (options.T) {
dbQuery.getAllWorkflow(dbConfig.base_db)
.then(async(data) => {
await generateWorkflowResult(data);
console.log(chalk.green('✓ Successfully generate and save all workflow results'));
}).catch((err) => Error(err));
}
}).on('--help', function() {
console.log(chalk.blue('\n Examples: \n'));
console.log(chalk.blue(' $ tangerine-reporting create-all -a'));
console.log(chalk.blue(' $ tangerine-reporting create-all -r'));
console.log(chalk.blue(' $ tangerine-reporting create-all -t'));
console.log(chalk.blue(' $ tangerine-reporting create-all -w \n'));
});
/**
* This part generates a csv file.
* It is executed when the command `tangerine-reporting generate-csv <docId>` is run.
*
* @param {string} docId - workflow id of the document
*/
tangerine
.version('0.1.0')
.command('generate-csv <docId>')
.description('creates a csv file')
.action((docId) => {
dbQuery.retrieveDoc(docId, dbConfig.result_db)
.then(async(docHeaders) => {
const result = await dbQuery.getProcessedResults(docId, dbConfig.result_db);
await generateCSV(docHeaders, result);
console.log(chalk.green('✓ CSV Successfully Generated'));
})
.catch((err) => Error(err));
});
/**
* This part retrieves a document from the database.
* It is executed when the command `tangerine-reporting get <id>` is run.
*
* @param {string} id - id of the document
*/
tangerine
.version('0.1.0')
.command('get <id>')
.description('retrieve a document from the database')
.action(function(id) {
dbQuery.retrieveDoc(id, dbConfig.base_db)
.then((data) => {
console.log(data);
console.log(chalk.green('✓ Successfully get document'));
})
.catch((err) => Error(err));
});
/**
* This part retrieves all result by tripId.
* It is executed when the command `tangerine-reporting get-result <id>` is run.
*
* @param {string} id - id of the result document
*/
tangerine
.version('0.1.0')
.command('get-processed-result <id>')
.description('retrieve all processed results by id from the database')
.action(function(id) {
dbQuery.getProcessedResults(id, dbConfig.result_db)
.then((data) => {
console.log(data);
console.log(chalk.green('✓ Successfully retrieved all processed results'));
})
.catch((err) => Error(err));
});
tangerine.parse(process.argv);
if (process.argv.length === 0) {
tangerine.help();
}
console.log(chalk.green('✓ Tangerine Reporting CLI Tool'));