-
Notifications
You must be signed in to change notification settings - Fork 1
/
filestore.go
203 lines (157 loc) · 6.51 KB
/
filestore.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package cadastre
import "os"
import "time"
import "fmt"
import "log"
import "regexp"
import "path/filepath"
import "bufio"
import "strings"
type FileStore struct {
DataDirectory string
absDataDirectory string
RetentionPeriod time.Duration
}
func (me *FileStore) Initialize() error {
// Make sure our data directory is a qualified, absolute path.
absDataDirectory, err := filepath.Abs(me.DataDirectory)
if err != nil {
return fmt.Errorf("Error normalizing the data directory path! %s", err)
}
me.absDataDirectory = absDataDirectory
return nil
}
func (me *FileStore) RetrieveSnapshot(identifier string, timestamp time.Time) (*Snapshot, error) {
// First, parse our timestamp into YYYY-MM-DD and then YYYY-MM-DD-hh-mm-ss so we can build a filepath to try and access.
baseDirectoryName := timestamp.Format("2006-01-02")
snapshotFileName := timestamp.Format("2006-01-02-15-04-05") + ".spl"
finalDirectoryPath := filepath.Join(me.absDataDirectory, baseDirectoryName, identifier)
// See if the directory pointed to by this timestamp actually exists.
if !doesDirectoryExist(finalDirectoryPath) {
// We didn't find anything.... that's weird. Maybe we only just started? In any case, it could
// be a case of legitimately having no data, so just return nil.
return nil, fmt.Errorf("No data available for the given time period.")
}
finalFilePath := filepath.Join(finalDirectoryPath, snapshotFileName)
// Now see if the file is there.
if !doesFileExist(finalFilePath) {
// Well that's weird, but again, this could be an erroneous request and the timestamp could
// be in the future or the client could be buggy or whatever. Just return nil.
return nil, fmt.Errorf("No data available for the given time period.")
}
return hydrateSnapshotFromFile(finalFilePath)
}
func (me *FileStore) RetrieveCounts(identifier string, datestamp time.Time) (*Counts, error) {
counts := make([]Count, 0)
// First, parse our datestamp into YYYY-MM-DD so we can build a filepath to try and access.
baseDirectoryName := datestamp.Format("2006-01-02")
finalDirectoryPath := filepath.Join(me.absDataDirectory, baseDirectoryName, identifier)
// See if the directory pointed to by this datestamp actually exists.
if !doesDirectoryExist(finalDirectoryPath) {
// We didn't find anything.... that's weird. Maybe we only just started? In any case, it could
// be a case of legitimately having no data, so just return nil.
return nil, fmt.Errorf("No data available for the given time period.")
}
splFileRegexp := regexp.MustCompile("^\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}-\\d{2}\\.spl$")
// Now we gotta walk all the files in the directory to grab the counts.
filepath.Walk(finalDirectoryPath, func(path string, info os.FileInfo, _ error) error {
// We're only looking for files.
if info.IsDir() {
return nil
}
// See if the filename matches our single snapshot naming scheme.
if splFileRegexp.MatchString(filepath.Base(path)) {
// We should have a legitimate SPL file here, so hydrate it and get the count.
snapshot, err := hydrateSnapshotFromFile(path)
if err != nil {
// Nothing to do here. Move on to the next one.
return nil
}
// Add this count to the rest.
counts = append(counts, snapshot.GetCount())
}
return nil
})
return &Counts{Counts: counts}, nil
}
func (me *FileStore) Persist(identifier string, value *Snapshot) error {
timestamp := time.Unix(value.Timestamp, 0)
// Make sure the folder exists for our identifier, and within that, today's date. Create either if they don't exist.
topLevelTime := timestamp.Format("2006-01-02")
lowLevelTime := timestamp.Format("2006-01-02-15-04-05")
targetDirectory := filepath.Join(me.absDataDirectory, topLevelTime, identifier)
if err := createDirectoryIfNotExists(targetDirectory); err != nil {
return fmt.Errorf("Failed to create the directory to store a process list snapshot! Identifier: %s, Error: %s", identifier, err)
}
targetDataFile := filepath.Join(targetDirectory, lowLevelTime+".spl")
// We have our target directory now, so let's create/open our data file and give the snapshot a writer to serialize itself to.
targetFileHandle, err := os.OpenFile(targetDataFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0774)
if err != nil {
return fmt.Errorf("Failed to get a file handle to our target data file! %s", err)
}
// Create an I/O writer buffer to pass to the snapshot when we ask it to serialize itself.
targetFileWriter := bufio.NewWriter(targetFileHandle)
defer func() {
if err := targetFileWriter.Flush(); err != nil {
log.Fatalf("Failed to flush the writer for '%s'!: %s", targetDataFile, err)
}
if err := targetFileHandle.Close(); err != nil {
log.Fatalf("Failed to close the file handle for '%s'! %s", targetDataFile, err)
}
}()
// Tell the snapshot to serialize/write itself out.
if err := value.WriteTo(targetFileWriter); err != nil {
return fmt.Errorf("Failed to read from snapshot during persisting to disk! %s", err)
}
return nil
}
func (me *FileStore) clean() error {
return nil
}
func createDirectoryIfNotExists(path string) error {
// Check if the directory even exist.
if !doesDirectoryExist(path) {
// It doesn't exist, so let's create it.
if err := os.MkdirAll(path, 0775); err != nil {
return fmt.Errorf("Unable to create folder! Path: %s, Error: %s", path, err)
}
}
// If we're here, our folder exited or was created without error.
return nil
}
func doesDirectoryExist(path string) bool {
if stat, _ := os.Stat(path); stat != nil && stat.IsDir() {
return true
}
return false
}
func doesFileExist(path string) bool {
if stat, _ := os.Stat(path); stat != nil && !stat.IsDir() {
return true
}
return false
}
func getFileWithoutExtension(filename string) string {
baseFile := filepath.Base(filename)
return strings.Replace(baseFile, filepath.Ext(baseFile), "", -1)
}
func hydrateSnapshotFromFile(path string) (*Snapshot, error) {
// Let's open the data file and give the snapshot a reader to deserialize itself from.
snapshotFileHandle, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Failed to get a file handle to our latest data file! %s", err)
}
// Create an I/O reader buffer to pass to the snapshot when we ask it to unserialize itself.
snapshotFileReader := bufio.NewReader(snapshotFileHandle)
defer func() {
if err := snapshotFileHandle.Close(); err != nil {
log.Fatalf("Failed to close the file handle for '%s'! %s", path, err)
}
}()
// Ask for a snapshot back.
snapshot, err := NewSnapshotFromReader(snapshotFileReader)
if err != nil {
return nil, err
}
return snapshot, nil
}