feat: upload done.

This commit is contained in:
chiayin
2023-12-29 16:05:27 +08:00
parent cd2ab42125
commit 9ef441ee83
8 changed files with 384 additions and 207 deletions

View File

@@ -2,37 +2,11 @@ import { defineStore } from "pinia";
import axios from "axios";
import moment from 'moment';
import apiError from '@/module/apiError.js';
import Swal from 'sweetalert2';
import { uploadFailed, uploadloader, uploadSuccess } from '@/module/alertModal.js'
export default defineStore('filesStore', {
state: () => ({
allFilter: [
{
log: {},
fileType: '',
ownerName: '',
}
],
allConformanceLog: [
{
log: {},
fileType: '',
ownerName: '',
}
],
allConformanceFilter: [
{
filter: {},
fileType: '',
ownerName: '',
}
],
allEventLog: [
{
parentLog: '',
fileType: '',
ownerName: '',
}
],
allEventFiles: [
{
parentLog: '',
@@ -48,6 +22,10 @@ export default defineStore('filesStore', {
},
filesTag: 'ALL',
httpStatus: 200,
uploadId: null,
allUploadDetail: null,
uploadLogId: null,
uploadFileName: null,
}),
getters: {
/**
@@ -62,105 +40,14 @@ export default defineStore('filesStore', {
return result;
},
/**
* Get upload preview
*/
uploadDetail: state => {
return state.allUploadDetail;
}
},
actions: {
/**
* Fetch event logs api
*/
async fetchEventLog() {
const api = '/api/logs';
try {
const response = await axios.get(api);
this.allEventLog = response.data;
this.allEventLog.map(o => {
o.icon = 'work_history';
o.parentLog = o.name;
o.fileType = "Log";
o.ownerName = o.owner.name;
o.updated_base = o.updated_at;
o.accessed_base = o.accessed_at;
o.updated_at = moment(o.updated_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm');
o.accessed_at = o.accessed_at ? moment(o.accessed_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm') : null;
return this.allEventLog
})
} catch(error) {
apiError(error, 'Failed to load the logs.');
};
},
/**
* Fetch filters api
*/
async fetchFilter() {
const api = '/api/filters';
try {
const response = await axios.get(api);
this.allFilter = response.data;
this.allFilter.map(o => {
o.icon = 'tornado';
o.parentLog = o.log.name;
o.fileType = "Filter";
o.ownerName = o.owner.name;
o.updated_base = o.updated_at;
o.accessed_base = o.accessed_at;
o.updated_at = moment(o.updated_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm');
o.accessed_at = o.accessed_at ? moment(o.accessed_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm') : null;
});
} catch(error) {
apiError(error, 'Failed to load the filters.');
};
},
/**
* Fetch Conformance Log api
*/
async fetchConformanceLog() {
const api = '/api/log-checks';
try {
const response = await axios.get(api);
this.allConformanceLog = response.data;
this.allConformanceLog.map(o => {
o.icon = 'local_police';
o.parentLog = o.log.name;
o.fileType = "Rule";
o.ownerName = o.owner.name;
o.updated_base = o.updated_at;
o.accessed_base = o.accessed_at;
o.updated_at = moment(o.updated_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm');
o.accessed_at = o.accessed_at ? moment(o.accessed_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm') : null;
});
} catch(error) {
apiError(error, 'Failed to load the filters.');
};
},
/**
* Fetch Conformance Filter api
*/
async fetchConformanceFilter() {
const api = '/api/filter-checks';
try {
const response = await axios.get(api);
this.allConformanceFilter = response.data;
this.allConformanceFilter.map(o => {
o.icon = 'local_police';
o.parentLog = o.filter.name;
o.fileType = "Rule";
o.ownerName = o.owner.name;
o.updated_base = o.updated_at;
o.accessed_base = o.accessed_at;
o.updated_at = moment(o.updated_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm');
o.accessed_at = o.accessed_at ? moment(o.accessed_at).utcOffset('+08:00').format('YYYY-MM-DD HH:mm') : null;
});
} catch(error) {
apiError(error, 'Failed to load the filters.');
};
},
/**
* Fetch All Files api
*/
@@ -192,6 +79,9 @@ export default defineStore('filesStore', {
fileType = 'Rule';
parentLog = o.parent.name;
break;
case 'design':
icon = 'shape_line';
break;
}
o.icon = icon;
o.parentLog = parentLog;
@@ -206,7 +96,92 @@ export default defineStore('filesStore', {
apiError(error, 'Failed to load the files.');
};
},
// fetchRule(){o.icon = local_police}
// fetchDesign(){o.icon = shape_line}
/**
* Uploads a CSV log file. 第一階段上傳
* @param {Object} fromData
*/
async upload(fromData) {
const api = '/api/logs/csv-uploads';
const config = {
data: true,
headers: {
'Content-Type': 'multipart/form-data',
},
};
uploadloader(); // 進度條
try {
const response = await axios.post(api, fromData, config);
this.uploadId = response.data.id;
this.$router.push({name: 'Upload'});
Swal.close(); // 關閉進度條
} catch(error) {
if(error.response.status === 422) {
// msg: 'not in UTF-8' | 'insufficient columns' | 'the csv file is empty' | 'the filename does not ends with .csv'
// type: 'encoding' | 'insufficient_columns' | 'empty' | 'name_suffix'
uploadFailed(error.response.data.detail[0].type, error.response.data.detail[0].msg);
} else {
Swal.close(); // 關閉進度條
apiError(error, 'Failed to upload the files.');
}
}
},
/**
* Fetch upload detail
*/
async getUploadDetail() {
const uploadId = this.uploadId;
const api = `/api/logs/csv-uploads/${uploadId}`;
try {
const response = await axios.get(api);
this.allUploadDetail = response.data.preview;
} catch(error) {
apiError(error, 'Failed to get upload detail.');
}
},
/**
* Add a Log from an Uploaded CSV Log File. 第二階段上傳
* @param {Object} data
*/
async uploadLog(data) {
const uploadId = this.uploadId;
const api = `/api/logs/csv-uploads/${uploadId}`;
uploadloader(); // 進度條
try {
const response = await axios.post(api, data);
this.uploadLogId = response.data.id;
Swal.close(); // 關閉進度條
await this.rename(); // 改檔名
await uploadSuccess();
this.$router.push({name: 'Files'});
} catch(error) {
console.log('error:', error);
if(error.response.status === 422) {
uploadFailed(error.response.data.detail[0].type, error.response.data.detail[0].msg);
} else {
Swal.close(); // 關閉進度條
apiError(error, 'Failed to upload the log files.');
}
}
},
/**
* Rename a Log
*/
async rename() {
const id = this.uploadLogId;
const api = `/api/logs/${id}/name`;
const data = {"name": this.uploadFileName};
try {
const response = await axios.put(api, data);
console.log('response:', response);
this.uploadFileName = null;
} catch(error) {
console.log('error:', error);
apiError(error, 'Failed to rename.');
}
}
},
})