Skip to content

Commit

Permalink
Find the nearest rent company
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Apr 30, 2024
1 parent 79f56bd commit 523c46c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
7 changes: 4 additions & 3 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func InitDb(dsn string) (*gorm.DB, error) {

if err == nil {
db.AutoMigrate(
&models.User{},
&models.Interest{},
&models.AvailableFlight{},
&models.Offer{},
&models.Interest{},
&models.Journey{},
&models.Offer{},
&models.Rent{},
&models.User{},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TMComputeDistanceUserAirport(client worker.JobClient, job entities.Job) {
return
}
variables["distance"] = distance.GetDistance() / 1000
log.Info("[%s] [%d] Found a distance of: %d km", job.Type, jobKey, variables["distance"])
log.Infof("[%s] [%d] Found a distance of: %d km", job.Type, jobKey, variables["distance"])

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ package handlers

import (
"context"
"time"

"github.com/charmbracelet/log"

pb "github.com/acme-sky/geodistance-api/pkg/distance/proto"
"github.com/acme-sky/workers/internal/config"
"github.com/acme-sky/workers/internal/db"
acmejob "github.com/acme-sky/workers/internal/job"
"github.com/acme-sky/workers/internal/models"
"github.com/camunda/zeebe/clients/go/v8/pkg/entities"
"github.com/camunda/zeebe/clients/go/v8/pkg/worker"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

// Task used to find the nearest rent company to the user' address
func TMFindNearestAvailableRentCompany(client worker.JobClient, job entities.Job) {
jobKey := job.GetKey()

Expand All @@ -18,15 +27,88 @@ func TMFindNearestAvailableRentCompany(client worker.JobClient, job entities.Job
return
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
db, _ := db.GetDb()
var offer models.Offer

if err := db.Where("id = ?", int(variables["offer_id"].(float64))).Preload("User").Preload("Journey").Preload("Journey.Flight1").First(&offer).Error; err != nil {
log.Errorf("[%s] [%d] Error on getting offer %s", job.Type, jobKey, err.Error())
}

if offer.User.Address == nil {
log.Warnf("[%s] [%d] User does not have an address", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}

conf, _ := config.GetConfig()

conn, err := grpc.Dial(conf.String("geodistance.api"), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Errorf("[%s] [%d] Can't connect to Geodistance url: %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}

log.Debug("Processing data:", variables)
defer conn.Close()
c := pb.NewDistanceClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

userGeometry, err := c.FindGeometry(ctx, &pb.AddressRequest{
Address: *offer.User.Address,
})
if err != nil {
log.Errorf("[%s] [%d] Can't find geometry for user: %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}

var rents []models.Rent
if err := db.Find(&rents).Error; err != nil {
log.Errorf("[%s] [%d] Rents not found %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}
var distances []int

for _, rent := range rents {
distance, err := c.FindDistance(ctx, &pb.DistanceRequest{
Origin: &pb.MapPosition{Latitude: userGeometry.Latitude, Longitude: userGeometry.Longitude},
Destination: &pb.MapPosition{Latitude: rent.Latitude, Longitude: rent.Longitude},
})
if err != nil {
log.Warn("[%s] [%d] Can't find distance for %s: %s", job.Type, jobKey, rent.Name, err.Error())
distances = append(distances, 9999999)
continue
}
distances = append(distances, int(distance.GetDistance()))
}

if len(distances) == 0 {
log.Errorf("[%s] [%d] There is no available rent company: %s", job.Type, jobKey, err.Error())
acmejob.FailJob(client, job)
return
}

var selectRentIndex = 0
var minRentDistance = distances[0]
for i := 1; i < len(distances); {
if distances[i] < minRentDistance {
minRentDistance = distances[i]
selectRentIndex = i
}
}

variables["rent_company"] = rents[selectRentIndex]

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
return
}

ctx := context.Background()
ctx = context.Background()
_, err = request.Send(ctx)
if err != nil {
acmejob.FailJob(client, job)
Expand Down
15 changes: 15 additions & 0 deletions internal/models/rent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

import (
"time"
)

// Rent model
type Rent struct {
Id uint `gorm:"column:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
Name string `gorm:"name" json:"name"`
Latitude float32 `gorm:"latitude" json:"latitude"`
Longitude float32 `gorm:"longitude" json:"longitude"`
Endpoint string `gorm:"endpoint" json:"endpoint"`
}

0 comments on commit 523c46c

Please sign in to comment.