/
aggregators.ts
200 lines (166 loc) · 6.18 KB
/
aggregators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { formatNumber, htmlEncode, tryGetText } from "../base";
import { Column, GroupTotals, NonDataRow, convertCompatFormatter, escapeHtml } from "@serenity-is/sleekgrid";
export { };
export namespace Aggregators
{
export function Avg(field: string): void {
this.field_ = field;
this.type_ = "Avg";
this.init = function () {
this.count_ = 0;
this.nonNullCount_ = 0;
this.sum_ = 0;
};
this.accumulate = function (item: any) {
var val = item[this.field_];
this.count_++;
if (val != null && val !== "" && !isNaN(val)) {
this.nonNullCount_++;
this.sum_ += parseFloat(val);
}
};
this.storeResult = function (groupTotals: any) {
if (!groupTotals.avg) {
groupTotals.avg = {};
}
if (this.nonNullCount_ != 0) {
groupTotals.avg[this.field_] = this.sum_ / this.nonNullCount_;
}
};
}
export function WeightedAvg(field: string, weightedField: string) {
this.field_ = field;
this.type_ = "WeightedAvg";
this.weightedField_ = weightedField;
this.init = function () {
this.sum_ = 0;
this.weightedSum_ = 0;
};
this.accumulate = function (item: any) {
var val = item[this.field_];
var valWeighted = item[this.weightedField_];
if (this.isValid(val) && this.isValid(valWeighted)) {
this.weightedSum_ += parseFloat(valWeighted);
this.sum_ += parseFloat(val) * parseFloat(valWeighted);
}
};
this.storeResult = function (groupTotals: any) {
if (!groupTotals.avg) {
groupTotals.avg = {};
}
if (this.sum_ && this.weightedSum_) {
groupTotals.avg[this.field_] = this.sum_ / this.weightedSum_;
}
};
this.isValid = function (val: any) {
return val !== null && val !== "" && !isNaN(val);
};
}
export function Min(field: string): void {
this.field_ = field;
this.type_ = "Min";
this.init = function () {
this.min_ = null;
};
this.accumulate = function (item: any) {
var val = item[this.field_];
if (val != null && val !== "" && !isNaN(val)) {
if (this.min_ == null || val < this.min_) {
this.min_ = val;
}
}
};
this.storeResult = function (groupTotals: any) {
if (!groupTotals.min) {
groupTotals.min = {};
}
groupTotals.min[this.field_] = this.min_;
}
}
export function Max(field: string): void {
this.field_ = field;
this.type_ = "Max";
this.init = function () {
this.max_ = null;
};
this.accumulate = function (item: any) {
var val = item[this.field_];
if (val != null && val !== "" && !isNaN(val)) {
if (this.max_ == null || val > this.max_) {
this.max_ = val;
}
}
};
this.storeResult = function (groupTotals: any) {
if (!groupTotals.max) {
groupTotals.max = {};
}
groupTotals.max[this.field_] = this.max_;
}
}
export function Sum(field: string): void {
this.field_ = field;
this.type_ = "Sum";
this.init = function () {
this.sum_ = null;
};
this.accumulate = function (item: any) {
var val = item[this.field_];
if (val != null && val !== "" && !isNaN(val)) {
this.sum_ += parseFloat(val);
}
};
this.storeResult = function (groupTotals: any) {
if (!groupTotals.sum) {
groupTotals.sum = {};
}
groupTotals.sum[this.field_] = this.sum_;
}
}
}
export namespace AggregateFormatting {
export function formatMarkup<TItem = any>(totals: GroupTotals, column: Column<TItem>, aggType: string): string {
var textKey = (aggType.substring(0, 1).toUpperCase() + aggType.substring(1));
var text = tryGetText("Enums.Serenity.SummaryType." + textKey) ?? textKey;
var value = (totals as any)[aggType][column.field];
var formattedValue = formatValue(column, value);
return "<span class='aggregate agg-" + aggType + "' title='" + htmlEncode(text) + "'>" +
formattedValue +
"</span>";
}
export function formatValue(column: Column, value: number): string {
var formatter = column.format ?? (column.formatter ? convertCompatFormatter(column.formatter) : null);
if (formatter != null) {
var item = new NonDataRow();
(item as any)[column.field] = value;
try {
var result = formatter({ column, escape: escapeHtml, item, value });
if (result instanceof Element)
return result.outerHTML;
else if (result instanceof DocumentFragment)
return Array.from((result as any as DocumentFragment).children).map(x => x.outerHTML).join("")
return result;
}
catch (e) {
}
}
if (typeof value === "number") {
var displayFormat = column.sourceItem?.displayFormat ?? "#,##0.##";
return htmlEncode(formatNumber(value, displayFormat));
}
else
return htmlEncode(value);
}
export function groupTotalsFormatter<TItem = any>(totals: GroupTotals, column: Column<TItem>): string {
if (!totals || !column)
return "";
var text: string = null;
["sum", "avg", "min", "max", "cnt"].forEach(function (aggType) {
if (text == null && (totals as any)[aggType] && (totals as any)[aggType][column.field] != null) {
text = formatMarkup(totals, column, aggType);
return false;
}
});
return text || "";
}
}