Skip to content

Commit

Permalink
Fix the refresh rate of the sync chip (#3784)
Browse files Browse the repository at this point in the history
* Fix the refresh rate of the sync chip

* format
  • Loading branch information
lasryaric authored Feb 16, 2024
1 parent 7a023c0 commit cb1f907
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
60 changes: 34 additions & 26 deletions front/components/data_source/DataSourceSyncChip.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import { Chip } from "@dust-tt/sparkle";
import type { ConnectorType } from "@dust-tt/types";
import { assertNever } from "@dust-tt/types";
import { useEffect, useState } from "react";

import { CONNECTOR_CONFIGURATIONS } from "@app/lib/connector_providers";
import { useConnector } from "@app/lib/swr";
import { timeAgoFrom } from "@app/lib/utils";

export default function ConnectorSyncingChip({
connector,
workspaceId,
dataSourceName,
}: {
connector: ConnectorType;
workspaceId: string;
dataSourceName: string;
}) {
const [computedTimeAgo, setComputedTimeAgo] = useState<string | null>(null);
const refreshedConnector = useConnector({
workspaceId: connector.workspaceId,
dataSourceName: connector.dataSourceName,
refreshInterval: 3000,
const { connector, isConnectorLoading, isConnectorError } = useConnector({
workspaceId: workspaceId,
dataSourceName: dataSourceName,
});

useEffect(() => {
if (refreshedConnector.connector?.lastSyncSuccessfulTime) {
setComputedTimeAgo(
timeAgoFrom(refreshedConnector.connector.lastSyncSuccessfulTime)
if (!connector) {
if (isConnectorError) {
return (
<Chip color="warning">Error loading synchronization information</Chip>
);
} else if (isConnectorLoading) {
return (
<Chip color="amber" isBusy>
Loading
</Chip>
);
} else {
// This should never happen, but is a typescript possible case
return <Chip color="warning">Connector not found</Chip>;
}
}, [refreshedConnector.connector?.lastSyncSuccessfulTime]);
}

if (refreshedConnector.connector?.errorType) {
if (connector.errorType) {
return (
<Chip color="warning">
{(() => {
switch (refreshedConnector.connector?.errorType) {
switch (connector.errorType) {
case "oauth_token_revoked":
return (
<>
Expand All @@ -44,30 +51,31 @@ export default function ConnectorSyncingChip({
return (
<>
We have encountered an error talking to{" "}
{
CONNECTOR_CONFIGURATIONS[refreshedConnector.connector.type]
.name
}
. We sent you an email with more details to resolve the issue.
{CONNECTOR_CONFIGURATIONS[connector.type].name}. We sent you
an email with more details to resolve the issue.
</>
);
default:
assertNever(refreshedConnector.connector?.errorType);
assertNever(connector.errorType);
}
return <></>;
})()}
</Chip>
);
} else if (!refreshedConnector.connector?.lastSyncSuccessfulTime) {
} else if (!connector.lastSyncSuccessfulTime) {
return (
<Chip color="amber" isBusy>
Synchronizing
{refreshedConnector.connector?.firstSyncProgress
? ` (${refreshedConnector.connector.firstSyncProgress})`
{connector.firstSyncProgress
? ` (${connector.firstSyncProgress})`
: null}
</Chip>
);
} else {
return <Chip color="slate">Last Sync ~ {computedTimeAgo} ago</Chip>;
return (
<Chip color="slate">
Last Sync ~ {timeAgoFrom(connector.lastSyncSuccessfulTime)} ago
</Chip>
);
}
}
19 changes: 13 additions & 6 deletions front/lib/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,21 +363,28 @@ export function useConnectorConfig({
export function useConnector({
workspaceId,
dataSourceName,
refreshInterval,
}: {
workspaceId: string;
dataSourceName: string;
refreshInterval: number;
}) {
if (refreshInterval < 1000) {
throw new Error("refreshInterval must be at least 1000ms");
}
const configFetcher: Fetcher<GetConnectorResponseBody> = fetcher;

const url = `/api/w/${workspaceId}/data_sources/${dataSourceName}/connector`;

const { data, error, mutate } = useSWR(url, configFetcher, {
refreshInterval,
refreshInterval: (connectorResBody) => {
if (connectorResBody?.connector.errorType !== undefined) {
// We have an error, no need to auto refresh.
return 0;
}
if (connectorResBody?.connector.lastSyncSuccessfulTime) {
// no sync in progress, no need to auto refresh.
return 0;
}

// We have a synchronization in progress, we'll refresh every 3 seconds.
return 3000;
},
});

return {
Expand Down
5 changes: 4 additions & 1 deletion front/pages/w/[wId]/builder/data-sources/[name]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ function ManagedDataSourceView({
)}
</div>
<div className="pt-2">
<ConnectorSyncingChip connector={connector} />
<ConnectorSyncingChip
workspaceId={connector.workspaceId}
dataSourceName={connector.dataSourceName}
/>
</div>

{isAdmin && (
Expand Down
5 changes: 4 additions & 1 deletion front/pages/w/[wId]/builder/data-sources/managed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,10 @@ export default function DataSourcesView({
);
} else {
return (
<ConnectorSyncingChip connector={ds.connector} />
<ConnectorSyncingChip
workspaceId={ds.connector.workspaceId}
dataSourceName={ds.connector.dataSourceName}
/>
);
}
})()}
Expand Down
5 changes: 4 additions & 1 deletion front/pages/w/[wId]/builder/data-sources/public-urls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ export default function DataSourcesView({
</Button.List>
}
>
<ConnectorSyncingChip connector={ds.connector} />
<ConnectorSyncingChip
workspaceId={ds.connector.workspaceId}
dataSourceName={ds.connector.dataSourceName}
/>
<ContextItem.Description>
{" "}
<br />
Expand Down

0 comments on commit cb1f907

Please sign in to comment.