-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.go
531 lines (485 loc) · 14.7 KB
/
start.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package main
import (
binary "encoding/binary"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
var (
parseableFileFullPath = filepath.Join(parseFolderPath, parseableFileName)
unparseableFileFullPath = filepath.Join(parseFolderPath, unparseableFileName)
)
const maxInt32 = 1<<(32-1) - 1
func RunAppCommands() (string, error) {
enteredCommand := kingpin.MustParse(app.Parse(os.Args[1:]))
switch enteredCommand {
case redgenVersion.FullCommand():
return helpVersion, nil
case redgenConfigStart.FullCommand():
return InitGenerator()
case redgenConfigGenerate.FullCommand():
return CmdGenerate(*redgenConfigGenerateArg)
case redgenConfigInit.FullCommand():
return CmdInit()
case redgenConfigPreview.FullCommand():
if *redgenConfigPreviewArg != "" {
CmdPreviewAction(*redgenConfigPreviewArg, *redgenConfigPreviewArgTime)
return "", nil
}
CmdPreviewAction(defaultProfileName, *redgenConfigPreviewArgTime)
return "", nil
case redgenConfigSend.FullCommand():
if *redgenConfigSendArg != "" {
CmdSendReadingsToServer(*redgenConfigSendArg)
return "", nil
}
return helpMsg, nil
case redgenConfigValidate.FullCommand():
if *redgenConfigValidateArg != "" {
CmdValidateAction(*redgenConfigValidateArg, true)
}
CmdValidateAction(*redgenConfigValidateArg, false)
return "", nil
case redgenConfigProfile.FullCommand():
if *redgenConfigProfileArg != "" {
CmdProfileAction(*redgenConfigProfileArg)
return "", nil
}
return helpMsg, nil
case redgenConfigShow.FullCommand():
if *redgenConfigShowFileName != "" {
if *redgenConfigShowDateFlag != "" {
ShowDateConsumption(*redgenConfigShowFileName, *redgenConfigShowDateFlag)
}
return "", nil
}
return helpMsg, nil
case redgenConfigClear.FullCommand():
return "", nil
default:
return helpMsg, fmt.Errorf("unknown command: %s", enteredCommand)
}
return "", nil
}
func CmdInit() (string, error) {
err := WriteProfileToFile(CreateDefaultProfile(""), defaultProfilePath, defaultProfileName)
if err != nil {
log.Fatal(err.Error())
}
return "Default profile file created into ./profiles", nil
}
func CmdGenerate(arg string) (string, error) {
if arg != "" {
namesArr := strings.Split(arg, ".")
extractedName := SanitizeName(namesArr[0])
err := WriteProfileToFile(CreateDefaultProfile(extractedName), defaultProfilePath, arg)
if err != nil {
log.Fatal(err.Error())
}
return fmt.Sprintf("%s file created into ./profiles", arg), nil
} else {
return helpMsg, nil
}
}
func CmdProfileAction(filename string) {
fileBytes, err := ioutil.ReadFile(filepath.Join(defaultProfilePath, filename))
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
if err != nil {
log.Fatal(err.Error())
}
GenerateReadings(profile, defaultReadingsPath)
}
func CmdPreviewAction(filename string, flag string) {
fileBytes, err := ioutil.ReadFile(filepath.Join(defaultProfilePath, filename))
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
profile.Validate()
fmt.Println("Generating sample consumption data for the profile")
profile = GeneratePreviewData(profile, flag)
startTime := profile.Readings[0].Time.String()
formattedDay := startTime[:10]
formattedMonth := startTime[:7]
formattedYear := startTime[:4]
if flag == "day" {
fmt.Println("Show for day")
ShowDayConsumption(profile, formattedDay)
} else if flag == "month" {
fmt.Println("Show for month")
ShowMonthConsumption(profile, formattedMonth)
} else if flag == "year" {
fmt.Println("Show for year")
ShowYearConsumption(profile, formattedYear)
} else {
fmt.Println("back to default, showing for day")
ShowDayConsumption(profile, formattedDay)
}
}
func GeneratePreviewData(profile Profile, timeFmt string) Profile {
var (
baseDailyConsumption = profile.BaseDailyConsumption
variability = profile.Variability
)
date, state, err := profile.StartAt()
if err != nil {
log.Fatal(err.Error())
}
var valuesToGenerate int
switch timeFmt {
case "day":
valuesToGenerate = 100
break
case "month":
valuesToGenerate = 3000
break
case "year":
valuesToGenerate = 36000
break
}
var readings []Reading
for i := 0; i < valuesToGenerate; i++ {
hourValue := strconv.Itoa(date.Hour())
weekValue := date.Weekday().String()
monthValue := date.Month().String()
var (
hourBase = profile.HourlyProfiles[hourValue]
weekBase = profile.WeeklyProfiles[weekValue[:3]]
monthBase = profile.MonthlyProfiles[monthValue[:3]]
)
reading := NewReading(date, profile.Unit, profile.Interval, baseDailyConsumption, hourBase, weekBase, monthBase, variability, state)
readings = append(readings, reading)
time.Sleep(5 * time.Millisecond)
date = date.Add(time.Duration(profile.Interval) * time.Minute)
}
profile.Readings = readings
return profile
}
func CmdValidateSingleFile(filename string) {
fmt.Printf("attempting to validate profile with name: %s", filename)
fileBytes, err := ioutil.ReadFile(filepath.Join(defaultProfilePath, filename))
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
if err != nil {
log.Fatal(err.Error())
}
err = profile.Validate()
if err != nil {
fmt.Println("The profile is not a valid profile")
}
fmt.Println("------The profile configuration is valid------------")
}
func CmdValidateAction(filename string, singleFile bool) {
if singleFile {
CmdValidateSingleFile(filename)
} else {
fileList, err := ioutil.ReadDir(defaultProfilePath)
if err != nil {
panic("could not complete the listing of profile configurations")
}
for _, file := range fileList {
CmdValidateSingleFile(file.Name())
}
}
}
func CmdSendReadingsToServer(filename string) {
fileBytes, err := ioutil.ReadFile(filepath.Join(defaultProfilePath, filename))
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
if err != nil {
log.Fatal(err.Error())
}
err = profile.Validate()
if err != nil {
log.Println("The profile is not valid", profile)
}
librarianService := new(LibrarianService)
// send the readings to the api at this point
resp, err := librarianService.sendReadingsAction(profile)
fmt.Println("SendReadingsAction() httpResp", resp)
}
// InitGenerator starts the application and calls the function which runs
// every 5 seconds for now (should be 15 mins ideally)
func InitGenerator() (string, error) {
t := time.NewTicker(time.Second * 5)
for {
SendReadingsOnStart()
<-t.C
}
return "", nil
}
func SendReadingsOnStart() {
parseableFileNamesList := GetAppendedParsedFileNames()
// check if a file with the last reading exists, if not create and leave empty
// generate readings at the specified interval in the config and add to the /readings/filename_readings.json file
// when you stop the app, and start again, it should get the last reading, compare the time interval of the last reading with current time
// account for the time lost by updating the state with an estimated value for the time it was offline but it should continue appending
// reading from now (just add the state from say State: 10 -> 12 -> [...offline for three missed readings] -> 20 [14->16->18 skipped] but state added
for _, filename := range parseableFileNamesList {
profile, _ := GetProfileFromFile(filename)
profile = GenerateSingleReading(profile)
librarianService := new(LibrarianService)
resp, err := librarianService.sendReadingsAction(profile)
if err == nil {
if resp != nil {
if resp.StatusCode == 200 || resp.StatusCode == 201 {
SaveReadings(profile, defaultReadingsPath)
} else {
log.Println("Sent reading responded with a status other than 200 OK success", resp.StatusCode)
}
}
} else {
log.Println("Encountered an error while sending reading to the API", err)
}
time.Sleep(time.Second * 1)
}
}
func GetProfileFromFile(filename string) (Profile, bool) {
if _, err := os.Stat(defaultReadingsPath); os.IsNotExist(err) {
os.Mkdir(defaultReadingsPath, os.ModePerm)
}
readingsFile := filepath.Join(defaultReadingsPath, filename)
if _, err := os.Stat(readingsFile); os.IsNotExist(err) {
_, err = os.Create(readingsFile)
profile, _ := GetProfileFromJson(filepath.Join(defaultProfilePath, filename))
_ = WriteProfileToFile(profile, defaultReadingsPath, filename)
return profile, false
} else {
profile, _ := GetProfileFromJson(filepath.Join(defaultReadingsPath, filename))
return profile, true
}
}
func GetProfileFromJson(filepath string) (Profile, error) {
fileBytes, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
if err != nil {
log.Fatal(err.Error())
}
err = profile.Validate()
return profile, err
}
func GetAppendedParsedFileNames() (validFileNames []string) {
parseableNamesList, _ := SplitParseableConfigFiles()
os.MkdirAll(parseFolderPath, os.ModePerm)
if _, err := os.Stat(parseableFileFullPath); os.IsNotExist(err) {
_, err = os.Create(parseableFileFullPath)
}
parseableNamesBytes := EncodeFileNamesAsBytes(parseableNamesList)
err := ioutil.WriteFile(parseableFileFullPath, parseableNamesBytes, 0644)
checkWithPanic(err)
parseableFileBytes, err := ioutil.ReadFile(parseableFileFullPath)
if err != nil {
log.Fatal(err)
}
parseableFileNamesList := DecodeBytesAsFileNames(parseableFileBytes)
return parseableFileNamesList
}
func SplitParseableConfigFiles() (parseableList []string, unparseableList []string) {
files, err := ioutil.ReadDir(defaultProfilePath)
if err != nil {
log.Fatal(err)
}
var parseableNamesList []string
var unparseableNamesList []string
for _, f := range files {
fileBytes, err := ioutil.ReadFile(filepath.Join(defaultProfilePath, f.Name()))
if err != nil {
log.Fatal(err.Error())
}
profile, err := NewProfileFromJson(fileBytes)
if err != nil {
log.Fatal(err.Error())
}
err = profile.Validate()
if err != nil {
unparseableNamesList = append(unparseableNamesList, f.Name())
panic(err)
} else {
parseableNamesList = append(parseableNamesList, f.Name())
}
}
return parseableNamesList, unparseableNamesList
}
func checkWithPanic(err error) {
if err != nil {
panic(err)
}
}
func writeLen(b []byte, l int) []byte {
if 0 > l || l > maxInt32 {
panic("writeLen: invalid length")
}
var lb [4]byte
binary.BigEndian.PutUint32(lb[:], uint32(l))
return append(b, lb[:]...)
}
func readLen(b []byte) ([]byte, int) {
if len(b) < 4 {
panic("readLen: invalid length")
}
l := binary.BigEndian.Uint32(b)
if l > maxInt32 {
panic("readLen: invalid length")
}
return b[4:], int(l)
}
func DecodeBytesAsFileNames(b []byte) []string {
b, ls := readLen(b)
s := make([]string, ls)
for i := range s {
b, ls = readLen(b)
s[i] = string(b[:ls])
b = b[ls:]
}
return s
}
func EncodeFileNamesAsBytes(s []string) []byte {
var b []byte
b = writeLen(b, len(s))
for _, ss := range s {
b = writeLen(b, len(ss))
b = append(b, ss...)
}
return b
}
func ShowDateConsumption(filename string, date string) {
profile, err := GetProfileFromJson(filepath.Join(defaultReadingsPath, filename))
if err != nil {
log.Fatal(err.Error())
}
splitDate := strings.Split(date, "-")
if len(splitDate) == 1 {
ShowYearConsumption(profile, date)
} else if len(splitDate) == 2 {
ShowMonthConsumption(profile, date)
} else if len(splitDate) == 3 {
ShowDayConsumption(profile, date)
}
}
func GetValueForEnergyUnit(unit string) float64 {
var unitValueMap = map[string]float64{
"mW": 1 / 1000,
"W": 1,
"kW": 1000,
"MW": 1000000,
"GW": 100000000,
}
if unitValueMap[unit] != 0 {
return unitValueMap[unit]
}
return 0
}
func ShowDayConsumption(profile Profile, day string) {
mDay := day
intervalMultiplier := 60 / profile.Interval
var newReadingValues []Reading
for _, reading := range profile.Readings {
if strings.Contains(reading.Time.String(), mDay) {
reading.State = reading.State * intervalMultiplier
newReadingValues = append(newReadingValues, reading)
}
}
// calculate the differences between two hours for each reading
readingHourMap := make(map[int]float64)
for _, reading := range newReadingValues {
if readingHourMap[reading.Time.Hour()] == 0 {
readingHourMap[reading.Time.Hour()] = reading.State
}
}
var hourKeys []int
var hourLabels []int
for k, _ := range readingHourMap {
hourKeys = append(hourKeys, k)
}
sort.Ints(hourKeys)
unit := ""
for _, k := range hourKeys {
switch profile.Unit {
case "kW":
hourLabels = append(hourLabels, int(readingHourMap[k]*GetValueForEnergyUnit(profile.Unit)))
unit = "W"
break
default:
hourLabels = append(hourLabels, int(readingHourMap[k]))
unit = profile.Unit
break
}
}
newHourKeys := GetStringSliceFromInt(hourKeys)
header := fmt.Sprintf("Hourly Consumption for %s in (%s) ", mDay, unit)
PlotBarChart(hourLabels, newHourKeys, header)
}
func ShowMonthConsumption(profile Profile, month string) {
mMonth := month
intervalMultiplier := 60 / profile.Interval
var newReadingValues []Reading
for _, reading := range profile.Readings {
if strings.Contains(reading.Time.String(), mMonth) {
reading.State = reading.State * intervalMultiplier
newReadingValues = append(newReadingValues, reading)
}
}
readingMonthMap := make(map[int]float64)
for _, reading := range newReadingValues {
readingMonthMap[reading.Time.Day()] = reading.State
}
var daysKeys []int
var daysLabels []float64
for k, _ := range readingMonthMap {
daysKeys = append(daysKeys, k)
daysLabels = append(daysLabels, readingMonthMap[k])
}
sort.Ints(daysKeys)
var normDaysLabels []string
for _, k := range daysKeys {
normDaysLabels = append(normDaysLabels, strconv.Itoa(k))
}
normDaysKeys := GetIntSliceFromFloat(daysLabels)
header := fmt.Sprintf("Monthly Consumption for %s in (%s) ", mMonth, profile.Unit)
PlotBarChart(normDaysKeys, normDaysLabels, header)
}
func ShowYearConsumption(profile Profile, year string) {
mYear := year
intervalMultiplier := 60 / profile.Interval
var newReadingValues []Reading
for _, reading := range profile.Readings {
if strings.Contains(reading.Time.String(), mYear) {
reading.State = reading.State * intervalMultiplier
newReadingValues = append(newReadingValues, reading)
}
}
readingMonthMap := make(map[time.Month]float64)
for _, reading := range newReadingValues {
readingMonthMap[reading.Time.Month()] = reading.State
}
var monthKeys []time.Month
var monthLabels []float64
for k, _ := range readingMonthMap {
monthKeys = append(monthKeys, k)
monthLabels = append(monthLabels, readingMonthMap[k])
}
var normMonthLabels []string
for _, k := range monthKeys {
normMonthLabels = append(normMonthLabels, k.String())
}
normMonthKeys := GetIntSliceFromFloat(monthLabels)
header := fmt.Sprintf("Monthly Consumption for %s in (%s) ", mYear, profile.Unit)
PlotBarChart(normMonthKeys, normMonthLabels, header)
}