-
Notifications
You must be signed in to change notification settings - Fork 373
/
errorbox.go
168 lines (144 loc) · 4.63 KB
/
errorbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package apistructs
import (
"strconv"
"time"
"github.com/pkg/errors"
)
type ErrorResourceType string
const (
PipelineError ErrorResourceType = "pipeline"
RuntimeError ErrorResourceType = "runtime"
AddonError ErrorResourceType = "addon"
)
// ErrorLogListRequest 错误日志查询请求
type ErrorLogListRequest struct {
// +required 鉴权需要
ScopeType ScopeType `schema:"scopeType"`
// +required 鉴权需要
ScopeID uint64 `schema:"scopeId"`
// +required 资源类型
ResourceType ErrorResourceType `schema:"resourceType"`
// +required 资源id
ResourceID string `schema:"resourceId"`
// +option 根据时间过滤错误日志
StartTime string `schema:"startTime"`
}
// Check 检查错误日志创建请求是否合法
func (el *ErrorLogListRequest) Check() error {
if el.ScopeType == "" {
return errors.Errorf("invalid request, scopeType couldn't be empty")
}
if el.ScopeID == 0 {
return errors.Errorf("invalid request, scopeId couldn't be empty")
}
if el.ResourceType == "" {
return errors.Errorf("invalid request, resourceType couldn't be empty")
}
if el.ResourceID == "" {
return errors.Errorf("invalid request, ResourceID couldn't be empty")
}
return nil
}
// GetFormartStartTime 获取格式化的开始时间
func (el *ErrorLogListRequest) GetFormartStartTime() (*time.Time, error) {
if el.StartTime == "" {
return nil, errors.Errorf("OccurrenceTime is empty")
}
result, err := time.ParseInLocation("2006-01-02 15:04:05", el.StartTime, time.Local)
if err != nil {
return nil, err
}
return &result, nil
}
// ErrorLogListResponse 错误日志查询具体响应
type ErrorLogListResponse struct {
Header
UserInfoHeader
Data *AuditsListResponseData `json:"data"`
}
// ErrorLogListResponseData 错误日志查询具体响应
type ErrorLogListResponseData struct {
List []ErrorLog `json:"list"`
}
type ErrorLogLevel string
const (
SuccessLevel ErrorLogLevel = "success"
InfoLevel ErrorLogLevel = "info"
ErrorLevel ErrorLogLevel = "error"
)
// ErrorLog 错误日志具体信息
type ErrorLog struct {
ID int64 `json:"id"`
Level ErrorLogLevel `json:"level"`
ResourceType ErrorResourceType `json:"resourceType"`
ResourceID string `json:"resourceId"`
OccurrenceTime string `json:"occurrenceTime"`
HumanLog string `json:"humanLog"`
PrimevalLog string `json:"primevalLog"`
DedupID string `json:"deDupId,omitempty"`
}
// ErrorLogCreateRequest 错误日志创建接口
type ErrorLogCreateRequest struct {
ErrorLog `json:"errorLog"`
}
// Check 检查错误日志创建请求是否合法
func (el *ErrorLogCreateRequest) Check() error {
if el.ErrorLog.ResourceType == "" {
return errors.Errorf("invalid request, ResourceType couldn't be empty")
}
if el.ErrorLog.ResourceID == "" {
return errors.Errorf("invalid request, ResourceID couldn't be empty")
}
if el.ErrorLog.OccurrenceTime == "" {
return errors.Errorf("invalid request, OccurrenceTime couldn't be empty")
}
if el.ErrorLog.PrimevalLog == "" {
return errors.Errorf("invalid request, PrimevalLog couldn't be empty")
}
if el.ErrorLog.Level == "" {
el.ErrorLog.Level = ErrorLevel
}
return nil
}
// FormartTime 返回time.Time
func (el *ErrorLog) FormartTime() (*time.Time, error) {
if el.OccurrenceTime == "" {
return nil, errors.Errorf("OccurrenceTime is empty")
}
timeInt, err := strconv.ParseInt(el.OccurrenceTime, 10, 64)
if err != nil {
return nil, err
}
timeStr := time.Unix(timeInt, 0).Format("2006-01-02 15:04:05")
result, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
if err != nil {
return nil, err
}
return &result, nil
}
// ErrorLogCreateResponse 错误日志创建响应
type ErrorLogCreateResponse struct {
Header
Data string `json:"data"`
}
// ErrorLogBatchCreateRequest 错误批量创建请求
type ErrorLogBatchCreateRequest struct {
Audits []Audit `json:"audits"`
}
// ErrorLogBatchCreateResponse 错误批量创建响应
type ErrorLogBatchCreateResponse struct {
Header
Data string `json:"data"`
}