- update dorm

- update jobs
- update log
master
李光春 2 years ago
parent 84468ee612
commit c06febc02a

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
@ -46,7 +47,7 @@ func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) {
}, },
), ),
NowFunc: func() time.Time { NowFunc: func() time.Time {
return time.Now().Local() return gotime.Current().Now().Local()
}, },
}) })
} else { } else {
@ -93,7 +94,7 @@ func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) {
}, },
), ),
NowFunc: func() time.Time { NowFunc: func() time.Time {
return time.Now().Local() return gotime.Current().Now().Local()
}, },
}) })
} else { } else {

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"go.dtapp.net/library/utils/goip"
"go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/client/v3"
"log" "log"
"time" "time"
@ -21,7 +20,7 @@ func NewEtcdWorker(config *EtcdConfig) (*Etcd, error) {
e.Endpoints = config.Endpoints e.Endpoints = config.Endpoints
e.DialTimeout = config.DialTimeout e.DialTimeout = config.DialTimeout
if config.LocalIP == "" { if config.LocalIP == "" {
config.LocalIP = goip.GetOutsideIp() return nil, errors.New("需要配置客户端的ip地址唯一性~")
} }
e.LocalIP = config.LocalIP e.LocalIP = config.LocalIP
e.Username = config.Username e.Username = config.Username

@ -3,12 +3,11 @@ package gojobs
import ( import (
"errors" "errors"
"fmt" "fmt"
go_library "go.dtapp.net/library" "go.dtapp.net/library"
"go.dtapp.net/library/utils/dorm" "go.dtapp.net/library/utils/dorm"
"go.dtapp.net/library/utils/goarray" "go.dtapp.net/library/utils/goarray"
"go.dtapp.net/library/utils/goip" "go.dtapp.net/library/utils/goip"
"go.dtapp.net/library/utils/gojobs/jobs_gorm_model" "go.dtapp.net/library/utils/gojobs/jobs_gorm_model"
"go.dtapp.net/library/utils/gotime"
"go.dtapp.net/library/utils/gouuid" "go.dtapp.net/library/utils/gouuid"
"gorm.io/gorm" "gorm.io/gorm"
"log" "log"
@ -85,17 +84,15 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
StatusCode: status, StatusCode: status,
Desc: desc, Desc: desc,
Version: j.runVersion, Version: j.runVersion,
CreatedAt: gotime.Current().Format(),
}) })
if statusCreate.RowsAffected == 0 { if statusCreate.RowsAffected == 0 {
log.Println("statusCreate", statusCreate.Error) log.Println("statusCreate", statusCreate.Error)
} }
if status == 0 { if status == 0 {
statusEdit := j.EditTask(j.db, info.Id). statusEdit := j.EditTask(j.db, info.Id).
Select("run_id", "updated_at"). Select("run_id").
Updates(jobs_gorm_model.Task{ Updates(jobs_gorm_model.Task{
RunId: gouuid.GetUuId(), RunId: gouuid.GetUuId(),
UpdatedAt: gotime.Current().Format(),
}) })
if statusEdit.RowsAffected == 0 { if statusEdit.RowsAffected == 0 {
log.Println("statusEdit", statusEdit.Error) 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 { if status == CodeSuccess {
// 执行成功 // 执行成功
statusEdit := j.EditTask(j.db, info.Id). 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{ Updates(jobs_gorm_model.Task{
StatusDesc: "执行成功", StatusDesc: "执行成功",
Number: info.Number + 1, Number: info.Number + 1,
RunId: gouuid.GetUuId(), RunId: gouuid.GetUuId(),
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
UpdatedAt: gotime.Current().Format(),
Result: desc, Result: desc,
}) })
if statusEdit.RowsAffected == 0 { if statusEdit.RowsAffected == 0 {
@ -122,13 +118,12 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
if status == CodeEnd { if status == CodeEnd {
// 执行成功、提前结束 // 执行成功、提前结束
statusEdit := j.EditTask(j.db, info.Id). 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{ Updates(jobs_gorm_model.Task{
Status: TASK_SUCCESS, Status: TASK_SUCCESS,
StatusDesc: "结束执行", StatusDesc: "结束执行",
Number: info.Number + 1, Number: info.Number + 1,
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
UpdatedAt: gotime.Current().Format(),
Result: desc, Result: desc,
}) })
if statusEdit.RowsAffected == 0 { if statusEdit.RowsAffected == 0 {
@ -138,13 +133,12 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
if status == CodeError { if status == CodeError {
// 执行失败 // 执行失败
statusEdit := j.EditTask(j.db, info.Id). 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{ Updates(jobs_gorm_model.Task{
StatusDesc: "执行失败", StatusDesc: "执行失败",
Number: info.Number + 1, Number: info.Number + 1,
RunId: gouuid.GetUuId(), RunId: gouuid.GetUuId(),
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
UpdatedAt: gotime.Current().Format(),
Result: desc, Result: desc,
}) })
if statusEdit.RowsAffected == 0 { 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 { if info.Number+1 >= info.MaxNumber {
// 关闭执行 // 关闭执行
statusEdit := j.EditTask(j.db, info.Id). statusEdit := j.EditTask(j.db, info.Id).
Select("status", "updated_at"). Select("status").
Updates(jobs_gorm_model.Task{ Updates(jobs_gorm_model.Task{
Status: TASK_TIMEOUT, Status: TASK_TIMEOUT,
UpdatedAt: gotime.Current().Format(),
}) })
if statusEdit.RowsAffected == 0 { if statusEdit.RowsAffected == 0 {
log.Println("statusEdit", statusEdit.Error) log.Println("statusEdit", statusEdit.Error)
@ -179,7 +172,6 @@ func (j *JobsGorm) RunAddLog(id uint, runId string) *gorm.DB {
Gomaxprocs: j.maxProCs, Gomaxprocs: j.maxProCs,
GoVersion: j.version, GoVersion: j.version,
MacAddrs: j.macAddrS, MacAddrs: j.macAddrS,
CreatedAt: gotime.Current().Format(),
}) })
} }
@ -208,8 +200,6 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
CreatedIp: j.outsideIp, CreatedIp: j.outsideIp,
SpecifyIp: config.SpecifyIp, SpecifyIp: config.SpecifyIp,
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
CreatedAt: gotime.Current().Format(),
UpdatedAt: gotime.Current().Format(),
}) })
if createStatus.RowsAffected == 0 { if createStatus.RowsAffected == 0 {
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) 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, CreatedIp: j.outsideIp,
SpecifyIp: config.SpecifyIp, SpecifyIp: config.SpecifyIp,
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
CreatedAt: gotime.Current().Format(),
UpdatedAt: gotime.Current().Format(),
}) })
if createStatus.RowsAffected == 0 { if createStatus.RowsAffected == 0 {
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) 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, CreatedIp: j.outsideIp,
SpecifyIp: config.SpecifyIp, SpecifyIp: config.SpecifyIp,
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
CreatedAt: gotime.Current().Format(),
UpdatedAt: gotime.Current().Format(),
}) })
if createStatus.RowsAffected == 0 { if createStatus.RowsAffected == 0 {
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) 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, CreatedIp: j.outsideIp,
SpecifyIp: config.SpecifyIp, SpecifyIp: config.SpecifyIp,
UpdatedIp: j.outsideIp, UpdatedIp: j.outsideIp,
CreatedAt: gotime.Current().Format(),
UpdatedAt: gotime.Current().Format(),
}) })
if createStatus.RowsAffected == 0 { if createStatus.RowsAffected == 0 {
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error)) return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))

@ -11,7 +11,7 @@ import (
func (j *JobsGorm) CheckManyTask(tx *gorm.DB, vs []jobs_gorm_model.Task) { func (j *JobsGorm) CheckManyTask(tx *gorm.DB, vs []jobs_gorm_model.Task) {
if len(vs) > 0 { if len(vs) > 0 {
for _, v := range vs { 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 { if diffInSecondWithAbs >= v.Frequency*3 {
log.Printf("每隔%v秒任务%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs) 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{}) 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 单任务检查 // CheckSingleTask 单任务检查
func (j *JobsGorm) CheckSingleTask(tx *gorm.DB, v jobs_gorm_model.Task) { 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 { if diffInSecondWithAbs >= v.Frequency*3 {
log.Printf("每隔%v秒任务%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs) 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{}) statusDelete := tx.Where("task_id = ?", v.Id).Where("run_id = ?", v.RunId).Delete(&jobs_gorm_model.TaskLogRun{})

@ -2,7 +2,6 @@ package gojobs
import ( import (
"go.dtapp.net/library/utils/gojobs/jobs_gorm_model" "go.dtapp.net/library/utils/gojobs/jobs_gorm_model"
"go.dtapp.net/library/utils/gotime"
"gorm.io/gorm" "gorm.io/gorm"
"log" "log"
"strings" "strings"
@ -133,10 +132,9 @@ func (j *JobsGorm) EditTask(tx *gorm.DB, id uint) *gorm.DB {
// UpdateFrequency 更新任务频率 // UpdateFrequency 更新任务频率
func (j *JobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB { func (j *JobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB {
return j.EditTask(tx, id). return j.EditTask(tx, id).
Select("frequency", "updated_at"). Select("frequency").
Updates(jobs_gorm_model.Task{ Updates(jobs_gorm_model.Task{
Frequency: frequency, Frequency: frequency,
UpdatedAt: gotime.Current().Format(),
}) })
} }

@ -2,29 +2,30 @@ package jobs_gorm_model
import ( import (
"gorm.io/gorm" "gorm.io/gorm"
"time"
) )
// Task 任务 // Task 任务
type Task struct { type Task struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
Status string `gorm:"index;comment:状态码" json:"status"` // 状态码 Status string `gorm:"index;comment:状态码" json:"status"` // 状态码
Params string `gorm:"comment:参数" json:"params"` // 参数 Params string `gorm:"comment:参数" json:"params"` // 参数
ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型 ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型
StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述 StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位) Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位)
Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数 Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数 MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数
RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 RunId string `gorm:"index;comment:执行编号" json:"run_id"` // 执行编号
CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号 CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号
CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence"` // 自定义顺序 CustomSequence int64 `gorm:"index;comment:自定义顺序" json:"custom_sequence"` // 自定义顺序
Type string `gorm:"index;comment:类型" json:"type"` // 类型 Type string `gorm:"index;comment:类型" json:"type"` // 类型
CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP
SpecifyIp string `gorm:"comment:指定外网IP" json:"specify_ip"` // 指定外网IP SpecifyIp string `gorm:"index;comment:指定外网IP" json:"specify_ip"` // 指定外网IP
UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP
Result string `gorm:"comment:结果" json:"result"` // 结果 Result string `gorm:"comment:结果" json:"result"` // 结果
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt string `gorm:"type:text;comment:更新时间" json:"updated_at"` // 更新时间 UpdatedAt time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"type:text;index;comment:删除时间" json:"deleted_at"` // 删除时间 DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at"` // 删除时间
} }
func (m *Task) TableName() string { func (m *Task) TableName() string {

@ -1,13 +1,15 @@
package jobs_gorm_model package jobs_gorm_model
import "time"
// TaskLog 任务日志模型 // TaskLog 任务日志模型
type TaskLog struct { type TaskLog struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号
StatusCode int `gorm:"comment:状态码" json:"status_code"` // 状态码 StatusCode int `gorm:"index;comment:状态码" json:"status_code"` // 状态码
Desc string `gorm:"comment:结果" json:"desc"` // 结果 Desc string `gorm:"comment:结果" json:"desc"` // 结果
Version string `gorm:"comment:版本" json:"version"` // 版本 Version string `gorm:"comment:版本" json:"version"` // 版本
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
} }
func (m *TaskLog) TableName() string { func (m *TaskLog) TableName() string {

@ -1,18 +1,20 @@
package jobs_gorm_model package jobs_gorm_model
import "time"
// TaskLogRun 任务执行日志模型 // TaskLogRun 任务执行日志模型
type TaskLogRun struct { type TaskLogRun struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号
RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号
OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip
InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip
Os string `gorm:"comment:系统类型" json:"os"` // 系统类型 Os string `gorm:"comment:系统类型" json:"os"` // 系统类型
Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构 Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构
Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数 Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数
GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本 GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本
MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址 MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
} }
func (m *TaskLogRun) TableName() string { func (m *TaskLogRun) TableName() string {

@ -7,27 +7,28 @@ import (
"gorm.io/datatypes" "gorm.io/datatypes"
"gorm.io/gorm" "gorm.io/gorm"
"log" "log"
"time"
"unicode/utf8" "unicode/utf8"
) )
// 模型结构体 // 模型结构体
type apiPostgresqlLog struct { type apiPostgresqlLog struct {
LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号
RequestTime TimeString `gorm:"index" json:"request_time,omitempty"` //【请求】时间 RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间
RequestUri string `gorm:"type:text" json:"request_uri,omitempty"` //【请求】链接 RequestUri string `json:"request_uri,omitempty"` //【请求】链接
RequestUrl string `gorm:"type:text" json:"request_url,omitempty"` //【请求】链接 RequestUrl string `json:"request_url,omitempty"` //【请求】链接
RequestApi string `gorm:"type:text;index" json:"request_api,omitempty"` //【请求】接口 RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】接口
RequestMethod string `gorm:"type:text;index" json:"request_method,omitempty"` //【请求】方式 RequestMethod string `gorm:"index" json:"request_method,omitempty"` //【请求】方式
RequestParams datatypes.JSON `gorm:"type:jsonb" json:"request_params,omitempty"` //【请求】参数 RequestParams datatypes.JSON `gorm:"type:jsonb" json:"request_params,omitempty"` //【请求】参数
RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】头部 RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】头部
ResponseHeader datatypes.JSON `gorm:"type:jsonb" json:"response_header,omitempty"` //【返回】头部 ResponseHeader datatypes.JSON `gorm:"type:jsonb" json:"response_header,omitempty"` //【返回】头部
ResponseStatusCode int `gorm:"type:bigint" json:"response_status_code,omitempty"` //【返回】状态码 ResponseStatusCode int `gorm:"index" json:"response_status_code,omitempty"` //【返回】状态码
ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body,omitempty"` //【返回】内容 ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body,omitempty"` //【返回】内容
ResponseContentLength int64 `gorm:"type:bigint" json:"response_content_length,omitempty"` //【返回】大小 ResponseContentLength int64 `json:"response_content_length,omitempty"` //【返回】大小
ResponseTime TimeString `gorm:"index" json:"response_time,omitempty"` //【返回】时间 ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间
SystemHostName string `gorm:"type:text" json:"system_host_name,omitempty"` //【系统】主机名 SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `gorm:"type:text" json:"system_inside_ip,omitempty"` //【系统】内网ip SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `gorm:"type:text" json:"go_version,omitempty"` //【程序】Go版本 GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本
} }
// 记录日志 // 记录日志
@ -59,7 +60,7 @@ func (c *ApiClient) GormMiddleware(request gorequest.Response) {
return return
} }
c.gormRecord(apiPostgresqlLog{ c.gormRecord(apiPostgresqlLog{
RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接 RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
@ -70,14 +71,14 @@ func (c *ApiClient) GormMiddleware(request gorequest.Response) {
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容 ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 ResponseTime: request.ResponseTime, //【返回】时间
}) })
} }
// GormMiddlewareXml 中间件 // GormMiddlewareXml 中间件
func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) { func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) {
c.gormRecord(apiPostgresqlLog{ c.gormRecord(apiPostgresqlLog{
RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接 RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
@ -88,14 +89,14 @@ func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) {
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: datatypes.JSON(gojson.JsonEncodeNoError(dorm.XmlDecodeNoError(request.ResponseBody))), //【返回】内容 ResponseBody: datatypes.JSON(gojson.JsonEncodeNoError(dorm.XmlDecodeNoError(request.ResponseBody))), //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 ResponseTime: request.ResponseTime, //【返回】时间
}) })
} }
// GormMiddlewareCustom 中间件 // GormMiddlewareCustom 中间件
func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response) { func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response) {
c.gormRecord(apiPostgresqlLog{ c.gormRecord(apiPostgresqlLog{
RequestTime: TimeString{Time: request.RequestTime}, //【请求】时间 RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接 RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: api, //【请求】接口 RequestApi: api, //【请求】接口
@ -106,6 +107,6 @@ func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response)
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容 ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: TimeString{Time: request.ResponseTime}, //【返回】时间 ResponseTime: request.ResponseTime, //【返回】时间
}) })
} }

@ -12,37 +12,38 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
"io/ioutil" "io/ioutil"
"net" "net"
"time"
) )
// 模型结构体 // 模型结构体
type ginPostgresqlLog struct { type ginPostgresqlLog struct {
LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号 LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号
TraceId string `gorm:"type:text" json:"trace_id,omitempty"` //【系统】链编号 TraceId string `gorm:"index" json:"trace_id,omitempty"` //【系统】链编号
RequestTime TimeString `gorm:"index" json:"request_time,omitempty"` //【请求】时间 RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间
RequestUri string `gorm:"type:text" json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 RequestUri string `json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数
RequestUrl string `gorm:"type:text" json:"request_url,omitempty"` //【请求】请求链接 域名+路径 RequestUrl string `json:"request_url,omitempty"` //【请求】请求链接 域名+路径
RequestApi string `gorm:"type:text;index" json:"request_api,omitempty"` //【请求】请求接口 路径 RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】请求接口 路径
RequestMethod string `gorm:"type:text;index" json:"request_method,omitempty"` //【请求】请求方式 RequestMethod string `gorm:"index" json:"request_method,omitempty"` //【请求】请求方式
RequestProto string `gorm:"type:text" json:"request_proto,omitempty"` //【请求】请求协议 RequestProto string `json:"request_proto,omitempty"` //【请求】请求协议
RequestUa string `gorm:"type:text" json:"request_ua,omitempty"` //【请求】请求UA RequestUa string `json:"request_ua,omitempty"` //【请求】请求UA
RequestReferer string `gorm:"type:text" json:"request_referer,omitempty"` //【请求】请求referer RequestReferer string `json:"request_referer,omitempty"` //【请求】请求referer
RequestBody datatypes.JSON `gorm:"type:jsonb" json:"request_body,omitempty"` //【请求】请求主体 RequestBody datatypes.JSON `gorm:"type:jsonb" json:"request_body,omitempty"` //【请求】请求主体
RequestUrlQuery datatypes.JSON `gorm:"type:jsonb" json:"request_url_query,omitempty"` //【请求】请求URL参数 RequestUrlQuery datatypes.JSON `gorm:"type:jsonb" json:"request_url_query,omitempty"` //【请求】请求URL参数
RequestIp string `gorm:"type:text" json:"request_ip,omitempty"` //【请求】请求客户端Ip RequestIp string `gorm:"index" json:"request_ip,omitempty"` //【请求】请求客户端Ip
RequestIpCountry string `gorm:"type:text" json:"request_ip_country,omitempty"` //【请求】请求客户端城市 RequestIpCountry string `gorm:"index" json:"request_ip_country,omitempty"` //【请求】请求客户端城市
RequestIpRegion string `gorm:"type:text" json:"request_ip_region,omitempty"` //【请求】请求客户端区域 RequestIpRegion string `gorm:"index" json:"request_ip_region,omitempty"` //【请求】请求客户端区域
RequestIpProvince string `gorm:"type:text" json:"request_ip_province,omitempty"` //【请求】请求客户端省份 RequestIpProvince string `gorm:"index" json:"request_ip_province,omitempty"` //【请求】请求客户端省份
RequestIpCity string `gorm:"type:text" json:"request_ip_city,omitempty"` //【请求】请求客户端城市 RequestIpCity string `gorm:"index" json:"request_ip_city,omitempty"` //【请求】请求客户端城市
RequestIpIsp string `gorm:"type:text" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 RequestIpIsp string `gorm:"index" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商
RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】请求头 RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】请求头
ResponseTime TimeString `gorm:"index" json:"response_time,omitempty"` //【返回】时间 ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间
ResponseCode int `gorm:"type:bigint" json:"response_code,omitempty"` //【返回】状态码 ResponseCode int `gorm:"index" json:"response_code,omitempty"` //【返回】状态码
ResponseMsg string `gorm:"type:text" json:"response_msg,omitempty"` //【返回】描述 ResponseMsg string `json:"response_msg,omitempty"` //【返回】描述
ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data,omitempty"` //【返回】数据 ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data,omitempty"` //【返回】数据
CostTime int64 `gorm:"type:bigint" json:"cost_time,omitempty"` //【系统】花费时间 CostTime int64 `json:"cost_time,omitempty"` //【系统】花费时间
SystemHostName string `gorm:"type:text" json:"system_host_name,omitempty"` //【系统】主机名 SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `gorm:"type:text" json:"system_inside_ip,omitempty"` //【系统】内网ip SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `gorm:"type:text" json:"go_version,omitempty"` //【程序】Go版本 GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本
} }
// gormRecord 记录日志 // gormRecord 记录日志
@ -129,7 +130,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
if len(jsonBody) > 0 { if len(jsonBody) > 0 {
c.gormRecord(ginPostgresqlLog{ c.gormRecord(ginPostgresqlLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: TimeString{Time: requestTime}, //【请求】时间 RequestTime: requestTime, //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
@ -146,7 +147,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头 RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头
ResponseTime: TimeString{Time: gotime.Current().Time}, //【返回】时间 ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码 ResponseCode: responseCode, //【返回】状态码
ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据 ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间 CostTime: endTime - startTime, //【系统】花费时间
@ -154,7 +155,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
} else { } else {
c.gormRecord(ginPostgresqlLog{ c.gormRecord(ginPostgresqlLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: TimeString{Time: requestTime}, //【请求】时间 RequestTime: requestTime, //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
@ -171,7 +172,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头 RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头
ResponseTime: TimeString{Time: gotime.Current().Time}, //【返回】时间 ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码 ResponseCode: responseCode, //【返回】状态码
ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据 ResponseData: datatypes.JSON(gojson.JsonEncodeNoError(responseBody)), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间 CostTime: endTime - startTime, //【系统】花费时间

Loading…
Cancel
Save