Extract duplicate API path logic into helper functions in conformance and files stores

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 21:16:26 +08:00
parent c3d0add548
commit ec0035a182
2 changed files with 70 additions and 196 deletions

View File

@@ -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;