-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdatabase.go
More file actions
125 lines (115 loc) · 3.3 KB
/
Copy pathdatabase.go
File metadata and controls
125 lines (115 loc) · 3.3 KB
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
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* database.go
*/
package hatchet
import (
"fmt"
"log"
"strings"
"unicode"
)
const (
SQLite3 = iota
Mongo
)
type NameValue struct {
Name string `bson:"name"`
Value int `bson:"value"`
}
type NameValues struct {
Name string
Values []interface{}
}
type HatchetEntry struct {
Name string
CreatedAt string
}
type Database interface {
Begin() error
Close() error
Commit() error
CountLogs(opts ...string) (int, error)
CreateMetaData() error
Drop() error
Rename(newName string) error
GetAcceptedConnsCounts(duration string) ([]NameValue, error)
GetAuditData() (map[string][]NameValues, error)
GetAverageOpTime(op string, duration string) ([]OpCount, error)
GetConnectionStats(chartType string, duration string) ([]RemoteClient, error)
GetHatchetInfo() HatchetInfo
GetHatchetNames() ([]string, error)
GetHatchetsWithTime() ([]HatchetEntry, error)
GetLogs(opts ...string) ([]LegacyLog, error)
GetOpsCounts(duration string) ([]NameValue, error)
GetReslenByAppName(appname string, duration string) ([]NameValue, error)
GetReslenByNamespace(ip string, duration string) ([]NameValue, error)
GetReslenByIP(ip string, duration string) ([]NameValue, error)
GetSlowOps(orderBy string, order string, collscan bool) ([]OpStat, error)
GetSlowestLogs(topN int) ([]LegacyLog, error)
GetVerbose() bool
InsertClientConn(index int, doc *Logv2Info) error
InsertDriver(index int, doc *Logv2Info) error
InsertFailedMessages(m *FailedMessages) error
InsertLog(index int, end string, doc *Logv2Info, stat *OpStat) error
SearchLogs(opts ...string) ([]LegacyLog, error)
SetVerbose(v bool)
UpdateHatchetInfo(info HatchetInfo) error
}
func GetDatabase(hatchetName string) (Database, error) {
var err error
var dbase Database
logv2 := GetLogv2()
if logv2.verbose {
log.Println("url", logv2.url, "hatchet name", hatchetName)
}
if GetLogv2().GetDBType() == Mongo {
if dbase, err = NewMongoDB(logv2.url, hatchetName); err != nil {
return nil, err
}
} else { // default is SQLite3
if dbase, err = NewSQLite3DB(logv2.url, hatchetName, logv2.cacheSize); err != nil {
return nil, err
}
}
dbase.SetVerbose(logv2.verbose)
return dbase, err
}
// GetExistingHatchetNames returns a list of existing hatchet names from the database
func GetExistingHatchetNames() ([]string, error) {
var err error
var dbase Database
logv2 := GetLogv2()
// Use a temporary name just to connect and query
if GetLogv2().GetDBType() == Mongo {
if dbase, err = NewMongoDB(logv2.url, "_temp"); err != nil {
return nil, err
}
} else {
if dbase, err = NewSQLite3DB(logv2.url, "_temp", logv2.cacheSize); err != nil {
return nil, err
}
}
defer dbase.Close()
return dbase.GetHatchetNames()
}
// ValidateHatchetName sanitizes and validates a hatchet name
func ValidateHatchetName(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
return "", fmt.Errorf("name cannot be empty")
}
// Replace special characters with underscore
for _, sep := range []string{"-", ".", " ", ":", ","} {
name = strings.ReplaceAll(name, sep, "_")
}
// Truncate if too long
if len(name) > MAX_SIZE {
name = name[:MAX_SIZE]
}
// Prepend underscore if starts with digit
if len(name) > 0 && unicode.IsDigit(rune(name[0])) {
name = "_" + name
}
return name, nil
}