This repository was archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_json.go
71 lines (57 loc) · 1.56 KB
/
storage_json.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
package main
import (
"fmt"
"log"
scribble "github.com/nanobox-io/golang-scribble"
)
const (
// Collection identifier for JSON collection about Metric
Collection = "metrics"
// DefaultPathStorageJSON is a default path to storage JSON
// when the STORAGE_URL env/config is not set
DefaultPathStorageJSON = "./data/"
)
// StorageJSON is the data storage layer using JSON file
type StorageJSON struct {
db *scribble.Driver
}
// NewStorageJSON create a new driver to storage JSON in file
func NewStorageJSON(location string) (*StorageJSON, error) {
var err error
stg := new(StorageJSON)
if len(location) < 1 {
location = DefaultPathStorageJSON
}
stg.db, err = scribble.New(location, nil)
if err != nil {
return nil, err
}
return stg, nil
}
func getFileNameJSON(metric Metric) string {
timestampFormat := "20060102_150405.000"
timeOfMetric := metric.Timestamp.Format(timestampFormat)
name := metric.Prefix
if len(name) < 1 {
name = metric.Hostname
}
return fmt.Sprintf("%s_%s", name, timeOfMetric)
}
// SaveMetric save a new metric
func (s *StorageJSON) SaveMetric(metric Metric) error {
log.Printf("metric: %+v\n", metric)
filename := getFileNameJSON(metric)
if err := s.db.Write(Collection, filename, metric); err != nil {
return err
}
return nil
}
// SaveItem save a new metric with SaveMetric
func (s *StorageJSON) SaveItem(metric Metric) error {
return s.SaveMetric(metric)
}
// ItemExists check with item exist
// not implemented to JSON, only function signature
func (s *StorageJSON) ItemExists(metric Metric) (found bool) {
return found
}