Enhance tender recommendation service with improved caching and pagination
continuous-integration/drone/push Build is passing

- Introduced a context with a timeout for Redis cache retrieval in the `getCachedAIRecommendations` method, ensuring more robust handling of potential delays.
- Added a new file `recommendation_page.go` to encapsulate the logic for building recommendation pages, improving code organization and readability.
- Implemented a `buildRecommendationPage` method to handle pagination and batch processing of recommendations, enhancing performance and user experience.
- Created a new test file `recommendation_page_test.go` to validate the functionality of the recommendation page building process, ensuring correct pagination and batch resolution.

This update significantly improves the efficiency and maintainability of the tender recommendation service by optimizing caching and implementing structured pagination handling.
This commit is contained in:
Mazyar
2026-07-08 01:32:27 +03:30
parent 2980a3beb4
commit f108733c2a
5 changed files with 271 additions and 96 deletions
+17 -65
View File
@@ -947,13 +947,8 @@ func (s *tenderService) Search(ctx context.Context, form *SearchForm, pagination
nil
}
const recommendRequestTimeout = 15 * time.Second
// Recommend retrieves AI-ranked tenders for the authenticated customer's company.
func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*SearchResponse, error) {
ctx, cancel := context.WithTimeout(ctx, recommendRequestTimeout)
defer cancel()
companyIDs := normalizeRecommendationCompanyScope(form.CompanyIDs)
companyID := ""
if form.CompanyID != nil {
@@ -988,23 +983,8 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
return s.emptySearchResponse(pagination), nil
}
var (
tenderByRef map[string]Tender
excluded map[string]struct{}
excludedByCompany map[string]map[string]struct{}
)
loadGroup, loadCtx := errgroup.WithContext(ctx)
loadGroup.Go(func() error {
var resolveErr error
tenderByRef, resolveErr = s.resolveRecommendedTenders(loadCtx, recommendations)
return resolveErr
})
loadGroup.Go(func() error {
var exclusionErr error
excluded, excludedByCompany, exclusionErr = s.loadRecommendationExclusions(loadCtx, form, companyID, companyIDs)
return exclusionErr
})
if err := loadGroup.Wait(); err != nil {
excluded, excludedByCompany, err := s.loadRecommendationExclusions(ctx, form, companyID, companyIDs)
if err != nil {
if form.ExcludeRejectedTenders {
s.logger.Error("Failed to build recommendation exclusions", map[string]interface{}{
"company_id": companyID,
@@ -1017,54 +997,26 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
lang := s.pickResponseLanguage(form.Language)
now := time.Now().Unix()
ordered := make([]rankedRecommendedTender, 0, len(recommendations))
seenTenderIDs := make(map[string]struct{}, len(recommendations))
for _, rec := range recommendations {
tenderRef := strings.TrimSpace(rec.TenderID)
tender, ok := tenderByRef[tenderRef]
if !ok {
continue
}
if len(companyIDs) > 0 {
if isRecommendedTenderExcludedForAllCompanies(tender, tenderRef, excludedByCompany, companyIDs) {
continue
}
} else if isRecommendedTenderExcluded(tender, tenderRef, excluded) {
continue
}
if form.OnlyActiveDeadlines && !tender.IsRecommendable(now) {
continue
}
if len(form.Status) > 0 && !statusAllowed(tender.Status, form.Status) {
continue
}
if markResolvedRecommendedTenderSeen(seenTenderIDs, tender) {
continue
}
ordered = append(ordered, rankedRecommendedTender{
tender: tender,
rank: rec.Rank,
analysis: rec.Analysis,
tenderRef: tenderRef,
})
pageResult, err := s.buildRecommendationPage(
ctx,
recommendations,
form,
companyIDs,
excluded,
excludedByCompany,
now,
pagination.Offset,
pagination.Limit,
)
if err != nil {
return nil, err
}
total := int64(len(ordered))
start := pagination.Offset
if start > len(ordered) {
start = len(ordered)
}
end := start + pagination.Limit
if end > len(ordered) {
end = len(ordered)
}
paged := s.buildRecommendedTenderListResponses(ordered[start:end], lang)
paged := s.buildRecommendedTenderListResponses(pageResult.items, lang)
return &SearchResponse{
Tenders: paged,
Metadata: pagination.ListMeta(total, "", end < len(ordered), start),
Metadata: pagination.ListMeta(pageResult.total, "", int(pagination.Offset)+len(paged) < int(pageResult.total), pagination.Offset),
}, nil
}