-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
72 lines (60 loc) · 1.42 KB
/
item.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
const (
TimeFormat = "2006-01-02 15:04:05"
)
var (
reItemKeyParams = regexp.MustCompile(`\[([^\]]+)\]`)
)
type Item struct {
ID string `json:"itemid"`
HostID string `json:"hostid"`
Name string `json:"name"`
ValueType string `json:"value_type"`
LastValue string `json:"lastvalue"`
LastClock interface{} `json:"lastclock"`
Key string `json:"key_"`
Type ItemType `json:"type"`
}
func (item *Item) DateTime() string {
if item.getLastClock() == "0" {
return "-"
}
return item.date().Format(TimeFormat)
}
func (item *Item) getLastClock() string {
switch typed := item.LastClock.(type) {
case string:
return typed
case float64:
return fmt.Sprint(int64(typed))
default:
fatalf("unexpected type '%v' for lastclock in item ID %s (%s)\nplease file a bug report.", typed, item.ID, item.Name)
return "error"
}
}
func (item *Item) date() time.Time {
date, err := strconv.ParseInt(item.getLastClock(), 10, 64)
if err != nil {
debugf("Error: %+v", err)
}
return time.Unix(date, 0)
}
func (item *Item) Format() string {
name := item.Name
match := reItemKeyParams.FindStringSubmatch(item.Key)
if len(match) == 0 {
return name
}
args := strings.Split(match[1], ",")
for index, arg := range args {
name = strings.Replace(name, fmt.Sprintf(`$%d`, index+1), arg, -1)
}
return name
}