From 33f783d6b63264eeadc57cf94e07979f0a819950 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 19 Jan 2022 14:43:47 -0500 Subject: [PATCH] fix(striker-ui-api): simplify calls to striker-access-database and /files GETs --- striker-ui-api/lib/accessDB.js | 68 +++++++++++++++++++ striker-ui-api/lib/consts/SERVER_PATHS.js | 5 ++ .../request_handlers/files/buildGetFiles.js | 29 ++++++++ .../request_handlers/files/getFileDetail.js | 24 +++++++ .../files/getFilesOverview.js | 12 ++++ 5 files changed, 138 insertions(+) create mode 100644 striker-ui-api/lib/accessDB.js create mode 100644 striker-ui-api/lib/request_handlers/files/buildGetFiles.js create mode 100644 striker-ui-api/lib/request_handlers/files/getFileDetail.js create mode 100644 striker-ui-api/lib/request_handlers/files/getFilesOverview.js diff --git a/striker-ui-api/lib/accessDB.js b/striker-ui-api/lib/accessDB.js new file mode 100644 index 00000000..448f905f --- /dev/null +++ b/striker-ui-api/lib/accessDB.js @@ -0,0 +1,68 @@ +const { spawnSync } = require('child_process'); + +const SERVER_PATHS = require('./consts/SERVER_PATHS'); + +const execStrikerAccessDatabase = ( + args, + options = { + timeout: 10000, + encoding: 'utf-8', + }, +) => { + const { error, stdout, stderr } = spawnSync( + SERVER_PATHS.usr.sbin['striker-access-database'].self, + args, + options, + ); + + if (error) { + throw error; + } + + if (stderr) { + throw new Error(stderr); + } + + let output; + + try { + output = JSON.parse(stdout); + } catch (stdoutParseError) { + output = stdout; + + console.warn( + `Failed to parse striker-access-database output [${output}]; error: [${stdoutParseError}]`, + ); + } + + return { + stdout: output, + }; +}; + +const accessDB = { + query: (query, accessMode, options) => { + const args = ['--query', query]; + + if (accessMode) { + args.push('--mode', accessMode); + } + + return execStrikerAccessDatabase(args, options); + }, + sub: (subName, subParams, options) => { + const args = ['--sub', subName]; + + if (subParams) { + args.push('--sub-params', JSON.stringify(subParams)); + } + + const { stdout } = execStrikerAccessDatabase(args, options); + + return { + stdout: stdout['sub_results'], + }; + }, +}; + +module.exports = accessDB; diff --git a/striker-ui-api/lib/consts/SERVER_PATHS.js b/striker-ui-api/lib/consts/SERVER_PATHS.js index 845eca78..79a5164a 100644 --- a/striker-ui-api/lib/consts/SERVER_PATHS.js +++ b/striker-ui-api/lib/consts/SERVER_PATHS.js @@ -6,6 +6,11 @@ const SERVER_PATHS = { incoming: {}, }, }, + usr: { + sbin: { + 'striker-access-database': {}, + }, + }, }; const generatePaths = ( diff --git a/striker-ui-api/lib/request_handlers/files/buildGetFiles.js b/striker-ui-api/lib/request_handlers/files/buildGetFiles.js new file mode 100644 index 00000000..086cf289 --- /dev/null +++ b/striker-ui-api/lib/request_handlers/files/buildGetFiles.js @@ -0,0 +1,29 @@ +const accessDB = require('../../accessDB'); + +const buildGetFiles = (query) => (request, response) => { + console.log('Calling CLI script to get data.'); + + let queryStdout; + + try { + ({ stdout: queryStdout } = accessDB.query( + typeof query === 'function' ? query(request) : query, + )); + } catch (queryError) { + console.log(`Query error: ${queryError}`); + + response.status(500).send(); + } + + console.log( + `Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify( + queryStdout, + null, + 2, + )}`, + ); + + response.json(queryStdout); +}; + +module.exports = buildGetFiles; diff --git a/striker-ui-api/lib/request_handlers/files/getFileDetail.js b/striker-ui-api/lib/request_handlers/files/getFileDetail.js new file mode 100644 index 00000000..c2828639 --- /dev/null +++ b/striker-ui-api/lib/request_handlers/files/getFileDetail.js @@ -0,0 +1,24 @@ +const buildGetFiles = require('./buildGetFiles'); + +const getFileDetail = buildGetFiles( + (request) => + `SELECT + fil.file_uuid, + fil.file_name, + fil.file_size, + fil.file_type, + fil.file_md5sum, + fil_loc.file_location_uuid, + fil_loc.file_location_active, + anv.anvil_uuid, + anv.anvil_name, + anv.anvil_description + FROM files AS fil + JOIN file_locations AS fil_loc + ON fil.file_uuid = fil_loc.file_location_file_uuid + JOIN anvils AS anv + ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid + WHERE fil.file_uuid = '${request.params.fileUUID}';`, +); + +module.exports = getFileDetail; diff --git a/striker-ui-api/lib/request_handlers/files/getFilesOverview.js b/striker-ui-api/lib/request_handlers/files/getFilesOverview.js new file mode 100644 index 00000000..b249d53f --- /dev/null +++ b/striker-ui-api/lib/request_handlers/files/getFilesOverview.js @@ -0,0 +1,12 @@ +const buildGetFiles = require('./buildGetFiles'); + +const getFilesOverview = buildGetFiles(` +SELECT + file_uuid, + file_name, + file_size, + file_type, + file_md5sum +FROM files;`); + +module.exports = getFilesOverview;