fix: set the duration and scroll position for on-demand logs (#104)

This commit is contained in:
Fine0830 2022-06-07 21:54:29 +08:00 committed by GitHub
parent c875c95c20
commit 3c37d7c197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 29 deletions

View File

@ -30,8 +30,6 @@ export const queryStreamingLogs = {
logs: ondemandPodLogs(condition: $condition) { logs: ondemandPodLogs(condition: $condition) {
errorReason errorReason
logs { logs {
timestamp
contentType
content content
} }
}`, }`,

View File

@ -31,6 +31,7 @@ interface DemandLogState {
logs: Log[]; logs: Log[];
loadLogs: boolean; loadLogs: boolean;
message: string; message: string;
total: number;
} }
export const demandLogStore = defineStore({ export const demandLogStore = defineStore({
@ -47,6 +48,7 @@ export const demandLogStore = defineStore({
logs: [], logs: [],
loadLogs: false, loadLogs: false,
message: "", message: "",
total: 0,
}), }),
actions: { actions: {
setLogCondition(data: Conditions) { setLogCondition(data: Conditions) {
@ -109,6 +111,7 @@ export const demandLogStore = defineStore({
this.setLogs("", res.data.data.logs.errorReason); this.setLogs("", res.data.data.logs.errorReason);
return res.data; return res.data;
} }
this.total = res.data.data.logs.logs.length;
const logs = res.data.data.logs.logs const logs = res.data.data.logs.logs
.map((d: Log) => d.content) .map((d: Log) => d.content)
.join("\n"); .join("\n");

View File

@ -31,20 +31,25 @@ const demandLogStore = useDemandLogStore();
const monacoInstance = ref(); const monacoInstance = ref();
const logContent = ref<Nullable<HTMLDivElement>>(null); const logContent = ref<Nullable<HTMLDivElement>>(null);
onMounted(async () => { onMounted(() => {
init();
});
async function init() {
const monaco = await import("monaco-editor"); const monaco = await import("monaco-editor");
monacoInstanceGen(monaco); monacoInstanceGen(monaco);
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
editorLayout(); editorLayout();
}); });
}); }
function monacoInstanceGen(monaco: any) { function monacoInstanceGen(monaco: any) {
monacoInstance.value = monaco.editor.create(logContent.value, { monacoInstance.value = monaco.editor.create(logContent.value, {
value: "", value: "",
language: "text", language: "text",
wordWrap: true, wordWrap: true,
minimap: { enabled: false }, minimap: { enabled: false },
readonly: true,
}); });
toRaw(monacoInstance.value).updateOptions({ readOnly: true });
} }
function editorLayout() { function editorLayout() {
if (!logContent.value) { if (!logContent.value) {
@ -66,7 +71,19 @@ onUnmounted(() => {
watch( watch(
() => demandLogStore.logs, () => demandLogStore.logs,
() => { () => {
if (!toRaw(monacoInstance.value)) {
return;
}
toRaw(monacoInstance.value).setValue(demandLogStore.logs); toRaw(monacoInstance.value).setValue(demandLogStore.logs);
if (!demandLogStore.logs) {
return;
}
setTimeout(() => {
toRaw(monacoInstance.value).revealPosition({
column: 1,
lineNumber: demandLogStore.total,
});
}, 1000);
} }
); );
</script> </script>

View File

@ -110,11 +110,6 @@ limitations under the License. -->
v-model="excludingContentStr" v-model="excludingContentStr"
@change="addLabels('excludingKeywordsOfContent')" @change="addLabels('excludingKeywordsOfContent')"
/> />
<el-tooltip :content="t('keywordsOfContentLogTips')">
<span class="log-tips">
<Icon size="middle" iconName="help" class="ml-5 help" />
</span>
</el-tooltip>
</div> </div>
</div> </div>
<div class="flex-h row btn-row"> <div class="flex-h row btn-row">
@ -135,7 +130,7 @@ limitations under the License. -->
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref, reactive, watch, computed, onMounted, onUnmounted } from "vue"; import { ref, reactive, watch, onMounted, onUnmounted } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { useDemandLogStore } from "@/store/modules/demand-log"; import { useDemandLogStore } from "@/store/modules/demand-log";
import { useDashboardStore } from "@/store/modules/dashboard"; import { useDashboardStore } from "@/store/modules/dashboard";
@ -165,21 +160,6 @@ const state = reactive<any>({
}); });
/*global Nullable */ /*global Nullable */
const intervalFn = ref<Nullable<any>>(null); const intervalFn = ref<Nullable<any>>(null);
const rangeTime = computed(() => {
const times = {
start: getLocalTime(
appStore.utc,
new Date(new Date().getTime() - state.duration.value * 1000)
),
end: getLocalTime(appStore.utc, new Date()),
step: "SECOND",
};
return {
start: dateFormatStep(times.start, times.step, false),
end: dateFormatStep(times.end, times.step, false),
step: times.step,
};
});
onMounted(() => { onMounted(() => {
fetchSelectors(); fetchSelectors();
@ -195,8 +175,16 @@ async function fetchSelectors() {
} }
} }
async function getContainers() { async function getContainers() {
if (
!(
state.instance.id ||
(selectorStore.currentPod && selectorStore.currentPod.id)
)
) {
return;
}
const resp = await demandLogStore.getContainers( const resp = await demandLogStore.getContainers(
state.instance.id || selectorStore.currentPod.id || "" state.instance.id || selectorStore.currentPod.id
); );
if (resp.errors) { if (resp.errors) {
ElMessage.error(resp.errors); ElMessage.error(resp.errors);
@ -229,14 +217,16 @@ function runInterval() {
}, state.duration.value * 1000); }, state.duration.value * 1000);
} }
function searchLogs() { function searchLogs() {
let instance = state.instance.id; let instance = "";
if (dashboardStore.entity === EntityType[3].value) { if (dashboardStore.entity === EntityType[3].value) {
instance = selectorStore.currentPod.id; instance = selectorStore.currentPod.id;
} }
const serviceInstanceId =
instance || (state.instance && state.instance.id) || "";
demandLogStore.setLogCondition({ demandLogStore.setLogCondition({
serviceInstanceId: instance || state.instance.id || "", serviceInstanceId,
container: state.container.value, container: state.container.value,
duration: rangeTime.value, duration: rangeTime(),
keywordsOfContent: keywordsOfContent.value.length keywordsOfContent: keywordsOfContent.value.length
? keywordsOfContent.value ? keywordsOfContent.value
: undefined, : undefined,
@ -244,8 +234,30 @@ function searchLogs() {
? excludingKeywordsOfContent.value ? excludingKeywordsOfContent.value
: undefined, : undefined,
}); });
if (!serviceInstanceId) {
return;
}
queryLogs(); queryLogs();
} }
function rangeTime() {
{
const times = {
start: getLocalTime(
appStore.utc,
new Date(new Date().getTime() - state.duration.value * 1000)
),
end: getLocalTime(appStore.utc, new Date()),
step: "SECOND",
};
return {
start: dateFormatStep(times.start, times.step, false),
end: dateFormatStep(times.end, times.step, false),
step: times.step,
};
}
}
async function queryLogs() { async function queryLogs() {
const res = await demandLogStore.getDemandLogs(); const res = await demandLogStore.getDemandLogs();
if (res && res.errors) { if (res && res.errors) {