Skip to content

Commit

Permalink
refactor: Fix code linting issues
Browse files Browse the repository at this point in the history
Implements #762
  • Loading branch information
mountaindude committed Aug 22, 2023
1 parent 4d9abb2 commit b6312ac
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 320 deletions.
1 change: 1 addition & 0 deletions src/lib/service_uptime.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function serviceUptimeStart() {
// eslint-disable-next-line no-extend-native
Number.prototype.toTime = function (isSec) {
const ms = isSec ? this * 1e3 : this;
// eslint-disable-next-line no-bitwise
const lm = ~(4 * !!isSec);
/* limit fraction */
const fmt = new Date(ms).toISOString().slice(11, lm);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/slack_notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,8 @@ function sendReloadTaskFailureNotificationSlack(reloadParams) {
// This is needed to avoid breaking the Slack message JSON
const regExpSingle = /'/gm;
const regExpDouble = /"/gm;
templateContext.scriptLogHead = templateContext.scriptLogHead.replace(regExpSingle, "\'").replace(regExpDouble, "\\'");
templateContext.scriptLogTail = templateContext.scriptLogTail.replace(regExpSingle, "\'").replace(regExpDouble, "\\'");
templateContext.scriptLogHead = templateContext.scriptLogHead.replace(regExpSingle, "'").replace(regExpDouble, "\\'");
templateContext.scriptLogTail = templateContext.scriptLogTail.replace(regExpSingle, "'").replace(regExpDouble, "\\'");

// Replace all single and double quotes in executionDetailsConcatenated with escaped ditto
// This is needed to avoid breaking the Slack message JSON
Expand Down
13 changes: 10 additions & 3 deletions src/lib/winsvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ function status(serviceName, host = null) {
// Get state name
return resolve(stateName);
});

// We should never arrive here, but if we do return null
return null;
},

// Reject on error
Expand Down Expand Up @@ -173,16 +176,17 @@ function details(serviceName, host = null) {
// Run command to get service details with provided data
exec(command, (err, stdout) => {
let i = 0;
const startTypeRegex = new RegExp(/\d/);
const dependenciesRegex = new RegExp(/(?<=\s*DEPENDENCIES)(\s*:.*\r\n)*/);
const startTypeRegex = /\d/;
const dependenciesRegex = /(?<=\s*DEPENDENCIES)(\s*:.*\r\n)*/;

const deps = dependenciesRegex.exec(stdout)[0].toString().split('\r\n');

for (i = 0; i < deps.length; ++i) {
for (i = 0; i < deps.length; ) {
deps[i] = deps[i].replace(/\s*: /, '');
if (deps[i] === '') {
deps.splice(i, 1);
}
i += 1;
}

// On error, reject and exit
Expand Down Expand Up @@ -220,6 +224,9 @@ function details(serviceName, host = null) {
dependencies: deps,
});
});

// We should never arrive here, but if we do return null
return null;
},

// Reject on error
Expand Down
84 changes: 41 additions & 43 deletions src/qrs_util/app_tag_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,51 @@ const globals = require('../globals');
* @param {*} appId
* @returns
*/
function getAppTags(appId) {
return new Promise(async (resolve, reject) => {
globals.logger.debug(`GETAPPTAGS: Retrieving all tags of app ${appId}`);

async function getAppTags(appId) {
globals.logger.debug(`GETAPPTAGS: Retrieving all tags of app ${appId}`);

try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: path.resolve(globals.configQRS.certPaths.certPath),
keyFile: path.resolve(globals.configQRS.certPaths.keyPath),
},
});

// Get info about the task
try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: path.resolve(globals.configQRS.certPaths.certPath),
keyFile: path.resolve(globals.configQRS.certPaths.keyPath),
},
});

// Get info about the task
try {
globals.logger.debug(`GETAPPTAGS: app/full?filter=id eq ${appId}`);

const result = await qrsInstance.Get(`app/full?filter=id eq ${appId}`);
globals.logger.debug(`GETAPPTAGS: Got response: ${result.statusCode}`);

if (result.body.length === 1) {
// Yes, the task exists. Return all tags for this task

// Get array of all values for this CP, for this task
const appTags1 = result.body[0].tags;

// Get array of all CP values
const appTags2 = appTags1.map((item) => item.name);

resolve(appTags2);
} else {
// The task does not exist
resolve([]);
}
} catch (err) {
globals.logger.error(`GETAPPTAGS: Error while getting tags: ${err.message}`);
resolve([]);
globals.logger.debug(`GETAPPTAGS: app/full?filter=id eq ${appId}`);

const result = await qrsInstance.Get(`app/full?filter=id eq ${appId}`);
globals.logger.debug(`GETAPPTAGS: Got response: ${result.statusCode}`);

if (result.body.length === 1) {
// Yes, the task exists. Return all tags for this task

// Get array of all values for this CP, for this task
const appTags1 = result.body[0].tags;

// Get array of all CP values
const appTags2 = appTags1.map((item) => item.name);

return appTags2;
}

// The task does not exist
return [];
} catch (err) {
globals.logger.error(`GETAPPTAGS: Error while getting tags: ${err}`);
reject();
globals.logger.error(`GETAPPTAGS: Error while getting tags: ${err.message}`);
return [];
}
});
} catch (err) {
globals.logger.error(`GETAPPTAGS: Error while getting tags: ${err}`);
return false;
}
}

module.exports = {
Expand Down
80 changes: 39 additions & 41 deletions src/qrs_util/does_task_exist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,49 @@ const QrsInteract = require('qrs-interact');
const globals = require('../globals');

// Function for getting info about owner of Qlik Sense apps
module.exports.doesTaskExist = (taskId) =>
module.exports.doesTaskExist = async (taskId) => {
// eslint-disable-next-line no-unused-vars
new Promise(async (resolve, reject) => {
try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: globals.configQRS.certPaths.certPath,
keyFile: globals.configQRS.certPaths.keyPath,
},
});
try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: globals.configQRS.certPaths.certPath,
keyFile: globals.configQRS.certPaths.keyPath,
},
});

// Get info about the task
try {
globals.logger.debug(`TASKEXISTS 1: task?filter=id eq ${taskId}`);
// Get info about the task
try {
globals.logger.debug(`TASKEXISTS 1: task?filter=id eq ${taskId}`);

const result = await qrsInstance.Get(`task?filter=id eq ${taskId}`);
globals.logger.debug(`TASKEXISTS: Got response: ${result.statusCode} for task ID ${taskId}`);
const result = await qrsInstance.Get(`task?filter=id eq ${taskId}`);
globals.logger.debug(`TASKEXISTS: Got response: ${result.statusCode} for task ID ${taskId}`);

if (result.statusCode === 200 && result.body.length > 0) {
// Task exists
resolve({
exists: true,
task: {
taskId: result.body[0].id,
taskName: result.body[0].name,
},
});
} else {
// Task doesn't exist or other error (e.g. couldn't contact QRS)
resolve({
exists: false,
task: {},
});
}
} catch (err) {
globals.logger.error(`TASKEXISTS: Error while getting task: ${err.message}`);
resolve(false);
if (result.statusCode === 200 && result.body.length > 0) {
// Task exists
return {
exists: true,
task: {
taskId: result.body[0].id,
taskName: result.body[0].name,
},
};
}
// Task doesn't exist or other error (e.g. couldn't contact QRS)
return {
exists: false,
task: {},
};
} catch (err) {
globals.logger.error(`TASKEXISTS: Error while checking if task exists: ${JSON.stringify(err, null, 2)}`);
resolve(false);
globals.logger.error(`TASKEXISTS: Error while getting task: ${err.message}`);
return false;
}
});
} catch (err) {
globals.logger.error(`TASKEXISTS: Error while checking if task exists: ${JSON.stringify(err, null, 2)}`);
return false;
}
};
96 changes: 48 additions & 48 deletions src/qrs_util/get_app_owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@ const QrsInteract = require('qrs-interact');
const globals = require('../globals');

// Function for getting info about owner of Qlik Sense apps
module.exports.getAppOwner = (appId) =>
// eslint-disable-next-line no-unused-vars
new Promise(async (resolve, reject) => {
try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: globals.configQRS.certPaths.certPath,
keyFile: globals.configQRS.certPaths.keyPath,
},
});
module.exports.getAppOwner = async (appId) => {
try {
const qrsInstance = new QrsInteract({
hostname: globals.configQRS.host,
portNumber: globals.configQRS.port,
headers: {
'X-Qlik-User': 'UserDirectory=Internal; UserId=sa_repository',
},
certificates: {
certFile: globals.configQRS.certPaths.certPath,
keyFile: globals.configQRS.certPaths.keyPath,
},
});

// Step 1: Get app owner's userdirectory and userid
let appOwner = null;
try {
globals.logger.debug(`APPOWNER 1: app/${appId}`);
const result = await qrsInstance.Get(`app/${appId}`);
globals.logger.debug(`APPOWNER: Got response: ${result.statusCode} for app ID ${appId}`);
// Step 1: Get app owner's userdirectory and userid
let appOwner = null;
try {
globals.logger.debug(`APPOWNER 1: app/${appId}`);
const result = await qrsInstance.Get(`app/${appId}`);
globals.logger.debug(`APPOWNER: Got response: ${result.statusCode} for app ID ${appId}`);

appOwner = result.body.owner;
} catch (err) {
globals.logger.error(`APPOWNER: Error while getting app owner: ${JSON.stringify(err, null, 2)}`);
throw 'Error while getting app owner';
}
appOwner = result.body.owner;
} catch (err) {
globals.logger.error(`APPOWNER: Error while getting app owner: ${JSON.stringify(err, null, 2)}`);
throw new Error('Error while getting app owner');
}

// Step 2: Get additional info about the user identified in step 1
try {
globals.logger.debug(`APPOWNER 2: user/${appOwner.id}`);
const result = await qrsInstance.Get(`user/${appOwner.id}`);
globals.logger.debug(`APPOWNER: Got response: ${result.statusCode} for app owner ${appOwner.id}`);
// Step 2: Get additional info about the user identified in step 1
try {
globals.logger.debug(`APPOWNER 2: user/${appOwner.id}`);
const result = await qrsInstance.Get(`user/${appOwner.id}`);
globals.logger.debug(`APPOWNER: Got response: ${result.statusCode} for app owner ${appOwner.id}`);

// Find email attribute
const emailAttributes = result.body.attributes.filter((attribute) => attribute.attributeType.toLowerCase() === 'email');
const resultAttributes = emailAttributes.map((attribute) => attribute.attributeValue);
// Find email attribute
const emailAttributes = result.body.attributes.filter((attribute) => attribute.attributeType.toLowerCase() === 'email');
const resultAttributes = emailAttributes.map((attribute) => attribute.attributeValue);

// if (resultAttributes.length > 0) {
resolve({
id: appOwner.id,
directory: appOwner.userDirectory,
userId: appOwner.userId,
userName: appOwner.name,
emails: resultAttributes,
});
// }
} catch (err) {
globals.logger.error(`APPOWNER: Error while getting app owner details 1: ${JSON.stringify(err, null, 2)}`);
}
// if (resultAttributes.length > 0) {
return {
id: appOwner.id,
directory: appOwner.userDirectory,
userId: appOwner.userId,
userName: appOwner.name,
emails: resultAttributes,
};
// }
} catch (err) {
globals.logger.error(`APPOWNER: Error while getting app owner details 2: ${JSON.stringify(err, null, 2)}`);
globals.logger.error(`APPOWNER: Error while getting app owner details 1: ${JSON.stringify(err, null, 2)}`);
return false;
}
});
} catch (err) {
globals.logger.error(`APPOWNER: Error while getting app owner details 2: ${JSON.stringify(err, null, 2)}`);
return false;
}
};
Loading

0 comments on commit b6312ac

Please sign in to comment.