| 1 |
import { sum } from "d3-array"; |
| 2 |
|
| 3 |
import { Amount } from "../../entries"; |
| 4 |
import { Position } from "../../entries/position"; |
| 5 |
import { collect } from "../../lib/result"; |
| 6 |
import type { ValidationT, Validator } from "../../lib/validation"; |
| 7 |
import { |
| 8 |
array, |
| 9 |
boolean, |
| 10 |
constant, |
| 11 |
constants, |
| 12 |
date, |
| 13 |
defaultValue, |
| 14 |
number, |
| 15 |
object, |
| 16 |
optional, |
| 17 |
record, |
| 18 |
string, |
| 19 |
tagged_union, |
| 20 |
unknown, |
| 21 |
} from "../../lib/validation"; |
| 22 |
import { NumberColumn, StringColumn } from "../../sort"; |
| 23 |
|
| 24 |
const query_column_type = constants( |
| 25 |
"bool", |
| 26 |
"date", |
| 27 |
"int", |
| 28 |
"object", |
| 29 |
"set", |
| 30 |
"str", |
| 31 |
"Amount", |
| 32 |
"Decimal", |
| 33 |
"Inventory", |
| 34 |
"Position", |
| 35 |
); |
| 36 |
|
| 37 |
/** The query result data columns can have one of these various types. */ |
| 38 |
type QueryColumnType = ValidationT<typeof query_column_type>; |
| 39 |
|
| 40 |
interface QueryType { |
| 41 |
dtype: QueryColumnType; |
| 42 |
name: string; |
| 43 |
} |
| 44 |
|
| 45 |
const query_type = object({ |
| 46 |
dtype: query_column_type, |
| 47 |
name: string, |
| 48 |
}); |
| 49 |
|
| 50 |
const query_table_raw = object({ |
| 51 |
types: array(query_type), |
| 52 |
rows: array(array(unknown)), |
| 53 |
}); |
| 54 |
|
| 55 |
const optional_number_record = optional(record(number)); |
| 56 |
|
| 57 |
export class Inventory { |
| 58 |
constructor(readonly value: Record<string, number>) {} |
| 59 |
|
| 60 |
static validator: Validator<Inventory> = (json) => |
| 61 |
optional_number_record(json).map((v) => new Inventory(v ?? {})); |
| 62 |
} |
| 63 |
|
| 64 |
export type QueryCell = |
| 65 |
| null |
| 66 |
| Amount |
| 67 |
| boolean |
| 68 |
| Date |
| 69 |
| number |
| 70 |
| Inventory |
| 71 |
| Position |
| 72 |
| string[] |
| 73 |
| string; |
| 74 |
|
| 75 |
class StringSortedQueryColumn<T> extends StringColumn<QueryCell[]> { |
| 76 |
dtype: QueryColumnType; |
| 77 |
constructor( |
| 78 |
type: QueryType, |
| 79 |
readonly index: number, |
| 80 |
readonly validator: Validator<T>, |
| 81 |
str_value_for_sorting: (v: T) => string, |
| 82 |
) { |
| 83 |
super(type.name, (row) => str_value_for_sorting(row[index] as T)); |
| 84 |
this.dtype = type.dtype; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
class NumberSortedQueryColumn<T> extends NumberColumn<QueryCell[]> { |
| 89 |
dtype: QueryColumnType; |
| 90 |
constructor( |
| 91 |
type: QueryType, |
| 92 |
readonly index: number, |
| 93 |
readonly validator: Validator<T>, |
| 94 |
num_value_for_sorting: (v: T) => number, |
| 95 |
) { |
| 96 |
super(type.name, (row) => num_value_for_sorting(row[index] as T)); |
| 97 |
this.dtype = type.dtype; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function get_query_column(type: QueryType, index: number) { |
| 102 |
switch (type.dtype) { |
| 103 |
case "bool": |
| 104 |
return new StringSortedQueryColumn(type, index, boolean, (v) => |
| 105 |
v.toString(), |
| 106 |
); |
| 107 |
case "date": |
| 108 |
return new NumberSortedQueryColumn(type, index, optional(date), (v) => |
| 109 |
v == null ? 0 : +v, |
| 110 |
); |
| 111 |
case "int": |
| 112 |
case "Decimal": |
| 113 |
return new NumberSortedQueryColumn( |
| 114 |
type, |
| 115 |
index, |
| 116 |
optional(number), |
| 117 |
(v) => v ?? 0, |
| 118 |
); |
| 119 |
case "set": |
| 120 |
return new StringSortedQueryColumn(type, index, array(string), (v) => |
| 121 |
v.join(","), |
| 122 |
); |
| 123 |
case "object": |
| 124 |
case "str": |
| 125 |
return new StringSortedQueryColumn( |
| 126 |
type, |
| 127 |
index, |
| 128 |
defaultValue(string, () => ""), |
| 129 |
(v) => v, |
| 130 |
); |
| 131 |
case "Amount": |
| 132 |
return new NumberSortedQueryColumn( |
| 133 |
type, |
| 134 |
index, |
| 135 |
Amount.validator, |
| 136 |
(v) => v.number, |
| 137 |
); |
| 138 |
case "Inventory": |
| 139 |
return new NumberSortedQueryColumn( |
| 140 |
type, |
| 141 |
index, |
| 142 |
Inventory.validator, |
| 143 |
(v) => sum(Object.values(v.value)), |
| 144 |
); |
| 145 |
case "Position": |
| 146 |
return new NumberSortedQueryColumn( |
| 147 |
type, |
| 148 |
index, |
| 149 |
Position.validator, |
| 150 |
(v) => v.units.number, |
| 151 |
); |
| 152 |
default: |
| 153 |
return type.dtype; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
type QueryColumn = ReturnType<typeof get_query_column>; |
| 158 |
|
| 159 |
/** The parsed and validated result for a query table result. */ |
| 160 |
export interface QueryResultTable { |
| 161 |
readonly t: "table"; |
| 162 |
readonly columns: QueryColumn[]; |
| 163 |
readonly rows: QueryCell[][]; |
| 164 |
} |
| 165 |
|
| 166 |
/** The parsed and validated result for a query text result. */ |
| 167 |
export interface QueryResultText { |
| 168 |
readonly t: "string"; |
| 169 |
readonly contents: string; |
| 170 |
} |
| 171 |
|
| 172 |
export type QueryResult = QueryResultText | QueryResultTable; |
| 173 |
|
| 174 |
export const query_table_validator: Validator<QueryResultTable> = ( |
| 175 |
json: unknown, |
| 176 |
) => |
| 177 |
query_table_raw(json).and_then(({ types, rows }) => { |
| 178 |
const columns = types.map(get_query_column); |
| 179 |
const validators: Validator<QueryCell>[] = columns.map((c) => c.validator); |
| 180 |
|
| 181 |
const parsed_rows = collect( |
| 182 |
rows.map((row) => |
| 183 |
collect(validators.map((validator, index) => validator(row[index]))), |
| 184 |
), |
| 185 |
); |
| 186 |
|
| 187 |
return parsed_rows.map((r) => ({ t: "table", columns, rows: r })); |
| 188 |
}); |
| 189 |
|
| 190 |
export const query_validator: Validator<QueryResult> = tagged_union("t", { |
| 191 |
string: object({ t: constant("string"), contents: string }), |
| 192 |
table: query_table_validator, |
| 193 |
}); |