Skip to content

refactor: filters refactor - Not urgent#3401

Closed
ogzhanolguncu wants to merge 5 commits intomainfrom
filters-refactor
Closed

refactor: filters refactor - Not urgent#3401
ogzhanolguncu wants to merge 5 commits intomainfrom
filters-refactor

Conversation

@ogzhanolguncu
Copy link
Contributor

@ogzhanolguncu ogzhanolguncu commented Jun 24, 2025

What does this PR do?

This PR will help us bootstrap new filters easily, including zod schemas, nuqs params, and more. We'll be able to easily create new useFiltersHooks just by passing the generated filters, e.g.,
export const useFilters = createUseFilters(logsSchema);

To validate the idea, I tried refactoring the rate limits overview filters and hooks, and it worked great. I'm planning to refactor them next, or we can target any other filters or sections. This change will help us refactor dashboard filters gradually.

You can easily see the amount of duplicate code eliminated as a result of this PR.

Fixes # (issue)

If there is not an issue for this, please create one first. This is used to tracking purposes and also helps use understand why this PR exists

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?

  • Make sure there no regressions in /logs pages.

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

  • Refactor
    • Unified and simplified log filter handling using a new centralized filter schema and builder utility.
    • Updated filter parameter naming conventions (e.g., methodmethods, pathpaths) for consistency across the dashboard.
    • Replaced manual filter logic in hooks and components with reusable helper functions for improved maintainability.
    • Streamlined and consolidated schema definitions for query parameters and filters.
  • Chores
    • Removed obsolete schema files and related test files.
    • Updated internal imports to reflect new schema locations and naming.

@changeset-bot
Copy link

changeset-bot bot commented Jun 24, 2025

⚠️ No Changeset found

Latest commit: d4151b6

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 Jun 24, 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 Jun 24, 2025 1:28pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
engineering ⬜️ Ignored (Inspect) Visit Preview Jun 24, 2025 1:28pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 24, 2025

Caution

Review failed

Failed to post review comments.

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

📥 Commits

Reviewing files that changed from the base of the PR and between c1d5fb1 and d4151b6.

📒 Files selected for processing (1)
  • apps/dashboard/lib/filters/filter-hook.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test Packages / Test ./internal/clickhouse
🔇 Additional comments (1)
apps/dashboard/lib/filters/filter-hook.ts (1)

1-40: Well-designed generic filter hook factory

The generic type system and overall architecture provide excellent reusability and type safety. This approach successfully reduces code duplication as intended by the PR objectives.

📝 Walkthrough

Walkthrough

This change refactors the log filtering system by introducing a centralized, type-safe filter schema builder and a generic filter hook. It removes manual schema and filter logic, updates all filter-related code to use plural keys (e.g., methods, paths), and consolidates query parameter construction. Related Zod schemas and test files are deleted or replaced.

Changes

File(s) Change Summary
.../components/table/components/log-details/components/hooks/use-logs-query.ts
.../components/table/logs-table.tsx
Renamed filter query parameter keys from singular to plural (methodmethods, pathpaths).
.../components/charts/hooks/use-fetch-timeseries.ts
.../components/table/hooks/use-logs-query.ts
Replaced manual query parameter construction with a call to the new buildQueryParams helper.
.../components/charts/query-timeseries.schema.ts
.../components/table/query-logs.schema.ts
.../lib/trpc/routers/logs/query-logs/utils.test.ts
Deleted Zod schema files for log/timeseries queries and related test file.
.../filters.query-params.ts Added new buildQueryParams function to build log filter query payloads.
.../filters.schema.ts Refactored logs filter schema using a new schema builder; updated types, exports, and operator/field handling.
.../hooks/use-filters.ts Replaced custom filter hook implementation with a generic createUseFilters(logsSchema).
.../lib/filters/filter-builder.ts Added a new generic filter builder utility for type-safe schema and parser creation.
.../lib/filters/filter-hook.ts Added a new generic filter hook factory for managing filter state from URL query parameters.
.../lib/trpc/routers/logs/query-logs/index.ts Updated import path for queryLogsPayload to use the new schema.
.../lib/trpc/routers/logs/query-logs/utils.ts
.../lib/trpc/routers/logs/query-timeseries/utils.ts
Updated utility functions to use new filter types and plural property names.
.../lib/trpc/routers/logs/query-timeseries/index.ts Changed input schema for queryTimeseries to use the new query payload omitting limit and cursor.

Sequence Diagram(s)

sequenceDiagram
    participant UI
    participant useFilters (generic hook)
    participant buildQueryParams
    participant API

    UI->>useFilters: Get current filters from URL/query params
    useFilters-->>UI: Return filters array and helpers

    UI->>buildQueryParams: Build query payload with filters & timestamp
    buildQueryParams-->>UI: Return query payload

    UI->>API: Fetch logs/timeseries with query payload
    API-->>UI: Return filtered log data
Loading

Possibly related PRs

  • feat: new filter logic #2804: Introduced new filter logic and pluralization of filter keys, similar to this PR’s changes to filter state management and query parameter handling.
  • refactor: filter types for logs page #2878: Refactored filter types and enums used in logs page schemas, closely related to this PR’s updates to filter data structures and validation.

Suggested reviewers

  • perkinsjr
  • mcstepp
  • chronark
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@ogzhanolguncu ogzhanolguncu marked this pull request as ready for review June 24, 2025 13:23
@github-actions
Copy link
Contributor

github-actions bot commented Jun 24, 2025

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

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: 4

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 49000fc and c1d5fb1.

📒 Files selected for processing (16)
  • apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/components/table/components/log-details/components/hooks/use-logs-query.ts (1 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/components/table/logs-table.tsx (1 hunks)
  • apps/dashboard/app/(app)/logs/components/charts/hooks/use-fetch-timeseries.ts (1 hunks)
  • apps/dashboard/app/(app)/logs/components/charts/query-timeseries.schema.ts (0 hunks)
  • apps/dashboard/app/(app)/logs/components/table/hooks/use-logs-query.ts (2 hunks)
  • apps/dashboard/app/(app)/logs/components/table/query-logs.schema.ts (0 hunks)
  • apps/dashboard/app/(app)/logs/filters.query-params.ts (1 hunks)
  • apps/dashboard/app/(app)/logs/filters.schema.ts (1 hunks)
  • apps/dashboard/app/(app)/logs/hooks/use-filters.ts (1 hunks)
  • apps/dashboard/lib/filters/filter-builder.ts (1 hunks)
  • apps/dashboard/lib/filters/filter-hook.ts (1 hunks)
  • apps/dashboard/lib/trpc/routers/logs/query-logs/index.ts (1 hunks)
  • apps/dashboard/lib/trpc/routers/logs/query-logs/utils.test.ts (0 hunks)
  • apps/dashboard/lib/trpc/routers/logs/query-logs/utils.ts (1 hunks)
  • apps/dashboard/lib/trpc/routers/logs/query-timeseries/index.ts (1 hunks)
  • apps/dashboard/lib/trpc/routers/logs/query-timeseries/utils.ts (2 hunks)
💤 Files with no reviewable changes (3)
  • apps/dashboard/app/(app)/logs/components/table/query-logs.schema.ts
  • apps/dashboard/lib/trpc/routers/logs/query-logs/utils.test.ts
  • apps/dashboard/app/(app)/logs/components/charts/query-timeseries.schema.ts
🔇 Additional comments (14)
apps/dashboard/lib/filters/filter-builder.ts (3)

14-42: Type definitions are well-structured.

The use of marker interfaces with boolean flags for discriminated unions is a clean pattern. The type system provides good compile-time safety.


205-250: Well-implemented schema generation with proper field type handling.

The createApiQuerySchema function correctly handles all field types including special time fields, and the conditional pagination support is cleanly implemented.


118-122: ```shell
#!/bin/bash

Locate the TConfigs definition and inspect its operators property

rg -n -C3 "type TConfigs" .
rg -n -C3 "interface TConfigs" .
rg -n -C3 "TConfigs.*operators" .


</details>
<details>
<summary>apps/dashboard/lib/trpc/routers/logs/query-logs/index.ts (1)</summary>

`1-1`: **Import path correctly updated to use centralized schema.**

The change from component-specific schema to the centralized filters schema aligns with the refactoring objectives.

</details>
<details>
<summary>apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/components/table/components/log-details/components/hooks/use-logs-query.ts (1)</summary>

`16-17`: **Filter keys correctly pluralized to match new schema.**

The change from `method`/`path` to `methods`/`paths` is consistent with the refactored filter schema structure.

</details>
<details>
<summary>apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/components/table/logs-table.tsx (1)</summary>

`219-220`: **Prefetch query parameters correctly updated.**

The pluralization of filter keys in the prefetch query maintains consistency with the refactored schema.

</details>
<details>
<summary>apps/dashboard/lib/trpc/routers/logs/query-timeseries/index.ts (1)</summary>

`4-4`: **Excellent schema reuse with appropriate field omission.**

The approach of reusing `queryLogsPayload` while omitting pagination fields (`limit` and `cursor`) for timeseries queries is clean and reduces code duplication. This maintains consistency across the codebase while adapting to specific endpoint requirements.



Also applies to: 12-12

</details>
<details>
<summary>apps/dashboard/app/(app)/logs/components/charts/hooks/use-fetch-timeseries.ts (1)</summary>

`4-8`: **Clean refactor to centralize query parameter construction.**

The simplification to use `buildQueryParams` effectively removes code duplication and centralizes filter logic as intended by the PR objectives.

</details>
<details>
<summary>apps/dashboard/lib/trpc/routers/logs/query-logs/utils.ts (1)</summary>

`1-18`: **Consistent updates to align with the new filter schema.**

The changes correctly update the import path, parameter type, and apply the plural naming convention for filter keys (`paths`, `methods`) as intended by the refactor.

</details>
<details>
<summary>apps/dashboard/app/(app)/logs/components/table/hooks/use-logs-query.ts (1)</summary>

`5-34`: **Effective simplification using the centralized query builder.**

The replacement of manual query parameter construction with `buildQueryParams` successfully reduces code complexity while maintaining all hook functionality.

</details>
<details>
<summary>apps/dashboard/app/(app)/logs/hooks/use-filters.ts (1)</summary>

`1-4`: **Excellent implementation of the generic filter hook pattern.**

This concise implementation perfectly demonstrates the PR's main objective - enabling easy creation of new filter hooks by simply passing a schema to `createUseFilters`. This pattern will significantly reduce code duplication across the codebase.

</details>
<details>
<summary>apps/dashboard/lib/trpc/routers/logs/query-timeseries/utils.ts (1)</summary>

`1-30`: **Consistent application of the filter schema refactor.**

The updates correctly align with the new centralized schema, using appropriate type constraints with `Omit` and maintaining the plural naming convention for filter keys.

</details>
<details>
<summary>apps/dashboard/app/(app)/logs/filters.query-params.ts (1)</summary>

`25-75`: **Well-structured filter processing logic**

The switch statement properly handles all filter types with appropriate type checking and error handling. The exhaustive check pattern on line 71 is a good practice for catching unhandled cases.

</details>
<details>
<summary>apps/dashboard/app/(app)/logs/filters.schema.ts (1)</summary>

`5-55`: **Clean schema refactoring with good separation of concerns**

The refactoring to use `createFilterSchema` successfully centralizes filter configuration and reduces code duplication. The status color coding logic (lines 9-18) appropriately follows HTTP status code conventions.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@coderabbitai coderabbitai bot mentioned this pull request Jun 30, 2025
18 tasks
@ogzhanolguncu ogzhanolguncu changed the title refactor: filters refactor refactor: filters refactor - Not urgent Jul 3, 2025
@coderabbitai coderabbitai bot mentioned this pull request Aug 24, 2025
18 tasks
@chronark chronark deleted the filters-refactor branch September 29, 2025 15:16
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.

1 participant