Skip to content

Commit 42331d7

Browse files
committed
add --csv-file flag to write data to a csv file. fixes #22
- add csv-file flag - when flag is populated, write csv data to that file and do not write data to influx.
1 parent 230fdcf commit 42331d7

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

internal/app/csv.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package app
2+
3+
import (
4+
"encoding/csv"
5+
"os"
6+
"strconv"
7+
)
8+
9+
func WriteCsv(records []ElectricUsage, csvFile string) error {
10+
// Open the CSV file for writing
11+
file, err := os.Create(csvFile)
12+
if err != nil {
13+
return err
14+
}
15+
defer file.Close()
16+
17+
// Create a CSV writer
18+
writer := csv.NewWriter(file)
19+
defer writer.Flush()
20+
21+
// Write comments
22+
_, err = file.WriteString(`# Notes:
23+
# MeterName will only be populated if there are multiple meters returned in the data.
24+
# StartUnixMillis and EndUnixMillis will only be in the correct timezone if you specify the correct timezone in the config.
25+
`)
26+
if err != nil {
27+
return err
28+
}
29+
30+
// Write the header row
31+
header := []string{"StartUnixMillis", "EndUnixMillis", "WattHours", "CostInCents", "MeterName"}
32+
if err := writer.Write(header); err != nil {
33+
return err
34+
}
35+
36+
// Write the data rows
37+
for _, record := range records {
38+
meterName := ""
39+
if record.MeterName != nil {
40+
meterName = *record.MeterName
41+
}
42+
costInCents := ""
43+
if record.CostInCents != nil {
44+
costInCents = strconv.FormatInt(*record.CostInCents, 10)
45+
}
46+
row := []string{
47+
strconv.FormatInt(record.StartTime.UnixMilli(), 10),
48+
strconv.FormatInt(record.EndTime.UnixMilli(), 10),
49+
strconv.FormatInt(record.WattHours, 10),
50+
costInCents,
51+
meterName,
52+
}
53+
if err := writer.Write(row); err != nil {
54+
return err
55+
}
56+
}
57+
return nil
58+
}

internal/app/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Main() error {
4747
startFlag := flag.String("start", "", "Start date of period to extract from electric co.")
4848
endFlag := flag.String("end", "", "End date of period to extract from electric co.")
4949
debugFlag := flag.Bool("debug", false, "Enable to print out verbose debugging logs.")
50+
csvFlag := flag.String("csv-file", "", "Path to output data in csv format, as an alternative to writing to influx")
5051
flag.Parse()
5152

5253
debug = *debugFlag
@@ -119,10 +120,18 @@ func Main() error {
119120
if err != nil {
120121
return err
121122
}
122-
fmt.Println("Writing data to database...")
123-
err = WriteMetrics(usage, config.InfluxDB)
124-
if err != nil {
125-
return err
123+
if *csvFlag != "" {
124+
fmt.Println("Writing data to csv file...")
125+
err = WriteCsv(usage, *csvFlag)
126+
if err != nil {
127+
return err
128+
}
129+
} else {
130+
fmt.Println("Writing data to database...")
131+
err = WriteMetrics(usage, config.InfluxDB)
132+
if err != nil {
133+
return err
134+
}
126135
}
127136
fmt.Println("Done")
128137
return nil

0 commit comments

Comments
 (0)