Skip to content

Commit 0464864

Browse files
authored
array data source grouping I (#731)
1 parent ff1455b commit 0464864

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

vuu-ui/packages/vuu-data/src/array-data-source/array-data-source.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import {
4040
VuuUIMessageInRPCEditSuccess,
4141
} from "../vuuUIMessageTypes";
4242

43+
import { groupRows } from "./group-utils";
44+
4345
export interface ArrayDataSourceConstructorProps
4446
extends Omit<DataSourceConstructorProps, "bufferSize" | "table"> {
4547
columnDescriptors: ColumnDescriptor[];
@@ -96,6 +98,7 @@ export class ArrayDataSource
9698
private status = "initialising";
9799
private disabled = false;
98100
private filteredData: undefined | DataSourceRow[];
101+
private groupedData: undefined | DataSourceRow[];
99102
private suspended = false;
100103
private clientCallback: SubscribeCallback | undefined;
101104
private tableMeta: VuuTableMeta;
@@ -312,7 +315,7 @@ export class ArrayDataSource
312315
this.rangeChangeRowset === "delta" && !forceFullRefresh
313316
? rangeNewItems(this.lastRangeServed, this.#range)
314317
: this.#range;
315-
const data = this.filteredData ?? this.#data;
318+
const data = this.groupedData ?? this.filteredData ?? this.#data;
316319
this.clientCallback?.({
317320
clientViewportId: this.viewport,
318321
rows: data
@@ -364,11 +367,6 @@ export class ArrayDataSource
364367
debug?.(`filter ${JSON.stringify(filter)}`);
365368
// TODO should we wait until server ACK before we assign #sort ?
366369

367-
this.#config = {
368-
...this.#config,
369-
filter,
370-
};
371-
372370
this.#config = {
373371
...this.#config,
374372
filter,
@@ -395,7 +393,28 @@ export class ArrayDataSource
395393
}
396394

397395
set groupBy(groupBy: VuuGroupBy) {
398-
// this.#config.groupBy = groupBy;
396+
this.#config = {
397+
...this.#config,
398+
groupBy,
399+
};
400+
401+
if (groupBy.length) {
402+
console.time("group");
403+
this.groupedData = groupRows(this.#data, groupBy, this.#columnMap).map(
404+
(row, i) => {
405+
const dolly = row.slice() as DataSourceRow;
406+
dolly[0] = i;
407+
dolly[1] = i;
408+
return dolly;
409+
}
410+
);
411+
console.timeEnd("group");
412+
} else {
413+
this.groupedData = undefined;
414+
}
415+
this.setRange(resetRange(this.#range), true);
416+
417+
this.emit("config", this.#config);
399418
}
400419

401420
get title() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { VuuGroupBy } from "@finos/vuu-protocol-types";
2+
import { ColumnMap } from "@finos/vuu-utils";
3+
import { DataSourceRow } from "../data-source";
4+
5+
type RowSortComparator = (
6+
item1: DataSourceRow,
7+
item2: DataSourceRow
8+
) => 0 | -1 | 1;
9+
10+
const sortComparator =
11+
(idx: number): RowSortComparator =>
12+
(row1, row2) => {
13+
const v1 = row1[idx];
14+
const v2 = row2[idx];
15+
return v1 > v2 ? 1 : v2 > v1 ? -1 : 0;
16+
};
17+
18+
export const groupRows = (
19+
rows: readonly DataSourceRow[],
20+
groupBy: VuuGroupBy,
21+
columnMap: ColumnMap
22+
): DataSourceRow[] => {
23+
const sortIndices = groupBy.map<number>((colName) => columnMap[colName]);
24+
// 1) sort the data
25+
const sortedRows = rows.slice().sort(sortComparator(sortIndices[0]));
26+
27+
// 2) collapse int groups
28+
29+
return sortedRows;
30+
};

vuu-ui/showcase/scripts/showcase.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { execWait } from "../../scripts/utils.mjs";
66

77
fs.copyFileSync("./templates/index.html", "./index.html");
88

9-
execWait("yarn vite");
9+
// execWait("yarn vite");
10+
execWait("node ../node_modules/.bin/vite");
1011

1112
console.log(`opening showcase at ${chalk.green("http://127.0.0.1:5173/")} ...`);
1213

0 commit comments

Comments
 (0)