fix market fetching

This commit is contained in:
Samuel Tariku 2025-04-14 19:48:22 +03:00
parent 35b846bcc8
commit d5be4803ae
12 changed files with 587 additions and 577 deletions

View File

@ -15,12 +15,26 @@ INSERT INTO odds (
is_active,
source,
fetched_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12, $13, $14, $15
)
ON CONFLICT (event_id, market_id) DO UPDATE SET
odds_value = EXCLUDED.odds_value,
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13,
$14,
$15
) ON CONFLICT (event_id, market_id) DO
UPDATE
SET odds_value = EXCLUDED.odds_value,
raw_odds = EXCLUDED.raw_odds,
market_type = EXCLUDED.market_type,
market_name = EXCLUDED.market_name,
@ -31,13 +45,8 @@ ON CONFLICT (event_id, market_id) DO UPDATE SET
is_active = EXCLUDED.is_active,
source = EXCLUDED.source,
fi = EXCLUDED.fi;
-- name: GetPrematchOdds :many
SELECT
event_id,
SELECT event_id,
fi,
market_type,
market_name,
@ -53,11 +62,10 @@ SELECT
source,
is_active
FROM odds
WHERE is_active = true AND source = 'b365api';
WHERE is_active = true
AND source = 'b365api';
-- name: GetALLPrematchOdds :many
SELECT
event_id,
SELECT event_id,
fi,
market_type,
market_name,
@ -73,23 +81,20 @@ SELECT
source,
is_active
FROM odds
WHERE is_active = true AND source = 'b365api';
WHERE is_active = true
AND source = 'b365api';
-- name: GetRawOddsByMarketID :many
SELECT
id,
SELECT id,
raw_odds,
fetched_at
FROM odds
WHERE
market_id = $1 AND
is_active = true AND
source = 'b365api'
LIMIT $2 OFFSET $3;
WHERE market_id = $1
AND fi = $2
AND is_active = true
AND source = 'b365api'
LIMIT $3 OFFSET $4;
-- name: GetPrematchOddsByUpcomingID :many
SELECT
o.event_id,
SELECT o.event_id,
o.fi,
o.market_type,
o.market_name,

View File

@ -1354,53 +1354,6 @@ const docTemplate = `{
}
}
},
"/prematch/odds/raw/{market_id}": {
"get": {
"description": "Retrieve raw odds records using a Market ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"prematch"
],
"summary": "Retrieve raw odds by Market ID",
"parameters": [
{
"type": "string",
"description": "Market ID",
"name": "market_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.RawOddsByMarketID"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
}
}
}
},
"/prematch/odds/upcoming/{upcoming_id}": {
"get": {
"description": "Retrieve prematch odds by upcoming event ID (FI from Bet365) with optional pagination",
@ -1460,6 +1413,60 @@ const docTemplate = `{
}
}
},
"/prematch/odds/upcoming/{upcoming_id}/market/{market_id}": {
"get": {
"description": "Retrieve raw odds records using a Market ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"prematch"
],
"summary": "Retrieve raw odds by Market ID",
"parameters": [
{
"type": "string",
"description": "Upcoming ID",
"name": "upcoming_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Market ID",
"name": "market_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.RawOddsByMarketID"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
}
}
}
},
"/prematch/odds/{event_id}": {
"get": {
"description": "Retrieve prematch odds for a specific event by event ID",

View File

@ -1346,53 +1346,6 @@
}
}
},
"/prematch/odds/raw/{market_id}": {
"get": {
"description": "Retrieve raw odds records using a Market ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"prematch"
],
"summary": "Retrieve raw odds by Market ID",
"parameters": [
{
"type": "string",
"description": "Market ID",
"name": "market_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.RawOddsByMarketID"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
}
}
}
},
"/prematch/odds/upcoming/{upcoming_id}": {
"get": {
"description": "Retrieve prematch odds by upcoming event ID (FI from Bet365) with optional pagination",
@ -1452,6 +1405,60 @@
}
}
},
"/prematch/odds/upcoming/{upcoming_id}/market/{market_id}": {
"get": {
"description": "Retrieve raw odds records using a Market ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"prematch"
],
"summary": "Retrieve raw odds by Market ID",
"parameters": [
{
"type": "string",
"description": "Upcoming ID",
"name": "upcoming_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Market ID",
"name": "market_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.RawOddsByMarketID"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
}
}
}
},
"/prematch/odds/{event_id}": {
"get": {
"description": "Retrieve prematch odds for a specific event by event ID",

View File

@ -1704,37 +1704,6 @@ paths:
summary: Retrieve prematch odds for an event
tags:
- prematch
/prematch/odds/raw/{market_id}:
get:
consumes:
- application/json
description: Retrieve raw odds records using a Market ID
parameters:
- description: Market ID
in: path
name: market_id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/domain.RawOddsByMarketID'
type: array
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Retrieve raw odds by Market ID
tags:
- prematch
/prematch/odds/upcoming/{upcoming_id}:
get:
consumes:
@ -1775,6 +1744,42 @@ paths:
summary: Retrieve prematch odds by upcoming ID (FI)
tags:
- prematch
/prematch/odds/upcoming/{upcoming_id}/market/{market_id}:
get:
consumes:
- application/json
description: Retrieve raw odds records using a Market ID
parameters:
- description: Upcoming ID
in: path
name: upcoming_id
required: true
type: string
- description: Market ID
in: path
name: market_id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/domain.RawOddsByMarketID'
type: array
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Retrieve raw odds by Market ID
tags:
- prematch
/search/branch:
get:
consumes:

View File

@ -148,7 +148,6 @@ type Odd struct {
MarketCategory pgtype.Text `json:"market_category"`
MarketID pgtype.Text `json:"market_id"`
Name pgtype.Text `json:"name"`
Header pgtype.Text `json:"header"`
Handicap pgtype.Text `json:"handicap"`
OddsValue pgtype.Float8 `json:"odds_value"`
Section string `json:"section"`

View File

@ -253,15 +253,17 @@ SELECT id,
fetched_at
FROM odds
WHERE market_id = $1
AND fi = $2
AND is_active = true
AND source = 'b365api'
LIMIT $2 OFFSET $3
LIMIT $3 OFFSET $4
`
type GetRawOddsByMarketIDParams struct {
MarketID pgtype.Text
Limit int32
Offset int32
MarketID pgtype.Text `json:"market_id"`
Fi pgtype.Text `json:"fi"`
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
type GetRawOddsByMarketIDRow struct {
@ -271,7 +273,12 @@ type GetRawOddsByMarketIDRow struct {
}
func (q *Queries) GetRawOddsByMarketID(ctx context.Context, arg GetRawOddsByMarketIDParams) ([]GetRawOddsByMarketIDRow, error) {
rows, err := q.db.Query(ctx, GetRawOddsByMarketID, arg.MarketID, arg.Limit, arg.Offset)
rows, err := q.db.Query(ctx, GetRawOddsByMarketID,
arg.MarketID,
arg.Fi,
arg.Limit,
arg.Offset,
)
if err != nil {
return nil, err
}
@ -324,37 +331,15 @@ VALUES (
$13,
$14,
$15
) ON CONFLICT (market_id, name, handicap) DO
) ON CONFLICT (event_id, market_id) DO
UPDATE
SET odds_value = EXCLUDED.odds_value,
raw_odds = EXCLUDED.raw_odds,
market_type = EXCLUDED.market_type,
market_name = EXCLUDED.market_name,
event_id,
fi,
market_type,
market_name,
market_category,
market_id,
name,
handicap,
odds_value,
section,
category,
raw_odds,
is_active,
source,
fetched_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12, $13, $14, $15
)
ON CONFLICT (event_id, market_id) DO UPDATE SET
odds_value = EXCLUDED.odds_value,
raw_odds = EXCLUDED.raw_odds,
market_type = EXCLUDED.market_type,
market_name = EXCLUDED.market_name,
market_category = EXCLUDED.market_category,
name = EXCLUDED.name,
handicap = EXCLUDED.handicap,
fetched_at = EXCLUDED.fetched_at,
is_active = EXCLUDED.is_active,
source = EXCLUDED.source,

View File

@ -176,9 +176,10 @@ func (s *Store) GetALLPrematchOdds(ctx context.Context) ([]domain.Odd, error) {
return domainOdds, nil
}
func (s *Store) GetRawOddsByMarketID(ctx context.Context, rawOddsID string) (domain.RawOddsByMarketID, error) {
func (s *Store) GetRawOddsByMarketID(ctx context.Context, rawOddsID string, upcomingID string) (domain.RawOddsByMarketID, error) {
params := dbgen.GetRawOddsByMarketIDParams{
MarketID: pgtype.Text{String: rawOddsID, Valid: true},
Fi: pgtype.Text{String: upcomingID, Valid: true},
Limit: 1,
Offset: 0,
}

View File

@ -10,7 +10,5 @@ type Service interface {
FetchNonLiveOdds(ctx context.Context) error
GetPrematchOdds(ctx context.Context, eventID string) ([]domain.Odd, error)
GetALLPrematchOdds(ctx context.Context) ([]domain.Odd, error)
GetRawOddsByMarketID(ctx context.Context, marketID string) ([]domain.RawOddsByMarketID, error)
GetRawOddsByMarketID(ctx context.Context, marketID string, upcomingID string) ([]domain.RawOddsByMarketID, error)
}

View File

@ -22,7 +22,6 @@ func New(token string, store *repository.Store) *ServiceImpl {
return &ServiceImpl{token: token, store: store}
}
func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
eventIDs, err := s.store.GetAllUpcomingEvents(ctx)
if err != nil {
@ -112,8 +111,6 @@ type OddsSection struct {
Sp map[string]OddsMarket `json:"sp"`
}
func (s *ServiceImpl) GetPrematchOdds(ctx context.Context, eventID string) ([]domain.Odd, error) {
return s.store.GetPrematchOdds(ctx, eventID)
}
@ -122,8 +119,8 @@ func (s *ServiceImpl) GetALLPrematchOdds(ctx context.Context) ([]domain.Odd, err
return s.store.GetALLPrematchOdds(ctx)
}
func (s *ServiceImpl) GetRawOddsByMarketID(ctx context.Context, marketID string) ([]domain.RawOddsByMarketID, error) {
rows, err := s.store.GetRawOddsByMarketID(ctx, marketID)
func (s *ServiceImpl) GetRawOddsByMarketID(ctx context.Context, marketID string, upcomingID string) ([]domain.RawOddsByMarketID, error) {
rows, err := s.store.GetRawOddsByMarketID(ctx, marketID, upcomingID)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package httpserver
import (
// "context"
"log"
eventsvc "github.com/SamuelTariku/FortuneBet-Backend/internal/services/event"
@ -26,7 +27,6 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
// },
// },
// {
// spec: "*/5 * * * * *", // Every 5 seconds
// task: func() {
@ -36,7 +36,6 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
// },
// },
// {
// spec: "*/5 * * * * *", // Every 5 seconds
// task: func() {
@ -45,12 +44,10 @@ func StartDataFetchingCrons(eventService eventsvc.Service, oddsService oddssvc.S
// }
// },
// },
}
for _, job := range schedule {
job.task()
if _, err := c.AddFunc(job.spec, job.task); err != nil {
log.Fatalf("Failed to schedule cron job: %v", err)
}

View File

@ -36,6 +36,7 @@ func GetPrematchOdds(logger *slog.Logger, prematchSvc *odds.ServiceImpl) fiber.H
return response.WriteJSON(c, fiber.StatusOK, "Prematch odds retrieved successfully", odds, nil)
}
}
// GetALLPrematchOdds
// @Summary Retrieve all prematch odds
// @Description Retrieve all prematch odds from the database
@ -55,28 +56,35 @@ func GetALLPrematchOdds(logger *slog.Logger, prematchSvc *odds.ServiceImpl) fibe
return response.WriteJSON(c, fiber.StatusOK, "All prematch odds retrieved successfully", odds, nil)
}
}
// GetRawOddsByMarketID
// @Summary Retrieve raw odds by Market ID
// @Description Retrieve raw odds records using a Market ID
// @Tags prematch
// @Accept json
// @Produce json
// @Param upcoming_id path string true "Upcoming ID"
// @Param market_id path string true "Market ID"
// @Success 200 {array} domain.RawOddsByMarketID
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /prematch/odds/raw/{market_id} [get]
// @Router /prematch/odds/upcoming/{upcoming_id}/market/{market_id} [get]
func GetRawOddsByMarketID(logger *slog.Logger, prematchSvc *odds.ServiceImpl) fiber.Handler {
return func(c *fiber.Ctx) error {
marketID := c.Params("market_id")
upcomingID := c.Params("upcoming_id")
if marketID == "" {
return response.WriteJSON(c, fiber.StatusBadRequest, "Missing market_id", nil, nil)
}
rawOdds, err := prematchSvc.GetRawOddsByMarketID(c.Context(), marketID)
if upcomingID == "" {
return response.WriteJSON(c, fiber.StatusBadRequest, "Missing upcoming_id", nil, nil)
}
rawOdds, err := prematchSvc.GetRawOddsByMarketID(c.Context(), marketID, upcomingID)
if err != nil {
logger.Error("failed to fetch raw odds", "error", err)
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to retrieve raw odds", nil, nil)
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to retrieve raw odds", err, nil)
}
return response.WriteJSON(c, fiber.StatusOK, "Raw odds retrieved successfully", rawOdds, nil)
@ -101,6 +109,7 @@ func GetAllUpcomingEvents(logger *slog.Logger, eventSvc event.Service) fiber.Han
return response.WriteJSON(c, fiber.StatusOK, "All upcoming events retrieved successfully", events, nil)
}
}
// @Summary Retrieve an upcoming by ID
// @Description Retrieve an upcoming event by ID
// @Tags prematch
@ -126,6 +135,7 @@ func GetUpcomingEventByID(logger *slog.Logger, eventSvc event.Service) fiber.Han
return response.WriteJSON(c, fiber.StatusOK, "Upcoming event retrieved successfully", event, nil)
}
}
// @Summary Retrieve prematch odds by upcoming ID (FI)
// @Description Retrieve prematch odds by upcoming event ID (FI from Bet365) with optional pagination
// @Tags prematch
@ -163,4 +173,3 @@ func GetPrematchOddsByUpcomingID(logger *slog.Logger, prematchSvc *odds.ServiceI
return response.WriteJSON(c, fiber.StatusOK, "Prematch odds retrieved successfully", odds, nil)
}
}

View File

@ -59,7 +59,7 @@ func (a *App) initAppRoutes() {
a.fiber.Get("/prematch/odds/:event_id", handlers.GetPrematchOdds(a.logger, a.prematchSvc))
a.fiber.Get("/prematch/odds", handlers.GetALLPrematchOdds(a.logger, a.prematchSvc))
a.fiber.Get("/prematch/odds/raw/:market_id", handlers.GetRawOddsByMarketID(a.logger, a.prematchSvc))
a.fiber.Get("/prematch/odds/upcoming/:upcoming_id/market/:market_id", handlers.GetRawOddsByMarketID(a.logger, a.prematchSvc))
a.fiber.Get("/prematch/events/:id", handlers.GetUpcomingEventByID(a.logger, a.eventSvc))
a.fiber.Get("/prematch/events", handlers.GetAllUpcomingEvents(a.logger, a.eventSvc))