1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-08 17:40:08 +08:00

129 lines
3.7 KiB
Vue
Raw Normal View History

2022-11-04 19:02:15 +08:00
<template>
<div>
<div>
<el-select @change="searchLogs" style="width: 10%; float: left" v-model="logSearch.mode">
<el-option v-for="item in timeOptions" :key="item.label" :value="item.value" :label="item.label" />
</el-select>
<div style="margin-left: 20px; float: left">
<el-checkbox border v-model="logSearch.isWatch">{{ $t('commons.button.watch') }}</el-checkbox>
</div>
<el-button style="margin-left: 20px" @click="onDownload" icon="Download">
{{ $t('file.download') }}
</el-button>
</div>
<codemirror
:autofocus="true"
placeholder="None data"
:indent-with-tab="true"
:tabSize="4"
2023-01-09 10:38:54 +08:00
style="margin-top: 10px; max-height: 700px"
2022-11-04 19:02:15 +08:00
:lineWrapping="true"
:matchBrackets="true"
theme="cobalt"
:styleActiveLine="true"
:extensions="extensions"
2022-12-23 15:38:13 +08:00
@ready="handleReady"
2022-11-04 19:02:15 +08:00
v-model="logInfo"
:readOnly="true"
/>
</div>
</template>
<script lang="ts" setup>
import { logContainer } from '@/api/modules/container';
import i18n from '@/lang';
2023-01-16 15:55:53 +08:00
import { dateFormatForName } from '@/utils/util';
2022-12-23 15:38:13 +08:00
import { nextTick, reactive, ref, shallowRef } from 'vue';
2022-11-04 19:02:15 +08:00
import { Codemirror } from 'vue-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { oneDark } from '@codemirror/theme-one-dark';
const extensions = [javascript(), oneDark];
const logInfo = ref();
2022-12-23 15:38:13 +08:00
const view = shallowRef();
const handleReady = (payload) => {
view.value = payload.view;
};
2022-11-04 19:02:15 +08:00
const logSearch = reactive({
isWatch: false,
container: '',
containerID: '',
mode: 'all',
});
let timer: NodeJS.Timer | null = null;
const timeOptions = ref([
{ label: i18n.global.t('container.all'), value: 'all' },
{
label: i18n.global.t('container.lastDay'),
value: new Date(new Date().getTime() - 3600 * 1000 * 24 * 1).getTime() / 1000 + '',
},
{
label: i18n.global.t('container.last4Hour'),
value: new Date(new Date().getTime() - 3600 * 1000 * 4).getTime() / 1000 + '',
},
{
label: i18n.global.t('container.lastHour'),
value: new Date(new Date().getTime() - 3600 * 1000).getTime() / 1000 + '',
},
{
label: i18n.global.t('container.last10Min'),
value: new Date(new Date().getTime() - 600 * 1000).getTime() / 1000 + '',
},
]);
const onCloseLog = async () => {
2022-12-20 15:23:48 +08:00
logSearch.isWatch = false;
2022-11-04 19:02:15 +08:00
clearInterval(Number(timer));
2022-12-20 15:23:48 +08:00
timer = null;
2022-11-04 19:02:15 +08:00
};
const searchLogs = async () => {
const res = await logContainer(logSearch);
logInfo.value = res.data;
2022-12-23 15:38:13 +08:00
nextTick(() => {
const state = view.value.state;
view.value.dispatch({
selection: { anchor: state.doc.length, head: state.doc.length },
scrollIntoView: true,
});
});
2022-11-04 19:02:15 +08:00
};
const onDownload = async () => {
const downloadUrl = window.URL.createObjectURL(new Blob([logInfo.value]));
const a = document.createElement('a');
a.style.display = 'none';
a.href = downloadUrl;
2023-01-16 15:55:53 +08:00
a.download = logSearch.container + '-' + dateFormatForName(new Date()) + '.log';
2022-11-04 19:02:15 +08:00
const event = new MouseEvent('click');
a.dispatchEvent(event);
};
interface DialogProps {
2022-12-02 18:52:43 +08:00
container: string;
2022-11-04 19:02:15 +08:00
containerID: string;
}
const acceptParams = (props: DialogProps): void => {
logSearch.containerID = props.containerID;
logSearch.mode = 'all';
logSearch.isWatch = false;
2022-12-02 18:52:43 +08:00
logSearch.container = props.container;
2022-11-04 19:02:15 +08:00
searchLogs();
timer = setInterval(() => {
if (logSearch.isWatch) {
searchLogs();
}
}, 1000 * 5);
};
defineExpose({
acceptParams,
onCloseLog,
});
</script>