From 82a828e3dff1ce60b845f025d588df464be23d0b Mon Sep 17 00:00:00 2001 From: sadnub Date: Thu, 30 Dec 2021 23:56:12 -0500 Subject: [PATCH] automated task rework --- package-lock.json | 30 + package.json | 1 + src/components/agents/AutomatedTasksTab.vue | 8 +- .../automation/PolicyAutomatedTasksTab.vue | 8 +- src/components/tasks/AddAutomatedTask.vue | 351 ------- src/components/tasks/AutomatedTaskForm.vue | 968 ++++++++++++++++++ src/components/tasks/EditAutomatedTask.vue | 181 ---- src/composables/checks.js | 4 + src/utils/format.js | 54 + src/utils/validation.js | 4 + 10 files changed, 1069 insertions(+), 540 deletions(-) delete mode 100644 src/components/tasks/AddAutomatedTask.vue create mode 100644 src/components/tasks/AutomatedTaskForm.vue delete mode 100644 src/components/tasks/EditAutomatedTask.vue diff --git a/package-lock.json b/package-lock.json index 31c214f..52f0623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "quasar": "^2.3.4", "vue3-ace-editor": "^2.2.1", "vue3-apexcharts": "^1.4.0", + "vuedraggable": "^4.1.0", "vuex": "^4.0.2" }, "devDependencies": { @@ -10962,6 +10963,11 @@ "node": ">=0.10.0" } }, + "node_modules/sortablejs": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/sortablejs/-/sortablejs-1.14.0.tgz", + "integrity": "sha512-pBXvQCs5/33fdN1/39pPL0NZF20LeRbLQ5jtnheIPN9JQAaufGjKdWduZn4U7wCtVuzKhmRkI0DFYHYRbB2H1w==" + }, "node_modules/source-list-map": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz", @@ -12080,6 +12086,17 @@ "vue": "> 3.0.0" } }, + "node_modules/vuedraggable": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/vuedraggable/-/vuedraggable-4.1.0.tgz", + "integrity": "sha512-FU5HCWBmsf20GpP3eudURW3WdWTKIbEIQxh9/8GE806hydR9qZqRRxRE3RjqX7PkuLuMQG/A7n3cfj9rCEchww==", + "dependencies": { + "sortablejs": "1.14.0" + }, + "peerDependencies": { + "vue": "^3.0.1" + } + }, "node_modules/vuex": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/vuex/-/vuex-4.0.2.tgz", @@ -21101,6 +21118,11 @@ } } }, + "sortablejs": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/sortablejs/-/sortablejs-1.14.0.tgz", + "integrity": "sha512-pBXvQCs5/33fdN1/39pPL0NZF20LeRbLQ5jtnheIPN9JQAaufGjKdWduZn4U7wCtVuzKhmRkI0DFYHYRbB2H1w==" + }, "source-list-map": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz", @@ -21952,6 +21974,14 @@ "integrity": "sha512-96qP8JDqB9vwU7bkG5nVU+E0UGQn7yYQVqUUCLQMYWDuQyu2vE77H/UFZ1yI+hwzlSTBKT9BqnNG8JsFegB3eg==", "requires": {} }, + "vuedraggable": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/vuedraggable/-/vuedraggable-4.1.0.tgz", + "integrity": "sha512-FU5HCWBmsf20GpP3eudURW3WdWTKIbEIQxh9/8GE806hydR9qZqRRxRE3RjqX7PkuLuMQG/A7n3cfj9rCEchww==", + "requires": { + "sortablejs": "1.14.0" + } + }, "vuex": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/vuex/-/vuex-4.0.2.tgz", diff --git a/package.json b/package.json index 84dfbce..3d6321e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "quasar": "^2.3.4", "vue3-ace-editor": "^2.2.1", "vue3-apexcharts": "^1.4.0", + "vuedraggable": "^4.1.0", "vuex": "^4.0.2" }, "devDependencies": { diff --git a/src/components/agents/AutomatedTasksTab.vue b/src/components/agents/AutomatedTasksTab.vue index 34ff72a..0193706 100644 --- a/src/components/agents/AutomatedTasksTab.vue +++ b/src/components/agents/AutomatedTasksTab.vue @@ -249,8 +249,7 @@ import { notifySuccess, notifyError } from "@/utils/notify"; import { truncateText } from "@/utils/format"; // ui imports -import AddAutomatedTask from "@/components/tasks/AddAutomatedTask"; -import EditAutomatedTask from "@/components/tasks/EditAutomatedTask"; +import AutomatedTaskForm from "@/components/tasks/AutomatedTaskForm"; import ScriptOutput from "@/components/checks/ScriptOutput"; // static data @@ -380,7 +379,7 @@ export default { function showAddTask() { $q.dialog({ - component: AddAutomatedTask, + component: AutomatedTaskForm, componentProps: { parent: { agent: selectedAgent.value }, }, @@ -393,9 +392,10 @@ export default { if (task.managed_by_policy) return; $q.dialog({ - component: EditAutomatedTask, + component: AutomatedTaskForm, componentProps: { task: task, + parent: { agent: selectedAgent.value }, }, }).onOk(() => { getTasks(); diff --git a/src/components/automation/PolicyAutomatedTasksTab.vue b/src/components/automation/PolicyAutomatedTasksTab.vue index 0522e3e..48cc51c 100644 --- a/src/components/automation/PolicyAutomatedTasksTab.vue +++ b/src/components/automation/PolicyAutomatedTasksTab.vue @@ -164,8 +164,7 @@ \ No newline at end of file diff --git a/src/components/tasks/AutomatedTaskForm.vue b/src/components/tasks/AutomatedTaskForm.vue new file mode 100644 index 0000000..ca3f8b7 --- /dev/null +++ b/src/components/tasks/AutomatedTaskForm.vue @@ -0,0 +1,968 @@ + + + + + \ No newline at end of file diff --git a/src/components/tasks/EditAutomatedTask.vue b/src/components/tasks/EditAutomatedTask.vue deleted file mode 100644 index 9b65b3a..0000000 --- a/src/components/tasks/EditAutomatedTask.vue +++ /dev/null @@ -1,181 +0,0 @@ - - - \ No newline at end of file diff --git a/src/composables/checks.js b/src/composables/checks.js index 5bbc68b..cf66d4d 100644 --- a/src/composables/checks.js +++ b/src/composables/checks.js @@ -104,6 +104,10 @@ export function useCheckDropdown() { const checkOptions = ref([]) async function getCheckOptions({ agent, policy }, flat = false) { + if (!agent && !policy) { + console.error("Need to specify agent or policy object when calling getCheckOptions") + return + } checkOptions.value = formatCheckOptions(agent ? await fetchAgentChecks(agent) : await fetchPolicyChecks(policy), flat) } diff --git a/src/utils/format.js b/src/utils/format.js index 8541d57..cfe2bef 100644 --- a/src/utils/format.js +++ b/src/utils/format.js @@ -1,4 +1,5 @@ import { date } from "quasar"; +import { validateTimePeriod } from "@/utils/validation" // dropdown options formatting @@ -198,6 +199,11 @@ export function dateStringToUnix(drfString) { return parseInt(date.formatDate(d, "X")); } +// takes a unix timestamp and converts it to quasar datetime field value YYYY-MM-DD HH:mm:ss +export function formatDateInputField(unixtimestamp) { + return date.formatDate(unixtimestamp, "YYYY-MM-DD HH:mm:ss") +} + // string formatting export function capitalize(string) { @@ -232,3 +238,51 @@ export function convertMemoryToPercent(percent, memory) { const mb = memory * 1024; return Math.ceil((percent * mb) / 100).toLocaleString(); } + +// convert time period(str) to seconds(int) (3h -> 10800) used for comparing time intervals +export function convertPeriodToSeconds(period) { + if (!validateTimePeriod(period)) { + console.error("Time Period is invalid") + return NaN + } + + if (period.toUpperCase().includes("S")) + // remove last letter from string and return since already in seconds + return parseInt(period.slice(0, -1)) + else if (period.toUpperCase().includes("M")) + // remove last letter from string and multiple by 60 to get seconds + return parseInt(period.slice(0, -1)) * 60 + else if (period.toUpperCase().includes("H")) + // remove last letter from string and multiple by 60 twice to get seconds + return parseInt(period.slice(0, -1)) * 60 * 60 + else if (period.toUpperCase().includes("D")) + // remove last letter from string and multiply by 24 and 60 twice to get seconds + return parseInt(period.slice(0, -1)) * 24 * 60 * 60 +} + +// takes an integer and converts it to an array in binary format. i.e: 13 -> [8, 4, 1] +// Needed to work with multi-select fields in tasks form +export function convertToBitArray(number) { + let bitArray = [] + let binary = number.toString(2) + for (let i = 0; i < binary.length; ++i) { + if (binary[i] !== "0") { + // last binary digit + if (binary.slice(i).length === 1) { + bitArray.push(1) + } else { + bitArray.push(parseInt(binary.slice(i), 2) - parseInt(binary.slice(i + 1), 2)) + } + } + } + return bitArray +} + +// takes an array of integers and adds them together +export function convertFromBitArray(array) { + let result = 0 + for (let i = 0; i < array.length; i++) { + result += array[i] + } + return result +} \ No newline at end of file diff --git a/src/utils/validation.js b/src/utils/validation.js index 8f8ce7a..74daad4 100644 --- a/src/utils/validation.js +++ b/src/utils/validation.js @@ -34,4 +34,8 @@ export function validateEventID(val) { // validate script return code export function validateRetcode(val, done) { /^\d+$/.test(val) ? done(val) : done(); +} + +export function validateTimePeriod(val) { + return /^\d{1,3}(H|h|M|m|S|s|d|D)$/.test(val); } \ No newline at end of file