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

chore: refactor /database parallel rendering #255

Merged
merged 5 commits into from
May 27, 2024
Merged

chore: refactor /database parallel rendering #255

merged 5 commits into from
May 27, 2024

Conversation

duyet
Copy link
Owner

@duyet duyet commented May 27, 2024

No description provided.

Copy link

vercel bot commented May 27, 2024

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 May 27, 2024 11:17am

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 and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found

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 to tell me if it was helpful.

Comment on lines +1 to +2
export default function Page() {
return <></>
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Empty component

The Page component currently returns an empty fragment. Is this intentional? If this is a placeholder, consider adding a comment to clarify its purpose.

Comment on lines +1 to +3
import { DatabaseBreadcrumb } from './breadcrumb'

import { ErrorAlert } from '@/components/error-alert'
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbSeparator,
} from '@/components/ui/breadcrumb'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { fetchDataWithCache } from '@/lib/clickhouse'

import { listDatabases } from '../queries'
import { SingleLineSkeleton, TableSkeleton } from '@/components/skeleton'
Copy link
Contributor

Choose a reason for hiding this comment

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

question (bug_risk): Removed error handling

The error handling for fetching databases has been removed. How will errors be handled in the new implementation?

Comment on lines 57 to +61
const query_id = resultSet.query_id

const data = await resultSet.json<T>()
const end = new Date()
const duration = (end.getTime() - start.getTime()) / 1000
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Query duration logging

Logging the duration of queries is a good addition. Ensure that this does not introduce significant overhead.

Suggested change
const query_id = resultSet.query_id
const data = await resultSet.json<T>()
const end = new Date()
const duration = (end.getTime() - start.getTime()) / 1000
const start = performance.now()
const client = getClient({ web: false })
const resultSet = await client.query({
query: QUERY_COMMENT + query,
const query_id = resultSet.query_id
const data = await resultSet.json<T>()
const end = performance.now()
const duration = (end - start) / 1000

@@ -6,13 +6,15 @@ import { Button } from '@/components/ui/button'
interface ErrorAlertProps {
title?: string
message?: string | React.ReactNode | React.ReactNode[]
query?: string
Copy link
Contributor

Choose a reason for hiding this comment

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

🚨 suggestion (security): Optional query prop

The query prop is a useful addition for debugging. Ensure that sensitive information is not exposed in production environments.

Comment on lines +2 to +5
import { engineType } from '../engine-type'
import { Extras } from '../extras/extras'
import { SampleData } from '../extras/sample-data'
import { TableDDL } from '../extras/table-ddl'
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Error handling for engineType

Consider adding error handling for the engineType function call to handle cases where the fetch might fail.

Comment on lines 4 to 13
const resp = await fetchDataWithCache()<{ engine: string }[]>({
query: `
SELECT engine
FROM system.tables
WHERE (database = {database: String})
AND (name = {table: String})
`,
query_params: { database, table },
})

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Error handling for fetchDataWithCache

Consider adding error handling for the fetchDataWithCache call to handle potential network or data issues.

@@ -24,6 +26,12 @@ export function ErrorAlert({
<code className="overflow-auto hover:text-clip">{message}</code>
</div>

{query ? (
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 refactoring to handle both message and query in a more unified way.

The new code introduces increased complexity due to additional conditional logic and repetition. To simplify, consider refactoring to handle both message and query in a more unified way. Here's a possible refactor:

import React from 'react';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';

interface ErrorAlertProps {
  title?: string;
  message?: string | React.ReactNode | React.ReactNode[];
  query?: string;
  reset?: () => void;
  className?: string;
}

export function ErrorAlert({
  title = 'Something went wrong!',
  message = 'Checking console for more details.',
  query,
  reset,
  className,
}: ErrorAlertProps) {
  const renderContent = (content: string | React.ReactNode | React.ReactNode[]) => (
    <div className="mb-5 font-light">
      <code className="overflow-auto hover:text-clip">{content}</code>
    </div>
  );

  return (
    <Alert className={className}>
      <AlertTitle className="text-lg">{title}</AlertTitle>
      <AlertDescription>
        {renderContent(message)}
        {query && renderContent(query)}
        {reset && (
          <Button
            variant="outline"
            onClick={() => reset()}
          >
            Try again
          </Button>
        )}
      </AlertDescription>
    </Alert>
  );
}

This refactor reduces repetition, improves readability, and maintains the same functionality.

} else if (
typeof errorFallback === 'string' ||
typeof errorFallback === 'number'
) {
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 simplifying the type checking conditions for errorFallback.

The new code introduces some logical errors and reduces readability. Specifically, the type checking conditions for errorFallback are incorrect, leading to potential bugs. The conditions for checking if errorFallback is a string or number are not properly grouped, causing incorrect logic. Additionally, the code is harder to read due to these fragmented conditions. Here is a revised version that maintains simplicity and correctness:

import { LoadingIcon } from '@/components/loading-icon';

function defaultFallbackRender({ error, resetErrorBoundary }: FallbackProps) {
  // Call resetErrorBoundary() to reset the error boundary and retry the render.
  return <ErrorAlert message={error.message} reset={resetErrorBoundary} />;
}

export type ServerComponentLazyProps = {
  children: ReactNode;
  errorFallback?: null | string | number | ((props: FallbackProps) => ReactNode);
};

export function ServerComponentLazy({
  children,
  errorFallback,
}: ServerComponentLazyProps) {
  let fallbackRender: (props: FallbackProps) => ReactNode;

  if (errorFallback === null) {
    fallbackRender = () => null;
  } else if (typeof errorFallback === 'string' || typeof errorFallback === 'number') {
    fallbackRender = () => <span>{errorFallback}</span>;
  } else if (typeof errorFallback === 'function') {
    fallbackRender = errorFallback;
  } else {
    fallbackRender = defaultFallbackRender;
  }

  return (
    <ErrorBoundary fallbackRender={fallbackRender}>
      <Suspense fallback={<LoadingIcon />}>{children}</Suspense>
    </ErrorBoundary>
  );
}

This version corrects the type checking and improves readability, making the code easier to maintain.

@duyet duyet merged commit c1965f1 into main May 27, 2024
10 checks passed
@duyet duyet deleted the fix/typo-rewrite branch May 27, 2024 11:21
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