-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
119 lines (106 loc) · 3.25 KB
/
storage.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
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"github.com/go-kit/kit/log/level"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
// Storage instance for one Source path
type Storage struct {
MainPath string `yaml:"-" json:"-"`
TimePath string `yaml:"-" json:"-"`
SourcePath string `yaml:"sourcePath" json:"sourcePath"`
Prepared bool `yaml:"-" json:"-"`
Accessible bool `yaml:"accessible" json:"accessible"`
}
func NewStorage(path string, sources Sources) *Storage {
return NewNameStorage(path, sources.Instance)
}
func NewNameStorage(path string, sources string) *Storage {
storage := Storage{}
var err error
storage.MainPath, err = filepath.Abs(path)
if err != nil {
storage.MainPath = path
}
storage.TimePath = filepath.Join(storage.MainPath, timeStart.Format("20060102-1504"))
if config.StoreDirect {
storage.TimePath = storage.MainPath
}
storage.SourcePath = filepath.Join(storage.TimePath, cleanFilePathName(sources))
storage.Prepared = false
storage.Accessible = false
_ = level.Debug(logger).Log("msg", fmt.Sprintf("final main store path %s", storage.TimePath))
return &storage
}
func (s *Storage) prepareDirectory() error {
if !s.Prepared {
if !dirExists(s.SourcePath) {
err := os.MkdirAll(s.SourcePath, os.ModePerm)
if err != nil {
_ = level.Error(logger).Log("msg", "problem create directory and path for store final data", "error", err, "path", s.SourcePath)
return err
}
}
}
s.Prepared = true
s.Accessible = dirAccessible(s.SourcePath)
_ = level.Debug(logger).Log("msg", "directory for store final data exist and accessible", "path", s.SourcePath)
return nil
}
func (s *Storage) saveOrganized(services OrganizedServices, timeRange v1.Range) {
d := OrganizedForFile{
From: timeRange.Start.Format(time.RFC3339),
To: timeRange.End.Format(time.RFC3339),
Step: config.Step,
Data: services,
}
data, err := json.Marshal(d)
f := fmt.Sprintf("organize-%s.json", filepath.Base(s.SourcePath))
if err != nil {
_ = level.Error(logger).Log("msg", "problem convert data to JSON file", "error", err, "file", f)
return
}
s.saveJson(data, filepath.Join(s.TimePath, f))
}
func (s *Storage) saveAllData(saveAllData []SaveAllData, fileName string) {
if s.Accessible {
data, err := json.Marshal(saveAllData)
if err != nil {
_ = level.Error(logger).Log("msg", "problem convert data to JSON file", "error", err, "file", fileName)
return
}
s.saveJson(data, filepath.Join(s.SourcePath, fileName))
}
}
func (s *Storage) saveJson(data []byte, fullFileName string) {
if s.Accessible {
err := os.WriteFile(fullFileName, data, os.ModePerm)
f := filepath.Base(fullFileName)
if err != nil {
_ = level.Error(logger).Log("msg", "problem save file", "error", err, "file", f)
} else {
_ = level.Debug(logger).Log("msg", "data successful saved to disk", "file", f)
}
}
}
func cleanFilePathName(path string) string {
trimmed := strings.TrimSpace(path)
trimmed = strings.ToLower(trimmed)
resp := ""
re := regexp.MustCompile(`(?i)(?i)([a-z0-9 \-_~.])`)
for i := 0; i < len(trimmed); i++ {
if re.MatchString(string(trimmed[i])) {
resp = resp + string(trimmed[i])
}
}
if len(resp) < 1 {
resp = RandomString()
}
return strings.TrimRight(resp, ".")
}