Skip to content

Commit

Permalink
feat: add timeframe
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Mar 24, 2024
1 parent aa6e0db commit aef3c45
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion backend/pkd/fileimport/postcodeimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func updateCountyStatePrices(plzs []string) {
gasStationStids = append(gasStationStids, myGasStation.ID)
plzGasStation[myGasStation.PostCode] = append(plzGasStation[myGasStation.PostCode], myGasStation)
}
myGasPrices := gasstation.FindPricesByStids(&gasStationStids, 5)
myGasPrices := gasstation.FindPricesByStids(&gasStationStids, 5, gasstation.Month)
gasStationIdGasPrices := make(map[string][]gsmodel.GasPrice)
for _, myGasPrice := range myGasPrices {
gasStationIdGasPrices[myGasPrice.GasStationID] = append(gasStationIdGasPrices[myGasPrice.GasStationID], myGasPrice)
Expand Down
20 changes: 16 additions & 4 deletions backend/pkd/gasstation/gsbaserepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ import (

//const earthRadius = 6371.0

type TimeFrame uint

const (
Day TimeFrame = iota
Month TimeFrame = iota
)

type minMaxSquare struct {
MinLat float64
MinLng float64
Expand Down Expand Up @@ -83,7 +90,7 @@ func createGasStationIdGasPriceMap(postCodeGasStationsMap *map[string][]gsmodel.
}
}
//log.Printf("gasStationIds: %v", len(gasStationIds))
gasPrices := FindPricesByStidsDistinct(&gasStationIds, 0)
gasPrices := FindPricesByStidsDistinct(&gasStationIds, 0, Month)
//log.Printf("gasPrices: %v", len(gasPrices))
gasStationIdGasPriceMap := make(map[string]gsmodel.GasPrice)
for _, myGasPrice := range gasPrices {
Expand All @@ -104,11 +111,16 @@ func createPostCodeGasStationsMap() map[string][]gsmodel.GasStation {
return postCodeGasStationsMap
}

func findPricesByStids(stids *[]string, resultLimit int, distinct bool) []gsmodel.GasPrice {
func findPricesByStids(stids *[]string, resultLimit int, distinct bool, timeframe TimeFrame) []gsmodel.GasPrice {
var myGasPrices []gsmodel.GasPrice
gasStationidGasPriceMap := make(map[string]gsmodel.GasPrice)
oneMonthAgo := time.Now().Add(time.Hour * -720)
dateStr := fmt.Sprintf("%04d-%02d-%02d", oneMonthAgo.Year(), oneMonthAgo.Month(), oneMonthAgo.Day())
var myTimeFrame time.Time
if timeframe == Day {
time.Now().Add(time.Hour * -24)
} else {
time.Now().Add(time.Hour * -720)
}
dateStr := fmt.Sprintf("%04d-%02d-%02d", myTimeFrame.Year(), myTimeFrame.Month(), myTimeFrame.Day())
//log.Printf("Cut off date: %v", dateStr)
chuncks := createInChunks(stids, true)
database.DB.Transaction(func(tx *gorm.DB) error {
Expand Down
17 changes: 12 additions & 5 deletions backend/pkd/gasstation/gsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func UpdatePrice(gasStationPrices *[]GasStationPrices) {
stationPricesKeys = append(stationPricesKeys, value.GasStationID)
}
gasPriceUpdateMap := make(map[string]gsmodel.GasPrice)
stationPricesDb := FindPricesByStids(&stationPricesKeys, 0)
stationPricesDb := FindPricesByStids(&stationPricesKeys, 0, Month)
log.Printf("StationPricesKeys: %v StationPricesDb: %v", len(stationPricesKeys), len(stationPricesDb))
for _, value := range stationPricesDb {
if _, found := gasPriceUpdateMap[value.GasStationID]; !found {
Expand Down Expand Up @@ -228,6 +228,13 @@ func ReCalcCountyStatePrices() {
})
myDuration := time.Now().Sub(myStart)
log.Printf("recalcCountyStatePrices finished for %v states and %v counties in %v.", len(idStateDataMap), len(idCountyDataMap), myDuration)
go CalcCountyTimeSlots()
}

func CalcCountyTimeSlots() {
//TODO read yesterdays gas price changes per county
//TODO calc average changes in 10 min slots
//TODO store changes in countytimeslots
}

func FindById(id string) gsmodel.GasStation {
Expand All @@ -238,13 +245,13 @@ func FindById(id string) gsmodel.GasStation {
return myGasStation
}

func FindPricesByStids(stids *[]string, resultLimit int) []gsmodel.GasPrice {
myGasPrice := findPricesByStids(stids, resultLimit, false)
func FindPricesByStids(stids *[]string, resultLimit int, timeframe TimeFrame) []gsmodel.GasPrice {
myGasPrice := findPricesByStids(stids, resultLimit, false, timeframe)
return myGasPrice
}

func FindPricesByStidsDistinct(stids *[]string, resultLimit int) []gsmodel.GasPrice {
myGasPrice := findPricesByStids(stids, resultLimit, true)
func FindPricesByStidsDistinct(stids *[]string, resultLimit int, timeframe TimeFrame) []gsmodel.GasPrice {
myGasPrice := findPricesByStids(stids, resultLimit, true, timeframe)
return myGasPrice
}

Expand Down

0 comments on commit aef3c45

Please sign in to comment.