-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhauled how things were processed
We now process every csv file in the Input directory for power data. SampleData.csv moved to Input folder. Split some functions out to separate files for ease of reading. runCalculations now returns a struct of PowerDataReturn that we can use to print output later. WaitGroup was setup to prevent exiting before the go routines finish collecting data from the files.
- Loading branch information
1 parent
8c0fad1
commit e0cc17c
Showing
5 changed files
with
184 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import "sync" | ||
|
||
// Run the calculations on the file and return a struct of PowerDataReturn | ||
func runCalculations(path string, data [][]PowerData, wg *sync.WaitGroup) PowerDataReturn { | ||
var totalkWh float64 | ||
var totalDataPoints float64 | ||
var totalDailykWh float64 | ||
var totalDays float64 | ||
var lowestDay string | ||
var highestDay string | ||
|
||
lowestDaykWh, highestDaykWh := 100.00, 0.0 // We default lowestDay to 100 and highestDay to 0 | ||
dailykWh := make(map[string]float64) // Track daily | ||
hourlykWh := make(map[string]float64) // Track hourly | ||
highestHour := make(map[string]float64) // Track highest seen per hour | ||
lowestHour := make(map[string]float64) // Track lowest seen per hour | ||
|
||
// Loop for all the lines in data | ||
for _, dayData := range data { | ||
// Loop for each hour | ||
for _, hourData := range dayData { | ||
// Check to see if the map for that lowestHour exists, if not create it with the default value of 100 | ||
if _, ok := lowestHour[hourData.date.Format("03:04:05 PM")]; !ok { | ||
lowestHour[hourData.date.Format("03:04:05 PM")] = 100 | ||
} | ||
|
||
// Add the kWh usage to the totals | ||
totalkWh += hourData.kWh | ||
dailykWh[hourData.date.Format("01/02/2006")] += hourData.kWh | ||
|
||
// Track the totals by hour as well | ||
hourlykWh[hourData.date.Format("03:04:05 PM")] += hourData.kWh | ||
if hourData.kWh > highestHour[hourData.date.Format("03:04:05 PM")] { | ||
highestHour[hourData.date.Format("03:04:05 PM")] = hourData.kWh | ||
} | ||
|
||
if hourData.kWh < lowestHour[hourData.date.Format("03:04:05 PM")] && hourData.kWh > 0 { | ||
lowestHour[hourData.date.Format("03:04:05 PM")] = hourData.kWh | ||
} | ||
|
||
// Increase the data point total | ||
totalDataPoints += 1 | ||
} | ||
} | ||
|
||
// Loop for each day | ||
for day, dailyusage := range dailykWh { | ||
if dailyusage > highestDaykWh { | ||
highestDaykWh = dailyusage | ||
highestDay = day | ||
} | ||
|
||
if dailyusage < lowestDaykWh && dailyusage > 0 { | ||
lowestDaykWh = dailyusage | ||
lowestDay = day | ||
} | ||
|
||
// Add to the total daily usage | ||
totalDailykWh += dailyusage | ||
|
||
// Increase the number of days | ||
totalDays += 1 | ||
} | ||
|
||
wg.Done() // Tell the wg we're done. | ||
|
||
// Return the struct | ||
return PowerDataReturn{ | ||
filePath: path, | ||
lowestDay: lowestDay, | ||
highestDay: highestDay, | ||
totalkWh: totalkWh, | ||
averageDailykWh: totalDailykWh / totalDays, | ||
averageHourlykWh: totalkWh / totalDataPoints, | ||
lowestDailykWh: lowestDaykWh, | ||
highestDailykWh: highestDaykWh, | ||
totalDays: totalDays, | ||
totalDataPoints: totalDataPoints, | ||
hourlykWh: hourlykWh, | ||
highestHour: highestHour, | ||
lowestHour: lowestHour, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" // Used for printing error messages | ||
"os" // Needed for os.FileInfo | ||
"path/filepath" // Needed for filepath.Walk | ||
"strings" // Needed for strings.Contains | ||
) | ||
|
||
// Get a list of the files we want to take input from | ||
func returnInputFiles(inputFolder string) []string { | ||
var inputFiles []string | ||
err := filepath.Walk(inputFolder, func(path string, fileInfo os.FileInfo, err error) error { | ||
|
||
// Check for any errors | ||
if err != nil { | ||
fmt.Println("Error accessing:", err) | ||
return err | ||
} | ||
|
||
// Ignore any folder not our input folder - we're not doing recursive searching | ||
if fileInfo.IsDir() && fileInfo.Name() != "Input" { | ||
return filepath.SkipDir | ||
} | ||
|
||
// Make sure the file is a .csv | ||
if strings.Contains(path, ".csv") { | ||
inputFiles = append(inputFiles, path) | ||
} | ||
|
||
// Return no error if we made it this far | ||
return nil | ||
}) | ||
|
||
// Error handling for the above function | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
} | ||
|
||
return inputFiles | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters