This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfetchMetadata.js
135 lines (119 loc) · 5.56 KB
/
fetchMetadata.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
const enigma = require('enigma.js');
const WebSocket = require('ws');
const path = require('path');
const fs = require('fs')
const promise = require('bluebird');
const qrsInteract = require('qrs-interact');
const config = require('./config/config.js');
// load libraries
const schema = require(config.engine.schemaPath);
const stringExtensions = require('./lib/stringExtensions');
const writeHeaders = require('./lib/writeHeaders');
const sysInfo = require('./lib/getSysInfo');
const customPropertyDefinitions = require('./lib/getCustomPropertyDefinitions');
const entityCustomPropertyValues = require('./lib/getCustomPropertiesForEntity');
const logLevels = require('./lib/getLogLevels');
const users = require('./lib/getUsers');
const sheets = require('./lib/getSheets');
const apps = require('./lib/getApps');
const appsEngine = require('./lib/getAppsEngine');
var writeCSV = require('./lib/writeCSV');
stringExtensions();
// repository connection setup
var qrsConfig = {
hostname: config.global.hostname,
certificates: {
certFile: (path.isAbsolute(config.global.certificatesPath) ? config.global.certificatesPath : path.join(__dirname, config.global.certificatesPath)) + "client.pem",
keyFile: (path.isAbsolute(config.global.certificatesPath) ? config.global.certificatesPath : path.join(__dirname, config.global.certificatesPath)) + "client_key.pem"
}
}
var qrsInteractInstance = new qrsInteract(qrsConfig);
// Helper function to read the contents of the certificate files:
const readCert = filename => fs.readFileSync((path.isAbsolute(config.global.certificatesPath) ? config.global.certificatesPath : path.join(__dirname, config.global.certificatesPath)) + filename);
// App specific session func
function createSession(appId) {
return enigma.create({
schema,
url: `wss://${config.global.hostname}:${config.engine.port}/app/${appId}`,
createSocket: url => new WebSocket(url, {
ca: [readCert('root.pem')],
key: readCert('client_key.pem'),
cert: readCert('client.pem'),
headers: {
'X-Qlik-User': `UserDirectory=${encodeURIComponent(config.global.userDirectory)}; UserId=${encodeURIComponent(config.global.userId)}`,
},
rejectUnauthorized: false
}),
});
}
// session app object to reuse later
var sessionObjectParams = {
"qInfo": {
"qType": "SheetList"
},
"qAppObjectListDef": {
"qType": "sheet",
"qData": {
"title": "/qMetaDef/title",
"description": "/qMetaDef/description",
"thumbnail": "/thumbnail",
"cells": "/cells",
"rank": "/rank",
"columns": "/columns",
"rows": "/rows"
}
}
};
// Start making requests and building metadata files
// create output folder if it doesn't exist
try {
fs.mkdirSync(config.filenames.outputDir);
} catch (err) {
console.log("Output folder already created.");
}
// delete all files in folder
var files = fs.readdirSync(config.filenames.outputDir)
files.forEach(function(file) {
fs.unlinkSync(path.join(config.filenames.outputDir, file));
});
writeHeaders.writeAllHeaders(config.filenames.outputDir);
var systemInfoPath = path.join(config.filenames.outputDir, config.filenames.systemInfo_table);
sysInfo.writeToFile(qrsInteractInstance, systemInfoPath);
var customPropertyDefinitionPath = path.join(config.filenames.outputDir, config.filenames.customPropertyDefinitions_table);
customPropertyDefinitions.writeToFile(qrsInteractInstance, customPropertyDefinitionPath);
var customPropertiesPath = path.join(config.filenames.outputDir, config.filenames.entityCustomPropertyMap_table);
entityCustomPropertyValues.writeToFile(qrsInteractInstance, "app", customPropertiesPath);
var logLevelsPath = path.join(config.filenames.outputDir, config.filenames.logLevel_table);
logLevels.writeToFile(qrsInteractInstance, logLevelsPath);
var usersPath = path.join(config.filenames.outputDir, config.filenames.users_table);
users.writeToFile(qrsInteractInstance, usersPath).then(() => {
var sheetsPath = path.join(config.filenames.outputDir, config.filenames.sheets_table);
return sheets.writeToFile(qrsInteractInstance, sheetsPath).then(() => {
var appsPath = path.join(config.filenames.outputDir, config.filenames.apps_table);
return apps.writeToFile(qrsInteractInstance, appsPath).then(function(ids) {
var visualizationsPath = path.join(config.filenames.outputDir, config.filenames.visualizations_table);
var dataMatrix = [];
return promise.each(ids, (element, index) => {
console.log("Getting data for app: " + element);
var appSession = createSession(element);
var dataRow = [];
dataRow.push(element);
dataRow.push(new Date().toJSON());
return appsEngine.writeToFile(appSession, element, sessionObjectParams, visualizationsPath).then(function() {
console.log("Done app " + (index + 1) + " of " + ids.length);
dataRow.push("Success");
dataRow.push("OK");
}).catch(function(err) {
dataRow.push("Fail");
dataRow.push(err);
}).then(function() {
dataMatrix.push(dataRow);
});
}).then(function() {
writeCSV.writeDataToFile(path.join(config.filenames.outputDir, config.filenames.outputStatus_table), dataMatrix);
});
});
});
}).then(() => {
console.log("Writing files is done.");
});