Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

chore(frontend): use explicit cache for admin analytics#64077

Merged
stefanhengl merged 6 commits into
mainfrom
sh/adminanalytics/explicit-cache
Jul 26, 2024
Merged

chore(frontend): use explicit cache for admin analytics#64077
stefanhengl merged 6 commits into
mainfrom
sh/adminanalytics/explicit-cache

Conversation

@stefanhengl

@stefanhengl stefanhengl commented Jul 25, 2024

Copy link
Copy Markdown
Member

Relates to #64041

This refactors the adminanalytics package to set the cache explicitly instead of implicitly relying on the global redispool.Store. The global store obfuscated the dependency and also made testing a bit awkward.

Test plan:

  • new unit test
  • I ran a local instance and checked for panics in the logs from the worker job that updates the cache on startup.
  • Checked that the following GQL query returned results
query {
  site {
    analytics {
      search(dateRange: LAST_MONTH, grouping: WEEKLY) {
        searches {
          nodes {
            date
            count
          }
          summary {
            totalCount
            totalUniqueUsers
            totalRegisteredUsers
          }
        }
      }
    }
  }
}
  • I deleted the cache and ran the GQL query again and verified that cache had the following new entries
1) "adminanalytics:Search:Searches:LAST_MONTH:WEEKLY:nodes"
2) "adminanalytics:Search:Searches:LAST_MONTH:WEEKLY:summary"

@cla-bot cla-bot Bot added the cla-signed label Jul 25, 2024
@github-actions github-actions Bot added team/product-platform team/search-platform Issues owned by the search platform team labels Jul 25, 2024
Comment on lines +15 to +16
// Cache is optional. If nil, caching is disabled.
cache redispool.KeyValue

@stefanhengl stefanhengl Jul 25, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the core of the change. We replace cache bool with an explicit cache. Most of the other changes follow from that.

Comment on lines +73 to +77
if cache != nil {
err = setArrayToCache(cache, cacheKey, items)
if err != nil {
return nil, err
}

@stefanhengl stefanhengl Jul 25, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slightly different behavior. Before we always set the cache, even if it was disabled.

I am a bit torn whether we should move the check for a nil cache inside setArrayToCache (and return nil) to make the call sites prettier. Opinions?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a comment above if we even need this, but if we do it might be nice to either make setArrayToCache do the nil check like you said, or more explicitly, to pass a NoopKeyValue into the analytics 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea with NoopKeyValue. It's the least surprising and there is no strange coupling between the feature flag and a nil check somewhere else.

Comment thread internal/adminanalytics/users.go
Comment thread internal/adminanalytics/cache.go
@stefanhengl stefanhengl marked this pull request as ready for review July 25, 2024 11:58
@stefanhengl stefanhengl requested a review from eseliger July 25, 2024 11:58

@eseliger eseliger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing blocking, thanks for reworking this :)

Comment thread internal/adminanalytics/cache.go
Comment on lines +26 to +28
if useCache := !featureflag.FromContext(ctx).GetBoolOr("admin-analytics-cache-disabled", false); useCache {
cache = redispool.Store
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does anyone really ever use this? Or could we simplify a lot here and not have this != nil thing everywhere? What would be a reason to disable caching, which will inherently increase database load by a lot?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be helpful for debugging and testing to disable the cache but I can only guess why this was added.

Comment on lines +73 to +77
if cache != nil {
err = setArrayToCache(cache, cacheKey, items)
if err != nil {
return nil, err
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a comment above if we even need this, but if we do it might be nice to either make setArrayToCache do the nil check like you said, or more explicitly, to pass a NoopKeyValue into the analytics 🤔

@stefanhengl stefanhengl merged commit 590b7e2 into main Jul 26, 2024
@stefanhengl stefanhengl deleted the sh/adminanalytics/explicit-cache branch July 26, 2024 08:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed team/product-platform team/search-platform Issues owned by the search platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants