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

feat(insights): Add concurrent refresh of insights on dashboard #22197

Merged
merged 13 commits into from
Jun 6, 2024
43 changes: 32 additions & 11 deletions frontend/src/scenes/dashboard/dashboardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ export interface RefreshStatus {

export const AUTO_REFRESH_INITIAL_INTERVAL_SECONDS = 1800

async function runWithLimit<T>(tasks: (() => Promise<T>)[], limit: number): Promise<T[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm just finding this pretty hard to read in a recursive form (which also applied to loadNextPromise()), so I'm wondering if this routine can be made iterative?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

const results: T[] = []
const activePromises: Set<Promise<void>> = new Set()
const remainingTasks = [...tasks]

const startTask = async (task: () => Promise<T>): Promise<void> => {
const promise = task()
.then((result) => {
results.push(result)
})
.catch((error) => {
console.error('Error executing task:', error)
})
.finally(() => {
void activePromises.delete(promise)
})
activePromises.add(promise)
await promise
}

while (remainingTasks.length > 0 || activePromises.size > 0) {
if (activePromises.size < limit && remainingTasks.length > 0) {
void startTask(remainingTasks.shift()!)
} else {
await Promise.race(activePromises)
}
}

return results
}

// to stop kea typegen getting confused
export type DashboardTileLayoutUpdatePayload = Pick<DashboardTile, 'id' | 'layouts'>

Expand Down Expand Up @@ -1038,17 +1069,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
}
})

async function loadNextPromise(): Promise<void> {
if (!cancelled && fetchItemFunctions.length > 0) {
const nextPromise = fetchItemFunctions.shift()
if (nextPromise) {
await nextPromise()
await loadNextPromise()
}
}
}

void loadNextPromise()
await runWithLimit(fetchItemFunctions, 2)

eventUsageLogic.actions.reportDashboardRefreshed(dashboardId, values.newestRefreshed)
},
Expand Down
Loading