-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.go
104 lines (94 loc) · 2.78 KB
/
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
/*
* Copyright (c) 2023 NetEase Inc.
*
* 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.
*/
/*
* Project: Curve-Manager
* Created Date: 2023-03-16
* Author: wanghai (SeanHai)
*/
package storage
import (
"database/sql"
"fmt"
"time"
"github.com/opencurve/curve-manager/internal/common"
)
type Log struct {
id int64
TimeMs int64 `json:"-"`
IP string `json:"ip"`
Time string `json:"time"`
User string `json:"user"`
Module string `json:"module"`
Method string `json:"method"`
ErrorCode int `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
Content string `json:"content"`
}
type LogInfo struct {
Total int64 `json:"total"`
Info []Log `json:"info"`
}
func AddSystemLog(log *Log) error {
return gStorage.execSQL(ADD_SYSTEM_LOG, log.TimeMs, log.IP, log.User, log.Module, log.Method, log.ErrorCode,
log.ErrorMsg, log.Content)
}
func DeleteSystemLog(expirationMs int64) error {
return gStorage.execSQL(DELETE_SYSTEM_LOG, expirationMs)
}
func GetSystemLog(start, end int64, limit, offset uint32, filter, userName string) (LogInfo, error) {
logs := LogInfo{}
filter = fmt.Sprintf("%%%s%%", filter)
// get total
// admin can get all system operation log, others can only get themself's
var rows *sql.Rows
var err error
if userName == USER_ADMIN_NAME {
rows, err = gStorage.querySQL(GET_SYSTEM_LOG_NUM, start, end, filter)
} else {
rows, err = gStorage.querySQL(GET_SYSTEM_LOG_NUM_OF_USER, start, end, userName, filter)
}
if err != nil {
return logs, err
}
defer rows.Close()
if rows.Next() {
rows.Scan(&logs.Total)
}
if logs.Total > int64(offset) {
rows.Close()
if userName == USER_ADMIN_NAME {
rows, err = gStorage.querySQL(GET_SYSTEM_LOG, start, end, filter, limit, offset)
} else {
rows, err = gStorage.querySQL(GET_SYSTEM_LOG_OF_USER, start, end, userName, filter, limit, offset)
}
if err != nil {
return logs, err
}
for rows.Next() {
var log Log
err = rows.Scan(&log.id, &log.TimeMs, &log.IP, &log.User, &log.Module, &log.Method, &log.ErrorCode,
&log.ErrorMsg, &log.Content)
if err != nil {
return logs, err
}
sec := log.TimeMs / 1000
t := time.Unix(sec, log.TimeMs%1000*int64(time.Millisecond))
log.Time = t.Format(common.TIME_MS_FORMAT)
logs.Info = append(logs.Info, log)
}
}
return logs, nil
}