forked from veracity-tech/csvExport
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv.js
More file actions
126 lines (118 loc) · 4.6 KB
/
csv.js
File metadata and controls
126 lines (118 loc) · 4.6 KB
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
// There is a bit of magic in the require call for a plugin.
// You can choose from a whitelist of internal system objects.
// If the required file is not in that list, it will be required from disk.
const lrsPlugin = require('./utils/plugins/lrsPlugin.js');
//This is probably not quite what you were expecting. This relates to how we sandbox plugins
const stringify = require(process.cwd() + '/plugins/csvExport/node_modules/csv-stringify')
// When requiring a node_module, if the compiled LRS uses that module, it will be returned from the compiled bundle.
// Otherwise, you'll need to make sure to npm install it alongside the plugin.
const express = require('express');
// Define the column headings and the path to the cooresponding value in the statement.
const columns = {
'Statement ID': 'id',
'Actor ID': 'actor.account.name',
'Actor Name': 'actor.name',
'Verb ID': 'verb.id',
'Activity ID': 'object.id',
'Activity Type': 'object.definition.type',
'Complete': 'result.completion',
'Success': 'result.success',
'Duration': 'result.duration',
'Response': 'result.response',
'Score': 'result.score.scaled',
'Timestamp': 'timestamp',
'Registration': 'context.registration',
'Platform': 'context.platform',
'Grouping Activity ID': 'context.contextActivities.grouping.0.id',
'Grouping Activity Type': 'context.contextActivities.grouping.0.definition.type',
'Parent Activity ID': 'context.contextActivities.parent.0.id',
'Parent Activity Type': 'context.contextActivities.parent.0.definition.type',
};
module.exports = class CSVExporter extends lrsPlugin {
constructor(lrs, dal, settings) {
super(lrs, dal, settings);
// put an export link on the LRS sidebar
this.on('lrsSidebar', (event, lrs) => ({
text: 'CSV Export',
href: this.getLink('/export', 'lrs'), // get a link to this plugin's lrs scoped router
id: this.uuid,
}));
// Set up an Express Router to handle the request
const router = express.Router();
// Get the export link
router.get('/export', async (req, res, next) => {
const connectionPool = require('./utils/connectionPool.js');
const DAL = await connectionPool.dal(this.lrs.uuid, this.lrs.strict, this.lrs.preferRead);
const Mongo = DAL.db;
const statements = Mongo.collection('statements');
const cursor = statements.find({});
const stringifier = stringify({
delimiter: ','
})
stringifier.pipe(res);
res.setHeader('Content-Disposition', 'attachment; filename="export.csv"');
// Send the column headers
stringifier.write(Object.keys(columns));
// Send rows for each statement
cursor.each((err, item) => {
if (item == null) {
return res.end();
}
const statement = item.statement;
const values = Object.values(columns).map((column) => get(column, statement));
stringifier.write(values);
});
});
// Associate the router with the plugin at the LRS level
this.setRouter('lrs', router);
}
// Metadata for display
static get display() {
return {
title: 'CSV Exporter',
description: 'A write CSV files to a HTTP stream.',
};
}
// Additional metadata for display
static get metadata() {
return {
author: 'Veracity Technology Consultants',
version: '1.0.0',
moreInfo: 'https://www.veracity.it',
};
}
// No form is necessary, since there are no per instance settings.
static get settingsForm() {
return [
];
}
};
/**
* Safely retrieves a value from a nested object following the specified path.
*
* For example, if you have the following object:
*
* const example = {
* verb: {
* id: "abc123"
* }
* }
*
* To get the verb ID, you'd typically call `example.verb.id`. However, if `verb` was null,
* `example.verb.id` would genereate an error ("cannot get property id of undefined").
* To _safely_ get that value, without first checking for its existence:
*
* get('verb.id', example)
*
* @param {string} path A dot-delimeted string describing the path to the desired value (e.g. "verb.id").
* @param {*} source The source object.
*/
function get(path, source) {
if (typeof path !== 'string' || !source) {
return null;
}
return path
.split('.')
.reduce((value, key) => value && value[key] ? value[key] : null, source);
}
module.exports.pluginName = "csvExport";