-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
381 lines (367 loc) · 15.3 KB
/
index.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2022, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const Fs = require('fs');
const {Utf8String, Int32, Uint32, Float32, DataFrame, Series, Float64} = require('@rapidsai/cudf');
const {RecordBatchStreamWriter, Field, Vector, List, Table} = require('apache-arrow');
const Path = require('path');
const {promisify} = require('util');
const Stat = promisify(Fs.stat);
const fastifyCors = require('@fastify/cors');
const fastify = require('fastify');
const arrowPlugin = require('fastify-arrow');
const gpu_cache = require('../../util/gpu_cache.js');
const root_schema = require('../../util/schema.js');
module.exports = async function(fastify, opts) {
fastify.register(arrowPlugin);
fastify.register(fastifyCors, {origin: 'http://localhost:3001'});
fastify.decorate('cacheObject', gpu_cache.cacheObject);
fastify.decorate('getData', gpu_cache.getData);
fastify.decorate('listDataframes', gpu_cache.listDataframes);
fastify.decorate('readGraphology', gpu_cache.readGraphology);
fastify.decorate('readLargeGraphDemo', gpu_cache.readLargeGraphDemo);
fastify.decorate('clearDataFrames', gpu_cache.clearDataframes);
fastify.get('/', async function(request, reply) { return root_schema['graphology']; });
fastify.route({
method: 'POST',
url: '/read_large_demo',
schema: {
querystring: {filename: {type: 'string'}, 'rootkeys': {type: 'array'}},
response: {
200: {
type: 'object',
properties:
{success: {type: 'boolean'}, message: {type: 'string'}, params: {type: 'string'}}
}
}
},
handler: async (request, reply) => {
const query = request.query;
let result = {
'params': JSON.stringify(query),
success: false,
message: 'Finding file.',
statusCode: 200
};
try {
let path = undefined;
if (query.filename === undefined) {
result.message = 'Parameter filename is required';
result.statusCode = 400;
} else if (query.filename.search('http') != -1) {
result.message = 'Remote files not supported yet.';
result.statusCode = 406;
} else {
result.message = 'File is not remote';
// does the file exist?
const path = Path.join(__dirname, query.filename);
console.log('Does the file exist?');
const stats = await Stat(path);
const message = 'File is available';
result.message = message;
result.success = true;
try {
const graphology = await fastify.readLargeGraphDemo(path);
await fastify.cacheObject('nodes', graphology['nodes']);
await fastify.cacheObject('edges', graphology['edges']);
await fastify.cacheObject('options', graphology['options']);
result.message = 'File read onto GPU.';
} catch (e) {
result.success = false;
result.message = e;
result.statusCode = 500;
}
}
} catch (e) {
result.success = false;
result.message = e.message;
result.statusCode = 404;
};
await reply.code(result.statusCode).send(result);
}
});
fastify.route({
method: 'POST',
url: '/read_json',
schema: {
querystring: {filename: {type: 'string'}, 'rootkeys': {type: 'array'}},
response: {
200: {
type: 'object',
properties:
{success: {type: 'boolean'}, message: {type: 'string'}, params: {type: 'string'}}
}
}
},
handler: async (request, reply) => {
const query = request.query;
let result = {
'params': JSON.stringify(query),
success: false,
message: 'Finding file.',
statusCode: 200
};
try {
let path = undefined;
if (query.filename === undefined) {
result.message = 'Parameter filename is required';
result.statusCode = 400;
} else if (query.filename.search('http') != -1) {
result.message = 'Remote files not supported yet.';
result.statusCode = 406;
} else {
result.message = 'File is not remote';
// does the file exist?
const path = Path.join(__dirname, query.filename);
console.log('Does the file exist?');
const stats = await Stat(path);
const message = 'File is available';
result.message = message;
result.success = true;
try {
const graphology = await fastify.readGraphology(path);
await fastify.cacheObject('nodes', graphology['nodes']);
await fastify.cacheObject('edges', graphology['edges']);
await fastify.cacheObject('clusters', graphology['clusters']);
await fastify.cacheObject('tags', graphology['tags']);
result.message = 'File read onto GPU.';
} catch (e) {
result.success = false;
result.message = e;
result.statusCode = 500;
}
}
} catch (e) {
result.success = false;
result.message = e.message;
result.statusCode = 404;
};
await reply.code(result.statusCode).send(result);
}
});
fastify.route({
method: 'GET',
url: '/list_tables',
handler: async (request, reply) => {
let message = 'Error';
let result = {success: false, message: message};
const list = await fastify.listDataframes();
return list;
}
});
fastify.route({
method: 'GET',
url: '/get_column/:table/:column',
schema: {querystring: {table: {type: 'string'}, 'column': {type: 'string'}}},
handler: async (request, reply) => {
let message = 'Error';
let result = {'params': JSON.stringify(request.params), success: false, message: message};
const table = await fastify.getData(request.params.table);
if (table == undefined) {
result.message = 'Table not found';
await reply.code(404).send(result);
} else {
try {
const name = request.params.column;
const column = table.get(name);
const newDfObject = {};
newDfObject[name] = column;
const result = new DataFrame(newDfObject);
const writer = RecordBatchStreamWriter.writeAll(result.toArrow());
await reply.code(200).send(writer.toNodeStream());
} catch (e) {
if (e.substring('Unknown column name') != -1) {
result.message = e;
console.log(result);
await reply.code(404).send(result);
} else {
result.message = e;
console.log(result);
await reply.code(500).send(result);
}
}
}
}
});
fastify.route({
method: 'GET',
url: '/get_table/:table',
schema: {querystring: {table: {type: 'string'}}},
handler: async (request, reply) => {
let message = 'Error';
let result = {'params': JSON.stringify(request.params), success: false, message: message};
const table = await fastify.getData(request.params.table);
if (table == undefined) {
result.message = 'Table not found';
await reply.code(404).send(result);
} else {
const writer = RecordBatchStreamWriter.writeAll(table.toArrow());
await reply.code(200).send(writer.toNodeStream());
}
}
});
fastify.route({
method: 'GET',
url: '/nodes/bounds',
handler: async (request, reply) => {
let message = 'Error';
let result = {success: false, message: message};
const df = await fastify.getData('nodes');
if (df == undefined) {
result.message = 'Table not found';
await reply.code(404).send(result);
} else {
// compute xmin, xmax, ymin, ymax
const x = df.get('x');
const y = df.get('y');
const [xmin, xmax] = x.minmax();
const [ymin, ymax] = y.minmax();
result.bounds = {xmin: xmin, xmax: xmax, ymin: ymin, ymax: ymax};
result.message = 'Success';
result.success = true;
await reply.code(200).send(result);
}
}
});
fastify.route({
method: 'GET',
url: '/nodes',
handler: async (request, reply) => {
let message = 'Error';
let result = {success: false, message: message};
const df = await fastify.getData('nodes');
if (df == undefined) {
result.message = 'Table not found';
await reply.code(404).send(result);
} else {
// tile x, y, size, color
let tiled = Series.sequence({type: new Float32, init: 0.0, size: (4 * df.numRows)});
let base_offset = Series.sequence({type: new Int32, init: 0.0, size: df.numRows}).mul(4);
//
// Duplicatin the sigma.j createNormalizationFunction here because there's no other way
// to let the Graph object compute it.
//
const x = df.get('x');
const y = df.get('y');
let color = df.get('color');
const color_ints = color.hexToIntegers(new Uint32).bitwiseOr(0xef000000);
const [xMin, xMax] = x.minmax();
const [yMin, yMax] = y.minmax();
const ratio = Math.max(xMax - xMin, yMax - yMin);
const dX = (xMax + xMin) / 2.0;
const dY = (yMax + yMin) / 2.0;
const x_scaled = x.add(-1.0 * dX).mul(1.0 / ratio).add(0.5);
const y_scaled = y.add(-1.0 * dY).mul(1.0 / ratio).add(0.5);
tiled = tiled.scatter(x_scaled, base_offset.cast(new Int32));
tiled = tiled.scatter(y_scaled, base_offset.add(1).cast(new Int32));
tiled = tiled.scatter(df.get('size').mul(2), base_offset.add(2).cast(new Int32));
tiled = tiled.scatter(color_ints.view(new Float32), base_offset.add(3).cast(new Int32));
const writer = RecordBatchStreamWriter.writeAll(new DataFrame({nodes: tiled}).toArrow());
await reply.code(200).send(writer.toNodeStream());
}
}
});
fastify.route({
method: 'GET',
url: '/edges',
handler: async (request, reply) => {
let message = 'Error';
let result = {success: false, message: message};
/** @type DataFrame<{x: Float32, y: Float32}> */
const df = await fastify.getData('nodes');
/** @type DataFrame<{x: Int32, y: Int32}> */
const edges = await fastify.getData('edges');
if (df == undefined) {
result.message = 'Table not found';
await reply.code(404).send(result);
} else {
try {
// tile x, y, size, color
let tiled = Series.sequence({type: new Float32, init: 0.0, size: (6 * edges.numRows)});
let base_offset =
Series.sequence({type: new Int32, init: 0.0, size: edges.numRows}).mul(3);
//
// Duplicatin the sigma.js createNormalizationFunction here because this is the best way
// to let the Graph object compute it on GPU.
//
// Remap the indices in the key table to their real targets.
const keys = df.get('key');
const keys_df = new DataFrame({'keys': keys});
const source_unordered = edges.get('source');
const target_unordered = edges.get('target');
source_df = new DataFrame({
'keys': source_unordered,
'idx': Series.sequence({size: source_unordered.length, init: 0})
});
target_df = new DataFrame({
'keys': target_unordered,
'idx': Series.sequence({size: target_unordered.length, init: 0})
});
const source_idx_df = keys_df.join({other: source_df, on: ['keys'], how: 'left'});
const target_idx_df = keys_df.join({other: target_df, on: ['keys'], how: 'left'});
let source_map = source_idx_df.get('idx')
let target_map = target_idx_df.get('idx')
if (source_map.nullCount > 0) { throw 'Edge sources do not match node keys'; }
if (target_map.nullCount > 0) { throw 'Edge targets do not match node keys'; }
let x = df.get('x');
let y = df.get('y');
const [xMin, xMax] = x.minmax();
const [yMin, yMax] = y.minmax();
const ratio = Math.max(xMax - xMin, yMax - yMin);
const dX = (xMax + xMin) / 2.0;
const dY = (yMax + yMin) / 2.0;
x = x.add(-1.0 * dX).mul(1.0 / ratio).add(0.5);
y = y.add(-1.0 * dY).mul(1.0 / ratio).add(0.5);
const source_xmap = x.gather(source_map, false);
const source_ymap = y.gather(source_map, false);
const target_xmap = x.gather(target_map, false);
const target_ymap = y.gather(target_map, false);
const color = Series.new(['#999'])
.hexToIntegers(new Int32)
.bitwiseOr(0xff000000)
.view(new Float32)
.toArray()[0];
tiled =
tiled.scatter(Series.new(source_xmap), Series.new(base_offset.mul(2)).cast(new Int32));
tiled = tiled.scatter(Series.new(source_ymap),
Series.new(base_offset.mul(2).add(1)).cast(new Int32));
tiled = tiled.scatter(color, Series.new(base_offset.mul(2).add(2).cast(new Int32)));
tiled = tiled.scatter(Series.new(target_xmap),
Series.new(base_offset.mul(2).add(3)).cast(new Int32));
tiled = tiled.scatter(Series.new(target_ymap),
Series.new(base_offset.mul(2).add(4)).cast(new Int32));
tiled = tiled.scatter(color, Series.new(base_offset.mul(2).add(5).cast(new Int32)));
const writer = RecordBatchStreamWriter.writeAll(new DataFrame({edges: tiled}).toArrow());
await reply.code(200).send(writer.toNodeStream());
} catch (e) {
if (e.includes('do not match') >= 0) {
result.statusCode = 422;
} else {
result.statusCode = 500;
}
result.success = false;
result.message = e;
await reply.code(result.statusCode).send(result);
}
}
}
});
fastify.route({
method: 'POST',
url: '/release',
handler: async (request, reply) => {
await fastify.clearDataFrames();
await reply.code(200).send({message: 'OK'})
}
});
}