优化错误收集

This commit is contained in:
xh
2025-06-26 00:23:35 +08:00
parent edcb95787e
commit 2d473ac4ed
17 changed files with 130 additions and 143 deletions

File diff suppressed because one or more lines are too long

View File

@@ -18,8 +18,7 @@ export type type_monitor_client = {
City?: string
Operator?: string
Ip?: string
Width?: number
Height?: number
Ua?: string
CreateTime?: string
}
@@ -35,8 +34,7 @@ export type type_monitor_client_query = {
City?: string
Operator?: string
Ip?: string
Width?: number
Height?: number
Ua?: string
CreateTimeStart?: string
CreateTimeEnd?: string
@@ -54,8 +52,7 @@ export type type_monitor_client_edit = {
City?: string
Operator?: string
Ip?: string
Width?: number
Height?: number
Ua?: string
}

View File

@@ -23,7 +23,7 @@ interface Props {
}
const props = withDefaults(defineProps<Props>(), {
modelValue: () => ({}),
pageSizes: () => [15, 20, 30, 40],
pageSizes: () => [10, 15, 20, 30, 40],
layout: 'total, sizes, prev, pager, next, jumper'
})

View File

@@ -21,7 +21,7 @@ interface Options {
*
*/
export function usePaging<T>(options: Options) {
const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options
const { page = 1, size = 10, fetchFun, params = {}, firstLoading = false } = options
// 记录分页初始参数
const paramsInit: Record<any, any> = Object.assign({}, toRaw(params))
// 分页数据

View File

@@ -172,10 +172,6 @@
<el-table-column label="城市" prop="City" /> -->
<el-table-column label="运营商" prop="Operator" />
<el-table-column label="屏幕" prop="Width">
<template #default="{ row }"> {{ row.Width }} * {{ row.Height }} </template>
</el-table-column>
<!-- <el-table-column label="屏幕高度" prop="Height" min-width="130" /> -->
<!-- <el-table-column label="ua记录" prop="Ua" min-width="380" /> -->
<el-table-column label="创建时间" prop="CreateTime" min-width="140" />
@@ -238,8 +234,7 @@ const queryParams = reactive<type_monitor_client_query>({
City: null,
Operator: null,
Ip: null,
Width: null,
Height: null,
Ua: null,
CreateTimeStart: null,
CreateTimeEnd: null

View File

@@ -154,17 +154,16 @@ func (hd *MonitorClientHandler) Add(c *gin.Context) {
ip := c.ClientIP()
if err == nil {
last := lastClient.UserId + lastClient.Width.String() + lastClient.Height.String() + lastClient.Ip + lastClient.Ua
newStr := *addReq.UserId + addReq.Width.String() + addReq.Height.String() + ip + uaStr
last := lastClient.UserId + "_" + lastClient.Ip + "_" + lastClient.Ua
newStr := *addReq.UserId + "_" + ip + "_" + uaStr
if last == newStr {
// 前后数据一样,不用创建新的数据
fmt.Println("前后数据一样,不用创建新的数据")
fmt.Println("前后用户端数据一样,不用创建新的数据")
c.Data(200, "image/gif", img_util.EmptyGif())
return
} else {
// 新建的话需要清除lastClient对应的缓存
monitorService.MonitorClientService.CacheUtil.RemoveCache("ClientId:" + lastClient.ClientId)
}
}

View File

@@ -17,8 +17,7 @@ type MonitorClient struct {
City string `gorm:"comment:'城市'"` // 城市
Operator string `gorm:"comment:'电信运营商'"` // 电信运营商
Ip string `gorm:"comment:'ip'"` // ip
Width core.NullInt `gorm:"comment:'屏幕'"` // 屏幕
Height core.NullInt `gorm:"comment:'屏幕高度'"` // 屏幕高度
Ua string `gorm:"comment:'ua记录'"` // ua记录
CreateTime core.NullTime `gorm:"autoCreateTime;comment:'创建时间'"` // 创建时间
// ClientTime core.NullTime `gorm:"comment:'更新时间'"` // 更新时间

View File

@@ -9,5 +9,7 @@ type MonitorErrorList struct {
Id int `gorm:"primarykey;comment:'id'"`
Eid string `gorm:"comment:'错误id'"` // 错误表id
Cid string `gorm:"comment:'客户端id'"` // 客户端表id
Width core.NullInt `gorm:"comment:'屏幕'"` // 屏幕
Height core.NullInt `gorm:"comment:'屏幕高度'"` // 屏幕高度
CreateTime core.NullTime `gorm:"autoCreateTime;comment:'创建时间'"` // 创建时间
}

View File

@@ -45,7 +45,7 @@ INSERT INTO x_system_auth_menu (pid, menu_type, menu_name, perms,is_cache, is_sh
// MonitorErrorRoute(rg)
func MonitorErrorRoute(rg *gin.RouterGroup) {
handle := monitorController.MonitorErrorHandler{}
rg.GET("/monitor_error/add", middleware.RecordLog("监控-错误列新增"), handle.Add)
rg.GET("/monitor_error/add", handle.Add)
r := rg.Group("/", middleware.TokenAuth())
r.GET("/monitor_error/list", handle.List)

View File

@@ -18,12 +18,9 @@ type MonitorClientListReq struct {
Operator *string // 电信运营商
Ip *string // ip
Width *int // 屏幕
Height *int // 屏幕高度
Ua *string // ua记录
CreateTimeStart *string // 开始创建时间
CreateTimeEnd *string // 结束创建时间
}
// MonitorClientAddReq 监控-客户端信息新增参数
@@ -38,8 +35,6 @@ type MonitorClientAddReq struct {
City *string // 城市
Operator *string // 电信运营商
Ip *string // ip
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
Ua *string // ua记录
}
@@ -57,8 +52,6 @@ type MonitorClientEditReq struct {
City *string // 城市
Operator *string // 电信运营商
Ip *string // ip
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
Ua *string // ua记录
}
@@ -90,8 +83,9 @@ type MonitorClientResp struct {
City string // 城市
Operator string // 电信运营商
Ip string // ip
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
Ua string // ua记录
CreateTime core.NullTime // 创建时间
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
}

View File

@@ -8,6 +8,8 @@ import (
type MonitorErrorListAddReq struct {
Eid string // 错误id
Cid string // 客户端id
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
// ProjectKey string // 项目id
}
@@ -16,6 +18,8 @@ type MonitorErrorListResp struct {
Id int // 项目id
Eid string // 错误id
Cid string // 客户端id
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
// ProjectKey string // 项目id
CreateTime core.NullTime // 创建时间
}

View File

@@ -12,6 +12,7 @@ type MonitorErrorListReq struct {
Message *string // 错误消息
Stack *string // 错误堆栈
Md5 *string // md5
CreateTimeStart *string // 开始创建时间
CreateTimeEnd *string // 结束创建时间
@@ -25,6 +26,8 @@ type MonitorErrorAddReq struct {
Path *string // URL地址
Message *string // 错误消息
Stack *string // 错误堆栈
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
}
// MonitorErrorDetailReq 监控-错误列详情参数
@@ -50,6 +53,8 @@ type MonitorErrorResp struct {
Path string // URL地址
Message string // 错误消息
Stack string // 错误堆栈
Width core.NullInt // 屏幕
Height core.NullInt // 屏幕高度
Md5 string // md5
CreateTime core.NullTime // 创建时间
}

View File

@@ -66,12 +66,7 @@ func (service monitorClientService) GetModel(listReq MonitorClientListReq) *gorm
if listReq.Ip != nil {
dbModel = dbModel.Where("ip = ?", *listReq.Ip)
}
if listReq.Width != nil {
dbModel = dbModel.Where("width = ?", *listReq.Width)
}
if listReq.Height != nil {
dbModel = dbModel.Where("height = ?", *listReq.Height)
}
if listReq.Ua != nil {
dbModel = dbModel.Where("ua = ?", *listReq.Ua)
}
@@ -170,8 +165,8 @@ func (service monitorClientService) Detail(Id int) (res MonitorClientResp, e err
// ErrorUser 监控-客户端信息详情
func (service monitorClientService) ErrorUsers(error_id int) (res []MonitorClientResp, e error) {
var obj = []model.MonitorClient{}
service.db.Raw("SELECT client.*,list.create_time AS create_time from x_monitor_error_list as list right join x_monitor_client as client on client.id = list.cid where list.eid = ? Order by list.id DESC LIMIT 0,20", error_id).Scan(&obj)
var obj = []MonitorClientResp{}
service.db.Raw("SELECT client.*,list.width,list.height,list.create_time AS create_time from x_monitor_error_list as list right join x_monitor_client as client on client.id = list.cid where list.eid = ? Order by list.id DESC LIMIT 0,20", error_id).Scan(&obj)
convert_util.Copy(&res, obj)
return

View File

@@ -111,8 +111,7 @@ func (service monitorErrorService) ListAll(listReq MonitorErrorListReq) (res []M
// Detail 监控-错误列详情
func (service monitorErrorService) Detail(Id int) (res MonitorErrorResp, e error) {
var obj = model.MonitorError{}
err := service.CacheUtil.GetCache(Id, &obj)
if err != nil {
err := service.db.Where("id = ?", Id).Limit(1).First(&obj).Error
if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
return
@@ -120,8 +119,6 @@ func (service monitorErrorService) Detail(Id int) (res MonitorErrorResp, e error
if e = response.CheckErr(err, "获取详情失败"); e != nil {
return
}
service.CacheUtil.SetCache(obj.Id, obj)
}
convert_util.Copy(&res, obj)
return
@@ -165,7 +162,7 @@ func (service monitorErrorService) Add(addReq MonitorErrorAddReq) (createId int,
return 0, err
}
createId = obj.Id
service.CacheUtil.SetCache(createId, obj)
service.CacheUtil.SetCache("md5:"+Md5, obj)
} else {
createId = errorDetails.Id
@@ -178,6 +175,8 @@ func (service monitorErrorService) Add(addReq MonitorErrorAddReq) (createId int,
_, err = MonitorErrorListService.Add(MonitorErrorListAddReq{
Eid: strconv.Itoa(createId),
Cid: strconv.Itoa(client.Id),
Width: addReq.Width,
Height: addReq.Height,
// ClientId: addReq.ClientId,
// ProjectKey: addReq.ProjectKey,
})
@@ -199,7 +198,7 @@ func (service monitorErrorService) Del(Id int) (e error) {
// 删除
err = service.db.Delete(&obj).Error
e = response.CheckErr(err, "删除失败")
service.CacheUtil.RemoveCache(obj.Id)
service.CacheUtil.RemoveCache("md5:" + obj.Md5)
return
}
@@ -225,7 +224,7 @@ func (service monitorErrorService) DelBatch(Ids []string) (e error) {
md5s = append(md5s, "md5:"+v.Md5)
}
// 删除缓存
service.CacheUtil.RemoveCache(Ids)
service.CacheUtil.RemoveCache(md5s)
return nil
}

View File

@@ -1,19 +1,15 @@
export type LogWithEnv = {
Type: "env";
ScreenHeight?: number;
ScreenWidth?: number;
Height?: number;
Width?: number;
};
export type LogWithError = {
Type: "error"|"event"|"resources"|'click';
export type LogWithError =LogWithEnv& {
Type: "error" | "event" | "resources" | "click";
EventType: string;
Path: string;
Message?: string;
Stack?: string;
};
// 扩展必须实现的接口
export interface IErrorEvent {
upload(url: string, data: object): Promise<void>;

View File

@@ -19,6 +19,8 @@ class Base {
private MessageList: any[] = [];
private timer: number = 0;
private lastTimeStamp: number = Date.now(); //上次上报时间戳
private diffTime: number = 1000 * 10; // 上报间隔
constructor(props: Props, platform: IErrorEvent) {
if (!props) {
@@ -50,10 +52,12 @@ class Base {
this.Push(params);
});
// 定时检查发送一次
this.timer = setInterval(() => {
if (Date.now() - this.lastTimeStamp >= this.diffTime) {
this.upload();
}, 1000 * 10);
}
}, 2000);
}
// 设置用户id
@@ -67,15 +71,9 @@ class Base {
this.Uid = u_id;
}
}
this.initEnv();
}
// 初始化环境信息并上传
private initEnv() {
let envInfo = this.platform?.getEnvInfo();
if (envInfo) {
this.uploadInfo(envInfo);
}
this.uploadInfo();
}
// 设置错误ID
private setClientID() {
const client_id = this.platform?.getCache("x_err_client_id");
@@ -109,7 +107,7 @@ class Base {
this.platform?.setCache("x_err_message_list", this.MessageList);
}
};
public uploadInfo = (envInfo: LogWithEnv) => {
public uploadInfo = () => {
if (!this.Dns) return; //未设置Dns服务器不上传
try {
@@ -118,8 +116,8 @@ class Base {
ProjectKey: this.Pid,
ClientId: this.client_id,
UserId: this.Uid,
Width: envInfo.ScreenWidth,
Height: envInfo.ScreenHeight,
// Width: envInfo.ScreenWidth,
// Height: envInfo.ScreenHeight,
})
.catch((err: any) => {
// 上传失败
@@ -133,14 +131,17 @@ class Base {
if (!this.MessageList.length) {
return;
}
this.lastTimeStamp = Date.now();
try {
this.platform
?.upload(this.Dns + `/admin/monitor_error/add`, this.MessageList)
.catch((err: any) => {
// 上传失败
});
})
.then(() => {
this.MessageList = [];
this.platform?.delCache("x_err_message_list");
});
} catch (error) {}
}
public unListen() {

View File

@@ -2,11 +2,10 @@ import type {
LogWithError,
LogWithEnv,
ListenCallbackFn,
IErrorEvent
IErrorEvent,
} from "../types";
interface LoggerProps {
}
interface LoggerProps {}
class Web implements IErrorEvent {
props: LoggerProps;
constructor(props?: LoggerProps) {
@@ -54,13 +53,12 @@ class Web implements IErrorEvent {
public getEnvInfo(): LogWithEnv {
const env: LogWithEnv = {
Type: "env",
ScreenHeight: 0,
ScreenWidth: 0,
Height: 0,
Width: 0,
};
if (window) {
env.ScreenHeight = window.innerHeight || 0; // 获取显示屏信息
env.ScreenWidth = window.innerWidth || 0;
env.Height = window.innerHeight || 0; // 获取显示屏信息
env.Width = window.innerWidth || 0;
}
return env;
}
@@ -76,6 +74,7 @@ class Web implements IErrorEvent {
Path: target.src,
Message: "",
Stack: "",
Height: 0,
});
} else if (target?.localName === "link") {
this.callback({
@@ -91,6 +90,7 @@ class Web implements IErrorEvent {
Path: window.location.href,
Message: err.message,
Stack: this.handleStack(err.error?.stack || ""),
...this.getEnvInfo(),
});
}
};
@@ -103,6 +103,7 @@ class Web implements IErrorEvent {
Path: window.location.href,
Message: err.reason,
Stack: "",
...this.getEnvInfo(),
});
} else if (err && typeof err.reason === "object") {
this.callback({
@@ -111,11 +112,11 @@ class Web implements IErrorEvent {
Path: window.location.href,
Message: err.reason?.message || "",
Stack: this.handleStack(err.reason?.stack || ""),
...this.getEnvInfo(),
});
}
};
private handleStack(stack: string): string {
let newStack: string[] = [];
if (stack) {