Skip to content

Commit

Permalink
chore: move ComputeBordeauxAPIResponse to forecast_usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
vareversat committed Oct 30, 2023
1 parent 7c51093 commit 7041678
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 99 deletions.
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ require (

require (
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
Expand All @@ -25,7 +29,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -52,7 +56,7 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
Expand Down
15 changes: 9 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/getsentry/sentry-go v0.25.0 h1:q6Eo+hS+yoJlTO3uu/azhQadsD8V+jQn2D8VvX1eOyI=
github.com/getsentry/sentry-go v0.25.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
Expand Down Expand Up @@ -65,8 +65,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
Expand Down Expand Up @@ -111,7 +111,10 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -161,8 +164,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
Expand Down
2 changes: 2 additions & 0 deletions internal/domains/forecast_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ type ForecastUsecase interface {
totalItemCount *int,
) error
RefreshAll(ctx context.Context) (Refresh, error)
ComputeBordeauxAPIResponse(forecasts *Forecasts, boredeauxAPIResponse BordeauxAPIResponse) error
RefreshIsNeeded(ctx context.Context) bool
}
76 changes: 71 additions & 5 deletions internal/usecases/forecast_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/sirupsen/logrus"
"github.com/vareversat/chabo-api/internal/domains"
"github.com/vareversat/chabo-api/internal/utils"
)
Expand Down Expand Up @@ -141,7 +142,7 @@ func (fU *forecastUsecase) RefreshAll(ctx context.Context) (domains.Refresh, err
ctx, cancel := context.WithTimeout(ctx, fU.contextTimeout)
defer cancel()

if refreshIsNeeded(ctx, fU.refreshRepository) {
if fU.RefreshIsNeeded(ctx) {
var openDataForecasts domains.BordeauxAPIResponse
var forecasts domains.Forecasts

Expand All @@ -153,9 +154,12 @@ func (fU *forecastUsecase) RefreshAll(ctx context.Context) (domains.Refresh, err
return domains.Refresh{}, fmt.Errorf(errGet.Error())
}
// Compute all forecasts
utils.ComputeForecasts(&forecasts, openDataForecasts)
err := fU.ComputeBordeauxAPIResponse(&forecasts, openDataForecasts)
if err != nil {
return domains.Refresh{}, err
}
// Delete all forecasts
_, err := fU.forecastRepository.DeleteAll(ctx)
_, err = fU.forecastRepository.DeleteAll(ctx)
if err != nil {
return domains.Refresh{}, err
}
Expand Down Expand Up @@ -183,13 +187,75 @@ func (fU *forecastUsecase) RefreshAll(ctx context.Context) (domains.Refresh, err
return domains.Refresh{}, fmt.Errorf("data does not need to be refresh (aborting the refresh)")
}

func (fU *forecastUsecase) ComputeBordeauxAPIResponse(
forecasts *domains.Forecasts,
boredeauxAPIResponse domains.BordeauxAPIResponse,
) error {
// alreadySeenBoatNames is used to compute the maneuver of each boats
var alreadySeenBoatNames []string

for _, openAPIForecast := range boredeauxAPIResponse.Records {
_, offset := openAPIForecast.RecordTimestamp.Zone()
closingReason := utils.MapClosingReason(openAPIForecast.Fields.Boat)
circulationClosingDate, errClosingDate := utils.FormatDataTime(
openAPIForecast.Fields.ClosingTime,
openAPIForecast.Fields.ClosingDate,
offset,
*time.UTC,
)
if errClosingDate != nil {
logrus.Fatalf(errClosingDate.Error())
return errClosingDate
}
circulationReopeningDate, errReopeningDate := utils.FormatDataTime(
openAPIForecast.Fields.OpeningTime,
openAPIForecast.Fields.ClosingDate,
offset,
*time.UTC,
)
if errReopeningDate != nil {
logrus.Fatalf(errReopeningDate.Error())
return errReopeningDate
}

// Check if the forecast is during 2 days
if circulationReopeningDate.Compare(circulationClosingDate) == -1 {
// On day is added because the closing date is after the reopening date
circulationReopeningDate = circulationReopeningDate.AddDate(0, 0, 1)
}
closingDuration := circulationReopeningDate.Sub(circulationClosingDate)
*forecasts = append(*forecasts, domains.Forecast{
ID: openAPIForecast.RecordID,
ClosingDuration: closingDuration,
CirculationClosingDate: circulationClosingDate,
CirculationReopeningDate: circulationReopeningDate,
ClosingType: utils.MapClosingType(openAPIForecast.Fields.TotalClosing),
ClosingReason: closingReason,
Boats: utils.MapBoats(
closingReason,
openAPIForecast.Fields.Boat,
closingDuration,
circulationClosingDate,
&alreadySeenBoatNames,
openAPIForecast.RecordID,
),
Link: domains.APIResponseSelfLink{
Self: domains.APIResponseLink{Link: "/v1/forecasts/" + openAPIForecast.RecordID},
},
})
}
logrus.Infof("all %d forecasts computed with success", len(*forecasts))
return nil

}

// Check if it's possible to perform a data refresh
func refreshIsNeeded(ctx context.Context, refreshRepository domains.RefreshRepository) bool {
func (fU *forecastUsecase) RefreshIsNeeded(ctx context.Context) bool {

var lastRefresh domains.Refresh

// Get the last refresh to be sure this is not too early
err := refreshRepository.GetLast(ctx, &lastRefresh)
err := fU.refreshRepository.GetLast(ctx, &lastRefresh)

if err != nil {
// An error here means that the collection is empty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package utils
package usecases

import (
"testing"
"time"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vareversat/chabo-api/internal/domains"
)

func TestComputeForecasts(t *testing.T) {
forecastRepository := new(domains.ForecastRepository)
refreshRepository := new(domains.RefreshRepository)

var forecasts domains.Forecasts
recordTimestamp, _ := time.Parse(time.RFC3339, "2023-02-26T21:00:00Z")
openDataForecasts := domains.BordeauxAPIResponse{
bordeauxAPIForecasts := domains.BordeauxAPIResponse{
Hits: 1,
Parameters: domains.BordeauxAPIResponseParameters{
Dataset: "dataset",
Expand Down Expand Up @@ -39,7 +42,7 @@ func TestComputeForecasts(t *testing.T) {
circulationClosingDate, _ := time.Parse(time.RFC3339, "2023-02-26T21:00:00Z")
circulationReopeningDate, _ := time.Parse(time.RFC3339, "2023-02-26T23:00:00Z")
approximativeCrossingDate, _ := time.Parse(time.RFC3339, "2023-02-26T22:00:00Z")
want := domains.Forecasts{
expectedForecasts := domains.Forecasts{
{
ID: "recordid",
ClosingType: domains.TwoWay,
Expand All @@ -60,12 +63,8 @@ func TestComputeForecasts(t *testing.T) {
},
}

// Prevent NPE
InitForecast(log.WithFields(log.Fields{
"channel": "test",
}))
ComputeForecasts(&forecasts, openDataForecasts)
if !(want.AreEqual(forecasts)) {
t.Fatalf(`ComputeForecasts("...") = %q, want match for %#q`, forecasts, want)
}
u := NewForecastUsecase(*forecastRepository, *refreshRepository, time.Second*2)
u.ComputeBordeauxAPIResponse(&forecasts, bordeauxAPIForecasts)

assert.True(t, true, expectedForecasts.AreEqual(forecasts))
}
69 changes: 0 additions & 69 deletions internal/utils/forecast.go

This file was deleted.

4 changes: 0 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ func init() {
openApiLogger := log.WithFields(log.Fields{
"channel": "open_api",
})
forecastLogger := log.WithFields(log.Fields{
"channel": "forecast",
})
utils.InitOpenApi(openApiLogger)
utils.InitForecast(forecastLogger)
// Init Mongo
mongoDatabase = *repositories.NewMongoClient().Database(mongoDatabaseName)
}
Expand Down

0 comments on commit 7041678

Please sign in to comment.