Skip to content

Commit 4a62d30

Browse files
committed
fix: optimize WUDetails call
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 68feefd commit 4a62d30

3 files changed

Lines changed: 85 additions & 52 deletions

File tree

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/comms/src/ecl/query.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,31 +172,43 @@ export class Query extends StateObject<QueryEx, QueryEx> implements QueryEx {
172172
const meta = promises[1];
173173
const metrics: WsWorkunits.Scope[] = promises[2];
174174
const data = metrics.map(metric => {
175-
if (metric.Id[0] === "a" || metric.Id[0] === "e") {
175+
const firstChar = metric.Id[0];
176+
if (firstChar === "a" || firstChar === "e") {
176177
const item = graph.idx[metric.Id.substring(1)];
178+
if (!item) return metric;
179+
const existingProperties = new Set(
180+
metric.Properties.Property.map(prop => prop.Name)
181+
);
182+
const newProperties: WsWorkunits.Property[] = [];
177183
for (const key in item) {
178-
if (key.charAt(0) !== "_" && key.charAt(0) === key.charAt(0).toUpperCase() && (typeof item[key] === "string" || typeof item[key] === "number" || typeof item[key] === "boolean")) {
179-
180-
if (!metric.Properties.Property.some(row => row.Name === key)) {
181-
const isNum = isNumber(item[key]);
182-
let rawValue = isNum ? parseFloat(item[key] as string) : item[key];
183-
let formatted = item[key];
184+
const firstCharOfKey = key.charAt(0);
185+
if (firstCharOfKey !== "_" &&
186+
firstCharOfKey === firstCharOfKey.toUpperCase() &&
187+
!existingProperties.has(key)) {
188+
const value = item[key];
189+
const valueType = typeof value;
190+
if (valueType === "string" || valueType === "number" || valueType === "boolean") {
191+
const isNum = isNumber(value);
192+
let rawValue = isNum ? parseFloat(value as string) : value;
193+
let formatted = value;
184194
if (key.indexOf("Time") >= 0) {
185-
rawValue = rawValue as number / 1000000000;
195+
rawValue = (rawValue as number) / 1000000000;
186196
formatted = siFormatter(rawValue) + "s";
187197
}
188-
metric.Properties.Property.push({
198+
newProperties.push({
189199
Name: key,
190200
RawValue: rawValue as any,
191201
Formatted: formatted
192202
} as WsWorkunits.Property);
193203
}
194204
}
195205
}
206+
if (newProperties.length > 0) {
207+
metric.Properties.Property.push(...newProperties);
208+
}
196209
}
197210
return metric;
198211
});
199-
200212
return wu.normalizeDetails(meta, data);
201213
});
202214
}

packages/comms/src/ecl/workunit.ts

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -675,50 +675,61 @@ export class Workunit extends StateObject<UWorkunitState, IWorkunitState> implem
675675
Measure: "label"
676676
}
677677
};
678-
const data: IScope[] = [];
679-
for (const scope of scopes) {
680-
const props = {};
681-
const formattedProps = {};
682-
if (scope && scope.Id && scope.Properties && scope.Properties.Property) {
683-
for (const key in scope.Properties.Property) {
684-
const scopeProperty = scope.Properties.Property[key];
685-
if (scopeProperty.Measure === "ns") {
678+
const activityMap = new Map<number, string>();
679+
for (const activity of meta.Activities?.Activity ?? []) {
680+
activityMap.set(activity.Kind, activity.Name);
681+
}
682+
const data: IScope[] = new Array(scopes.length);
683+
for (let i = 0; i < scopes.length; i++) {
684+
const scope = scopes[i];
685+
const props: { [key: string]: any } = {};
686+
const formattedProps: { [key: string]: any } = {};
687+
if (scope?.Id && scope.Properties?.Property) {
688+
for (const scopeProperty of scope.Properties.Property) {
689+
const measure = scopeProperty.Measure;
690+
const name = scopeProperty.Name;
691+
const rawValue = scopeProperty.RawValue;
692+
if (measure === "ns") {
686693
scopeProperty.Measure = "s";
687694
}
688-
if (scopeProperty.Name === "Kind") {
689-
const rawValue = parseInt(scopeProperty.RawValue, 10);
690-
scopeProperty.Formatted = meta.Activities.Activity.filter(a => a.Kind === rawValue)[0].Name ?? scopeProperty.RawValue;
695+
if (name === "Kind") {
696+
const rawValueInt = parseInt(rawValue, 10);
697+
scopeProperty.Formatted = activityMap.get(rawValueInt) ?? rawValue;
691698
}
692-
columns[scopeProperty.Name] = { ...scopeProperty };
693-
safeDelete(columns, scopeProperty.Name, "RawValue");
694-
safeDelete(columns, scopeProperty.Name, "Formatted");
699+
columns[name] = {
700+
Name: scopeProperty.Name,
701+
Measure: scopeProperty.Measure,
702+
Creator: scopeProperty.Creator,
703+
CreatorType: scopeProperty.CreatorType
704+
};
705+
const numericRawValue = +rawValue;
695706
switch (scopeProperty.Measure) {
696707
case "bool":
697-
props[scopeProperty.Name] = !!+scopeProperty.RawValue;
708+
props[name] = !!numericRawValue;
698709
break;
699710
case "sz":
700-
props[scopeProperty.Name] = +scopeProperty.RawValue;
711+
props[name] = numericRawValue;
701712
break;
702713
case "s":
703-
props[scopeProperty.Name] = +scopeProperty.RawValue / 1000000000;
714+
props[scopeProperty.Name] = numericRawValue / 1000000000;
704715
break;
705716
case "ns":
706-
props[scopeProperty.Name] = +scopeProperty.RawValue;
717+
props[name] = numericRawValue;
707718
break;
708719
case "ts":
709-
props[scopeProperty.Name] = new Date(+scopeProperty.RawValue / 1000).toISOString();
720+
props[scopeProperty.Name] = new Date(numericRawValue / 1000).toISOString();
710721
break;
711722
case "cnt":
712-
props[scopeProperty.Name] = +scopeProperty.RawValue;
723+
props[name] = numericRawValue;
713724
break;
714725
case "cost":
715-
props[scopeProperty.Name] = +scopeProperty.RawValue / 1000000;
726+
props[scopeProperty.Name] = numericRawValue / 1000000;
716727
break;
717728
case "node":
718-
props[scopeProperty.Name] = +scopeProperty.RawValue;
729+
props[name] = numericRawValue;
719730
break;
720731
case "skw":
721-
props[scopeProperty.Name] = +scopeProperty.RawValue;
732+
props[name] = numericRawValue;
722733
break;
723734
case "cpu":
724735
case "ppm":
@@ -729,11 +740,10 @@ export class Workunit extends StateObject<UWorkunitState, IWorkunitState> implem
729740
case "id":
730741
case "fname":
731742
default:
732-
props[scopeProperty.Name] = scopeProperty.RawValue;
743+
props[name] = rawValue;
733744
}
734-
formattedProps[scopeProperty.Name] = formatNum(scopeProperty.Formatted ?? props[scopeProperty.Name]);
745+
formattedProps[name] = formatNum(scopeProperty.Formatted ?? props[name]);
735746
}
736-
// Other properties ---
737747
}
738748
const normalizedScope: IScope = {
739749
id: scope.Id,
@@ -748,37 +758,47 @@ export class Workunit extends StateObject<UWorkunitState, IWorkunitState> implem
748758
__StdDevsSource: "",
749759
...props
750760
};
751-
if (normalizedScope[DEFINITION_LIST]) {
761+
const definitionList = normalizedScope[DEFINITION_LIST];
762+
if (definitionList) {
752763
try {
753-
const definitionList = JSON.parse(normalizedScope[DEFINITION_LIST].split("\\").join("\\\\"));
754-
normalizedScope[DEFINITION_LIST] = [];
755-
definitionList.forEach((definition, idx) => {
756-
const matches = definition.match(definitionRegex);
764+
const parsedList = JSON.parse(definitionList.split("\\").join("\\\\"));
765+
const processedDefinitions: Array<{ filePath: string, line: number, col: number }> = [];
766+
767+
for (let k = 0; k < parsedList.length; k++) {
768+
const matches = parsedList[k].match(definitionRegex);
757769
if (matches) {
758-
const filePath = (matches[1] ?? "") + matches[2] + matches[3];
759-
const line = parseInt(matches[5]);
760-
const col = parseInt(matches[6]);
761-
normalizedScope[DEFINITION_LIST].push({ filePath, line, col });
770+
processedDefinitions.push({
771+
filePath: (matches[1] ?? "") + matches[2] + matches[3],
772+
line: parseInt(matches[5], 10),
773+
col: parseInt(matches[6], 10)
774+
});
762775
}
763-
});
776+
}
777+
normalizedScope[DEFINITION_LIST] = processedDefinitions;
764778
} catch (e) {
765-
logger.error(`Unexpected "DefinitionList": ${normalizedScope[DEFINITION_LIST]}`);
779+
logger.error(`Unexpected "DefinitionList": ${definitionList}`);
766780
}
767781
}
782+
768783
const dedup: DedupProperties = {};
784+
let maxStdDevs = 0;
785+
let maxStdDevsSource = "";
769786
for (const key in normalizedScope) {
770-
if (key.indexOf("__") !== 0) {
787+
if (!key.startsWith("__")) {
771788
const row = formatValues(normalizedScope, key, dedup);
772789
if (row) {
773790
normalizedScope.__groupedProps[row.Key] = row;
774-
if (!isNaN(row.StdDevs) && normalizedScope.__StdDevs < row.StdDevs) {
775-
normalizedScope.__StdDevs = row.StdDevs;
776-
normalizedScope.__StdDevsSource = row.Key;
791+
if (!isNaN(row.StdDevs) && row.StdDevs > maxStdDevs) {
792+
maxStdDevs = row.StdDevs;
793+
maxStdDevsSource = row.Key;
777794
}
778795
}
779796
}
780797
}
781-
data.push(normalizedScope);
798+
normalizedScope.__StdDevs = maxStdDevs;
799+
normalizedScope.__StdDevsSource = maxStdDevsSource;
800+
801+
data[i] = normalizedScope;
782802
}
783803
return {
784804
meta,

0 commit comments

Comments
 (0)