From c06febc02a96bc279865f43c452433abe3363127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 25 Jun 2022 10:01:24 +0800 Subject: [PATCH] - update dorm - update jobs - update log --- utils/dorm/gorm.go | 5 +- utils/gojobs/etcd_worker.go | 3 +- utils/gojobs/jobs_gorm.go | 32 +++------- utils/gojobs/jobs_gorm_check_task.go | 4 +- utils/gojobs/jobs_gorm_model.go | 4 +- utils/gojobs/jobs_gorm_model/task.go | 39 ++++++------ utils/gojobs/jobs_gorm_model/task_log.go | 14 +++-- utils/gojobs/jobs_gorm_model/task_log_run.go | 24 ++++---- utils/golog/api_gorm.go | 45 +++++++------- utils/golog/gin_gorm.go | 63 ++++++++++---------- 10 files changed, 111 insertions(+), 122 deletions(-) diff --git a/utils/dorm/gorm.go b/utils/dorm/gorm.go index 619b81ba..ed64b878 100644 --- a/utils/dorm/gorm.go +++ b/utils/dorm/gorm.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/sirupsen/logrus" + "go.dtapp.net/library/utils/gotime" "gorm.io/driver/mysql" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -46,7 +47,7 @@ func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) { }, ), NowFunc: func() time.Time { - return time.Now().Local() + return gotime.Current().Now().Local() }, }) } else { @@ -93,7 +94,7 @@ func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) { }, ), NowFunc: func() time.Time { - return time.Now().Local() + return gotime.Current().Now().Local() }, }) } else { diff --git a/utils/gojobs/etcd_worker.go b/utils/gojobs/etcd_worker.go index 587ba992..1f99a6d0 100644 --- a/utils/gojobs/etcd_worker.go +++ b/utils/gojobs/etcd_worker.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "go.dtapp.net/library/utils/goip" "go.etcd.io/etcd/client/v3" "log" "time" @@ -21,7 +20,7 @@ func NewEtcdWorker(config *EtcdConfig) (*Etcd, error) { e.Endpoints = config.Endpoints e.DialTimeout = config.DialTimeout if config.LocalIP == "" { - config.LocalIP = goip.GetOutsideIp() + return nil, errors.New("需要配置客户端的ip地址,唯一性~!") } e.LocalIP = config.LocalIP e.Username = config.Username diff --git a/utils/gojobs/jobs_gorm.go b/utils/gojobs/jobs_gorm.go index cabb208c..514f863e 100644 --- a/utils/gojobs/jobs_gorm.go +++ b/utils/gojobs/jobs_gorm.go @@ -3,12 +3,11 @@ package gojobs import ( "errors" "fmt" - go_library "go.dtapp.net/library" + "go.dtapp.net/library" "go.dtapp.net/library/utils/dorm" "go.dtapp.net/library/utils/goarray" "go.dtapp.net/library/utils/goip" "go.dtapp.net/library/utils/gojobs/jobs_gorm_model" - "go.dtapp.net/library/utils/gotime" "go.dtapp.net/library/utils/gouuid" "gorm.io/gorm" "log" @@ -85,17 +84,15 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { StatusCode: status, Desc: desc, Version: j.runVersion, - CreatedAt: gotime.Current().Format(), }) if statusCreate.RowsAffected == 0 { log.Println("statusCreate", statusCreate.Error) } if status == 0 { statusEdit := j.EditTask(j.db, info.Id). - Select("run_id", "updated_at"). + Select("run_id"). Updates(jobs_gorm_model.Task{ - RunId: gouuid.GetUuId(), - UpdatedAt: gotime.Current().Format(), + RunId: gouuid.GetUuId(), }) if statusEdit.RowsAffected == 0 { log.Println("statusEdit", statusEdit.Error) @@ -106,13 +103,12 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { if status == CodeSuccess { // 执行成功 statusEdit := j.EditTask(j.db, info.Id). - Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result"). + Select("status_desc", "number", "run_id", "updated_ip", "result"). Updates(jobs_gorm_model.Task{ StatusDesc: "执行成功", Number: info.Number + 1, RunId: gouuid.GetUuId(), UpdatedIp: j.outsideIp, - UpdatedAt: gotime.Current().Format(), Result: desc, }) if statusEdit.RowsAffected == 0 { @@ -122,13 +118,12 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { if status == CodeEnd { // 执行成功、提前结束 statusEdit := j.EditTask(j.db, info.Id). - Select("status", "status_desc", "number", "updated_ip", "updated_at", "result"). + Select("status", "status_desc", "number", "updated_ip", "result"). Updates(jobs_gorm_model.Task{ Status: TASK_SUCCESS, StatusDesc: "结束执行", Number: info.Number + 1, UpdatedIp: j.outsideIp, - UpdatedAt: gotime.Current().Format(), Result: desc, }) if statusEdit.RowsAffected == 0 { @@ -138,13 +133,12 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { if status == CodeError { // 执行失败 statusEdit := j.EditTask(j.db, info.Id). - Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result"). + Select("status_desc", "number", "run_id", "updated_ip", "result"). Updates(jobs_gorm_model.Task{ StatusDesc: "执行失败", Number: info.Number + 1, RunId: gouuid.GetUuId(), UpdatedIp: j.outsideIp, - UpdatedAt: gotime.Current().Format(), Result: desc, }) if statusEdit.RowsAffected == 0 { @@ -155,10 +149,9 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) { if info.Number+1 >= info.MaxNumber { // 关闭执行 statusEdit := j.EditTask(j.db, info.Id). - Select("status", "updated_at"). + Select("status"). Updates(jobs_gorm_model.Task{ - Status: TASK_TIMEOUT, - UpdatedAt: gotime.Current().Format(), + Status: TASK_TIMEOUT, }) if statusEdit.RowsAffected == 0 { log.Println("statusEdit", statusEdit.Error) @@ -179,7 +172,6 @@ func (j *JobsGorm) RunAddLog(id uint, runId string) *gorm.DB { Gomaxprocs: j.maxProCs, GoVersion: j.version, MacAddrs: j.macAddrS, - CreatedAt: gotime.Current().Format(), }) } @@ -208,8 +200,6 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error { CreatedIp: j.outsideIp, SpecifyIp: config.SpecifyIp, UpdatedIp: j.outsideIp, - CreatedAt: gotime.Current().Format(), - UpdatedAt: gotime.Current().Format(), }) if createStatus.RowsAffected == 0 { return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) @@ -246,8 +236,6 @@ func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) erro CreatedIp: j.outsideIp, SpecifyIp: config.SpecifyIp, UpdatedIp: j.outsideIp, - CreatedAt: gotime.Current().Format(), - UpdatedAt: gotime.Current().Format(), }) if createStatus.RowsAffected == 0 { return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) @@ -282,8 +270,6 @@ func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNu CreatedIp: j.outsideIp, SpecifyIp: config.SpecifyIp, UpdatedIp: j.outsideIp, - CreatedAt: gotime.Current().Format(), - UpdatedAt: gotime.Current().Format(), }) if createStatus.RowsAffected == 0 { return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) @@ -322,8 +308,6 @@ func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdM CreatedIp: j.outsideIp, SpecifyIp: config.SpecifyIp, UpdatedIp: j.outsideIp, - CreatedAt: gotime.Current().Format(), - UpdatedAt: gotime.Current().Format(), }) if createStatus.RowsAffected == 0 { return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) diff --git a/utils/gojobs/jobs_gorm_check_task.go b/utils/gojobs/jobs_gorm_check_task.go index e508303b..6ac58479 100644 --- a/utils/gojobs/jobs_gorm_check_task.go +++ b/utils/gojobs/jobs_gorm_check_task.go @@ -11,7 +11,7 @@ import ( func (j *JobsGorm) CheckManyTask(tx *gorm.DB, vs []jobs_gorm_model.Task) { if len(vs) > 0 { for _, v := range vs { - diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrentParse(v.UpdatedAt).Time) + diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrent(v.UpdatedAt).Time) if diffInSecondWithAbs >= v.Frequency*3 { log.Printf("每隔%v秒任务:%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs) statusDelete := tx.Where("task_id = ?", v.Id).Where("run_id = ?", v.RunId).Delete(&jobs_gorm_model.TaskLogRun{}) @@ -25,7 +25,7 @@ func (j *JobsGorm) CheckManyTask(tx *gorm.DB, vs []jobs_gorm_model.Task) { // CheckSingleTask 单任务检查 func (j *JobsGorm) CheckSingleTask(tx *gorm.DB, v jobs_gorm_model.Task) { - diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrentParse(v.UpdatedAt).Time) + diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrent(v.UpdatedAt).Time) if diffInSecondWithAbs >= v.Frequency*3 { log.Printf("每隔%v秒任务:%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs) statusDelete := tx.Where("task_id = ?", v.Id).Where("run_id = ?", v.RunId).Delete(&jobs_gorm_model.TaskLogRun{}) diff --git a/utils/gojobs/jobs_gorm_model.go b/utils/gojobs/jobs_gorm_model.go index 083c23fd..96c2e25a 100644 --- a/utils/gojobs/jobs_gorm_model.go +++ b/utils/gojobs/jobs_gorm_model.go @@ -2,7 +2,6 @@ package gojobs import ( "go.dtapp.net/library/utils/gojobs/jobs_gorm_model" - "go.dtapp.net/library/utils/gotime" "gorm.io/gorm" "log" "strings" @@ -133,10 +132,9 @@ func (j *JobsGorm) EditTask(tx *gorm.DB, id uint) *gorm.DB { // UpdateFrequency 更新任务频率 func (j *JobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB { return j.EditTask(tx, id). - Select("frequency", "updated_at"). + Select("frequency"). Updates(jobs_gorm_model.Task{ Frequency: frequency, - UpdatedAt: gotime.Current().Format(), }) } diff --git a/utils/gojobs/jobs_gorm_model/task.go b/utils/gojobs/jobs_gorm_model/task.go index 5b6c957e..582830bc 100644 --- a/utils/gojobs/jobs_gorm_model/task.go +++ b/utils/gojobs/jobs_gorm_model/task.go @@ -2,29 +2,30 @@ package jobs_gorm_model import ( "gorm.io/gorm" + "time" ) // Task 任务 type Task struct { - Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 - Status string `gorm:"index;comment:状态码" json:"status"` // 状态码 - Params string `gorm:"comment:参数" json:"params"` // 参数 - ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型 - StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述 - Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位) - Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数 - MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数 - RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 - CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号 - CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence"` // 自定义顺序 - Type string `gorm:"index;comment:类型" json:"type"` // 类型 - CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP - SpecifyIp string `gorm:"comment:指定外网IP" json:"specify_ip"` // 指定外网IP - UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP - Result string `gorm:"comment:结果" json:"result"` // 结果 - CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 - UpdatedAt string `gorm:"type:text;comment:更新时间" json:"updated_at"` // 更新时间 - DeletedAt gorm.DeletedAt `gorm:"type:text;index;comment:删除时间" json:"deleted_at"` // 删除时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + Status string `gorm:"index;comment:状态码" json:"status"` // 状态码 + Params string `gorm:"comment:参数" json:"params"` // 参数 + ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型 + StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述 + Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位) + Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数 + MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数 + RunId string `gorm:"index;comment:执行编号" json:"run_id"` // 执行编号 + CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号 + CustomSequence int64 `gorm:"index;comment:自定义顺序" json:"custom_sequence"` // 自定义顺序 + Type string `gorm:"index;comment:类型" json:"type"` // 类型 + CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP + SpecifyIp string `gorm:"index;comment:指定外网IP" json:"specify_ip"` // 指定外网IP + UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP + Result string `gorm:"comment:结果" json:"result"` // 结果 + CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间 + UpdatedAt time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at"` // 更新时间 + DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at"` // 删除时间 } func (m *Task) TableName() string { diff --git a/utils/gojobs/jobs_gorm_model/task_log.go b/utils/gojobs/jobs_gorm_model/task_log.go index fac2de3a..4f29c8ab 100644 --- a/utils/gojobs/jobs_gorm_model/task_log.go +++ b/utils/gojobs/jobs_gorm_model/task_log.go @@ -1,13 +1,15 @@ package jobs_gorm_model +import "time" + // TaskLog 任务日志模型 type TaskLog struct { - Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 - TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 - StatusCode int `gorm:"comment:状态码" json:"status_code"` // 状态码 - Desc string `gorm:"comment:结果" json:"desc"` // 结果 - Version string `gorm:"comment:版本" json:"version"` // 版本 - CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号 + StatusCode int `gorm:"index;comment:状态码" json:"status_code"` // 状态码 + Desc string `gorm:"comment:结果" json:"desc"` // 结果 + Version string `gorm:"comment:版本" json:"version"` // 版本 + CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间 } func (m *TaskLog) TableName() string { diff --git a/utils/gojobs/jobs_gorm_model/task_log_run.go b/utils/gojobs/jobs_gorm_model/task_log_run.go index 7df23c7d..4d014970 100644 --- a/utils/gojobs/jobs_gorm_model/task_log_run.go +++ b/utils/gojobs/jobs_gorm_model/task_log_run.go @@ -1,18 +1,20 @@ package jobs_gorm_model +import "time" + // TaskLogRun 任务执行日志模型 type TaskLogRun struct { - Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 - TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 - RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 - OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip - InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip - Os string `gorm:"comment:系统类型" json:"os"` // 系统类型 - Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构 - Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数 - GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本 - MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址 - CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号 + RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 + OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip + InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip + Os string `gorm:"comment:系统类型" json:"os"` // 系统类型 + Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构 + Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数 + GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本 + MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址 + CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间 } func (m *TaskLogRun) TableName() string { diff --git a/utils/golog/api_gorm.go b/utils/golog/api_gorm.go index 06c5d958..28917011 100644 --- a/utils/golog/api_gorm.go +++ b/utils/golog/api_gorm.go @@ -7,27 +7,28 @@ import ( "gorm.io/datatypes" "gorm.io/gorm" "log" + "time" "unicode/utf8" ) // 模型结构体 type apiPostgresqlLog struct { - LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 - RequestTime TimeString `gorm:"index" json:"request_time,omitempty"` //【请求】时间 - RequestUri string `gorm:"type:text" json:"request_uri,omitempty"` //【请求】链接 - RequestUrl string `gorm:"type:text" json:"request_url,omitempty"` //【请求】链接 - RequestApi string `gorm:"type:text;index" json:"request_api,omitempty"` //【请求】接口 - RequestMethod string `gorm:"type:text;index" json:"request_method,omitempty"` //【请求】方式 - RequestParams datatypes.JSON `gorm:"type:jsonb" json:"request_params,omitempty"` //【请求】参数 - RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】头部 - ResponseHeader datatypes.JSON `gorm:"type:jsonb" json:"response_header,omitempty"` //【返回】头部 - ResponseStatusCode int `gorm:"type:bigint" json:"response_status_code,omitempty"` //【返回】状态码 - ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body,omitempty"` //【返回】内容 - ResponseContentLength int64 `gorm:"type:bigint" json:"response_content_length,omitempty"` //【返回】大小 - ResponseTime TimeString `gorm:"index" json:"response_time,omitempty"` //【返回】时间 - SystemHostName string `gorm:"type:text" json:"system_host_name,omitempty"` //【系统】主机名 - SystemInsideIp string `gorm:"type:text" json:"system_inside_ip,omitempty"` //【系统】内网ip - GoVersion string `gorm:"type:text" json:"go_version,omitempty"` //【程序】Go版本 + LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 + RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间 + RequestUri string `json:"request_uri,omitempty"` //【请求】链接 + RequestUrl string `json:"request_url,omitempty"` //【请求】链接 + RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】接口 + RequestMethod string `gorm:"index" json:"request_method,omitempty"` //【请求】方式 + RequestParams datatypes.JSON `gorm:"type:jsonb" json:"request_params,omitempty"` //【请求】参数 + RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】头部 + ResponseHeader datatypes.JSON `gorm:"type:jsonb" json:"response_header,omitempty"` //【返回】头部 + ResponseStatusCode int `gorm:"index" json:"response_status_code,omitempty"` //【返回】状态码 + ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body,omitempty"` //【返回】内容 + ResponseContentLength int64 `json:"response_content_length,omitempty"` //【返回】大小 + ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间 + SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名 + SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip + GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本 } // 记录日志 @@ -59,7 +60,7 @@ func (c *ApiClient) GormMiddleware(request gorequest.Response) { return } c.gormRecord(apiPostgresqlLog{ - RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 + RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 @@ -70,14 +71,14 @@ func (c *ApiClient) GormMiddleware(request gorequest.Response) { ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseBody: request.ResponseBody, //【返回】内容 ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 + ResponseTime: request.ResponseTime, //【返回】时间 }) } // GormMiddlewareXml 中间件 func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) { c.gormRecord(apiPostgresqlLog{ - RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 + RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 @@ -88,14 +89,14 @@ func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) { ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseBody: datatypes.JSON(gojson.JsonEncodeNoError(dorm.XmlDecodeNoError(request.ResponseBody))), //【返回】内容 ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 + ResponseTime: request.ResponseTime, //【返回】时间 }) } // GormMiddlewareCustom 中间件 func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response) { c.gormRecord(apiPostgresqlLog{ - RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 + RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestApi: api, //【请求】接口 @@ -106,6 +107,6 @@ func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response) ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseBody: request.ResponseBody, //【返回】内容 ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 + ResponseTime: request.ResponseTime, //【返回】时间 }) } diff --git a/utils/golog/gin_gorm.go b/utils/golog/gin_gorm.go index fb55d3cb..c1999f9b 100644 --- a/utils/golog/gin_gorm.go +++ b/utils/golog/gin_gorm.go @@ -12,37 +12,38 @@ import ( "gorm.io/gorm" "io/ioutil" "net" + "time" ) // 模型结构体 type ginPostgresqlLog struct { - LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 - TraceId string `gorm:"type:text" json:"trace_id,omitempty"` //【系统】链编号 - RequestTime TimeString `gorm:"index" json:"request_time,omitempty"` //【请求】时间 - RequestUri string `gorm:"type:text" json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 - RequestUrl string `gorm:"type:text" json:"request_url,omitempty"` //【请求】请求链接 域名+路径 - RequestApi string `gorm:"type:text;index" json:"request_api,omitempty"` //【请求】请求接口 路径 - RequestMethod string `gorm:"type:text;index" json:"request_method,omitempty"` //【请求】请求方式 - RequestProto string `gorm:"type:text" json:"request_proto,omitempty"` //【请求】请求协议 - RequestUa string `gorm:"type:text" json:"request_ua,omitempty"` //【请求】请求UA - RequestReferer string `gorm:"type:text" json:"request_referer,omitempty"` //【请求】请求referer - RequestBody datatypes.JSON `gorm:"type:jsonb" json:"request_body,omitempty"` //【请求】请求主体 - RequestUrlQuery datatypes.JSON `gorm:"type:jsonb" json:"request_url_query,omitempty"` //【请求】请求URL参数 - RequestIp string `gorm:"type:text" json:"request_ip,omitempty"` //【请求】请求客户端Ip - RequestIpCountry string `gorm:"type:text" json:"request_ip_country,omitempty"` //【请求】请求客户端城市 - RequestIpRegion string `gorm:"type:text" json:"request_ip_region,omitempty"` //【请求】请求客户端区域 - RequestIpProvince string `gorm:"type:text" json:"request_ip_province,omitempty"` //【请求】请求客户端省份 - RequestIpCity string `gorm:"type:text" json:"request_ip_city,omitempty"` //【请求】请求客户端城市 - RequestIpIsp string `gorm:"type:text" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 - RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】请求头 - ResponseTime TimeString `gorm:"index" json:"response_time,omitempty"` //【返回】时间 - ResponseCode int `gorm:"type:bigint" json:"response_code,omitempty"` //【返回】状态码 - ResponseMsg string `gorm:"type:text" json:"response_msg,omitempty"` //【返回】描述 - ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data,omitempty"` //【返回】数据 - CostTime int64 `gorm:"type:bigint" json:"cost_time,omitempty"` //【系统】花费时间 - SystemHostName string `gorm:"type:text" json:"system_host_name,omitempty"` //【系统】主机名 - SystemInsideIp string `gorm:"type:text" json:"system_inside_ip,omitempty"` //【系统】内网ip - GoVersion string `gorm:"type:text" json:"go_version,omitempty"` //【程序】Go版本 + LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 + TraceId string `gorm:"index" json:"trace_id,omitempty"` //【系统】链编号 + RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间 + RequestUri string `json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 + RequestUrl string `json:"request_url,omitempty"` //【请求】请求链接 域名+路径 + RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】请求接口 路径 + RequestMethod string `gorm:"index" json:"request_method,omitempty"` //【请求】请求方式 + RequestProto string `json:"request_proto,omitempty"` //【请求】请求协议 + RequestUa string `json:"request_ua,omitempty"` //【请求】请求UA + RequestReferer string `json:"request_referer,omitempty"` //【请求】请求referer + RequestBody datatypes.JSON `gorm:"type:jsonb" json:"request_body,omitempty"` //【请求】请求主体 + RequestUrlQuery datatypes.JSON `gorm:"type:jsonb" json:"request_url_query,omitempty"` //【请求】请求URL参数 + RequestIp string `gorm:"index" json:"request_ip,omitempty"` //【请求】请求客户端Ip + RequestIpCountry string `gorm:"index" json:"request_ip_country,omitempty"` //【请求】请求客户端城市 + RequestIpRegion string `gorm:"index" json:"request_ip_region,omitempty"` //【请求】请求客户端区域 + RequestIpProvince string `gorm:"index" json:"request_ip_province,omitempty"` //【请求】请求客户端省份 + RequestIpCity string `gorm:"index" json:"request_ip_city,omitempty"` //【请求】请求客户端城市 + RequestIpIsp string `gorm:"index" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 + RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】请求头 + ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间 + ResponseCode int `gorm:"index" json:"response_code,omitempty"` //【返回】状态码 + ResponseMsg string `json:"response_msg,omitempty"` //【返回】描述 + ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data,omitempty"` //【返回】数据 + CostTime int64 `json:"cost_time,omitempty"` //【系统】花费时间 + SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名 + SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip + GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本 } // gormRecord 记录日志 @@ -129,7 +130,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { if len(jsonBody) > 0 { c.gormRecord(ginPostgresqlLog{ TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 - RequestTime: TimeString{Time: requestTime}, //【请求】时间 + RequestTime: requestTime, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 @@ -146,7 +147,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头 - ResponseTime: TimeString{Time: gotime.Current().Time}, //【返回】时间 + ResponseTime: gotime.Current().Time, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据 CostTime: endTime - startTime, //【系统】花费时间 @@ -154,7 +155,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { } else { c.gormRecord(ginPostgresqlLog{ TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 - RequestTime: TimeString{Time: requestTime}, //【请求】时间 + RequestTime: requestTime, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 @@ -171,7 +172,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc { RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头 - ResponseTime: TimeString{Time: gotime.Current().Time}, //【返回】时间 + ResponseTime: gotime.Current().Time, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据 CostTime: endTime - startTime, //【系统】花费时间