Skip to content

Commit 02c1263

Browse files
committed
feat: Add normalized global metrics support
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent e26c952 commit 02c1263

3 files changed

Lines changed: 131 additions & 21 deletions

File tree

packages/comms/index.html

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
margin-top: 50px;
1717
}
1818

19-
#placeholder {
19+
.placeholder {
2020
width: 100%;
21-
height: 500px;
21+
height: 640px;
2222
background-color: #fff;
23-
margin-top: 20px;
23+
margin: 20px auto;
24+
padding: 20px;
25+
box-sizing: border-box;
26+
overflow: auto;
27+
white-space: pre-wrap;
28+
font-family: 'Courier New', monospace;
29+
font-size: 12px;
30+
line-height: 1;
31+
border: 1px solid #ddd;
32+
border-radius: 4px;
2433
}
2534
</style>
2635

@@ -29,25 +38,42 @@
2938

3039
<body>
3140
<h1>ESM Quick Test</h1>
32-
<div id="placeholder"></div>
41+
<div id="wuPlaceholder" class="placeholder"></div>
3342
<script type="module">
34-
import { Workunit } from "./src/index.browser.ts";
35-
36-
Workunit.submit({ baseUrl: "http://localhost:8010" }, "hthor", "'Hello and Welcome!';")
37-
.then((wu) => {
38-
return wu.watchUntilComplete();
39-
}).then((wu) => {
40-
return wu.fetchResults().then((results) => {
41-
return results[0].fetchRows();
42-
}).then((rows) => {
43-
return wu;
44-
});
45-
}).then((wu) => {
46-
return wu.delete().then(() => wu);
47-
}).then(wu => {
48-
}).catch((e) => {
49-
console.error(e);
50-
});
43+
import { testWUDetailsMeta } from "./tests/index.ts";
44+
45+
testWUDetailsMeta("wuPlaceholder");
46+
</script>
47+
48+
<div id="placeholder" class="placeholder"></div>
49+
<script type="module">
50+
import { SMCService } from "./src/index.browser.ts";
51+
52+
const service = new SMCService({ baseUrl: "http://localhost:8010" });
53+
const placeholder = document.getElementById("placeholder");
54+
55+
const end = new Date();
56+
const start = new Date(end);
57+
start.setUTCMonth(start.getUTCMonth() - 1);
58+
start.setUTCHours(0, 0, 0, 0);
59+
60+
const request = {
61+
DateTimeRange: {
62+
Start: start.toISOString(),
63+
End: end.toISOString()
64+
}
65+
};
66+
service.GetNormalisedGlobalMetrics(request).then(response => {
67+
console.log("GetNormalisedGlobalMetrics Response:", response);
68+
if (placeholder) {
69+
placeholder.textContent = JSON.stringify(response, null, 2);
70+
}
71+
}).catch(err => {
72+
console.error("Error calling GetNormalisedGlobalMetrics:", err);
73+
if (placeholder) {
74+
placeholder.textContent = JSON.stringify(err, null, 2);
75+
}
76+
});
5177
</script>
5278
</body>
5379

packages/comms/src/services/wsSMC.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { SMCServiceBase, WsSMC } from "./wsdl/WsSMC/v1.28/WsSMC.ts";
22
import { IOptions } from "../connection.ts";
3+
import { timeParse } from "d3-time-format";
34

45
export {
56
WsSMC
67
};
78

9+
const dateParser = timeParse("%Y%m%d%H");
10+
11+
interface NormalisedGlobalMetric {
12+
Category: string;
13+
Start: Date;
14+
End: Date;
15+
dimensions: { [key: string]: any };
16+
stats: { [key: string]: any };
17+
}
18+
819
export class SMCService extends SMCServiceBase {
920

1021
connectionOptions(): IOptions {
@@ -21,4 +32,45 @@ export class SMCService extends SMCServiceBase {
2132
};
2233
});
2334
}
35+
36+
protected parseGlobalMetric(name: string, value: any): any {
37+
// Known Prefixes: Cost, Critical, Definition, Disk, Distribute, Ecl, Enum, Id, Interface, Is, Library, Load, Match, Meta, Num, Original, Output, Patch, Per, Persist, Predicted, Record, Section, Service, Signed, Size, Source, Spill, Target, Time, Updated, When
38+
if (name.startsWith("Cost")) {
39+
return +value / 1000000;
40+
} else if (name.startsWith("Date")) {
41+
return dateParser(value);
42+
} else if (name.startsWith("Num")) {
43+
return +value;
44+
} else if (name.startsWith("Time")) {
45+
return +value / 1000000000;
46+
} else if (name.startsWith("When")) {
47+
return new Date(+value / 1000).toISOString();
48+
} else if (!isNaN(value)) {
49+
return +value;
50+
}
51+
return value;
52+
}
53+
54+
GetNormalisedGlobalMetrics(request: Partial<WsSMC.GetGlobalMetrics>): Promise<NormalisedGlobalMetric[]> {
55+
return super.GetGlobalMetrics(request).then(response => {
56+
const retVal: NormalisedGlobalMetric[] = [];
57+
for (const metric of response?.GlobalMetrics?.GlobalMetric || []) {
58+
const row: NormalisedGlobalMetric = {
59+
Category: metric.Category,
60+
Start: this.parseGlobalMetric("Date", metric.DateTimeRange?.Start),
61+
End: this.parseGlobalMetric("Date", metric.DateTimeRange?.End),
62+
dimensions: {},
63+
stats: {}
64+
};
65+
for (const dimension of metric.Dimensions?.Dimension || []) {
66+
row["dimensions"][dimension.Name] = dimension.Value;
67+
}
68+
for (const stat of metric.Stats?.Stat || []) {
69+
row["stats"][stat.Name] = this.parseGlobalMetric(stat.Name, stat.Value);
70+
}
71+
retVal.push(row);
72+
}
73+
return retVal;
74+
});
75+
}
2476
}

packages/comms/tests/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { SMCService, WorkunitsService } from "../src/index.browser.ts";
2+
3+
export function testWUDetailsMeta(placeholder: string) {
4+
5+
const wuService = new WorkunitsService({ baseUrl: "http://localhost:8010" });
6+
const wuPlaceholder = document.getElementById(placeholder);
7+
wuService.WUDetailsMeta({}).then(response => {
8+
const prefixSet = new Set<string>();
9+
response?.Properties?.Property.forEach(field => {
10+
if (field.Name) {
11+
// Find the first capital letter after the first character
12+
for (let i = 1; i < field.Name.length; i++) {
13+
if (field.Name[i] === field.Name[i].toUpperCase() && field.Name[i] !== field.Name[i].toLowerCase()) {
14+
prefixSet.add(field.Name.substring(0, i));
15+
break;
16+
}
17+
}
18+
}
19+
});
20+
console.info("WUDetailsMeta Response:", response);
21+
console.info("Prefixes:", Array.from(prefixSet).sort());
22+
if (wuPlaceholder) {
23+
wuPlaceholder.textContent = JSON.stringify(Array.from(prefixSet).sort(), null, 2) + "\n\n" + JSON.stringify(response, null, 2);
24+
}
25+
}).catch(err => {
26+
console.error("Error calling WUDetailsMeta:", err);
27+
if (wuPlaceholder) {
28+
wuPlaceholder.textContent = JSON.stringify(err, null, 2);
29+
}
30+
});
31+
32+
}

0 commit comments

Comments
 (0)