forked from nwilkens/graphite-ng
/
text.go
101 lines (91 loc) · 2.34 KB
/
text.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
package stores
import (
"bufio"
"fmt"
"github.com/graphite-ng/graphite-ng/chains"
"github.com/graphite-ng/graphite-ng/config"
"github.com/graphite-ng/graphite-ng/metrics"
"io/ioutil"
"os"
"strconv"
"strings"
)
func (t *TextStore) path(name string) string {
return fmt.Sprintf("%s/%s.txt", t.BasePath, name)
}
type TextStore struct {
BasePath string
}
func NewTextStore(config config.Main) Store {
path := config.StoreText.Path
return TextStore{path}
}
func init() {
InitFn["text"] = NewTextStore
}
func (t TextStore) Add(metric metrics.Metric) (err error) {
panic("todo")
}
func (t TextStore) Get(name string) (our_el *chains.ChainEl, err error) {
our_el = chains.NewChainEl()
go func(our_el *chains.ChainEl) {
var file *os.File
path := t.path(name)
if file, err = os.Open(path); err != nil {
panic(err)
}
defer file.Close()
from := <-our_el.Settings
until := <-our_el.Settings
scanner := bufio.NewScanner(file)
first := true
// this will be used to fill the potential gap between last datapoint and until,
// but also if there were no (matching) datapoints in the file at all.
last_ts := from - 60
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, " ")
ts, _ := strconv.ParseInt(parts[0], 10, 32)
val, _ := strconv.ParseFloat(parts[1], 64)
known, _ := strconv.ParseBool(parts[2])
dp := metrics.NewDatapoint(int32(ts), val, known)
if first {
if from < dp.Ts {
for new_ts := from; new_ts < dp.Ts; new_ts += 60 {
our_el.Link <- *metrics.NewDatapoint(new_ts, 0.0, false)
}
}
}
if dp.Ts >= from && dp.Ts <= until {
our_el.Link <- *dp
last_ts = dp.Ts
}
first = false
}
if err := scanner.Err(); err != nil {
panic(fmt.Sprintf("error reading %s: %s", path, err.Error()))
}
if last_ts < until {
for new_ts := last_ts + 60; new_ts <= until+60; new_ts += 60 {
our_el.Link <- *metrics.NewDatapoint(new_ts, 0.0, false)
}
}
}(our_el)
return our_el, nil
}
func (t TextStore) Has(name string) (found bool, err error) {
_, err = os.Stat(t.path(name))
return (err == nil), nil
}
func (t TextStore) List() (list []string, err error) {
file_info, err := ioutil.ReadDir(t.BasePath)
if err != nil {
return
}
list = make([]string, len(file_info))
for i, fi := range file_info {
name := fi.Name()
list[i] = name[:len(name)-4]
}
return
}