Skip to content

refactor: RQ hook for ratelimits logs#3147

Merged
chronark merged 5 commits intomainfrom
ENG-1751
Apr 23, 2025
Merged

refactor: RQ hook for ratelimits logs#3147
chronark merged 5 commits intomainfrom
ENG-1751

Conversation

@ogzhanolguncu
Copy link
Contributor

@ogzhanolguncu ogzhanolguncu commented Apr 17, 2025

What does this PR do?

Before: Every page navigation created a new timestamp, which triggered unnecessary refetches
Before: Manual refreshes weren't working because we kept using the same old timestamp

Our solution:

Created a shared timestamp that doesn't change on navigation
Added a way to explicitly update this timestamp when refreshing
Made components react to timestamp changes automatically

Now page navigation doesn't trigger refetches.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Test A
  • Test B

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Introduced a unified query time provider, ensuring all relevant dashboard components use a consistent timestamp for data queries.
    • Added the ability to manually refresh the query time across logs and timeseries views, improving data consistency during refresh actions.
  • Refactor

    • Updated multiple components and hooks to utilize the centralized query time context instead of individual local timestamps.

@linear
Copy link

linear bot commented Apr 17, 2025

@changeset-bot
Copy link

changeset-bot bot commented Apr 17, 2025

⚠️ No Changeset found

Latest commit: f1072b0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Apr 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 22, 2025 2:43pm
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 22, 2025 2:43pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 17, 2025

Warning

Rate limit exceeded

@ogzhanolguncu has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 3 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d089516 and f1072b0.

📒 Files selected for processing (1)
  • apps/dashboard/providers/query-time-provider.tsx (1 hunks)
📝 Walkthrough

Walkthrough

This change introduces a centralized query time management system across several dashboard components by implementing a new QueryTimeProvider and associated useQueryTime hook. Multiple hooks and components that previously relied on local timestamps (Date.now()) now use the shared query time from this provider to calculate time-based query parameters. Refresh controls are updated to trigger a refresh of the global query time before invalidating related queries. The main layout is modified to wrap its children in the new provider, ensuring consistent query time state throughout the workspace.

Changes

File(s) Change Summary
apps/dashboard/providers/query-time-provider.tsx Introduces a new context provider, QueryTimeProvider, and hook, useQueryTime, to manage and distribute a global query timestamp and refresh logic.
apps/dashboard/app/(app)/layout.tsx Wraps children in QueryTimeProvider when the workspace is enabled, enabling global query time access.
apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/controls/components/logs-refresh.tsx
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/controls/components/logs-refresh.tsx
Updates refresh controls to use refreshQueryTime from the new provider before invalidating queries.
apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/table/hooks/use-logs-query.ts
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/hooks/use-logs-query.ts
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/charts/bar-chart/hooks/use-fetch-timeseries.ts
apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/charts/hooks/use-fetch-timeseries.ts
Updates hooks to use the shared query timestamp from useQueryTime instead of local Date.now() for time-based query parameters.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LogsRefresh
    participant QueryTimeProvider
    participant TRPCQuery

    User->>LogsRefresh: Click Refresh
    LogsRefresh->>QueryTimeProvider: refreshQueryTime()
    QueryTimeProvider-->>LogsRefresh: Updated timestamp
    LogsRefresh->>TRPCQuery: Invalidate queries (using new timestamp)
Loading
sequenceDiagram
    participant Component
    participant QueryTimeProvider

    Component->>QueryTimeProvider: useQueryTime()
    QueryTimeProvider-->>Component: Provide { queryTime, refreshQueryTime }
    Component->>QueryTimeProvider: (on refresh) refreshQueryTime()
    QueryTimeProvider-->>Component: Notify with updated queryTime
Loading

Suggested labels

Improvement

Suggested reviewers

  • perkinsjr
  • mcstepp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 17, 2025

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Collaborator

@chronark chronark left a comment

Choose a reason for hiding this comment

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

I like this

couple of questions:

  • should this be a provider instead? maybe that's more ergonomic? idk
  • can we have an api like this? Is this cleaner? idk
const { queryTime, refreshQueryTime } = useQueryTime()

// alternatively if we don't tend to destructure it
const {now, refresh} = useQueryTime()

@chronark chronark self-assigned this Apr 22, 2025
Copy link
Contributor Author

Okay. Yeah If we are on the same page Provider would be better coz every other RQ will implement this. I'll do it like you proposed. I like it :chef_kissing_fingers_intensifies:

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/hooks/use-logs-query.ts (1)

28-29: Consider updating or removing this comment.

This comment doesn't provide useful information anymore, as the primary purpose of the refactoring is to prevent double TRPC calls by using a consistent timestamp. Consider updating it to explain the relationship with the shared timestamp or removing it if no longer relevant.

- //Required for preventing double trpc call during initial render
+
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33c8afd and 0b6b931.

📒 Files selected for processing (7)
  • apps/dashboard/app/(app)/layout.tsx (2 hunks)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/charts/bar-chart/hooks/use-fetch-timeseries.ts (2 hunks)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/hooks/use-logs-query.ts (3 hunks)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/charts/hooks/use-fetch-timeseries.ts (2 hunks)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/controls/components/logs-refresh.tsx (1 hunks)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/table/hooks/use-logs-query.ts (4 hunks)
  • apps/dashboard/providers/query-time-provider.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/controls/components/logs-refresh.tsx
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/table/hooks/use-logs-query.ts
🧰 Additional context used
🧬 Code Graph Analysis (4)
apps/dashboard/app/(app)/layout.tsx (1)
apps/dashboard/providers/query-time-provider.tsx (1)
  • QueryTimeProvider (21-38)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/hooks/use-logs-query.ts (4)
apps/dashboard/providers/query-time-provider.tsx (1)
  • useQueryTime (40-46)
apps/dashboard/components/logs/hooks/use-sort.tsx (1)
  • useSort (16-60)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/query-logs.schema.ts (2)
  • SortFields (5-5)
  • RatelimitQueryOverviewLogsPayload (45-45)
apps/dashboard/components/logs/constants.ts (1)
  • HISTORICAL_DATA_WINDOW (2-2)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/charts/hooks/use-fetch-timeseries.ts (3)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/hooks/use-filters.ts (1)
  • useFilters (26-149)
apps/dashboard/providers/query-time-provider.tsx (1)
  • useQueryTime (40-46)
apps/dashboard/components/logs/constants.ts (1)
  • TIMESERIES_DATA_WINDOW (3-3)
apps/dashboard/providers/query-time-provider.tsx (2)
apps/dashboard/lib/trpc/context.ts (1)
  • createContext (7-31)
go/pkg/system_errors/errors.go (1)
  • Error (36-40)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Test Packages / Test ./internal/hash
  • GitHub Check: Test Packages / Test ./internal/billing
  • GitHub Check: Test Packages / Test ./internal/encryption
  • GitHub Check: Test API / API Test Local
  • GitHub Check: Test Agent Local / test_agent_local
  • GitHub Check: Build / Build
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (18)
apps/dashboard/providers/query-time-provider.tsx (4)

5-6: Good implementation of a global query timestamp state

The approach of using module-level variables for the timestamp and subscribers list is appropriate for maintaining a single source of truth across the entire application.


8-12: Clean implementation of the refresh mechanism

This function elegantly handles both updating the timestamp and notifying all subscribers in a predictable manner.


21-38: Well-structured context provider component

The QueryTimeProvider correctly subscribes to timestamp updates and handles cleanup to prevent memory leaks. The subscription mechanism ensures that all instances of the provider stay in sync with the global timestamp.


40-46: Good error handling in the hook

The hook appropriately throws an error if used outside of a provider, which will help catch misuse during development.

apps/dashboard/app/(app)/layout.tsx (2)

9-9: Correctly imported the QueryTimeProvider

The import path is accurate and follows the project's import conventions.


53-53: Well-placed provider in the component hierarchy

The QueryTimeProvider is appropriately wrapped around children only when the workspace is enabled, ensuring the context is available to all components that need it.

apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/charts/bar-chart/hooks/use-fetch-timeseries.ts (4)

4-4: Correctly imported the useQueryTime hook

The import path is accurate and follows the project's import conventions.


11-11: Good use of destructuring with renaming

Destructuring with renaming (queryTime: timestamp) improves code readability by making it clear what this value represents in the context of this hook.


16-17: Successfully replaced local timestamp with shared timestamp

The query parameters now use the shared timestamp, which will ensure consistency across different components and prevent unnecessary refetches during navigation.


56-56: Correctly updated useMemo dependencies

The dependency array now includes 'timestamp' instead of a local timestamp variable, ensuring that the query params are recalculated when the shared timestamp changes.

apps/dashboard/app/(app)/ratelimits/[namespaceId]/logs/components/charts/hooks/use-fetch-timeseries.ts (4)

4-4: Correctly imported the useQueryTime hook

The import path is accurate and follows the project's import conventions.


11-11: Good use of destructuring with renaming

Destructuring with renaming (queryTime: timestamp) improves code readability by making it clear what this value represents in the context of this hook.


16-17: Successfully replaced local timestamp with shared timestamp

The query parameters now use the shared timestamp, which will ensure consistency across different components and prevent unnecessary refetches during navigation.


56-56: Correctly updated useMemo dependencies

The dependency array now includes 'timestamp' instead of a local timestamp variable, ensuring that the query params are recalculated when the shared timestamp changes.

apps/dashboard/app/(app)/ratelimits/[namespaceId]/_overview/components/table/hooks/use-logs-query.ts (4)

3-3: Imported shared timestamp provider to enable consistent timestamps.

This import enables the hook to use the global timestamp from the QueryTimeProvider, supporting the goal of maintaining a consistent timestamp during page navigation.


22-22: Good usage of the shared timestamp mechanism.

Replacing the local timestamp with the shared queryTime from the provider ensures that all components use the same timestamp reference point, preventing unnecessary refetches during page navigation.


33-34: Correctly updated time window calculation to use shared timestamp.

Using the shared timestamp for calculating the query time window (startTime and endTime) ensures consistency across components and during refreshes.


89-89: Properly updated dependency array to include the new timestamp.

Including timestamp in the dependency array ensures that the query parameters are properly recalculated when the timestamp is refreshed, supporting the manual refresh functionality.

@ogzhanolguncu
Copy link
Contributor Author

Now we can navigate back and forth between log pages (basically any page that contains RQ with Date.now()) with 0 load times.

Copy link
Contributor Author

@chronark you gonna love this. I think this should be our first step before all those RQ optimizations. It's minimal effort with lots of gains

Copy link
Collaborator

clean :chef_kissing_fingers_intensifies:

I’d love some explanations as inline docs in the provider file, on why we need this, cause right now if you have no context ,it loosk like we’re too stupid to use Date.now() and overarchitected it

Copy link
Contributor Author

yeaaahh. hahahhhah 😄 i'll add it in 5 min

@chronark chronark added this pull request to the merge queue Apr 23, 2025
Merged via the queue into main with commit f077ac8 Apr 23, 2025
28 of 35 checks passed
@chronark chronark deleted the ENG-1751 branch April 23, 2025 07:10
@coderabbitai coderabbitai bot mentioned this pull request Apr 24, 2025
18 tasks
@ogzhanolguncu ogzhanolguncu mentioned this pull request Apr 24, 2025
18 tasks
@coderabbitai coderabbitai bot mentioned this pull request May 14, 2025
18 tasks
@coderabbitai coderabbitai bot mentioned this pull request Aug 22, 2025
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants