Skip to content

Commit

Permalink
⚡️ Implemented Resume criteria service
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Sep 27, 2024
1 parent 361e8a8 commit 047439c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 56 deletions.
7 changes: 4 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ func main() {

selectCriteriaByID := criteria.MakeSelectByID(db)
collectExecutionDAORows := database.MakeCollectRows[criteria.ExecutionDAO]()
selectLastDayExecutedByCriteriaID := criteria.MakeSelectLastDayExecutedByCriteriaID(db)
selectExecutionsByStatuses := criteria.MakeSelectExecutionsByStatuses(db, collectExecutionDAORows)
scrapperEnqueueCriteria := scrapper.MakeEnqueueCriteria(httpClient, os.Getenv("ENQUEUE_CRITERIA_API_URL"))
enqueueCriteria := criteria.MakeEnqueue(selectCriteriaByID, selectLastDayExecutedByCriteriaID, selectExecutionsByStatuses, scrapperEnqueueCriteria)
enqueueCriteria := criteria.MakeEnqueue(selectCriteriaByID, selectExecutionsByStatuses, scrapperEnqueueCriteria)

initCriteria := criteria.MakeInit(selectExecutionsByStatuses, enqueueCriteria)
selectLastDayExecutedByCriteriaID := criteria.MakeSelectLastDayExecutedByCriteriaID(db)
resumeCriteria := criteria.MakeResume(selectCriteriaByID, selectLastDayExecutedByCriteriaID, scrapperEnqueueCriteria)
initCriteria := criteria.MakeInit(selectExecutionsByStatuses, resumeCriteria)

insertCriteriaExecution := criteria.MakeInsertExecution(db)

Expand Down
54 changes: 40 additions & 14 deletions cmd/api/search/criteria/enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import (
"ahbcc/internal/scrapper"
)

// Enqueue retrieves the criteria by ID from the database and enqueues its information
type Enqueue func(ctx context.Context, criteriaID int, forced bool) error
type (
// Enqueue retrieves the criteria by ID from the database and enqueues its information
Enqueue func(ctx context.Context, criteriaID int, forced bool) error

// Resume retrieves the criteria by ID from the database, searches its last execution day and
// enqueues data starting from that day
Resume func(ctx context.Context, criteriaID int) error
)

// MakeEnqueue creates a new Enqueue
func MakeEnqueue(selectCriteriaByID SelectByID, selectLastDayExecutedByCriteria SelectLastDayExecutedByCriteriaID, selectExecutionsByStatuses SelectExecutionsByStatuses, enqueueCriteria scrapper.EnqueueCriteria) Enqueue {
func MakeEnqueue(selectCriteriaByID SelectByID, selectExecutionsByStatuses SelectExecutionsByStatuses, enqueueCriteria scrapper.EnqueueCriteria) Enqueue {
return func(ctx context.Context, criteriaID int, forced bool) error {
criteriaDAO, err := selectCriteriaByID(ctx, criteriaID)
if err != nil {
log.Error(ctx, err.Error())
return FailedToExecuteSelectCriteriaByID
}

if forced {
lastDayExecutedDate, err := selectLastDayExecutedByCriteria(ctx, criteriaID)
if err != nil {
if !errors.Is(err, NoExecutionDaysFoundForTheGivenCriteriaID) {
log.Error(ctx, err.Error())
return FailedToExecuteSelectLastDayExecutedByCriteriaID
}
} else {
criteriaDAO.Since = lastDayExecutedDate
}
} else {
if !forced {
executionsDAO, err := selectExecutionsByStatuses(ctx, []string{PendingStatus, InProgressStatus})
if err != nil {
log.Error(ctx, err.Error())
Expand All @@ -53,3 +49,33 @@ func MakeEnqueue(selectCriteriaByID SelectByID, selectLastDayExecutedByCriteria
return nil
}
}

// MakeResume creates a new Resume
func MakeResume(selectCriteriaByID SelectByID, selectLastDayExecutedByCriteria SelectLastDayExecutedByCriteriaID, enqueueCriteria scrapper.EnqueueCriteria) Resume {
return func(ctx context.Context, criteriaID int) error {
criteriaDAO, err := selectCriteriaByID(ctx, criteriaID)
if err != nil {
log.Error(ctx, err.Error())
return FailedToExecuteSelectCriteriaByID
}

lastDayExecutedDate, err := selectLastDayExecutedByCriteria(ctx, criteriaID)
if err != nil {
// if err == NoExecutionDaysFoundForTheGivenCriteriaID the criteria hasn’t started yet, but it was enqueued once before
if !errors.Is(err, NoExecutionDaysFoundForTheGivenCriteriaID) {
log.Error(ctx, err.Error())
return FailedToExecuteSelectLastDayExecutedByCriteriaID
}
} else { // The criteria has been executed once before and is needed to start from the last day it was executed
criteriaDAO.Since = lastDayExecutedDate
}

err = enqueueCriteria(ctx, criteriaDAO.toCriteriaDTO())
if err != nil {
log.Error(ctx, err.Error())
return FailedToExecuteEnqueueCriteria
}

return nil
}
}
98 changes: 67 additions & 31 deletions cmd/api/search/criteria/enqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,22 @@ func TestEnqueue_success(t *testing.T) {
for _, tt := range tests {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

got := enqueueCriteria(context.Background(), 1, tt.forced)

assert.Nil(t, got)
}
}

func TestEnqueue_failsWhenSelectLastDayExecutedByCriteriaThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(time.Time{}, errors.New("failed to execute select last day executed by criteria id"))
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

want := criteria.FailedToExecuteSelectLastDayExecutedByCriteriaID
got := enqueueCriteria(context.Background(), 1, true)

assert.Equal(t, want, got)
}

func TestEnqueue_failsWhenSelectCriteriaByIDThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), errors.New("failed to execute select criteria by id"))
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

want := criteria.FailedToExecuteSelectCriteriaByID
got := enqueueCriteria(context.Background(), 1, false)
Expand All @@ -63,11 +48,10 @@ func TestEnqueue_failsWhenSelectCriteriaByIDThrowsError(t *testing.T) {

func TestEnqueue_failsWhenSelectExecutionsByStatusesThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), errors.New("failed to execute select executions by statuses"))
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

want := criteria.FailedToExecuteSelectExecutionsByStatuses
got := enqueueCriteria(context.Background(), 1, false)
Expand All @@ -77,11 +61,10 @@ func TestEnqueue_failsWhenSelectExecutionsByStatusesThrowsError(t *testing.T) {

func TestEnqueue_failsWhenThereIsAlreadyAnExecutionWithTheSameCriteriaIDEnqueued(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

want := criteria.AnExecutionOfThisCriteriaIDIsAlreadyEnqueued
got := enqueueCriteria(context.Background(), 4, false)
Expand All @@ -91,14 +74,67 @@ func TestEnqueue_failsWhenThereIsAlreadyAnExecutionWithTheSameCriteriaIDEnqueued

func TestEnqueue_failsWhenEnqueueCriteriaThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(errors.New("failed to execute enqueue criteria"))

enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)

want := criteria.FailedToExecuteEnqueueCriteria
got := enqueueCriteria(context.Background(), 1, false)

assert.Equal(t, want, got)
}

func TestResume_success(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(criteria.MockExecutionsDAO(), nil)
enqueueCriteria := criteria.MakeEnqueue(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockSelectExecutionsByStatuses, mockEnqueueCriteria)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

resumeCriteria := criteria.MakeResume(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockEnqueueCriteria)

got := resumeCriteria(context.Background(), 1)

assert.Nil(t, got)
}

func TestResume_failsWhenSelectCriteriaByIDThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), errors.New("failed to execute select criteria by id"))
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

resumeCriteria := criteria.MakeResume(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockEnqueueCriteria)

want := criteria.FailedToExecuteSelectCriteriaByID
got := resumeCriteria(context.Background(), 1)

assert.Equal(t, want, got)
}

func TestResume_failsWhenSelectLastDayExecutedByCriteriaThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(time.Time{}, errors.New("failed to execute select last day executed by criteria id"))
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(nil)

resumeCriteria := criteria.MakeResume(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockEnqueueCriteria)

want := criteria.FailedToExecuteSelectLastDayExecutedByCriteriaID
got := resumeCriteria(context.Background(), 1)

assert.Equal(t, want, got)
}

func TestResume_failsWhenEnqueueCriteriaThrowsError(t *testing.T) {
mockSelectCriteriaByID := criteria.MockSelectByID(criteria.MockCriteriaDAO(), nil)
mockDate := time.Date(2024, time.September, 19, 0, 0, 0, 0, time.Local)
mockSelectLastDayExecutedByCriteriaID := criteria.MockSelectLastDayExecutedByCriteriaID(mockDate, nil)
mockEnqueueCriteria := scrapper.MockEnqueueCriteria(errors.New("failed to execute enqueue criteria"))

resumeCriteria := criteria.MakeResume(mockSelectCriteriaByID, mockSelectLastDayExecutedByCriteriaID, mockEnqueueCriteria)

want := criteria.FailedToExecuteEnqueueCriteria
got := enqueueCriteria(context.Background(), 1, false)
got := resumeCriteria(context.Background(), 1)

assert.Equal(t, want, got)
}
4 changes: 2 additions & 2 deletions cmd/api/search/criteria/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Init func(ctx context.Context) error

// MakeInit creates a new Init
func MakeInit(selectExecutionsByStatuses SelectExecutionsByStatuses, enqueue Enqueue) Init {
func MakeInit(selectExecutionsByStatuses SelectExecutionsByStatuses, resume Resume) Init {
return func(ctx context.Context) error {
executionsDAO, err := selectExecutionsByStatuses(ctx, []string{PendingStatus, InProgressStatus})
if err != nil {
Expand All @@ -19,7 +19,7 @@ func MakeInit(selectExecutionsByStatuses SelectExecutionsByStatuses, enqueue Enq
}

for _, execution := range executionsDAO {
err = enqueue(ctx, execution.SearchCriteriaID, true)
err = resume(ctx, execution.SearchCriteriaID)
if err != nil {
log.Error(ctx, err.Error())
return FailedToExecuteEnqueueCriteria
Expand Down
12 changes: 6 additions & 6 deletions cmd/api/search/criteria/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
func TestInit_success(t *testing.T) {
mockExecutionsDAO := criteria.MockExecutionsDAO()
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(mockExecutionsDAO, nil)
mockEnqueue := criteria.MockEnqueue(nil)
mockResume := criteria.MockResume(nil)

init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockEnqueue)
init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockResume)

got := init(context.Background())

Expand All @@ -24,9 +24,9 @@ func TestInit_success(t *testing.T) {

func TestInit_failsWhenSelectExecutionsByStatusesThrowsError(t *testing.T) {
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(nil, errors.New("failed while executing select executions by statuses"))
mockEnqueue := criteria.MockEnqueue(nil)
mockResume := criteria.MockResume(nil)

init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockEnqueue)
init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockResume)

want := criteria.FailedToExecuteSelectExecutionsByStatuses
got := init(context.Background())
Expand All @@ -37,9 +37,9 @@ func TestInit_failsWhenSelectExecutionsByStatusesThrowsError(t *testing.T) {
func TestInit_failsWhenEnqueueThrowsError(t *testing.T) {
mockExecutionsDAO := criteria.MockExecutionsDAO()
mockSelectExecutionsByStatuses := criteria.MockSelectExecutionsByStatuses(mockExecutionsDAO, nil)
mockEnqueue := criteria.MockEnqueue(errors.New("failed while executing enqueue"))
mockResume := criteria.MockResume(errors.New("failed while executing resume"))

init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockEnqueue)
init := criteria.MakeInit(mockSelectExecutionsByStatuses, mockResume)

want := criteria.FailedToExecuteEnqueueCriteria
got := init(context.Background())
Expand Down
7 changes: 7 additions & 0 deletions cmd/api/search/criteria/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func MockEnqueue(err error) Enqueue {
}
}

// MockResume mocks Resume function
func MockResume(err error) Resume {
return func(ctx context.Context, criteriaID int) error {
return err
}
}

// MockInit mocks Init function
func MockInit(err error) Init {
return func(ctx context.Context) error {
Expand Down

0 comments on commit 047439c

Please sign in to comment.