-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
dataset-schema.ts
181 lines (159 loc) · 4.65 KB
/
dataset-schema.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
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import pick from 'lodash.pick';
import {console as globalConsole} from 'global/window';
import {ProtoDataset} from 'actions';
import {RGBColor} from 'types';
import {VERSIONS} from './versions';
import Schema from './schema';
import {getFieldsFromData, getSampleForTypeAnalyze} from '@kepler.gl/processors';
export type SavedField = {
name: string;
type: string;
format?: string;
analyzerType?: string;
};
export type ParsedField = {
name: string;
type: string;
format: string;
analyzerType: string;
};
export type SavedDatasetV1 = {
version: 'v1';
data: {
id: string;
label: string;
color: RGBColor;
allData: any[][];
fields: SavedField[];
};
};
export type ParsedDataset = {
data: {
fields: ParsedField[];
rows: any[][];
};
info: {
id?: string;
label?: string;
color?: RGBColor;
};
};
// version v0
export const fieldPropertiesV0 = {
name: null,
type: null
};
export const fieldPropertiesV1 = {
name: null,
type: null,
format: null,
analyzerType: null,
metadata: null
};
export class FieldSchema extends Schema {
save(fields) {
return {
[this.key]: fields.map(f => this.savePropertiesOrApplySchema(f)[this.key])
};
}
load(fields) {
return {[this.key]: fields};
}
}
export const propertiesV0 = {
id: null,
label: null,
color: null,
allData: null,
fields: new FieldSchema({
key: 'fields',
version: VERSIONS.v0,
properties: fieldPropertiesV0
})
};
export const propertiesV1 = {
...propertiesV0,
fields: new FieldSchema({
key: 'fields',
version: VERSIONS.v1,
properties: fieldPropertiesV1
})
};
export class DatasetSchema extends Schema {
key = 'dataset';
save(dataset): SavedDatasetV1['data'] {
const datasetFlattened = dataset.dataContainer
? {
...dataset,
allData: dataset.dataContainer.flattenData()
}
: dataset;
return this.savePropertiesOrApplySchema(datasetFlattened)[this.key];
}
load(dataset: SavedDatasetV1['data']): ProtoDataset {
const {fields, allData} = dataset;
let updatedFields = fields;
// recalculate field type
// because we have updated type-analyzer
// we need to add format to each field
const needCalculateMeta =
fields[0] &&
(!fields[0].hasOwnProperty('format') || !fields[0].hasOwnProperty('analyzerType'));
if (needCalculateMeta) {
const fieldOrder = fields.map(f => f.name);
const sampleData = getSampleForTypeAnalyze({
fields: fieldOrder,
rows: allData
});
const meta = getFieldsFromData(sampleData, fieldOrder);
updatedFields = meta.map((f, i) => ({
...pick(meta[i], ['name', 'type', 'format']),
analyzerType: meta[i].analyzerType
}));
updatedFields.forEach((f, i) => {
if (fields[i].type !== f.type) {
// if newly detected field type is different from saved type
// we log it but won't update it, cause we don't want to break people's map
globalConsole.warn(`detect ${f.name} type is now ${f.type} instead of ${fields[i].type}`);
}
});
}
// get format of all fields
return {
data: {fields: updatedFields, rows: dataset.allData},
info: pick(dataset, ['id', 'label', 'color'])
};
}
}
export const datasetSchema = {
[VERSIONS.v0]: new DatasetSchema({
key: 'dataset',
version: VERSIONS.v0,
properties: propertiesV0
}),
[VERSIONS.v1]: new DatasetSchema({
key: 'dataset',
version: VERSIONS.v1,
properties: propertiesV1
})
};
export default datasetSchema;