diff --git a/src/stores/conformance.ts b/src/stores/conformance.ts index ff3f886..28e6d28 100644 --- a/src/stores/conformance.ts +++ b/src/stores/conformance.ts @@ -16,6 +16,36 @@ import abbreviateNumber from '@/module/abbreviateNumber.js'; import apiClient from "@/api/client.js"; import apiError from '@/module/apiError.js'; +/** + * Returns the API base path for the current conformance check, + * prioritizing temp IDs over created IDs, and filter over log. + * @param {object} state - The store state. + * @returns {string} The API base path. + */ +function getCheckApiBase(state) { + const { conformanceFilterTempCheckId, conformanceLogTempCheckId, + conformanceFilterCreateCheckId, conformanceLogCreateCheckId } = state; + if (conformanceFilterTempCheckId !== null) return `/api/temp-filter-checks/${conformanceFilterTempCheckId}`; + if (conformanceLogTempCheckId !== null) return `/api/temp-log-checks/${conformanceLogTempCheckId}`; + if (conformanceFilterCreateCheckId !== null) return `/api/filter-checks/${conformanceFilterCreateCheckId}`; + if (conformanceLogCreateCheckId !== null) return `/api/log-checks/${conformanceLogCreateCheckId}`; + return ''; +} + +/** + * Returns the API path for conformance params/temp-checks, + * prioritizing filter over log. + * @param {object} state - The store state. + * @returns {{prefix: string, idParam: string}} The API prefix and ID query param. + */ +function getFileTypeApi(state) { + const { conformanceFilterId, conformanceLogId } = state; + if (conformanceFilterId !== null) { + return { prefix: 'filter', idParam: `filter_id=${conformanceFilterId}` }; + } + return { prefix: 'log', idParam: `log_id=${conformanceLogId}` }; +} + /** Pinia store for conformance checking and rule management. */ export const useConformanceStore = defineStore('conformanceStore', { state: () => ({ @@ -202,17 +232,8 @@ export const useConformanceStore = defineStore('conformanceStore', { * fetch Log Conformance Parameters api for Rule Settings. */ async getConformanceParams() { - const logId = this.conformanceLogId; - const filterId = this.conformanceFilterId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterId !== null) { - api = `/api/filter-checks/params?filter_id=${filterId}`; - } - else { - api = `/api/log-checks/params?log_id=${logId}`; - } + const { prefix, idParam } = getFileTypeApi(this); + const api = `/api/${prefix}-checks/params?${idParam}`; try { const response = await apiClient.get(api); this.allConformanceTask = response.data.tasks; @@ -230,24 +251,14 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {object} data - The request payload for the backend. */ async addConformanceCheckId(data) { - const logId = this.conformanceLogId; - const filterId = this.conformanceFilterId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterId !== null) { - api = `/api/temp-filter-checks?filter_id=${filterId}`; - } - else { - api = `/api/temp-log-checks?log_id=${logId}`; - } + const { prefix, idParam } = getFileTypeApi(this); + const api = `/api/temp-${prefix}-checks?${idParam}`; try { const response = await apiClient.post(api, data); - if(filterId !== null) { + if (prefix === 'filter') { this.conformanceFilterTempCheckId = response.data.id; - } - else { + } else { this.conformanceLogTempCheckId = response.data.id; } } catch(error) { @@ -259,25 +270,7 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {boolean} getRouteFile - Whether called to get the route file (used outside the Conformance page). */ async getConformanceReport(getRouteFile = false) { - const logTempCheckId = this.conformanceLogTempCheckId; - const filterTempCheckId = this.conformanceFilterTempCheckId; - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; - - // 先判斷 Temp 再判斷原 ID;先判斷 filter 檔,再判斷 log 檔。 - if(filterTempCheckId !== null) { - api = `/api/temp-filter-checks/${filterTempCheckId}`; - } - else if(logTempCheckId !== null) { - api = `/api/temp-log-checks/${logTempCheckId}`; - } - else if(filterCreateCheckId !== null) { - api = `/api/filter-checks/${filterCreateCheckId}`; - } - else if(logCreateCheckId !== null) { - api = `/api/log-checks/${logCreateCheckId}`; - } + const api = getCheckApiBase(this); try { const response = await apiClient.get(api); if(!getRouteFile) { @@ -294,26 +287,7 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {number} issueNo - The issue number. */ async getConformanceIssue(issueNo) { - const logTempCheckId = this.conformanceLogTempCheckId; - const filterTempCheckId = this.conformanceFilterTempCheckId; - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterTempCheckId !== null) { - api = `/api/temp-filter-checks/${filterTempCheckId}/issues/${issueNo}`; - } - else if(logTempCheckId !== null) { - api = `/api/temp-log-checks/${logTempCheckId}/issues/${issueNo}`; - } - else if(filterCreateCheckId !== null) { - api = `/api/filter-checks/${filterCreateCheckId}/issues/${issueNo}`; - } - else if(logCreateCheckId !== null) { - api = `/api/log-checks/${logCreateCheckId}/issues/${issueNo}`; - } - + const api = `${getCheckApiBase(this)}/issues/${issueNo}`; try { const response = await apiClient.get(api); this.allIssueTraces = response.data.traces; @@ -328,26 +302,7 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {number} start - The starting index for loading traces. */ async getConformanceTraceDetail(issueNo, traceId, start) { - const logTempCheckId = this.conformanceLogTempCheckId; - const filterTempCheckId = this.conformanceFilterTempCheckId; - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterTempCheckId !== null) { - api = `/api/temp-filter-checks/${filterTempCheckId}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=20`; - } - else if(logTempCheckId !== null) { - api = `/api/temp-log-checks/${logTempCheckId}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=20`; - } - else if(filterCreateCheckId !== null) { - api = `/api/filter-checks/${filterCreateCheckId}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=20`; - } - else if(logCreateCheckId !== null) { - api = `/api/log-checks/${logCreateCheckId}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=20`; - } - + const api = `${getCheckApiBase(this)}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=20`; try { const response = await apiClient.get(api); this.allTaskSeq = response.data.task_seq; @@ -366,18 +321,7 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {number} loopNo - The loop number. */ async getConformanceLoop(loopNo) { - const logTempCheckId = this.conformanceLogTempCheckId; - const filterTempCheckId = this.conformanceFilterTempCheckId; - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterTempCheckId !== null) api = `/api/temp-filter-checks/${filterTempCheckId}/loops/${loopNo}`; - else if(logTempCheckId !== null) api = `/api/temp-log-checks/${logTempCheckId}/loops/${loopNo}`; - else if(filterCreateCheckId !== null) api = `/api/filter-checks/${filterCreateCheckId}/loops/${loopNo}`; - else if(logCreateCheckId !== null) api = `/api/log-checks/${logCreateCheckId}/loops/${loopNo}`; - + const api = `${getCheckApiBase(this)}/loops/${loopNo}`; try { const response = await apiClient.get(api); this.allLoopTraces = response.data.traces; @@ -392,18 +336,7 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {number} start - The starting index for loading traces. */ async getConformanceLoopsTraceDetail(loopNo, traceId, start) { - const logTempCheckId = this.conformanceLogTempCheckId; - const filterTempCheckId = this.conformanceFilterTempCheckId; - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; - - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterTempCheckId !== null) api = `/api/temp-filter-checks/${filterTempCheckId}/loops/${loopNo}/traces/${traceId}?start=${start}&page_size=20`; - else if(logTempCheckId !== null) api = `/api/temp-log-checks/${logTempCheckId}/loops/${loopNo}/traces/${traceId}?start=${start}&page_size=20`; - else if(filterCreateCheckId !== null) api = `/api/filter-checks/${filterCreateCheckId}/loops/${loopNo}/traces/${traceId}?start=${start}&page_size=20`; - else if(logCreateCheckId !== null) api = `/api/log-checks/${logCreateCheckId}/loops/${loopNo}/traces/${traceId}?start=${start}&page_size=20`; - + const api = `${getCheckApiBase(this)}/loops/${loopNo}/traces/${traceId}?start=${start}&page_size=20`; try { const response = await apiClient.get(api); this.allLoopTaskSeq = response.data.task_seq; @@ -422,27 +355,21 @@ export const useConformanceStore = defineStore('conformanceStore', { * @param {string} value file's name */ async addConformanceCreateCheckId(value) { - const logId = this.conformanceLogId; - const filterId = this.conformanceFilterId; - let api = ''; + const { prefix, idParam } = getFileTypeApi(this); + const api = `/api/${prefix}-checks?${idParam}`; const data = { name: value, rule: this.conformanceRuleData }; - // 先判斷 filter 檔,再判斷 log 檔。 - if(filterId !== null) api = `/api/filter-checks?filter_id=${filterId}`; - else api = `/api/log-checks?log_id=${logId}`; - try { const response = await apiClient.post(api, data); - if(filterId !== null) { + if (prefix === 'filter') { this.conformanceFilterCreateCheckId = response.data.id; this.conformanceFilterTempCheckId = null; - } - else { + } else { this.conformanceLogCreateCheckId = response.data.id; - this.conformanceLogTempCheckId= null; + this.conformanceLogTempCheckId = null; } }catch(error) { apiError(error, 'Failed to add the Conformance Check for a file.'); @@ -452,15 +379,9 @@ export const useConformanceStore = defineStore('conformanceStore', { * Update an Existing Conformance Check, log and filter */ async updateConformance() { - const logCreateCheckId = this.conformanceLogCreateCheckId; - const filterCreateCheckId = this.conformanceFilterCreateCheckId; - let api = ''; + const api = getCheckApiBase(this); const data = this.conformanceRuleData; - // 先判斷有無 Temp ID,再判斷原始檔 ID - if(filterCreateCheckId !== null) api = `/api/filter-checks/${filterCreateCheckId}`; - else if(logCreateCheckId !== null) api = `/api/log-checks/${logCreateCheckId}`; - try { const response = await apiClient.put(api, data); this.isUpdateConformance = response.status === 200; diff --git a/src/stores/files.ts b/src/stores/files.ts index be9b965..2320002 100644 --- a/src/stores/files.ts +++ b/src/stores/files.ts @@ -17,6 +17,24 @@ import Swal from 'sweetalert2'; import { uploadFailedFirst, uploadFailedSecond, uploadloader, uploadSuccess, deleteSuccess } from '@/module/alertModal.js'; import { useLoadingStore } from '@/stores/loading'; +/** Map of file type to API path segment. */ +const FILE_TYPE_PATHS = { + 'log': 'logs', + 'filter': 'filters', + 'log-check': 'log-checks', + 'filter-check': 'filter-checks', +}; + +/** + * Returns the API base path for a file type and ID. + * @param {string} type - The file type. + * @param {number} id - The file ID. + * @returns {string} The API base path. + */ +function getFileApiBase(type, id) { + return `/api/${FILE_TYPE_PATHS[type]}/${id}`; +} + /** Pinia store for file CRUD operations and upload workflow. */ export const useFilesStore = defineStore('filesStore', { state: () => ({ @@ -205,25 +223,9 @@ export const useFilesStore = defineStore('filesStore', { fileName = this.uploadFileName; } - let api; const data = {"name": fileName}; - - switch (type) { - case 'log': - api = `/api/logs/${id}/name`; - break; - case 'filter': - api = `/api/filters/${id}/name`; - break; - case 'log-check': - api = `/api/log-checks/${id}/name`; - break; - case 'filter-check': - api = `/api/filter-checks/${id}/name`; - break; - } try { - await apiClient.put(api, data); + await apiClient.put(`${getFileApiBase(type, id)}/name`, data); this.uploadFileName = null; await this.fetchAllFiles(); } catch(error) { @@ -236,24 +238,8 @@ export const useFilesStore = defineStore('filesStore', { * @param {number} id - The file ID. */ async getDependents(type, id) { - let api; - - switch (type) { - case 'log': - api = `/api/logs/${id}/dependents`; - break; - case 'filter': - api = `/api/filters/${id}/dependents`; - break; - case 'log-check': - api = `/api/log-checks/${id}/dependents`; - break; - case 'filter-check': - api = `/api/filter-checks/${id}/dependents`; - break; - } try { - const response = await apiClient.get(api); + const response = await apiClient.get(`${getFileApiBase(type, id)}/dependents`); this.allDependentsData = response.data; } catch(error) { apiError(error, 'Failed to get Dependents of the files.'); @@ -265,30 +251,14 @@ export const useFilesStore = defineStore('filesStore', { * @param {number} id - The file ID. */ async deleteFile(type, id) { - let api; - if(id === null || id === undefined || isNaN(id)) { console.error('Delete File API Error: invalid id'); return; }; const loading = useLoadingStore(); loading.isLoading = true; - switch (type) { - case 'log': - api = `/api/logs/${id}`; - break; - case 'filter': - api = `/api/filters/${id}`; - break; - case 'log-check': - api = `/api/log-checks/${id}`; - break; - case 'filter-check': - api = `/api/filter-checks/${id}`; - break; - } try { - await apiClient.delete(api); + await apiClient.delete(getFileApiBase(type, id)); await this.fetchAllFiles(); await deleteSuccess(); } catch(error) { @@ -322,26 +292,9 @@ export const useFilesStore = defineStore('filesStore', { * @param {string} fileName - The file name. */ async downloadFileCSV(type, id, fileName) { - let api; - - switch (type) { - case 'log': - api = `/api/logs/${id}/csv`; - break; - case 'filter': - api = `/api/filters/${id}/csv`; - break; - // case 'log-check': - // api = `/api/log-checks/${id}/csv`; - // break; - // case 'filter-check': - // api = `/api/filter-checks/${id}/csv`; - // break; - default: - return; - } + if (type !== 'log' && type !== 'filter') return; try { - const response = await apiClient.get(api); + const response = await apiClient.get(`${getFileApiBase(type, id)}/csv`); const csvData = response.data; const blob = new Blob([csvData], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob);