Skip to content

Commit

Permalink
feat: add timeslices
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Mar 30, 2024
1 parent 31572eb commit 28956eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 25 additions & 7 deletions backend/pkd/gasstation/gsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,17 @@ func createCodeTimeSliceBuckets(postCodePostCodeLocationMap map[int]pcmodel.Post
return postCodeTimeSliceBuckets
}

func createTimeSliceBuckets(myCountyData pcmodel.CountyData, postCodeTimeSliceBuckets map[string]map[time.Time][]gsmodel.GasPrice) map[time.Time][]gsmodel.GasPrice {
func createTimeSliceBuckets(myCountyData pcmodel.CountyData, postCodePostCodeLocationMap map[int]pcmodel.PostCodeLocation, postCodeTimeSliceBuckets map[string]map[time.Time][]gsmodel.GasPrice) map[time.Time][]gsmodel.GasPrice {
timeSliceBuckets := make(map[time.Time][]gsmodel.GasPrice)
for _, myPcLocation := range myCountyData.PostCodeLocations {
var myPostCodeLocations []pcmodel.PostCodeLocation
for _, myPostCodeLocation := range postCodePostCodeLocationMap {
if myPostCodeLocation.CountyDataID == myCountyData.ID && myCountyData.County != "Kreisfrei" {
myPostCodeLocations = append(myPostCodeLocations, myPostCodeLocation)
}
}
log.Printf("myPostCodeLocations: %v\n", len(myPostCodeLocations))
for _, myPcLocation := range myPostCodeLocations {
//log.Printf("postCodeLocations: %v\n", len(myPcLocation.CountyData.PostCodeLocations))
myPostCode := postcode.FormatPostCode(myPcLocation.PostCode)
if myBuckets, ok := postCodeTimeSliceBuckets[myPostCode]; ok {
for mySlice, myNewPrices := range myBuckets {
Expand Down Expand Up @@ -313,29 +321,39 @@ func createTimeSlicesPriceSums(myCountyData pcmodel.CountyData, timeSliceBuckets
GsNumDiesel: int(dieselNum),
GsNumE10: int(e10Num),
GsNumE5: int(e5Num),
AvgE5: float64(e5Sum / e5Num),
AvgE10: float64(e10Sum / e10Num),
AvgDiesel: float64(dieselSum / dieselNum),
AvgE5: divZeroCheck(e5Sum, e5Num),
AvgE10: divZeroCheck(e10Sum, e10Num),
AvgDiesel: divZeroCheck(dieselSum, dieselNum),
},
}
timeSlicesPriceSums[mySlice] = myCountyTimeSlot
}
return timeSlicesPriceSums
}

func divZeroCheck(number int64, divisor int64) float64 {
if divisor == 0 {
return 0
}
return float64(number / divisor)
}

func calcCountyTimeSlots() {
log.Printf("calcCountyTimeSlots started.")
myStart := time.Now()
postCodePostCodeLocationMap, _, idCountyDataMap, postCodeGasStationsMap := createPostCodeGasStationMaps()
postCodeTimeSliceBuckets := createCodeTimeSliceBuckets(postCodePostCodeLocationMap, postCodeGasStationsMap)
//log.Printf("postCodeTimeSliceBuckets: %v\n", len(postCodeGasStationsMap))
database.DB.Transaction(func(tx *gorm.DB) error {
tx.Delete(&pcmodel.CountyTimeSlot{})
tx.Where("1=1").Delete(&pcmodel.CountyTimeSlot{})
return nil
})
//log.Printf("idCountyDataMap: %v\n", len(idCountyDataMap))
//calc average changes in 10 min slots
for _, myCountyData := range idCountyDataMap {
//10 min buckets
timeSliceBuckets := createTimeSliceBuckets(myCountyData, postCodeTimeSliceBuckets)
timeSliceBuckets := createTimeSliceBuckets(myCountyData, postCodePostCodeLocationMap, postCodeTimeSliceBuckets)
//log.Printf("createTimeSliceBuckets: %v\n", len(timeSliceBuckets))
timeSlicesPriceSums := createTimeSlicesPriceSums(myCountyData, timeSliceBuckets)
database.DB.Transaction(func(tx *gorm.DB) error {
for _, myCountyTimeSlots := range timeSlicesPriceSums {
Expand Down
2 changes: 1 addition & 1 deletion backend/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
export GOGC=off
export GOMEMLIMIT=256MiB
export GOMEMLIMIT=512MiB
export GODEBUG=gctrace=1
export GOMAXPROCS=12
#to support differen libc versions
Expand Down

0 comments on commit 28956eb

Please sign in to comment.