@@ -43,44 +43,57 @@ func Main() {
4343 if err != nil {
4444 log .Fatal (err )
4545 }
46+ if config .ExtractDays > 45 || config .ExtractDays < 2 {
47+ log .Fatal ("ExtractDays must be between 2 and 45 per smarthub" )
48+ }
4649
4750 // parse time flags
4851 startFlag := flag .String ("start" , "" , "Start date of period to extract from electric co." )
4952 endFlag := flag .String ("end" , "" , "End date of period to extract from electric co." )
5053 flag .Parse ()
5154 var startDate , endDate time.Time
5255 if * startFlag != "" {
53- startDate , err = time .Parse ("2006-01-02" , * startFlag )
56+ startDate , err = time .ParseInLocation ("2006-01-02" , * startFlag , time . Local )
5457 if err != nil {
5558 log .Fatal (err )
5659 }
5760 if * endFlag == "" {
5861 log .Fatal ("start and end parameters must both be provided" )
5962 }
60- endDate , err = time .Parse ("2006-01-02" , * endFlag )
63+ endDate , err = time .ParseInLocation ("2006-01-02" , * endFlag , time . Local )
6164 if err != nil {
6265 log .Fatal (err )
6366 }
67+ if endDate .Sub (startDate ).Hours () > 24 * 45 {
68+ log .Fatal ("start and end parameters must define a period of no more than 45 days" )
69+ }
70+ // endDate should be the last minute of the day for the VictoriaMetrics query.
71+ endDate = endDate .Add ((24 * time .Hour ) - time .Minute )
6472 } else {
65- endDate = time .Now ().Truncate (24 * time .Hour )
66- startDate = endDate .Add (time .Duration (- config .ExtractDays ) * 24 * time .Hour )
73+ // yesterday
74+ year , month , day := time .Now ().Date ()
75+ // endDate should be the last minute of the day for the VictoriaMetrics query.
76+ endDate = time .Date (year , month , day , 23 , 59 , 0 , 0 , time .Local )
77+ // subtract N days and 1 minute to get the start date
78+ startDate = endDate .Add (time .Duration (- config .ExtractDays ) * 48 * time .Hour ).Add (time .Minute )
6779 }
6880
69- path , err := DownloadCsv (config , startDate .Format ("01/02/2006" ), endDate .Format ("01/02/2002 " ))
81+ path , err := DownloadCsv (config , startDate .Format ("01/02/2006" ), endDate .Format ("01/02/2006 " ))
7082 if err != nil {
7183 log .Fatal (err )
7284 }
7385 fmt .Printf ("file downloaded: %s" , path )
7486
75- // parse csv
7687 records , err := ParseCsv (path )
7788 if err != nil {
7889 log .Fatal (err )
7990 }
80- fmt .Printf ("%+v" , records )
8191 existingPoints , err := QueryPreviousMetrics (startDate , endDate , config .InfluxDB )
8292 if err != nil {
8393 log .Fatal (err )
8494 }
85- WriteMetrics (records , config .InfluxDB , existingPoints )
95+ err = WriteMetrics (records , config .InfluxDB , existingPoints )
96+ if err != nil {
97+ log .Fatal (err )
98+ }
8699}
0 commit comments