codeant-ai-for-open-source[bot] commented on code in PR #41551: URL: https://github.com/apache/superset/pull/41551#discussion_r3690784522
########## superset-frontend/src/features/versionHistory/grouping.ts: ########## @@ -0,0 +1,270 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import type { + ActivityRecord, + RelatedEntry, + SaveGroup, + TimelineEntry, +} from './types'; + +/** + * Stable identity for one activity record, used to deduplicate rows + * when merging pages (offset pagination can re-serve rows if a new + * save lands between page fetches). + */ +export function recordKey(record: ActivityRecord): string { + return [ + record.transaction_id, + record.entity_kind, + record.entity_uuid ?? record.entity_name, + record.source, + record.kind, + record.operation, + JSON.stringify(record.path), + ].join('|'); +} + +/** Merge a newly fetched page into already loaded records, deduplicated. */ +export function mergeActivityPages( + existing: ActivityRecord[], + incoming: ActivityRecord[], +): ActivityRecord[] { + const seen = new Set(existing.map(recordKey)); + const merged = [...existing]; + incoming.forEach(record => { + const key = recordKey(record); + if (!seen.has(key)) { + seen.add(key); + merged.push(record); + } + }); + return merged; +} + +/** One related row per save of a related entity, regardless of how many + * change records that save produced. */ +export function relatedEntryKey(record: ActivityRecord): string { + return [ + record.transaction_id, + record.entity_kind, + record.entity_uuid ?? record.entity_name, + ].join('|'); +} + +/** Identity of the entity a related record belongs to, ignoring the + * transaction. */ +function relatedEntityKey(record: ActivityRecord): string { + return [record.entity_kind, record.entity_uuid ?? record.entity_name].join( + '|', + ); +} + +const RELATED_MERGE_WINDOW_MS = 60_000; + +/** + * The backend can split one logical save of a related entity into + * several transactions issued at (nearly) the same instant, which + * would render as duplicate-looking rows. Collapse adjacent related + * entries — no self save between them — for the same entity issued + * within a short window into the newer entry: its representative + * record is kept and the older save's records are absorbed so search + * over collapsed records keeps working. + */ +// TODO(version-history): backend workaround — remove when upstream emits +// one transaction per logical save. +function mergeAdjacentRelatedEntries( + entries: TimelineEntry[], +): TimelineEntry[] { + const merged: TimelineEntry[] = []; + entries.forEach(entry => { + const previous = merged[merged.length - 1]; + if ( + entry.type === 'related' && + previous?.type === 'related' && + relatedEntityKey(previous.record) === relatedEntityKey(entry.record) && + Math.abs( + Date.parse(previous.record.issued_at) - + Date.parse(entry.record.issued_at), + ) <= RELATED_MERGE_WINDOW_MS + ) { Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag the intentional client-side related-entry merge window in version-history grouping; it is a documented backend workaround for rendering one logical save split across multiple transactions, and should be removed only when the backend provides logical-save records. **Applied to:** - `superset-frontend/src/features/versionHistory/grouping.ts` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
