Skip to content

Commit

Permalink
perf: optimize sitestats for all stage times and sanity
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvISsReimu committed Mar 16, 2023
1 parent 85de1d0 commit 99536bc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
59 changes: 59 additions & 0 deletions internal/repo/drop_matrix_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,62 @@ func (s *DropMatrixElement) CalcTotalItemQuantityForShimSiteStats(ctx context.Co
}
return results, nil
}

func (s *DropMatrixElement) CalcTotalStageQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalStageTime, error) {
results := make([]*modelv2.TotalStageTime, 0)
err := s.getStageTimesQuery(server, false).Scan(ctx, &results)
if err != nil {
return nil, err
}
return results, nil
}

func (s *DropMatrixElement) CalcTotalSanityCostForShimSiteStats(ctx context.Context, server string) (sanity int, err error) {
mainq := s.db.NewSelect().
TableExpr("(?) AS subq4", s.getStageTimesQuery(server, true)).
ColumnExpr("SUM(sanity * total_times) AS sanity")
mainq.Scan(ctx, &sanity)
return sanity, err
}

func (s *DropMatrixElement) getStageTimesQuery(server string, containsSanity bool) *bun.SelectQuery {
subq3 := s.db.NewSelect().
TableExpr("drop_matrix_elements").
Column("stage_id", "item_id", "times", "day_num").
Where("server = ?", server).
Where("source_category = ?", constant.SourceCategoryAll).
Where("times > 0")

subq2 := s.db.NewSelect().
TableExpr("(?) AS subq3", subq3).
Column("stage_id", "item_id", "times").
Group("stage_id", "item_id", "times", "day_num")

subq1 := s.db.NewSelect().
TableExpr("(?) AS subq2", subq2).
Column("stage_id").
ColumnExpr("sum(times) AS total_times").
Group("stage_id", "item_id")

mainq := s.db.NewSelect().
Table("stages").
TableExpr("(?) AS subq1", subq1)

if containsSanity {
mainq = mainq.Column("ark_stage_id", "sanity")
} else {
mainq = mainq.Column("ark_stage_id")
}

mainq.ColumnExpr("max(total_times) AS total_times").
Where("subq1.stage_id = stages.stage_id").
Where("ark_stage_id != ?", "recruit").
Where("extra_process_type IS NULL")

if containsSanity {
mainq = mainq.Group("ark_stage_id", "sanity")
} else {
mainq = mainq.Group("ark_stage_id")
}
return mainq
}
12 changes: 0 additions & 12 deletions internal/repo/drop_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,6 @@ func (s *DropReport) CalcTotalTimesForTrend(
return results, nil
}

func (s *DropReport) CalcTotalSanityCostForShimSiteStats(ctx context.Context, server string) (sanity int, err error) {
err = pgqry.New(
s.DB.NewSelect().
TableExpr("drop_reports AS dr").
ColumnExpr("SUM(st.sanity * dr.times)").
Where("dr.reliability = 0 AND dr.server = ?", server),
).
UseStageById("dr.stage_id").
Q.Scan(ctx, &sanity)
return sanity, err
}

func (s *DropReport) CalcTotalStageQuantityForShimSiteStats(ctx context.Context, server string, isRecent24h bool) ([]*modelv2.TotalStageTime, error) {
results := make([]*modelv2.TotalStageTime, 0)

Expand Down
8 changes: 8 additions & 0 deletions internal/service/drop_matrix_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ func (s *DropMatrixElement) GetAllQuantityBucketsForGlobalDropMatrixMapByStageId
func (s *DropMatrixElement) CalcTotalItemQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalItemQuantity, error) {
return s.DropMatrixElementRepo.CalcTotalItemQuantityForShimSiteStats(ctx, server)
}

func (s *DropMatrixElement) CalcTotalStageQuantityForShimSiteStats(ctx context.Context, server string) ([]*modelv2.TotalStageTime, error) {
return s.DropMatrixElementRepo.CalcTotalStageQuantityForShimSiteStats(ctx, server)
}

func (s *DropMatrixElement) CalcTotalSanityCostForShimSiteStats(ctx context.Context, server string) (sanity int, err error) {
return s.DropMatrixElementRepo.CalcTotalSanityCostForShimSiteStats(ctx, server)
}
4 changes: 2 additions & 2 deletions internal/service/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *SiteStats) GetShimSiteStats(ctx context.Context, server string) (*model

func (s *SiteStats) RefreshShimSiteStats(ctx context.Context, server string) (*modelv2.SiteStats, error) {
valueFunc := func() (*modelv2.SiteStats, error) {
stageTimes, err := s.DropReportRepo.CalcTotalStageQuantityForShimSiteStats(ctx, server, false)
stageTimes, err := s.DropMatrixElementService.CalcTotalStageQuantityForShimSiteStats(ctx, server)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +52,7 @@ func (s *SiteStats) RefreshShimSiteStats(ctx context.Context, server string) (*m
return nil, err
}

sanity, err := s.DropReportRepo.CalcTotalSanityCostForShimSiteStats(ctx, server)
sanity, err := s.DropMatrixElementService.CalcTotalSanityCostForShimSiteStats(ctx, server)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 99536bc

Please sign in to comment.