/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF unknown KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BaseElement, element } from '../../base-ui/BaseElement';
import { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil';
import { threadPool } from '../database/SqlLite';
import { SpAiAnalysisPageHtml } from './SpAiAnalysisPage.html'
import { getTimeString } from './trace/sheet/TabPaneCurrentSelection';
import { WebSocketManager } from '../../webSocket/WebSocketManager';
import { TypeConstants } from '../../webSocket/Constants';
import { TraceRow } from './trace/base/TraceRow';
import { SpSystemTrace } from './SpSystemTrace';
@element('sp-ai-analysis')
export class SpAiAnalysisPage extends BaseElement {
private askQuestion: Element | null | undefined;
private aiAnswerBox: HTMLDivElement | null | undefined;
private newChatEl: HTMLImageElement | null | undefined;
private chatWindow: HTMLDivElement | null | undefined;
private inputEl: HTMLTextAreaElement | null | undefined;
private chatImg: HTMLImageElement | null | undefined;
private reportBar: HTMLImageElement | null | undefined;
private reportImg: HTMLImageElement | null | undefined;
private sendImg: HTMLImageElement | null | undefined;
private draftBtn: HTMLDivElement | null | undefined;
private downloadBtn: HTMLDivElement | null | undefined;
private draftList: HTMLDivElement | null | undefined;
private noDataEl: HTMLDivElement | null | undefined;
private loginTipEl: HTMLDivElement | null | undefined;
private loadingItem: HTMLDivElement | null | undefined;
private startTimeEl: HTMLSpanElement | null | undefined;
private endTimeEl: HTMLSpanElement | null | undefined;
private question: string = '';
private token: string = '';
// 是否点击了新建聊天
private isNewChat: boolean = false;
isCtrlDown: boolean = false;
static isRepeatedly: boolean = false;
// 拼接下载内容
private reportContent: string = '';
private md: unknown;
// 监听选中时间范围变化
static selectChangeListener(startTime: number, endTime: number) {
let startEl = document.querySelector("body > sp-application")!.shadowRoot!.querySelector("#sp-ai-analysis")!.shadowRoot?.querySelector("div.chatBox > div > div.report_details > div.selectionBox > div.startBox > span");
startEl!.innerHTML = getTimeString(startTime).toString();
let endEl = document.querySelector("body > sp-application")!.shadowRoot!.querySelector("#sp-ai-analysis")!.shadowRoot?.querySelector("div.chatBox > div > div.report_details > div.selectionBox > div.endBox > span");
endEl!.innerHTML = getTimeString(endTime).toString();
}
initElements(): void {
this.md = require('markdown-it')({
html: true,
typographer: true
});
let chatBar = this.shadowRoot?.querySelector('.chatBar');
this.askQuestion = this.shadowRoot?.querySelector('.ask_question');
this.reportBar = this.shadowRoot?.querySelector('.report');
let reportDetails = this.shadowRoot?.querySelector('.report_details');
let chatInputBox = this.shadowRoot?.querySelector('.chatInputBox');
this.chatWindow = this.shadowRoot?.querySelector('.ask_question');
this.inputEl = this.shadowRoot?.querySelector('.inputText');
this.chatImg = this.shadowRoot?.querySelector('.chatBar')?.getElementsByTagName('img')[0];
this.reportImg = this.shadowRoot?.querySelector('.report')?.getElementsByTagName('img')[0];
this.sendImg = document.querySelector("body > sp-application")!.shadowRoot!.querySelector("#sp-ai-analysis")!.shadowRoot?.querySelector("div.chatInputBox > div.chatInput > img");
this.newChatEl = document.querySelector("body > sp-application")!.shadowRoot!.querySelector("#sp-ai-analysis")!.shadowRoot?.querySelector("div.chatBox > div > div.chatInputBox > div.chatConfig > div > div.newChat > img");
// 诊断按钮
this.draftBtn = this.shadowRoot?.querySelector('.analysisBtn');
// 下载报告按钮
this.downloadBtn = this.shadowRoot?.querySelector('.downloadBtn');
// 报告列表
this.draftList = this.shadowRoot?.querySelector('.data-record');
// 空数据页面
this.noDataEl = this.shadowRoot?.querySelector('.no-data');
// 未连接提示弹窗
this.loginTipEl = this.shadowRoot?.querySelector('.loginTip');
// 时间展示区域
this.startTimeEl = this.shadowRoot?.querySelector('.startTime');
this.startTimeEl!.innerHTML = getTimeString(TraceRow.range?.startNS!);
this.endTimeEl = this.shadowRoot?.querySelector('.endTime');
this.endTimeEl!.innerHTML = getTimeString(TraceRow.range?.endNS!);
// 发送消息图标点击事件
this.sendImg?.addEventListener('click', () => {
this.sendMessage();
})
// 新建对话按钮点击事件
this.newChatEl?.addEventListener('click', () => {
this.isNewChat = true;
this.token = '';
this.askQuestion!.innerHTML = '';
this.createAiChatBox('有什么可以帮助您的吗?');
})
// 输入框发送消息
this.inputEl?.addEventListener('keydown', (e) => {
if (e.key.toLocaleLowerCase() === 'control' || e.keyCode === 17) {
this.isCtrlDown = true;
}
if (this.isCtrlDown) {
if (e.key.toLocaleLowerCase() === 'enter') {
this.inputEl!.value += '\n';
}
} else {
if (e.key.toLocaleLowerCase() === 'enter') {
this.sendMessage();
// 禁止默认的回车换行
e.preventDefault();
};
};
});
// 输入框聚焦/失焦--防止触发页面快捷键
this.inputEl?.addEventListener('focus', () => {
SpSystemTrace.isAiAsk = true;
});
this.inputEl?.addEventListener('blur', () => {
SpSystemTrace.isAiAsk = false;
})
// 监听浏览器刷新,清除db数据
window.onbeforeunload = function () {
caches.delete(`${window.localStorage.getItem('fileName')}.db`);
sessionStorage.removeItem('fileName');
}
// 监听ctrl抬起
this.inputEl?.addEventListener('keyup', (e) => {
if (e.key.toLocaleLowerCase() === 'control' || e.keyCode === 17) {
this.isCtrlDown = false;
};
});
// 下载诊断报告按钮监听
this.downloadBtn?.addEventListener('click', (e) => {
let a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([this.reportContent]));
a.download = window.sessionStorage.getItem('fileName')! + '诊断报告';
a.click();
})
// 诊断按钮
this.draftBtn?.addEventListener('click', async (e) => {
// 取消已经存在的诊断
this.draftList!.innerHTML = '';
// 没有登陆,弹窗提示,退出逻辑
if (!WebSocketManager.getInstance()?.isReady()) {
this.loginTipEl!.style.visibility = 'visible';
setTimeout(() => {
this.loginTipEl!.style.visibility = 'hidden';
}, 1000);
return;
}
// 同一个trace非第一次诊断,无需再发db文件过去
if (SpAiAnalysisPage.isRepeatedly) {
this.initiateDiagnosis();
} else {
// 首次诊断
WebSocketManager.getInstance()!.registerMessageListener(TypeConstants.DIAGNOSIS_TYPE, this.webSocketCallBack);
// 看缓存中有没有db,没有的话拿一个进行诊断并存缓存
let fileName = sessionStorage.getItem('fileName');
caches.match(`${fileName}.db`).then(async (res) => {
if (!res) {
this.cacheDb(fileName);
}
WebSocketManager.getInstance()!.sendMessage(TypeConstants.DIAGNOSIS_TYPE, TypeConstants.SENDDB_CMD, new TextEncoder().encode(await res!.text()));
});
};
// 隐藏nodata
this.noDataEl!.style.display = 'none';
// 加载中的loading模块
let loadingDiv = document.createElement('div');
loadingDiv.className = 'loadingBox';
loadingDiv.innerHTML = '`
let newQuestion = document.createElement('div');
newQuestion.className = "systemSay";
// @ts-ignore
newQuestion!.innerHTML = `