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

fix(insights): Fall back to dataNodeLogic results and add dashboard concurrency #22181

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wut

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
actions.clearResponse()
}
if (
!(props.cachedResults && props.key.includes('dashboard')) && // Don't load data on dashboard if cached results are available
!(
(props.cachedResults?.['result'] || props.cachedResults?.['results']) &&
props.key.includes('dashboard')
) && // Don't load data on dashboard if cached results are available
((!values.response?.['result'] && !values.response?.['results']) ||
!queryEqual(props.query, oldProps.query)) &&
(!props.cachedResults ||
(isInsightQueryNode(props.query) && !props.cachedResults['result'] && !props.cachedResults['results']))
) {
actions.loadData()
} else if (props.cachedResults) {
} else if (props.cachedResults && !values.response?.['result'] && !values.response?.['results']) {
// Use cached results if available, otherwise this logic will load the data again
actions.setResponse(props.cachedResults)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/dashboard/dashboardLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ describe('dashboardLogic', () => {
})
.toFinishAllListeners()
.toMatchValues({
refreshStatus: { 1001: { error: true, timer: expect.anything() } },
refreshStatus: { 1001: { refreshed: true, timer: expect.anything() } },
})
})
})
Expand Down
47 changes: 35 additions & 12 deletions frontend/src/scenes/dashboard/dashboardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ export interface RefreshStatus {

export const AUTO_REFRESH_INITIAL_INTERVAL_SECONDS = 1800

async function runWithLimit<T>(tasks: (() => Promise<T>)[], limit: number): Promise<T[]> {
const results: T[] = []
const activePromises: Promise<void>[] = []
const remainingTasks = [...tasks] // Clone the tasks array to manage remaining tasks

const enqueueTask = (): void => {
while (activePromises.length < limit && remainingTasks.length > 0) {
const task = remainingTasks.shift() // Get the next task and remove it from remaining
if (task) {
const promise = task()
.then((result) => {
results.push(result)
void activePromises.splice(activePromises.indexOf(promise), 1) // Remove promise when done
})
.catch((error) => {
void activePromises.splice(activePromises.indexOf(promise), 1) // Handle errors and remove promise
console.error('Error executing task:', error)
})
activePromises.push(promise)
}
}
}

enqueueTask() // Initial population of the activePromises array

while (activePromises.length > 0) {
await Promise.race(activePromises)
enqueueTask() // Check and enqueue new tasks if there's room
}

return results
}

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

Expand Down Expand Up @@ -1010,7 +1043,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
}
cancelled = true
} else {
actions.setRefreshError(insight.short_id)
actions.setRefreshStatus(insight.short_id)
}
}

Expand Down Expand Up @@ -1043,17 +1076,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