Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Back-end Support for LiveHouse #132

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/controller/v3/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Dataset) AggregatedItem(ctx *fiber.Ctx) error {
accountId.Valid = true
}

queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResults(ctx.Context(), server, "", ctx.Params("itemId"), accountId)
queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResultsForV3(ctx.Context(), server, "", ctx.Params("itemId"), accountId)
if err != nil {
return err
}
Expand All @@ -68,7 +68,7 @@ func (c *Dataset) AggregatedStage(ctx *fiber.Ctx) error {
accountId.Valid = true
}

queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResults(ctx.Context(), server, "stageId", "", accountId)
queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResultsForV3(ctx.Context(), server, "stageId", "", accountId)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/model/cache/caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
ItemDropSetByStageIDAndRangeID *cache.Set[[]int]
ItemDropSetByStageIdAndTimeRange *cache.Set[[]int]

MaxAccumulableDropMatrixResults *cache.Set[model.DropMatrixQueryResult]
ShimMaxAccumulableDropMatrixResults *cache.Set[modelv2.DropMatrixQueryResult]

Formula *cache.Singular[json.RawMessage]
Expand Down Expand Up @@ -121,8 +122,10 @@ func initializeCaches() {
SetMap["itemDropSet#server|stageId|startTime|endTime"] = ItemDropSetByStageIdAndTimeRange.Flush

// drop_matrix
MaxAccumulableDropMatrixResults = cache.NewSet[model.DropMatrixQueryResult]("maxAccumulableDropMatrixResults#server|sourceCategory")
ShimMaxAccumulableDropMatrixResults = cache.NewSet[modelv2.DropMatrixQueryResult]("shimMaxAccumulableDropMatrixResults#server|showClosedZoned")

SetMap["maxAccumulableDropMatrixResults#server|sourceCategory"] = MaxAccumulableDropMatrixResults.Flush
SetMap["shimMaxAccumulableDropMatrixResults#server|showClosedZoned"] = ShimMaxAccumulableDropMatrixResults.Flush

// formula
Expand Down
40 changes: 32 additions & 8 deletions internal/service/drop_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ func NewDropMatrix(
}
}

// FIXME: this will be used for v3 api
// Cache: maxAccumulableDropMatrixResults#server:{server}, 24 hrs, records last modified time
func (s *DropMatrix) GetMaxAccumulableDropMatrixResults(
func (s *DropMatrix) GetMaxAccumulableDropMatrixResultsForV3(
ctx context.Context, server string, stageFilterStr string, itemFilterStr string, accountId null.Int,
) (*modelv2.DropMatrixQueryResult, error) {
valueFunc := func() (*modelv2.DropMatrixQueryResult, error) {
savedDropMatrixResults, err := s.getMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll)
savedDropMatrixResults, err := s.GetMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -105,7 +106,7 @@ func (s *DropMatrix) GetShimMaxAccumulableDropMatrixResults(
ctx context.Context, server string, showClosedZones bool, stageFilterStr string, itemFilterStr string, accountId null.Int,
) (*modelv2.DropMatrixQueryResult, error) {
valueFunc := func() (*modelv2.DropMatrixQueryResult, error) {
savedDropMatrixResults, err := s.getMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll)
savedDropMatrixResults, err := s.GetMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,6 +173,14 @@ func (s *DropMatrix) RefreshAllDropMatrixElements(ctx context.Context, server st
if err := cache.ShimMaxAccumulableDropMatrixResults.Delete(server + constant.CacheSep + "false"); err != nil {
return err
}
for _, sourceCategory := range sourceCategories {
if err := cache.MaxAccumulableDropMatrixResults.Delete(server + constant.CacheSep + sourceCategory); err != nil {
return err
}
}

//TODO: call GetMaxAccumulableDropMatrixResults() here to send results and generation num to LiveHouse

return nil
}

Expand All @@ -188,12 +197,27 @@ func (s *DropMatrix) QueryDropMatrix(
}

// calc DropMatrixQueryResult for max accumulable timeranges
func (s *DropMatrix) getMaxAccumulableDropMatrixResults(ctx context.Context, server string, accountId null.Int, sourceCategory string) (*model.DropMatrixQueryResult, error) {
dropMatrixElements, err := s.getDropMatrixElements(ctx, server, accountId, sourceCategory)
if err != nil {
return nil, err
// Cache: maxAccumulableDropMatrixResults#server|sourceCategory:{server}|{sourceCategory}, 24 hrs
func (s *DropMatrix) GetMaxAccumulableDropMatrixResults(ctx context.Context, server string, accountId null.Int, sourceCategory string) (*model.DropMatrixQueryResult, error) {
valueFunc := func() (*model.DropMatrixQueryResult, error) {
dropMatrixElements, err := s.getDropMatrixElements(ctx, server, accountId, sourceCategory)
if err != nil {
return nil, err
}
return s.convertDropMatrixElementsToMaxAccumulableDropMatrixQueryResult(ctx, server, dropMatrixElements)
}

var results model.DropMatrixQueryResult
if !accountId.Valid {
key := server + constant.CacheSep + sourceCategory
_, err := cache.MaxAccumulableDropMatrixResults.MutexGetSet(key, &results, valueFunc, 24*time.Hour)
if err != nil {
return nil, err
}
return &results, nil
} else {
return valueFunc()
}
return s.convertDropMatrixElementsToMaxAccumulableDropMatrixQueryResult(ctx, server, dropMatrixElements)
}

// For global, get elements from DB; For personal, calc elements
Expand Down