import { defineStore } from "pinia"; import loadingStore from "./loading"; import pinia from '@/stores/main.js'; import {useToast} from 'vue-toast-notification'; import 'vue-toast-notification/dist/theme-sugar.css'; import moment from "moment"; import { Decimal } from 'decimal.js'; const loading = loadingStore(pinia); const $toast = useToast(); // Delay loading and toast 待模組化 let delay = (s = 0) => new Promise((resolve, reject) => setTimeout(resolve, s)); export default defineStore('conformanceStore', { state: () => ({ conformanceLogId: null, conformanceFilterId: null, conformanceTempCheckerId: null, allConformanceTask: [], allConformanceTempReportData: null, allIssueTraces: null, allTaskSeq: null, allCases: null, selectedRuleType: 'Have activity', // radio selectedActivitySequence: 'Start & End', // radio selectedMode: 'Directly follows', // radio selectedProcessScope: 'End to end', // radio selectedActSeqMore: 'All', // radio selectedActSeqFromTo: 'From', // radio }), getters: { conformanceTask: state => { return state.allConformanceTask.map(i => i.label); }, conformanceTempReportData: state => { return state.allConformanceTempReportData; }, issueTraces: state => { return state.allIssueTraces; }, taskSeq: state => { return state.allTaskSeq; }, cases: state => { if(state.allCases !== null){ state.allCases.map(c => { c.started_at = moment(c.started_at).format('YYYY/MM/DD HH:MM'); c.completed_at = moment(c.completed_at).format('YYYY/MM/DD HH:MM'); c.attributes.map(att => { switch (att.type) { case 'date': att.value = att.value !== null ? moment(att.value).format('YYYY/MM/DD HH:MM:ss') : null; break; case 'float': att.value = att.value !== null ? new Decimal(att.value).toFixed(2) : null; break default: break; } return att; }) return c; }); }; return state.allCases; }, }, actions: { /** * fetch Log Conformance Parameters api for Rule Settings. */ async getLogConformanceParams() { let logId = this.conformanceLogId; const api = `/api/log-checkers/params?log_id=${logId}`; try { const response = await this.$axios.get(api); this.allConformanceTask = response.data.tasks; } catch(error) { await delay(); loading.isLoading = true; await delay(1000); loading.isLoading = false; await delay(500); $toast.default('Failed to load the Conformance Parameters.',{position: 'bottom'}); } }, /** * Creates a new temporary checker for a log. */ async addLogConformanceTempCheckerId(data) { let logId = this.conformanceLogId; const api = `/api/temp-log-checkers?log_id=${logId}`; try { const response = await this.$axios.post(api, data); this.conformanceTempCheckerId = response.data.id; } catch(error) { await delay(); loading.isLoading = true; await delay(1000); loading.isLoading = false; await delay(500); $toast.default('Failed to add the Temporary Checker for a log.',{position: 'bottom'}); } }, /** * Get the Temporary Log Conformance Report */ async getLogConformanceTempReport() { let checkerId = this.conformanceTempCheckerId; const api = `/api/temp-log-checkers/${checkerId}`; try { const response = await this.$axios.get(api); this.allConformanceTempReportData = response.data; } catch(error) { await delay(); loading.isLoading = true; await delay(1000); loading.isLoading = false; await delay(500); $toast.default('Failed to Get the Temporary Log Conformance Report.',{position: 'bottom'}); } }, /** * Get the detail of a temporary log conformance issue. */ async getLogConformanceIssue(issueNo) { let checkerId = this.conformanceTempCheckerId; const api = `/api/temp-log-checkers/${checkerId}/issues/${issueNo}`; try { const response = await this.$axios.get(api); this.allIssueTraces = response.data.traces; } catch(error) { await delay(); loading.isLoading = true; await delay(1000); loading.isLoading = false; await delay(500); $toast.default('Failed to Get the detail of a temporary log conformance issue.',{position: 'bottom'}); } }, /** * Get the Trace Details of a Temporary Log Conformance lssue. */ async getLogConformanceTraceDetail(issueNo, traceId, start, pageSize) { let checkerId = this.conformanceTempCheckerId; const api = `/api/temp-log-checkers/${checkerId}/issues/${issueNo}/traces/${traceId}?start=${start}&page_size=${pageSize}`; try { const response = await this.$axios.get(api); this.allTaskSeq = response.data.task_seq; this.allCases = response.data.cases; } catch(error) { await delay(); loading.isLoading = true; await delay(1000); loading.isLoading = false; await delay(500); $toast.default('Failed to Get the detail of a temporary log conformance issue.',{position: 'bottom'}); } }, }, })