forked from project-sunbird/sunbird-report-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (224 loc) · 8.4 KB
/
index.js
File metadata and controls
261 lines (224 loc) · 8.4 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
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
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
var memoryCache = require('memory-cache');
var debug = require('debug')('parameters:index');
const { getSharedAccessSignature } = require('../../helpers/azure-storage');
const { envVariables } = require('../../helpers/envHelpers');
const { isUserSuperAdmin } = require('../../helpers/userHelper');
const CONSTANTS = require('../../resources/constants.json');
const { channelRead, frameworkRead } = require('../../helpers/learnerHelper');
/*
Each parameter file follows the given interface.
name : string
value: (user) => string
masterData: () => Array<string>
cache: boolean - whether to store masterData in mem cache or not
*/
const basename = path.basename(__filename);
const parameters = {};
//read all the parameter files inside the folder and build the parameters object;
((folderPath) => {
fs.readdirSync(folderPath)
.filter(file => file !== basename)
.forEach(file => {
const { name, ...rest } = require(path.join(folderPath, file));
parameters[name] = rest;
});
})(__dirname);
/**
@description check if the report is parameterized or not
*/
const isReportParameterized = (report) => _.get(report, 'parameters.length') > 0 && _.isArray(report.parameters);
/**
@description convert a string into base64
*/
const getHashedValue = (val) => Buffer.from(val).toString('base64');
/**
@description read the parameter value from the user context. refer to value function in params object
*/
const getParameterValue = (param, user) => {
if (param in parameters) {
return parameters[param].value(user);
}
return null;
};
/**
@description generate hash from parameters value of report
*/
const getParametersHash = (report, user) => {
const parameters = _.get(report, 'parameters');
const result = _.map(parameters, param => {
const userParamValue = getParameterValue(_.toLower(param), user);
if (!userParamValue) return null;
if (!_.isArray(userParamValue)) return getHashedValue(userParamValue);
return _.map(userParamValue, val => getHashedValue(val));
});
return _.flatMap(_.compact(result));
};
/**
@description populate report with parameter values
*/
const populateReportsWithParameters = (reports, user) => {
return _.reduce(reports, (results, report) => {
const isParameterized = isReportParameterized(report);
report.isParameterized = isParameterized;
if (isParameterized) {
if (user) {
const hash = getParametersHash(report, user);
if (isUserSuperAdmin(user)) {
results.push(report);
} else {
const childReports = _.uniqBy(_.concat(_.filter(_.get(report, 'children'), child => hash.includes(_.get(child, 'hashed_val'))),
_.map(hash, hashed_val => ({
hashed_val,
status: CONSTANTS.REPORT_STATUS.DRAFT,
reportid: _.get(report, 'reportid'),
materialize: true
}))), 'hashed_val');
if (childReports.length) {
if (childReports.length === 1) {
const mergedReport = _.assign(report.dataValues, _.pick(_.get(childReports, '[0]'), ['status', 'hashed_val']));
results.push(mergedReport);
} else {
report.children = childReports;
results.push(report);
}
}
}
}
}
else {
results.push(report);
}
return results;
}, []);
};
const getParameterFromPath = path => {
const existingParameters = Object.keys(parameters);
for (let param of existingParameters) {
if (_.includes(path, param)) {
return param;
}
}
return null;
};
const generateSasPredicate = ({ parameter, path, dataset }) => value => {
const resolvedPath = path.replace(parameter, value);
const promise = getSharedAccessSignature({ filePath: resolvedPath })
.then(({ sasUrl, expiresAt }) => ({ key: value, sasUrl, expiresAt }))
.catch(_ => ({ key: value, sasUrl: null, expiresAt: null }))
.then(data => {
const { key, sasUrl, expiresAt } = data;
dataset.data.push({
id: key,
type: parameter,
url: sasUrl,
expiresAt
});
});
return promise;
};
const getDataset = async ({ dataSource, user, req }) => {
let { id, path } = dataSource;
try {
const dataset = { dataset_id: id, isParameterized: false, parameters: null, data: [] };
// for backward compatibility
if (typeof path === 'string' && path.startsWith('/reports/fetch/')) {
path = path.replace('/reports/fetch/', '');
}
const parameter = getParameterFromPath(path);
if (parameter) {
dataset.isParameterized = true;
dataset.parameters = [parameter];
const { masterData, cache = false, value } = parameters[parameter];
const resolvedValue = value(user);
debug(parameter, 'Resolved Value', JSON.stringify(resolvedValue));
let masterDataForParameter;
if (isUserSuperAdmin(user)) {
//if the user is super REPORT_ADMIN then return all the masterData;
//get the master data from memory cache is available else call the master data fetch API for the parameter.
const cachedData = memoryCache.get(parameter);
debug(parameter, 'Cached Data', JSON.stringify(cachedData));
if (false && cachedData && cache) {
masterDataForParameter = cachedData;
} else {
masterDataForParameter = await masterData({ user, req });
debug(parameter, 'Master Data', JSON.stringify(masterDataForParameter));
memoryCache.put(parameter, masterDataForParameter, envVariables.MEMORY_CACHE_TIMEOUT);
}
} else {
//if the user is not super REPORT_ADMIN then return only the resolved parameter data;
masterDataForParameter = resolvedValue && (Array.isArray(resolvedValue) ? resolvedValue : [resolvedValue]);
}
if (Array.isArray(masterDataForParameter) && masterDataForParameter.length) {
await Promise.all(masterDataForParameter.map(generateSasPredicate({ parameter, path, dataset })));
}
} else {
const { sasUrl, expiresAt } = await getSharedAccessSignature({ filePath: path }).catch(error => null);
dataset.data = [{
id: 'default',
type: null,
url: sasUrl,
expiresAt
}];
}
return dataset;
} catch (error) {
return { dataset_id: id, data: [], parameters: null, isParameterized: false };
}
};
const getDatasets = async ({ document, user, req }) => {
let dataSources = _.get(document, 'reportconfig.dataSource');
if (!dataSources) return [];
dataSources = Array.isArray(dataSources) ? dataSources : [dataSources];
return Promise.all(dataSources.map(dataSource => getDataset({ dataSource, user, req })));
};
const setFrameworkCategoryParameters = async (req, user) => {
const channelId =
req.get('x-channel-id') ||
req.get('X-CHANNEL-ID') ||
_.get(user, 'rootOrg.hashTagId') ||
_.get(user, 'channel');
if (!channelId) {
const error = new Error('Channel ID is required');
error.statusCode = 400;
error.errorObject = { code: 'MISSING_CHANNEL_ID' };
throw error;
}
try {
const channelReadResponse = await channelRead({ channelId });
const frameworkName = _.get(channelReadResponse, 'data.result.channel.defaultFramework');
if (!frameworkName) {
const error = new Error('Default framework not found for the channel');
error.statusCode = 404;
error.errorObject = { code: 'MISSING_DEFAULT_FRAMEWORK' };
throw error;
}
const frameworkReadResponse = await frameworkRead({ frameworkId: frameworkName });
const frameworkData = _.get(frameworkReadResponse, 'data.result.framework');
const frameworkCategories = _.map(frameworkData.categories, 'code');
frameworkCategories.forEach(category => {
parameters[`$${category}`] = {
name: `$${category}`,
value: (user) => _.get(user, `framework.${category}`),
cache: false,
masterData: () => {
const categoryData = _.find(frameworkData.categories, ['code', category]);
return _.map(_.get(categoryData, 'terms', []), 'name');
}
};
});
Object.values(parameters).forEach(param => {
});
} catch (error) {
debug(`Failed to set framework category parameters for channel ${channelId}`, error);
}
};
module.exports = {
populateReportsWithParameters,
getDatasets,
reportParameters: parameters,
isReportParameterized,
setFrameworkCategoryParameters
};