Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event driven refactor #80

Merged
merged 22 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removed two_gis
all 2gis functionality has been removed
AlexDyakonov authored Dec 20, 2024
commit 2a7c965a024dc880c383f0bb2a3ce86f28f39ca0
4 changes: 0 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -12,10 +12,6 @@ POSTGRES_PORT=5432
POSTGRES_DB=root
AUTO_MIGRATE=True

# TwoGis
TWOGIS_API_KEY=your_key
TWOGIS_API_URL=https://catalog.api.2gis.com/3.0/items

# Config
DEFAULT_PRICE_AVG=500
DEFAULT_UPPER_DELTA_AVG=300
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ Prod: https://dishdash.ru/api/v1
```
cp .env.example .env
```
В частности в .env нужен `TWOGIS_API_KEY`

Есть два варианта запуска:

11 changes: 0 additions & 11 deletions cmd/server/config/config.go
Original file line number Diff line number Diff line change
@@ -25,10 +25,6 @@ type Config struct {
Database string `envconfig:"POSTGRES_DB"`
AutoMigrate bool `envconfig:"POSTGRES_AUTOMIGRATE"`
}
TwoGisApi struct {
Key string `envconfig:"TWOGIS_API_KEY"`
Url string `envconfig:"TWOGIS_API_URL"`
}
Defaults struct {
PriceAvg int `default:"500" envconfig:"DEFAULT_PRICE_AVG"`
PriceAvgUpperDelta int `default:"300" envconfig:"DEFAULT_UPPER_DELTA_AVG"`
@@ -56,13 +52,6 @@ func Load(envFile string) {
if err != nil {
log.Fatalf("can't parse config: %s", err)
}
if C.TwoGisApi.Url == "" {
C.TwoGisApi.Url = "https://catalog.api.2gis.com/3.0/items"
log.Warn("No two gis api url, using default one: " + C.TwoGisApi.Url)
}
if C.TwoGisApi.Key == "" {
log.Error("TwoGisApi.ApiKey is null or not set")
}
}

func Print() {
187 changes: 0 additions & 187 deletions external/twogis/api_client.go

This file was deleted.

17 changes: 0 additions & 17 deletions external/twogis/place_recommender.go

This file was deleted.

117 changes: 0 additions & 117 deletions external/twogis/response_struct.go

This file was deleted.

66 changes: 0 additions & 66 deletions internal/domain/twogis_place.go

This file was deleted.

31 changes: 0 additions & 31 deletions internal/repo/pg/place.go
Original file line number Diff line number Diff line change
@@ -406,34 +406,3 @@ func (pr *PlaceRepo) GetPlacesForLobby(ctx context.Context, lobby *domain.Lobby)
log.Debugf("Total places from database: %d", len(places))
return places, nil
}

func (pr *PlaceRepo) SaveTwoGisPlace(ctx context.Context, twogisPlace *domain.TwoGisPlace) (int64, error) {
var existingID int64

log.Debugf("Checking if place with title '%s' and address '%s' exists.", twogisPlace.Name, twogisPlace.Address)

err := pr.db.QueryRow(ctx, `
SELECT id FROM "place"
WHERE "title" = $1 AND "address" = $2;`, twogisPlace.Name, twogisPlace.Address).Scan(&existingID)
if err != nil {
log.Debugf("Error after executing query: %v", err)

if strings.Contains(err.Error(), "no rows in result set") {
log.Debug("No rows found, adding new place.")
place := twogisPlace.ToPlace()
id, err := pr.SavePlace(ctx, place)
if err != nil {
log.WithError(err).Error("Failed to save new place")
return 0, fmt.Errorf("failed to save place: %w", err)
}
log.Debugf("New place saved successfully. ID: %d", id)
return id, nil
}

log.WithError(err).Error("Unexpected error")
return 0, err
}

log.Debugf("Place already exists. ID: %d", existingID)
return existingID, nil
}
36 changes: 0 additions & 36 deletions internal/repo/pg/tag.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@ import (
"context"
"fmt"

log "github.com/sirupsen/logrus"

"github.com/jackc/pgx/v5"

"dishdash.ru/internal/domain"
@@ -201,37 +199,3 @@ func (tr *TagRepo) GetAllTags(ctx context.Context) ([]*domain.Tag, error) {

return tags, nil
}

func (tr *TagRepo) SaveApiTag(ctx context.Context, place *domain.TwoGisPlace) ([]int64, error) {
var placeTags []int64
log.Debugf("Starting to process tags for place Name: %v", place.Name)

for _, rubric := range place.Rubrics {
var id int64
log.Debugf("Processing tag: %s", rubric)
err := tr.db.QueryRow(ctx, `
WITH s AS (
SELECT id
FROM tag
WHERE name = $1
), i AS (
INSERT INTO tag (name, icon)
SELECT $1, ''
WHERE NOT EXISTS (SELECT 1 FROM s)
RETURNING id
)
SELECT id FROM i
UNION ALL
SELECT id FROM s
`, rubric).Scan(&id)
if err != nil {
log.WithError(err).Errorf("Can't insert or fetch tag '%s'", rubric)
continue
}
log.Debugf("Tag processed successfully: %d", id)
placeTags = append(placeTags, id)
}

log.Debugf("Finished processing tags for place: %v", place.Name)
return placeTags, nil
}
2 changes: 0 additions & 2 deletions internal/repo/repo.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import (
type Tag interface {
SaveTag(ctx context.Context, tag *domain.Tag) (int64, error)
GetAllTags(ctx context.Context) ([]*domain.Tag, error)
SaveApiTag(ctx context.Context, place *domain.TwoGisPlace) ([]int64, error)

DeleteTag(ctx context.Context, tagId int64) error
UpdateTag(ctx context.Context, tag *domain.Tag) (*domain.Tag, error)
@@ -27,7 +26,6 @@ type Place interface {
SavePlace(ctx context.Context, place *domain.Place) (int64, error)
UpdatePlace(ctx context.Context, place *domain.Place) error
DeletePlace(ctx context.Context, id int64) error
SaveTwoGisPlace(ctx context.Context, twogisPlace *domain.TwoGisPlace) (int64, error)
GetPlaceByID(ctx context.Context, id int64) (*domain.Place, error)
GetPlaceByUrl(ctx context.Context, url string) (*domain.Place, error)
GetAllPlaces(ctx context.Context) ([]*domain.Place, error)
8 changes: 0 additions & 8 deletions internal/usecase/place.go
Original file line number Diff line number Diff line change
@@ -92,14 +92,6 @@ func (p PlaceUseCase) DeletePlace(ctx context.Context, id int64) error {
return nil
}

func (p PlaceUseCase) SaveTwoGisPlace(ctx context.Context, twogisPlace *domain.TwoGisPlace) (int64, error) {
placeId, err := p.pRepo.SaveTwoGisPlace(ctx, twogisPlace)
if err != nil {
return 0, err
}
return placeId, nil
}

func (p PlaceUseCase) GetPlaceByID(ctx context.Context, id int64) (*domain.Place, error) {
place, err := p.pRepo.GetPlaceByID(ctx, id)
if err != nil {
85 changes: 6 additions & 79 deletions internal/usecase/place_recommender.go
Original file line number Diff line number Diff line change
@@ -11,22 +11,9 @@ import (
log "github.com/sirupsen/logrus"
)

type ApiPlaceRecommender interface {
RecommendPlaces(
ctx context.Context,
opts domain.RecommendOpts,
data domain.RecommendData,
) ([]*domain.TwoGisPlace, error)
}

type PlaceSaver interface {
SavePlace(ctx context.Context, placeInput SavePlaceInput) (*domain.Place, error)
}

type PlaceRecommender struct {
opts domain.RecommendOpts
dbPRRepo repo.PlaceRecommender
twogisPRRepo ApiPlaceRecommender
opts domain.RecommendOpts
dbPRRepo repo.PlaceRecommender

pRepo repo.Place
tRepo repo.Tag
@@ -35,16 +22,14 @@ type PlaceRecommender struct {
func NewPlaceRecommender(
opts domain.RecommendOpts,
dbPRRepo repo.PlaceRecommender,
twogisPRRepo ApiPlaceRecommender,
pRepo repo.Place,
tRepo repo.Tag,
) *PlaceRecommender {
return &PlaceRecommender{
opts: opts,
dbPRRepo: dbPRRepo,
twogisPRRepo: twogisPRRepo,
pRepo: pRepo,
tRepo: tRepo,
opts: opts,
dbPRRepo: dbPRRepo,
pRepo: pRepo,
tRepo: tRepo,
}
}

@@ -61,37 +46,6 @@ func (pr *PlaceRecommender) RecommendPlaces(ctx context.Context, data domain.Rec
return dbPlaces, nil
}

log.Debug("DB places not good enough, went to api")
allTags, err := pr.tRepo.GetAllTags(ctx)
if err != nil {
return nil, fmt.Errorf("can't get all tags: %w", err)
}

twogisPlaces, err := pr.twogisPRRepo.RecommendPlaces(ctx, pr.opts, data)
if err != nil {
return nil, fmt.Errorf("can't get places from twogis: %w", err)
}
log.Debugf("Got %d places from twogis", len(twogisPlaces))

for _, p := range twogisPlaces {
err = pr.saveTwoGisPlace(ctx, p, allTags)
if err != nil {
return nil, fmt.Errorf("can't save place from twogis: %w", err)
}
log.Debugf("Save %s place from twogis", p.Name)
}

dbPlaces, err = pr.dbPRRepo.RecommendPlaces(ctx, pr.opts, data)
if err != nil {
return nil, fmt.Errorf("can't recommend from db: %w", err)
}
log.Debugf("Got %d places from db", len(dbPlaces))

if pr.goodEnough(dbPlaces, data) {
log.Debug("DB places good enough")
return dbPlaces, nil
}

log.Debug("Can't recommend good enough places")

return nil, errors.New("can't recommend places good enough")
@@ -100,30 +54,3 @@ func (pr *PlaceRecommender) RecommendPlaces(ctx context.Context, data domain.Rec
func (pr *PlaceRecommender) goodEnough(_ []*domain.Place, _ domain.RecommendData) bool {
return true
}

func (pr *PlaceRecommender) saveTwoGisPlace(ctx context.Context, p *domain.TwoGisPlace, allTags []*domain.Tag) error {
id, err := pr.pRepo.SavePlace(ctx, p.ToPlace())
if err != nil {
return err
}

tagIDs := pr.twogisPlaceTagIDs(p, allTags)
return pr.tRepo.AttachTagsToPlace(ctx, tagIDs, id)
}

func (pr *PlaceRecommender) twogisPlaceTagIDs(p *domain.TwoGisPlace, allTags []*domain.Tag) []int64 {
tagIDs := make([]int64, 0)
tagMap := make(map[string]int64, len(allTags))

for _, tag := range allTags {
tagMap[tag.Name] = tag.ID
}
for _, rubric := range p.Rubrics {
tagID, found := tagMap[rubric]
if found {
tagIDs = append(tagIDs, tagID)
}
}

return tagIDs
}
3 changes: 0 additions & 3 deletions internal/usecase/setup.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package usecase

import (
"dishdash.ru/cmd/server/config"
"dishdash.ru/external/twogis"
"dishdash.ru/internal/domain"
"dishdash.ru/internal/repo/pg"
"github.com/jackc/pgx/v5/pgxpool"
@@ -15,7 +14,6 @@ func Setup(pool *pgxpool.Pool) Cases {
ur := pg.NewUserRepo(pool)
sr := pg.NewSwipeRepo(pool)
prr := pg.NewPlaceRecommenderRepo(pool)
twogisRepo := &twogis.PlaceRecommender{}

pu := NewPlaceUseCase(tr, pr)
lu := NewLobbyUseCase(lr, ur, tr, pr, sr)
@@ -28,7 +26,6 @@ func Setup(pool *pgxpool.Pool) Cases {
DistCoeff: float64(config.C.Recommendation.DistCoeff),
},
prr,
twogisRepo,
pr,
tr,
)
4 changes: 0 additions & 4 deletions internal/usecase/tag.go
Original file line number Diff line number Diff line change
@@ -28,10 +28,6 @@ func (t TagUseCase) GetAllTags(ctx context.Context) ([]*domain.Tag, error) {
return t.tRepo.GetAllTags(ctx)
}

func (t TagUseCase) SaveApiTag(ctx context.Context, place *domain.TwoGisPlace) ([]int64, error) {
return t.tRepo.SaveApiTag(ctx, place)
}

func (t TagUseCase) DeleteTag(ctx context.Context, tagId int64) error {
return t.tRepo.DeleteTag(ctx, tagId)
}
2 changes: 0 additions & 2 deletions internal/usecase/usecase.go
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ type Cases struct {
type Tag interface {
SaveTag(ctx context.Context, tag *domain.Tag) (*domain.Tag, error)
GetAllTags(ctx context.Context) ([]*domain.Tag, error)
SaveApiTag(ctx context.Context, place *domain.TwoGisPlace) ([]int64, error)
DeleteTag(ctx context.Context, tagId int64) error
UpdateTag(ctx context.Context, tag *domain.Tag) (*domain.Tag, error)
}
@@ -58,7 +57,6 @@ type Place interface {
SavePlace(ctx context.Context, placeInput SavePlaceInput) (*domain.Place, error)
UpdatePlace(ctx context.Context, place UpdatePlaceInput) (*domain.Place, error)
DeletePlace(ctx context.Context, id int64) error
SaveTwoGisPlace(ctx context.Context, twogisPlace *domain.TwoGisPlace) (int64, error)
GetPlaceByID(ctx context.Context, id int64) (*domain.Place, error)
GetPlaceByUrl(ctx context.Context, url string) (*domain.Place, error)
// GetAllPlaces is very long operation now