8000 feat(data-table): add custom sorting functions and enhance query config by duyet · Pull Request #487 · duyet/clickhouse-monitoring · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(data-table): add custom sorting functions and enhance query config #487

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

Merged
merged 1 commit into from
Apr 3, 2025

Conversation

duyet
Copy link
Owner
@duyet duyet commented Apr 3, 2025

Summary by Sourcery

Enhance data table functionality by adding custom sorting functions and extending query configuration to support flexible column sorting

New Features:

  • Introduce custom sorting functions for data tables that allow sorting columns using actual numeric values
  • Add support for specifying custom and built-in sorting functions in query configuration

Enhancements:

  • Extend query config type to include sorting function definitions
  • Improve column definition generation to support dynamic sorting function selection

Chores:

  • Create a new sorting functions utility module to centralize custom sorting logic

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 3, 2025
Copy link
vercel bot commented Apr 3, 2025

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

Name Status Preview Comments Updated (UTC)
clickhouse-monitoring ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2025 7:40am

@duyet duyet temporarily deployed to github-pages April 3, 2025 07:38 — with GitHub Actions Inactive
Copy link
Contributor
sourcery-ai bot commented Apr 3, 2025

Reviewer's Guide by Sourcery

This pull request introduces custom sorting functions to the data table component and enhances the query configuration for the tables overview page. The custom sorting functions allow users to sort columns based on specific logic, while the enhanced query configuration provides additional metrics related to data compression and part sizes.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Added the ability to specify custom sorting functions for columns in the data table.
  • Introduced getCustomSortingFns to define custom sorting logic.
  • Added a sortingFns option to the QueryConfig to specify which custom sorting function to use for a given column.
  • Updated getColumnDefs to apply the specified sorting function to the column definition.
  • Added CustomSortingFnNames and CustomSortingFn types to represent the custom sorting functions.
  • Updated the DataTable component to pass the custom sorting functions to the useReactTable hook.
components/data-table/column-defs.tsx
app/[host]/[query]/tables/tables-overview.ts
types/query-config.ts
components/data-table/data-table.tsx
types/generic.ts
components/data-table/sorting-fns.ts
Enhanced the query configuration for tables overview to include additional metrics related to data compression and part sizes.
  • Added new columns to the SQL query to calculate average and maximum compressed/uncompressed part sizes.
  • Added new columns to the SQL query to format average and maximum compressed/uncompressed part sizes into human-readable strings.
  • Added new columns to the SQL query to calculate the percentage of average and maximum compressed/uncompressed part sizes relative to the maximum value.
  • Added the new columns to the columns array in the tablesOverviewConfig.
  • Added the new columns to the columnFormats object in the tablesOverviewConfig and assigned the BackgroundBar format.
app/[host]/[query]/tables/tables-overview.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor
@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @duyet - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a validation step to ensure that the sortingFnName provided in the config actually exists, to prevent runtime errors.
  • The console.log statement should be removed before merging.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Review instructions: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


// Add the sorting function if specified
const sortingFnName = config.sortingFns?.[name]
console.log(`${name} => sortingFnName: ${sortingFnName}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Remove or disable debug logging in production code

The console.log appears intended for debugging but may generate unwanted noise or leak information in production. Consider removing it or gating it behind a debug flag.

Suggested change
console.log(`${name} => sortingFnName: ${sortingFnName}`)
if (process.env.NODE_ENV !== 'production') {
console.log(`${name} => sortingFnName: ${sortingFnName}`)
}

@@ -66,7 +74,8 @@ export const getColumnDefs = <
columnFormat = format as ColumnFormat
}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider using a unified lookup mapping for custom and built-in sorting functions to reduce nested conditionals and improve code readability and maintainability.

You can reduce the nested conditionals by creating a unified lookup mapping for both custom and built-in sorting functions. For example:

// Define your built-in sorting functions mapping
const builtInSortingFns: Record<string, BuiltInSortingFn> = {
  alphanumeric: 'alphanumeric',
  alphanumericCaseSensitive: 'alphanumericCaseSensitive',
  text: 'text',
  textCaseSensitive: 'textCaseSensitive',
  datetime: 'datetime',
  basic: 'basic',
}

// Merge custom and built-in functions
const customSortingFns = getCustomSortingFns<TData>()
const sortingFnMap = {
  ...customSortingFns,
  ...builtInSortingFns,
}

// Then assign sortingFn if found:
const sortingFnName = config.sortingFns?.[name]
if (sortingFnName && sortingFnMap[sortingFnName]) {
  columnDef.sortingFn = sortingFnMap[sortingFnName]
  console.log(`${name} => sortingFnName: ${sortingFnName}`)
}

Steps:

  1. Define a mapping for built-in functions.
  2. Merge your custom functions with the built-in ones.
  3. Replace the nested if-else with a simple lookup against the combined sortingFnMap.

This approach minimizes control flow complexity while keeping all functionality intact.

* @param <TData> - The type of the data in the table.
* @returns - The sorting functions for the table.
*/
export const getCustomSortingFns = <TData extends RowData>() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider exporting a constant object instead of a factory function for the sorting functions to simplify the code if the sorting functions are static and don't need to be recreated per call, potentially relaxing the generic parameter or defining the sorting function separately to maintain type safety if needed .

If the sorting functions are static and don’t need to be recreated per call, you can simplify by exporting a constant object instead of a factory. For example, if you can relax the generic parameter to a less strict type, you might refactor as follows:

export const customSortingFns: Record<string, SortingFn<unknown>> = {
  sort_column_using_actual_value: (rowA, rowB, columnId) => {
    const colName = columnId.replace('readable_', '').replace('pct_', '');
    const valueA = rowA.original[colName];
    const valueB = rowB.original[colName];

    if (typeof valueA === 'number' && typeof valueB === 'number') {
      return valueA - valueB;
    }

    return 0;
  },
};

If type-safety for TData is important, you could define the sorting function separately and then include it in a constant:

const sortColumnUsingActualValue = <TData extends RowData>(
  rowA: Row<TData>,
  rowB: Row<TData>,
  columnId: string
): number => {
  const colName = columnId.replace('readable_', '').replace('pct_', '');
  const valueA = rowA.original[colName as keyof TData];
  const valueB = rowB.original[colName as keyof TData];

  if (typeof valueA === 'number' && typeof valueB === 'number') {
    return valueA - valueB;
  }
  return 0;
};

export const customSortingFns = {
  sort_column_using_actual_value: sortColumnUsingActualValue,
} as Record<string, SortingFn<any>>;

These changes remove the extra abstraction layer while preserving functionality.

8000

@codecov-commenter
Copy link
codecov-commenter commented Apr 3, 2025

Bundle Report

Changes will increase total bundle size by 2.91kB (0.04%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
clickhouse-monitoring-bundle-server-cjs 2.58MB 2.38kB (0.09%) ⬆️
clickhouse-monitoring-bundle-client-array-push 3.79MB 533 bytes (0.01%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: clickhouse-monitoring-bundle-client-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
server/app/[host]/query/[query_id]/page_client-*.js 1 bytes 35.12kB 0.0%
server/app/[host]/disks/database/[database]/page_client-*.js 1 bytes 33.19kB 0.0%
server/app/[host]/[query]/page_client-*.js 1 bytes 32.99kB 0.0%
server/app/[host]/disks/page_client-*.js 1 bytes 32.91kB 0.0%
server/app/[host]/charts/[charts]/page_client-*.js 1 bytes 28.1kB 0.0%
server/app/[host]/overview/page_client-*.js 1 bytes 27.4kB 0.0%
server/app/[host]/dashboard/page_client-*.js 1 bytes 27.14kB 0.0%
server/app/[host]/database/[database]/[table]/@materializedview/page_client-*.js 1 bytes 27.14kB 0.0%
server/app/[host]/database/[database]/[table]/@dictionary/page_client-*.js 1 bytes 27.13kB 0.0%
server/app/[host]/database/[database]/[table]/@mergetree/page_client-*.js 1 bytes 27.13kB 0.0%
server/app/[host]/database/[database]/[table]/@view/page_client-*.js 1 bytes 27.13kB 0.0%
server/app/[host]/database/[database]/@nav/[table]/page_client-*.js 1 bytes 27.12kB 0.0%
server/app/[host]/database/[database]/[table]/page_client-*.js 1 bytes 27.12kB 0.0%
server/app/[host]/database/[database]/@nav/page_client-*.js 1 bytes 25.98kB 0.0%
server/app/[host]/database/[database]/page_client-*.js 1 bytes 25.97kB 0.0%
server/app/[host]/clusters/[cluster]/replicas-*.js 1 bytes 25.83kB 0.0%
server/app/play/page_client-*.js 1 bytes 25.63kB 0.0%
server/app/api/pageview/route_client-*.js 1 bytes 25.45kB 0.0%
server/app/api/timezone/route_client-*.js 1 bytes 25.45kB 0.0%
server/app/api/healthz/route_client-*.js 1 bytes 25.45kB 0.0%
server/app/api/version/route_client-*.js 1 bytes 25.45kB 0.0%
server/app/_not-*.js 1 bytes 25.44kB 0.0%
server/app/api/clean/route_client-*.js 1 bytes 25.44kB 0.0%
server/app/api/init/route_client-*.js 1 bytes 25.44kB 0.0%
server/app/healthz/route_client-*.js 1 bytes 25.44kB 0.0%
server/app/[host]/clusters/[cluster]/count-*.js 1 bytes 25.43kB 0.0%
server/app/[host]/clusters/[cluster]/parts-*.js 1 bytes 25.43kB 0.0%
server/app/page_client-*.js 1 bytes 25.39kB 0.0%
server/app/[host]/clusters/[cluster]/page_client-*.js 1 bytes 25.3kB 0.0%
server/app/[host]/replica/[replica]/tables/page_client-*.js 1 bytes 25.23kB 0.0%
server/app/[host]/clusters/page_client-*.js 1 bytes 25.23kB 0.0%
server/app/[host]/explain/page_client-*.js 1 bytes 24.94kB 0.0%
server/app/[host]/tables/[database]/[table]/page_client-*.js 1 bytes 24.82kB 0.0%
server/app/[host]/tables/[database]/page_client-*.js 1 bytes 24.69kB 0.0%
server/app/[host]/tables/page_client-*.js 1 bytes 24.62kB 0.0%
server/app/[host]/database/page_client-*.js 1 bytes 24.58kB 0.0%
static/chunks/1942-*.js (New) 23.83kB 23.83kB 100.0% 🚀
server/app/[host]/about/page_client-*.js 1 bytes 23.65kB 0.0%
static/chunks/5691.*.js (New) 2.46kB 2.46kB 100.0% 🚀
server/middleware-*.js 741 bytes 910 bytes 438.46% ⚠️
server/middleware-*.js -740 bytes 170 bytes -81.32%
static/rWDjWSpo0rBpHeCSN03Em/_buildManifest.js (New) 810 bytes 810 bytes 100.0% 🚀
react-*.json 1 bytes 168 bytes 0.6%
static/rWDjWSpo0rBpHeCSN03Em/_ssgManifest.js (New) 77 bytes 77 bytes 100.0% 🚀
static/chunks/9176-*.js (Deleted) -23.34kB 0 bytes -100.0% 🗑️
static/chunks/2890.*.js (Deleted) -2.46kB 0 bytes -100.0% 🗑️
static/4jxjLjv_ISBUByn3kClHE/_buildManifest.js (Deleted) -810 bytes 0 bytes -100.0% 🗑️
static/4jxjLjv_ISBUByn3kClHE/_ssgManifest.js (Deleted) -77 bytes 0 bytes -100.0% 🗑️
view changes for bundle: clickhouse-monitoring-bundle-server-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
../app/[host]/dashboard/page.js 2 bytes 155.49kB 0.0%
../app/[host]/[query]/page.js 1.93kB 51.62kB 3.89%
5555.js (New) 28.37kB 28.37kB 100.0% 🚀
2298.js (New) 2.43kB 2.43kB 100.0% 🚀
6967.js (Deleted) -27.92kB 0 bytes -100.0% 🗑️
6964.js (Deleted) -2.43kB 0 bytes -100.0% 🗑️

Files in 5555.js:

  • ./components/data-table/data-table.tsx → Total Size: 457 bytes

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.82%. Comparing base (7e8487c) to head (ca355f1).
Report is 6 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
- Coverage   77.16%   76.82%   -0.34%     
==========================================
  Files           5        5              
  Lines         162      164       +2     
  Branches       60       60              
==========================================
+ Hits          125      126       +1     
- Misses         34       35       +1     
  Partials        3        3              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dosubot dosubot bot added the enhancement New feature or request label Apr 3, 2025
@duyet duyet merged commit 6cc3c88 into main Apr 3, 2025
40 checks passed
@duyet duyet deleted the feat/sorting branch April 3, 2025 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0