-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
110 lines (95 loc) · 3.12 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
105
106
107
108
109
110
package harper
import (
"time"
)
const (
LogOrderAsc = "asc"
LogOrderDesc = "desc"
LogSearchTypeAll = ""
LogSearchTypeTimestamp = "timestamp"
LogSearchTypeUsername = "username"
LogSearchTypeHashValue = "hash_value"
)
type LogEntry struct {
Level string `json:"level"`
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}
type LogResponse struct {
File []LogEntry `json:"file"`
}
type TxLogEntry struct {
Operation string `json:"operation"`
UserName string `json:"user_name"`
Timestamp Timestamp `json:"timestamp"` // this is a float
HashValues []interface{} `json:"hash_values"`
Records []map[string]interface{} `json:"records"`
// It would be possible to pass a target struct
// to the log read func to enable custom unmarshalling
// however in a log it might not be possibe to know
// the exact structure of the data
}
type AuditLogEntry struct {
Operation string `json:"operation"`
UserName string `json:"user_name"`
Timestamp Timestamp `json:"timestamp"`
HashValues []interface{} `json:"hash_values"`
Records []map[string]interface{}
}
func (c *Client) ReadLog(limit, start int, from, until time.Time, order string) (*LogResponse, error) {
var result LogResponse
err := c.opRequest(operation{
Operation: OP_READ_LOG,
Limit: limit,
Start: start,
From: from.Format(DATE_FORMAT),
Until: until.Format(DATE_FORMAT),
Order: order,
}, &result)
return &result, err
}
// ReadTransactionLog requests the transaction log for a table.
// Use LogSearchType* constants to filter the log entries by searchValues,
// which should be an array/slice of searchType.
// Leave searchType empty (LogSearchTypeAll) to get all entries.
func (c *Client) ReadTransactionLog(schema, table, searchType string, searchValues interface{}) ([]TxLogEntry, error) {
var result []TxLogEntry
err := c.opRequest(operation{
Operation: OP_READ_TRANSACTION_LOG,
Schema: schema,
Table: table,
SearchType: searchType,
SearchValues: searchValues,
}, &result)
return result, err
}
// Leave searchType empty (LogSearchTypeAll) to get all entries.
func (c *Client) ReadAuditLog(schema, table string, searchType string, searchValues interface{}) ([]AuditLogEntry, error) {
var result []AuditLogEntry
err := c.opRequest(operation{
Operation: OP_READ_AUDIT_LOG,
Schema: schema,
Table: table,
SearchType: searchType,
SearchValues: searchValues,
}, &result)
return result, err
}
func (c *Client) DeleteTransactionLogsBefore(schema, table string, timestamp time.Time) error {
return c.opRequest(operation{
Operation: OP_DELETE_TRANSACTION_LOG,
Schema: schema,
Table: table,
Timestamp: timestamp.UnixNano(),
}, nil)
}
func (c *Client) DeleteAuditLogsBefore(schema, table string, timestamp time.Time) (*MessageResponse, error) {
var response MessageResponse
err := c.opRequest(operation{
Operation: OP_DELETE_AUDIT_LOGS_BEFORE,
Schema: schema,
Table: table,
Timestamp: timestamp.UnixMilli(),
}, &response)
return &response, err
}