From 85de1d05d8e32a6d8fd6900ee75784e923ac8743 Mon Sep 17 00:00:00 2001 From: AlvISsReimu Date: Thu, 16 Mar 2023 02:54:22 -0700 Subject: [PATCH] perf: optimize sitestats for all item quantities --- internal/repo/drop_matrix_element.go | 28 +++++++++++++++++++++++++ internal/repo/drop_report.go | 21 ------------------- internal/service/drop_matrix_element.go | 5 +++++ internal/service/site_stats.go | 13 ++++++++---- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/internal/repo/drop_matrix_element.go b/internal/repo/drop_matrix_element.go index 26817d0e..2dcc5355 100644 --- a/internal/repo/drop_matrix_element.go +++ b/internal/repo/drop_matrix_element.go @@ -4,10 +4,12 @@ import ( "context" "database/sql" + "exusiai.dev/gommon/constant" "github.com/pkg/errors" "github.com/uptrace/bun" "exusiai.dev/backend-next/internal/model" + modelv2 "exusiai.dev/backend-next/internal/model/v2" ) type DropMatrixElement struct { @@ -138,3 +140,29 @@ func (s *DropMatrixElement) GetAllQuantityBucketsForGlobalDropMatrix(ctx context } return results, nil } + +func (s *DropMatrixElement) CalcTotalItemQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalItemQuantity, error) { + types := []string{constant.ItemTypeMaterial, constant.ItemTypeFurniture, constant.ItemTypeChip} + + subq := s.db.NewSelect(). + TableExpr("drop_matrix_elements"). + Column("item_id"). + ColumnExpr("SUM(quantity) AS total_quantity"). + Where("server = ?", server). + Where("source_category = ?", constant.SourceCategoryAll). + Group("item_id") + + mainq := s.db.NewSelect(). + Table("items"). + TableExpr("(?) AS subq", subq). + Column("ark_item_id", "total_quantity"). + Where("subq.item_id = items.item_id"). + Where("items.type IN (?)", bun.In(types)) + + results := make([]*modelv2.TotalItemQuantity, 0) + err := mainq.Scan(ctx, &results) + if err != nil { + return nil, err + } + return results, nil +} diff --git a/internal/repo/drop_report.go b/internal/repo/drop_report.go index aeb9def7..469f9128 100644 --- a/internal/repo/drop_report.go +++ b/internal/repo/drop_report.go @@ -289,27 +289,6 @@ func (s *DropReport) CalcTotalStageQuantityForShimSiteStats(ctx context.Context, return results, nil } -func (s *DropReport) CalcTotalItemQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalItemQuantity, error) { - results := make([]*modelv2.TotalItemQuantity, 0) - - types := []string{constant.ItemTypeMaterial, constant.ItemTypeFurniture, constant.ItemTypeChip} - err := pgqry.New( - s.DB.NewSelect(). - TableExpr("drop_reports AS dr"). - Column("it.ark_item_id"). - ColumnExpr("SUM(dpe.quantity) AS total_quantity"). - Join("JOIN drop_pattern_elements AS dpe ON dpe.drop_pattern_id = dr.pattern_id"). - Where("dr.reliability = 0 AND dr.server = ? AND it.type IN (?)", server, bun.In(types)). - Group("it.ark_item_id"), - ). - UseItemById("dpe.item_id"). - Q.Scan(ctx, &results) - if err != nil { - return nil, err - } - return results, nil -} - func (s *DropReport) CalcRecentUniqueUserCountBySource(ctx context.Context, duration time.Duration) ([]*modelv2.UniqueUserCountBySource, error) { results := make([]*modelv2.UniqueUserCountBySource, 0) subq := s.DB.NewSelect(). diff --git a/internal/service/drop_matrix_element.go b/internal/service/drop_matrix_element.go index ed1204aa..ba005ba3 100644 --- a/internal/service/drop_matrix_element.go +++ b/internal/service/drop_matrix_element.go @@ -4,6 +4,7 @@ import ( "context" "exusiai.dev/backend-next/internal/model" + modelv2 "exusiai.dev/backend-next/internal/model/v2" "exusiai.dev/backend-next/internal/repo" ) @@ -79,3 +80,7 @@ func (s *DropMatrixElement) GetAllQuantityBucketsForGlobalDropMatrixMapByStageId } return result, nil } + +func (s *DropMatrixElement) CalcTotalItemQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalItemQuantity, error) { + return s.DropMatrixElementRepo.CalcTotalItemQuantityForShimSiteStats(ctx, server) +} diff --git a/internal/service/site_stats.go b/internal/service/site_stats.go index 081c4978..3acf8c50 100644 --- a/internal/service/site_stats.go +++ b/internal/service/site_stats.go @@ -10,12 +10,17 @@ import ( ) type SiteStats struct { - DropReportRepo *repo.DropReport + DropReportRepo *repo.DropReport + DropMatrixElementService *DropMatrixElement } -func NewSiteStats(dropReportRepo *repo.DropReport) *SiteStats { +func NewSiteStats( + dropReportRepo *repo.DropReport, + dropMatrixElementService *DropMatrixElement, +) *SiteStats { return &SiteStats{ - DropReportRepo: dropReportRepo, + DropReportRepo: dropReportRepo, + DropMatrixElementService: dropMatrixElementService, } } @@ -42,7 +47,7 @@ func (s *SiteStats) RefreshShimSiteStats(ctx context.Context, server string) (*m return nil, err } - itemQuantity, err := s.DropReportRepo.CalcTotalItemQuantityForShimSiteStats(ctx, server) + itemQuantity, err := s.DropMatrixElementService.CalcTotalItemQuantityForShimSiteStats(ctx, server) if err != nil { return nil, err }