forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_v0.ts
130 lines (116 loc) · 3.34 KB
/
csv_v0.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
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import papa from 'papaparse';
import { browser } from '$app/environment';
import { readFileFromGCS } from '$lib/utils';
import type { BaseData, GCSBaseData, DGNodeInterface } from '$lib/node_data_types';
export interface CSVNodeInterface extends DGNodeInterface<GCSBaseData> {}
interface CSVData extends BaseData {
filename: string;
size_kb: number;
gcs_path: string;
}
export default class CSVNode {
id: string;
data: CSVData;
position: { x: number; y: number };
type: string;
constructor(node_data: CSVNodeInterface) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
// these are the arguments that are passed to the compute function
async compute(
inputData: object,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
if (!this.data.dirty && this.data.output && this.data.output.length > 0) {
return this.data.output;
}
let contents;
if (this.data.gcs_path) {
// info('Loading ' + this.data.gcs_path.split('/').pop());
contents = await readFileFromGCS(this);
let parsedData: any = '';
if (browser) parsedData = this.paparseCSV(contents);
else parsedData = await this.csvParser(contents);
this.data.output = this.filterValidRows(parsedData);
}
this.data.dirty = false;
return this.data.output;
}
async csvParser(csvString: string): Promise<any[]> {
const csvModule = await import('csv-parser');
const csv = csvModule.default;
const streamModule = await import('stream');
const { Readable } = streamModule;
return new Promise((resolve, reject) => {
const results: any = [];
Readable.from(csvString)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => resolve(results))
.on('error', (err) => reject(err));
});
}
paparseCSV(csv: string) {
const parsedData = papa.parse(csv, { header: true }).data;
return parsedData;
}
filterValidRows(parsedData: any[]) {
const validRows = [];
for (const row of parsedData) {
if (this.isValidRow(row)) {
validRows.push(row);
}
}
return validRows;
}
isValidRow(row: object) {
for (const column in row) {
// @ts-ignore
row[column] = row[column].trim();
}
let allEmpty = true;
for (const column in row) {
// @ts-ignore
if (row[column] !== '') {
allEmpty = false;
break;
}
}
return !allEmpty;
}
}
// This data matters, as it is used for the tests
// please make sure it remains available
export const csv_node_data: CSVNodeInterface = {
id: 'csv',
data: {
label: 'csv',
filename: '',
size_kb: 0,
dirty: false,
gcs_path: '',
compute_type: 'csv_v0',
input_ids: {},
category: categories.input.id,
icon: 'csv_v0',
show_in_ui: true,
message: ''
},
position: { x: 0, y: 0 },
type: 'csv_v0'
};
// This is the node itself. It is created here as other parts of the
// code imports it for use in the system. Please keep it as is.
export const csv_node = new CSVNode(csv_node_data);
nodes.register(CSVNode, csv_node_data);