Skip to content

Commit

Permalink
perf: optimize sitestats for all item quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvISsReimu committed Mar 16, 2023
1 parent 5c06454 commit 85de1d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
28 changes: 28 additions & 0 deletions internal/repo/drop_matrix_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
21 changes: 0 additions & 21 deletions internal/repo/drop_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
5 changes: 5 additions & 0 deletions internal/service/drop_matrix_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
13 changes: 9 additions & 4 deletions internal/service/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down

0 comments on commit 85de1d0

Please sign in to comment.