-
Notifications
You must be signed in to change notification settings - Fork 81
/
model_log.go
127 lines (117 loc) · 3.38 KB
/
model_log.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
// Copyright 2022 The kubegems.io Authors
//
// 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 ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"os"
"path"
"time"
"gorm.io/gorm"
"kubegems.io/kubegems/pkg/utils"
"kubegems.io/kubegems/pkg/utils/loki"
)
// LogQueryHistory 日志查询历史
type LogQueryHistory struct {
ID uint `gorm:"primarykey"`
// 关联的集群
Cluster *Cluster `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"`
// 所属集群ID
ClusterID uint
// 标签
LabelJSON string `gorm:"type:varchar(1024)"`
// 正则标签
FilterJSON string `gorm:"type:varchar(1024)"`
// logql
LogQL string `gorm:"type:varchar(1024)"`
// 时间区间
TimeRange string `gorm:"type:varchar(256);default:''"`
// 创建时间
CreateAt time.Time `sql:"DEFAULT:'current_timestamp'"`
// 创建者
Creator *User `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"`
CreatorID uint
}
type LogQueryHistoryWithCount struct {
ID uint
Ids string
TimeRanges string
Cluster *Cluster
ClusterID uint
LabelJSON string
FilterJSON string
LogQL string
CreateAt time.Time
Creator *User
CreatorID uint
Total string
}
// LogQuerySnapshot 日志查询快照
type LogQuerySnapshot struct {
ID uint `gorm:"primarykey"`
// 关联的集群
Cluster *Cluster `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"`
// 所属集群ID
ClusterID uint
// 名称
SnapshotName string `gorm:"type:varchar(128)"`
SourceFile string `gorm:"type:varchar(128)"`
// 行数
SnapshotCount int
// 下载地址
DownloadURL string `gorm:"type:varchar(512)"`
StartTime time.Time
EndTime time.Time
// 创建时间
CreateAt time.Time `sql:"DEFAULT:'current_timestamp'"`
// 创建者
Creator *User `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"`
CreatorID uint
}
func (snapshot *LogQuerySnapshot) BeforeCreate(tx *gorm.DB) error {
var (
lineCount int64
err error
)
lokiExportDir := "data/lokiExport"
lokiSnapshotDir := path.Join(lokiExportDir, "snapshot", time.Now().UTC().Format("20060102"))
err = utils.EnsurePathExists(lokiSnapshotDir)
if err != nil {
return err
}
sourceFile := path.Join(lokiExportDir, snapshot.SourceFile)
if loki.FileExists(sourceFile) {
targetFile := path.Join(lokiSnapshotDir, snapshot.SnapshotName)
if !loki.FileExists(targetFile) {
lineCount, err = utils.CopyFileByLine(targetFile, sourceFile)
if err != nil {
return err
}
} else {
return fmt.Errorf("名字为 %s 的日志快照 已经存在,请换个名字保存", snapshot.SnapshotName)
}
snapshot.SnapshotCount = int(lineCount)
snapshot.DownloadURL = path.Join("/", lokiSnapshotDir, snapshot.SnapshotName)
return nil
}
return nil
}
func (snapshot *LogQuerySnapshot) BeforeDelete(tx *gorm.DB) error {
if snapshot.DownloadURL != "" && loki.FileExists(snapshot.DownloadURL) {
err := os.Remove(snapshot.DownloadURL)
if err != nil {
return err
}
}
return nil
}