10000 Limit BlockKey usage to RuntimeBlock objects, use string in ResultSpan by Copilot · Pull Request #51 · SergeiGolos/wod-wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Limit BlockKey usage to RuntimeBlock objects, use string in ResultSpan #51

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

Merged
merged 2 commits into from
May 23, 2025
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
3 changes: 1 addition & 2 deletions src/core/RuntimeSpan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BlockKey } from "./BlockKey";
import { ITimeSpan } from "./ITimeSpan";
import { RuntimeMetric } from "./RuntimeMetric";


export class RuntimeSpan {
blockKey?: string | BlockKey;
blockKey?: string;
index?: number;
timeSpans: ITimeSpan[] = [];
metrics: RuntimeMetric[] = [];
Expand Down
23 changes: 11 additions & 12 deletions src/core/metrics/ResultSpanBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,23 @@ export class ResultSpanBuilder {
public ForBlock(block: IRuntimeBlock): ResultSpanBuilder {
if (!block) return this;

const blockKeyString = block.blockKey.toString();

this.spans.forEach(span => {
span.blockKey = block.blockKey;
span.blockKey = blockKeyString;
span.index = block.blockKey.index;
span.leaf = block.leaf; // propagate leaf marker

// Also associate block key with each timespan
span.timeSpans.forEach(timeSpan => {
timeSpan.blockKey = block.blockKey.toString();
timeSpan.blockKey = blockKeyString;

if (timeSpan.start) {
timeSpan.start.blockKey = block.blockKey.toString();
timeSpan.start.blockKey = blockKeyString;
}

if (timeSpan.stop) {
timeSpan.stop.blockKey = block.blockKey.toString();
timeSpan.stop.blockKey = blockKeyString;
}
});
});
Expand All @@ -195,12 +197,9 @@ export class ResultSpanBuilder {
public registerSpan(span: RuntimeSpan): void {
if (!span) return;

// Convert blockKey to string if it's a BlockKey object
const blockKeyStr = this.getBlockKeyAsString(span.blockKey);

// Check if the span already exists by comparing key properties
const existingIndex = this.registeredSpans.findIndex(s =>
this.getBlockKeyAsString(s.blockKey) === blockKeyStr &&
s.blockKey === span.blockKey &&
s.index === span.index &&
s.start?.timestamp.getTime() === span.start?.timestamp.getTime()
);
Expand Down Expand Up @@ -241,7 +240,7 @@ export class ResultSpanBuilder {
public getSpansByBlockKey(blockKey: string | BlockKey): RuntimeSpan[] {
const blockKeyStr = this.getBlockKeyAsString(blockKey);
return this.registeredSpans.filter(span =>
this.getBlockKeyAsString(span.blockKey) === blockKeyStr
span.blockKey === blockKeyStr
);
}

Expand Down Expand Up @@ -351,11 +350,11 @@ export class ResultSpanBuilder {

// If no root block key provided, use all top-level spans
const rootSpans = rootBlockKeyStr
? this.registeredSpans.filter(span => this.getBlockKeyAsString(span.blockKey) === rootBlockKeyStr)
? this.registeredSpans.filter(span => span.blockKey === rootBlockKeyStr)
: this.registeredSpans.filter(span => {
// Spans with no parent references or that don't match any other span's blockKey
const isChild = this.registeredSpans.some(otherSpan =>
otherSpan.children?.includes(this.getBlockKeyAsString(span.blockKey) || '')
otherSpan.children?.includes(span.blockKey || '')
);
return !isChild;
});
Expand Down Expand Up @@ -386,7 +385,7 @@ export class ResultSpanBuilder {
span.children.forEach((childBlockKey: string) => {
// Find child spans with matching blockKey
const childSpans = this.registeredSpans.filter(s =>
this.getBlockKeyAsString(s.blockKey) === childBlockKey
s.blockKey === childBlockKey
);

// Add each child span as a node in the tree
Expand Down
25 changes: 13 additions & 12 deletions src/core/metrics/__tests__/BlockKeySpanIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ describe('BlockKey to string integration with ResultSpanBuilder', () => {
// Create a BlockKey
const blockKey = new BlockKey();
blockKey.push([{ id: 1 }, { id: 2 }] as any, 0);
const blockKeyString = blockKey.toString();

// Create a RuntimeSpan with BlockKey
// Create a RuntimeSpan with string blockKey
const span = new RuntimeSpan();
span.blockKey = blockKey;
span.blockKey = blockKeyString;
span.metrics = [];

// Register the span
Expand All @@ -26,7 +27,6 @@ describe('BlockKey to string integration with ResultSpanBuilder', () => {
expect(spansWithBlockKey).toHaveLength(1);

// Test with string
const blockKeyString = blockKey.toString();
const spansWithString = builder.getSpansByBlockKey(blockKeyString);
expect(spansWithString).toHaveLength(1);
});
Expand Down Expand Up @@ -61,19 +61,21 @@ describe('BlockKey to string integration with ResultSpanBuilder', () => {
// Create BlockKeys
const rootBlockKey = new BlockKey();
rootBlockKey.push([{ id: 1 }] as any, 0);
const rootBlockKeyString = rootBlockKey.toString();

const childBlockKey = new BlockKey();
childBlockKey.push([{ id: 2 }] as any, 0);
const childBlockKeyString = childBlockKey.toString();

// Create a root RuntimeSpan with BlockKey
// Create a root RuntimeSpan with string blockKey
const rootSpan = new RuntimeSpan();
rootSpan.blockKey = rootBlockKey;
rootSpan.blockKey = rootBlockKeyString;
rootSpan.metrics = [];
rootSpan.children = [childBlockKey.toString()];
rootSpan.children = [childBlockKeyString];

// Create a child RuntimeSpan with BlockKey
// Create a child RuntimeSpan with string blockKey
const childSpan = new RuntimeSpan();
childSpan.blockKey = childBlockKey;
childSpan.blockKey = childBlockKeyString;
childSpan.metrics = [];

// Register the spans
Expand All @@ -85,7 +87,6 @@ describe('BlockKey to string integration with ResultSpanBuilder', () => {
const hierarchyWithBlockKey = builder.createHierarchicalView(rootBlockKey);

// Test with string
const rootBlockKeyString = rootBlockKey.toString();
const hierarchyWithString = builder.createHierarchicalView(rootBlockKeyString);

// Assert
Expand All @@ -108,15 +109,15 @@ describe('BlockKey to string integration with ResultSpanBuilder', () => {
// Act
const builder = new ResultSpanBuilder();

// Create spans with different representations of the same BlockKey
// Create spans with different representations of the same BlockKey as strings
const spanWithOriginalKey = new RuntimeSpan();
spanWithOriginalKey.blockKey = originalBlockKey;
spanWithOriginalKey.blockKey = originalBlockKey.toString();

const spanWithStringKey = new RuntimeSpan();
spanWithStringKey.blockKey = stringRepresentation;

const spanWithRecreatedKey = new RuntimeSpan();
spanWithRecreatedKey.blockKey = recreatedBlockKey;
spanWithRecreatedKey.blockKey = recreatedBlockKey.toString();

builder.registerSpan(spanWithOriginalKey);

Expand Down
0