Skip to content

Commit 8f0397b

Browse files
Fix: frontend cache delta query issues
Fix issue with joining dataframes on delta cache queries which resulted in one of them failing if two were made
1 parent 504d5ab commit 8f0397b

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

src/datasourceCache.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ export class SiftDataSourceCache {
138138
}
139139

140140
let newFrames: DataFrame[][] = [];
141-
// Fire off sub‑queries for each missing range
142-
await Promise.all(
143-
fetchRanges.map(async (rng) => {
141+
// Sequential processing using reduce since parallel requests will cancel each other
142+
await fetchRanges.reduce(async (previousPromise, rng) => {
143+
await previousPromise; // Wait for the previous request to complete
144+
145+
try {
144146
const subReq: DataQueryRequest<SiftQuery> = {
145147
...request,
146148
range: {
@@ -157,28 +159,46 @@ export class SiftDataSourceCache {
157159
console.error(
158160
`Panel ${panelId} - Failed to fetch data from ${new Date(rng.from).toISOString()} to ${new Date(
159161
rng.to
160-
).toISOString()}`
162+
).toISOString()}`,
163+
subResp
161164
);
162165
}
163-
})
164-
);
166+
} catch (error) {
167+
console.error(
168+
`Panel ${panelId} - Error fetching range ${new Date(rng.from).toISOString()} to ${new Date(
169+
rng.to
170+
).toISOString()}:`,
171+
error
172+
);
173+
}
174+
}, Promise.resolve());
175+
176+
let refIdToFrameMap = new Map<string, DataFrame>();
165177

166-
let updatedCacheFrames: DataFrame[] = [];
178+
// Initialize the map with trimmed cache frames
179+
trimmedCacheFrames.forEach((frame) => {
180+
if (frame.refId) {
181+
refIdToFrameMap.set(frame.refId, frame);
182+
}
183+
});
184+
185+
// Process new frames and merge with cached ones
167186
newFrames.forEach((frames) => {
168187
frames.forEach((frame) => {
169-
const matchingCachedFrame = trimmedCacheFrames.find((cachedFrame) => {
170-
return cachedFrame.refId === frame.refId;
171-
});
172-
173-
// If cached frame exists, append to it
174-
if (matchingCachedFrame) {
175-
updatedCacheFrames.push(appendFramesByTime(matchingCachedFrame, frame));
176-
} else {
177-
updatedCacheFrames.push(frame);
188+
if (frame.refId) {
189+
const cachedFrame = refIdToFrameMap.get(frame.refId);
190+
if (cachedFrame) {
191+
refIdToFrameMap.set(frame.refId, appendFramesByTime(cachedFrame, frame));
192+
} else {
193+
refIdToFrameMap.set(frame.refId, frame);
194+
}
178195
}
179196
});
180197
});
181198

199+
// Convert map back to array
200+
const updatedCacheFrames = Array.from(refIdToFrameMap.values());
201+
182202
const filteredFrames = updatedCacheFrames.map((frame) => filterFrameByTimeRange(frame, newFrom, newTo));
183203

184204
const result: DataQueryResponse = { data: filteredFrames };

0 commit comments

Comments
 (0)