Skip to content

Commit f528c26

Browse files
feat: add waitForDataRender() function
Fixes an issue with visual report where it gets generated before all visualisations are loaded. Signed-off-by: tender-barbarian <[email protected]>
1 parent 7790110 commit f528c26

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

public/components/visual_report/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum VISUAL_REPORT_TYPE {
2020
export enum SELECTOR {
2121
dashboard = '#dashboardViewport',
2222
visualization = '.visEditor__content',
23+
visualizations = '.visualize',
2324
notebook = '.euiPageBody',
2425
}
2526

public/components/visual_report/generate_report.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,50 @@ const waitForSelector = (selector: string, timeout = 30000) => {
4040
() =>
4141
reject(
4242
'Timed out waiting for selector ' +
43-
selector +
44-
' while generating report.'
43+
selector +
44+
' while generating report.'
4545
),
4646
timeout
4747
)
4848
),
4949
]);
5050
};
5151

52+
const waitForDataRender = (selector: string, timeout = 300000) => {
53+
const elements = Array.from(document.querySelectorAll(selector))
54+
return Promise.all(
55+
elements.map(async (element) => {
56+
return Promise.race([
57+
new Promise((resolve) => {
58+
if (element.getAttribute('data-render-complete') == "true") {
59+
resolve(element);
60+
};
61+
62+
const observer = new MutationObserver((mutation, observer) => {
63+
resolve(mutation);
64+
observer.disconnect();
65+
});
66+
67+
observer.observe(element, {
68+
attributeFilter: ['data-render-complete'],
69+
});
70+
}),
71+
new Promise((resolve, reject) =>
72+
setTimeout(
73+
() =>
74+
reject(
75+
'Timed out waiting for element ' +
76+
element +
77+
' while generating report.'
78+
),
79+
timeout
80+
)
81+
),
82+
]);
83+
}),
84+
);
85+
};
86+
5287
const timeout = (ms: number) => {
5388
return new Promise((resolve) => setTimeout(resolve, ms));
5489
};
@@ -136,6 +171,7 @@ export const generateReport = async (id: string, forceDelay = 15000) => {
136171
switch (reportSource) {
137172
case VISUAL_REPORT_TYPE.dashboard:
138173
await waitForSelector(SELECTOR.dashboard);
174+
await waitForDataRender(SELECTOR.visualizations);
139175
break;
140176
case VISUAL_REPORT_TYPE.visualization:
141177
await waitForSelector(SELECTOR.visualization);

0 commit comments

Comments
 (0)