Skip to content

Commit

Permalink
feat: support use timestamp value on @timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
hengfeiyang committed Apr 11, 2022
1 parent 91eab75 commit b830eb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/core/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,19 @@ func (index *Index) BuildBlugeDocumentFromJSON(docID string, doc *map[string]int

timestamp := time.Now()
if v, ok := flatDoc["@timestamp"]; ok {
t, err := time.Parse(time.RFC3339, v.(string))
if err == nil && !t.IsZero() {
timestamp = t
delete(*doc, "@timestamp")
switch v := v.(type) {
case string:
if t, err := time.Parse(time.RFC3339, v); err == nil && !t.IsZero() {
timestamp = t
delete(*doc, "@timestamp")
}
case float64:
if t := zutils.Unix(int64(v)); !t.IsZero() {
timestamp = t
delete(*doc, "@timestamp")
}
default:
// noop
}
}
docByteVal, _ := json.Marshal(*doc)
Expand Down
13 changes: 13 additions & 0 deletions pkg/zutils/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ func FormatDuration(d time.Duration) string {
}
return strconv.FormatInt(int64(d.Seconds()), 10) + "s"
}

func Unix(n int64) time.Time {
if n > 1e18 {
return time.Unix(0, n)
}
if n > 1e15 {
return time.UnixMicro(n)
}
if n > 1e12 {
return time.UnixMilli(n)
}
return time.Unix(n, 0)
}

0 comments on commit b830eb8

Please sign in to comment.