Skip to content

Commit

Permalink
fix: calc
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Apr 1, 2024
1 parent c83833a commit e691db2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
1 change: 1 addition & 0 deletions backend/pkd/controller/basecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Start(embeddedFiles fs.FS) {
router.POST("/gasstation/search/location", token.CheckToken, searchGasStationLocation)
router.GET("/usernotification/new/:useruuid", token.CheckToken, getNewUserNotifications)
router.GET("/usernotification/current/:useruuid", token.CheckToken, getCurrentUserNotifications)
router.GET("/postcode/countydata/:id", getCountyDataByIdWithTimeSlots)

myPort := strings.TrimSpace(os.Getenv("PORT"))
portNum, err := strconv.ParseInt(myPort, 10, 0)
Expand Down
26 changes: 26 additions & 0 deletions backend/pkd/controller/pccontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
- Copyright 2022 Sven Loesekann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller

import (
"net/http"
postcode "react-and-go/pkd/postcode"

"github.com/gin-gonic/gin"
)

func getCountyDataByIdWithTimeSlots(c *gin.Context) {
countyDataId := c.Param("id")
myCountyData := postcode.FindCountyDataById(countyDataId)
c.JSON(http.StatusOK, myCountyData)
}
14 changes: 9 additions & 5 deletions backend/pkd/gasstation/gsbaserepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ func createPostCodeMaps() (map[int]pcmodel.PostCodeLocation, map[int]pcmodel.Sta
return postCodePostCodeLocationMap, idStateDataMap, idCountyDataMap
}

func createGasStationIdGasPriceMap(postCodeGasStationsMap *map[string][]gsmodel.GasStation, timeframe TimeFrame) map[string]gsmodel.GasPrice {
func createGasStationIdGasPriceMap(postCodeGasStationsMap *map[string][]gsmodel.GasStation, timeframe TimeFrame) map[string][]gsmodel.GasPrice {
gasPrices := findGasPricesByTimeframe(postCodeGasStationsMap, timeframe)
//log.Printf("gasPrices: %v", len(gasPrices))
gasStationIdGasPriceMap := make(map[string]gsmodel.GasPrice)
gasStationIdGasPriceMap := make(map[string][]gsmodel.GasPrice)
for _, myGasPrice := range gasPrices {
if _, ok := gasStationIdGasPriceMap[myGasPrice.GasStationID]; !ok {
gasStationIdGasPriceMap[myGasPrice.GasStationID] = myGasPrice
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local).Round(time.Hour)
if _, ok := gasStationIdGasPriceMap[myGasPrice.GasStationID]; !ok && myGasPrice.Date.Before(today) {
var myGasPrices []gsmodel.GasPrice
gasStationIdGasPriceMap[myGasPrice.GasStationID] = append(myGasPrices, myGasPrice)
} else if myGasPrice.Date.Before(today) {
gasStationIdGasPriceMap[myGasPrice.GasStationID] = append(gasStationIdGasPriceMap[myGasPrice.GasStationID], myGasPrice)
}
}
return gasStationIdGasPriceMap
Expand Down Expand Up @@ -136,7 +140,7 @@ func findPricesByStids(stids *[]string, resultLimit int, distinct bool, timefram
gasStationidGasPriceMap := make(map[string]gsmodel.GasPrice)
var myTimeFrame time.Time
if timeframe == Day {
myTimeFrame = time.Now().Add(time.Hour * -24)
myTimeFrame = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -1).Round(time.Hour)
} else {
myTimeFrame = time.Now().Add(time.Hour * -720)
}
Expand Down
69 changes: 35 additions & 34 deletions backend/pkd/gasstation/gsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,41 +164,42 @@ func ReCalcCountyStatePrices() {
for _, myPostCodeLocation := range postCodePostCodeLocationMap {
myPostCode := postcode.FormatPostCode(myPostCodeLocation.PostCode)
for _, myGasStation := range postCodeGasStationsMap[myPostCode] {
myGasPrice := gasStationIdGasPriceMap[myGasStation.ID]
if myGasPrice.E5 < 10 && myGasPrice.E10 < 10 && myGasPrice.Diesel < 10 {
continue
}
//log.Printf("%v", myGasPrice)
myStateData := idStateDataMap[int(myPostCodeLocation.StateData.ID)]
myCountyData := idCountyDataMap[int(myPostCodeLocation.CountyData.ID)]
myStateData.GasStationNum += 1
myCountyData.GasStationNum += 1
if myGasPrice.E5 > 10 {
myCountyData.GsNumE5 += 1
myStateData.GsNumE5 += 1
myStateData.AvgE5 += float64(myGasPrice.E5)
myCountyData.AvgE5 += float64(myGasPrice.E5)
/*
if myCountyData.ID == 51 {
gasStations := postCodeGasStationsMap[myPostCode]
log.Printf("GsNumE5: %v, AvgE5: %v, E5: %v, Id: %v, GasStations: %v", myCountyData.GsNumE5, myCountyData.AvgE5, myGasPrice.E5, myCountyData.ID, len(gasStations))
}
*/
}
if myGasPrice.E10 > 10 {
myCountyData.GsNumE10 += 1
myStateData.GsNumE10 += 1
myStateData.AvgE10 += float64(myGasPrice.E10)
myCountyData.AvgE10 += float64(myGasPrice.E10)
}
if myGasPrice.Diesel > 10 {
myCountyData.GsNumDiesel += 1
myStateData.GsNumDiesel += 1
myStateData.AvgDiesel += float64(myGasPrice.Diesel)
myCountyData.AvgDiesel += float64(myGasPrice.Diesel)
for _, myGasPrice := range gasStationIdGasPriceMap[myGasStation.ID] {
if myGasPrice.E5 < 10 && myGasPrice.E10 < 10 && myGasPrice.Diesel < 10 {
continue
}
//log.Printf("%v", myGasPrice)
myStateData := idStateDataMap[int(myPostCodeLocation.StateData.ID)]
myCountyData := idCountyDataMap[int(myPostCodeLocation.CountyData.ID)]
myStateData.GasStationNum += 1
myCountyData.GasStationNum += 1
if myGasPrice.E5 > 10 {
myCountyData.GsNumE5 += 1
myStateData.GsNumE5 += 1
myStateData.AvgE5 += float64(myGasPrice.E5)
myCountyData.AvgE5 += float64(myGasPrice.E5)
/*
if myCountyData.ID == 51 {
gasStations := postCodeGasStationsMap[myPostCode]
log.Printf("GsNumE5: %v, AvgE5: %v, E5: %v, Id: %v, GasStations: %v", myCountyData.GsNumE5, myCountyData.AvgE5, myGasPrice.E5, myCountyData.ID, len(gasStations))
}
*/
}
if myGasPrice.E10 > 10 {
myCountyData.GsNumE10 += 1
myStateData.GsNumE10 += 1
myStateData.AvgE10 += float64(myGasPrice.E10)
myCountyData.AvgE10 += float64(myGasPrice.E10)
}
if myGasPrice.Diesel > 10 {
myCountyData.GsNumDiesel += 1
myStateData.GsNumDiesel += 1
myStateData.AvgDiesel += float64(myGasPrice.Diesel)
myCountyData.AvgDiesel += float64(myGasPrice.Diesel)
}
idStateDataMap[int(myPostCodeLocation.StateData.ID)] = myStateData
idCountyDataMap[int(myPostCodeLocation.CountyData.ID)] = myCountyData
}
idStateDataMap[int(myPostCodeLocation.StateData.ID)] = myStateData
idCountyDataMap[int(myPostCodeLocation.CountyData.ID)] = myCountyData
}
}
//log.Printf("e5Count: %v, e10Count: %v, dieselCount: %v", e5Count, e10Count, dieselCount)
Expand Down
12 changes: 12 additions & 0 deletions backend/pkd/postcode/pcrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ func plzsToPlzInts(plzs []string) []int {
return plzInts
}

func FindCountyDataById(countyDataIdStr string) []pcmodel.CountyTimeSlot {
var myCountyTimeSlots []pcmodel.CountyTimeSlot
countyDataId, err := strconv.Atoi(countyDataIdStr)
if err == nil {
database.DB.Transaction(func(tx *gorm.DB) error {
tx.Where("county_data_id = ?", countyDataId).Order("start_date").Preload("CountyData").Find(&myCountyTimeSlots)
return nil
})
}
return myCountyTimeSlots
}

func FormatPostCode(postCode int32) string {
pcStr := strconv.Itoa(int(postCode))
for len(pcStr) < 5 {
Expand Down

0 comments on commit e691db2

Please sign in to comment.