Skip to content

Commit

Permalink
Improve wpilog metadata handling (closes #110)
Browse files Browse the repository at this point in the history
Show when hovering on key in sidebar and include in wpilog export
  • Loading branch information
jwbonner committed Dec 21, 2023
1 parent e7f4bd4 commit 6edb70f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/hub/Sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class Sidebar {
private setTuningModeActiveCallbacks: ((active: boolean) => void)[] = [];
private tuningModePublishCallbacks: (() => void)[] = [];
private tuningValueCache: { [key: string]: string } = {};
private updateMetadataCallbacks: (() => void)[] = [];

constructor() {
// Set up handle for resizing
Expand Down Expand Up @@ -286,6 +287,7 @@ export default class Sidebar {
this.selectGroupClearCallbacks = [];
this.setTuningModeActiveCallbacks = [];
this.tuningModePublishCallbacks = [];
this.updateMetadataCallbacks = [];

// Add new list
let tree = window.log.getFieldTree();
Expand All @@ -303,6 +305,9 @@ export default class Sidebar {

// Update search
this.updateSearchResults();
} else {
// Update metadata
this.updateMetadataCallbacks.forEach((callback) => callback());
}
}

Expand Down Expand Up @@ -641,6 +646,22 @@ export default class Sidebar {
numValueInput!.value = this.tuningValueCache[field.fullKey];
}
}

// Metadata callback
let updateMetadata = () => {
let metadata = window.log.getWpilibMetadata(field.fullKey!);
try {
let metadataParsed = JSON.parse(metadata);
label.title = Object.keys(metadataParsed)
.sort()
.map((key) => key + ": " + metadataParsed[key])
.join("\n");
} catch {
label.title = metadata;
}
};
this.updateMetadataCallbacks.push(updateMetadata);
updateMetadata();
}

// Add children
Expand Down
2 changes: 1 addition & 1 deletion src/hub/dataSources/wpilog/WPILOGDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class WPILOGDecoderRecord {
return this.timestamp;
}

/** Returns true if the record is a start control record. */
/** Returns true if the record is a control record. */
isControl(): boolean {
return this.entry === CONTROL_ENTRY;
}
Expand Down
6 changes: 6 additions & 0 deletions src/hub/dataSources/wpilog/wpilogWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ self.onmessage = (event) => {
break;
}
log.setWpilibType(startData.name, startData.type);
log.setWpilibMetadata(startData.name, startData.metadata);
} else if (record.isSetMetadata()) {
let setMetadataData = record.getSetMetadataData();
if (setMetadataData.entry in entryIds) {
log.setWpilibMetadata(entryIds[setMetadataData.entry], setMetadataData.metadata);
}
}
} else {
let key = entryIds[record.getEntry()];
Expand Down
12 changes: 11 additions & 1 deletion src/hub/exportWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function generateWPILOG(log: Log, fields: string[], progress: (progress: number)
let fieldData = log.getRange(field, -Infinity, Infinity);
let fieldType = log.getType(field);
let wpilibType = log.getWpilibType(field);
let wpilibMetadata = log.getWpilibMetadata(field);
if (fieldData === undefined || fieldType === undefined) return;

// Start record
Expand Down Expand Up @@ -193,12 +194,21 @@ function generateWPILOG(log: Log, fields: string[], progress: (progress: number)
typeStr = "int64[]";
}
}
if (wpilibMetadata === "") {
wpilibMetadata = JSON.stringify({ exportSource: "AdvantageScope" });
} else {
try {
let wpilibMetadataParsed = JSON.parse(wpilibMetadata);
wpilibMetadataParsed.exportSource = "AdvantageScope";
wpilibMetadata = JSON.stringify(wpilibMetadataParsed);
} catch {}
}
encoder.add(
WPILOGEncoderRecord.makeControlStart(0, {
entry: entryId, // Entry 0 is reserved
name: field,
type: typeStr,
metadata: ""
metadata: wpilibMetadata
})
);

Expand Down
16 changes: 16 additions & 0 deletions src/shared/log/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ export default class Log {
}
}

/** Returns the WPILib metadata string for a field. */
getWpilibMetadata(key: string): string {
if (key in this.fields) {
return this.fields[key].wpilibMetadata;
} else {
return "";
}
}

/** Sets the WPILib metadata string for a field. */
setWpilibMetadata(key: string, type: string) {
if (key in this.fields) {
this.fields[key].wpilibMetadata = type;
}
}

/** Returns whether the key is generated. */
isGenerated(key: string) {
let parentKeys = Array.from(this.generatedParents);
Expand Down
3 changes: 3 additions & 0 deletions src/shared/log/LogField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class LogField {
private data: LogValueSetAny = { timestamps: [], values: [] };
public structuredType: string | null = null;
public wpilibType: string | null = null; // Original type from WPILOG & NT4
public wpilibMetadata = "";

// Toggles when first value is removed, useful for creating striping effects that persist as data is updated
private stripingReference = false;
Expand Down Expand Up @@ -194,6 +195,7 @@ export default class LogField {
values: this.data.values,
structuredType: this.structuredType,
wpilibType: this.wpilibType,
wpilibMetadata: this.wpilibMetadata,
stripingReference: this.stripingReference
};
}
Expand All @@ -207,6 +209,7 @@ export default class LogField {
};
field.structuredType = serializedData.structuredType;
field.wpilibType = serializedData.wpilibType;
field.wpilibMetadata = serializedData.wpilibMetadata;
field.stripingReference = serializedData.stripingReference;
return field;
}
Expand Down

0 comments on commit 6edb70f

Please sign in to comment.