|
| 1 | +// Copyright (c) 2021, NVIDIA CORPORATION. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const {graphs, clients} = require('../graph'); |
| 16 | +const fs = require('fs') |
| 17 | +const util = require('util') |
| 18 | +const {pipeline} = require('stream') |
| 19 | +const pump = util.promisify(pipeline) |
| 20 | +const glob = require('glob'); |
| 21 | +const {Graph} = require('@rapidsai/cugraph'); |
| 22 | +const {loadEdges, loadNodes} = require('../graph/loader'); |
| 23 | +const {RecordBatchStreamWriter} = require('apache-arrow'); |
| 24 | +const path = require('path'); |
| 25 | +const {readDataFrame, getNodesForGraph, getEdgesForGraph, getPaginatedRows} = require('./utils'); |
| 26 | + |
| 27 | +module.exports = function(fastify, opts, done) { |
| 28 | + fastify.addHook('preValidation', (request, reply, done) => { |
| 29 | + // handle upload validation after reading request.file() in the route function itself |
| 30 | + if (request.url == '/datasets/upload') { |
| 31 | + done(); |
| 32 | + } else { |
| 33 | + request.query.id = |
| 34 | + (request.method == 'POST') ? `${request.body.id}:video` : `${request.query.id}:video`; |
| 35 | + if (request.query.id in fastify[clients]) { |
| 36 | + done(); |
| 37 | + } else { |
| 38 | + reply.code(500).send('client handshake not established'); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + async function renderGraph(id, data) { |
| 44 | + const asDeviceMemory = (buf) => new (buf[Symbol.species])(buf); |
| 45 | + const src = data.edges.dataframe.get(data.edges.src); |
| 46 | + const dst = data.edges.dataframe.get(data.edges.dst); |
| 47 | + const graph = Graph.fromEdgeList(src, dst, {directedEdges: true}); |
| 48 | + fastify[graphs][id] = { |
| 49 | + refCount: 0, |
| 50 | + nodes: await getNodesForGraph(asDeviceMemory, data.nodes, graph.numNodes), |
| 51 | + edges: await getEdgesForGraph(asDeviceMemory, data.edges), |
| 52 | + graph: graph, |
| 53 | + }; |
| 54 | + |
| 55 | + ++fastify[graphs][id].refCount; |
| 56 | + |
| 57 | + return { |
| 58 | + gravity: 0.0, |
| 59 | + linLogMode: false, |
| 60 | + scalingRatio: 5.0, |
| 61 | + barnesHutTheta: 0.0, |
| 62 | + jitterTolerance: 0.05, |
| 63 | + strongGravityMode: false, |
| 64 | + outboundAttraction: false, |
| 65 | + graph: fastify[graphs][id].graph, |
| 66 | + nodes: { |
| 67 | + ...fastify[graphs][id].nodes, |
| 68 | + length: fastify[graphs][id].graph.numNodes, |
| 69 | + }, |
| 70 | + edges: { |
| 71 | + ...fastify[graphs][id].edges, |
| 72 | + length: fastify[graphs][id].graph.numEdges, |
| 73 | + }, |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + fastify.post('/datasets/upload', async function(req, reply) { |
| 78 | + const data = await req.file(); |
| 79 | + const id = `${data.fields.id.value}:video`; |
| 80 | + if (id in fastify[clients]) { |
| 81 | + const basePath = `${__dirname}/../../data/`; |
| 82 | + const filepath = path.join(basePath, data.filename); |
| 83 | + const target = fs.createWriteStream(filepath); |
| 84 | + try { |
| 85 | + await pump(data.file, target); |
| 86 | + } catch (err) { console.log(err); } |
| 87 | + reply.send(); |
| 88 | + } else { |
| 89 | + reply.code(500).send('client handshake not established'); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + fastify.get('/datasets', async (request, reply) => { |
| 94 | + glob(`*.{csv,parquet}`, |
| 95 | + {cwd: `${__dirname}/../../data/`}, |
| 96 | + (er, files) => { reply.send(JSON.stringify(files.concat(['defaultExample']))); }); |
| 97 | + }); |
| 98 | + |
| 99 | + fastify.post('/dataframe/load', async (request, reply) => { |
| 100 | + const filePath = `${__dirname}/../../data/` |
| 101 | + if (fs.existsSync(`${filePath}${request.body.nodes}`) && |
| 102 | + fs.existsSync(`${filePath}${request.body.edges}`)) { |
| 103 | + fastify[clients][request.query.id].data.nodes.dataframe = |
| 104 | + await readDataFrame(`${filePath}${request.body.nodes}`); |
| 105 | + |
| 106 | + fastify[clients][request.query.id].data.edges.dataframe = |
| 107 | + await readDataFrame(`${filePath}${request.body.edges}`); |
| 108 | + } |
| 109 | + else { |
| 110 | + fastify[clients][request.query.id].data.nodes.dataframe = await loadNodes(); |
| 111 | + fastify[clients][request.query.id].data.edges.dataframe = await loadEdges(); |
| 112 | + } |
| 113 | + if (fastify[clients][request.query.id].data.nodes.dataframe.numRows == 0) { |
| 114 | + reply.code(500).send('no dataframe loaded'); |
| 115 | + } |
| 116 | + reply.send(JSON.stringify({ |
| 117 | + 'nodes': fastify[clients][request.query.id].data.nodes.dataframe.numRows, |
| 118 | + 'edges': fastify[clients][request.query.id].data.edges.dataframe.numRows |
| 119 | + })); |
| 120 | + }) |
| 121 | + |
| 122 | + fastify.get('/dataframe/columnNames/read', async (request, reply) => { |
| 123 | + reply.send(JSON.stringify({ |
| 124 | + nodesParams: fastify[clients][request.query.id].data.nodes.dataframe.names.concat([null]), |
| 125 | + edgesParams: fastify[clients][request.query.id].data.edges.dataframe.names.concat([null]) |
| 126 | + })); |
| 127 | + }); |
| 128 | + |
| 129 | + fastify.post('/dataframe/columnNames/update', async (request, reply) => { |
| 130 | + try { |
| 131 | + Object.assign(fastify[clients][request.query.id].data.nodes, request.body.nodes); |
| 132 | + Object.assign(fastify[clients][request.query.id].data.edges, request.body.edges); |
| 133 | + reply.code(200).send('successfully updated columnNames'); |
| 134 | + } catch (err) { reply.code(500).send(err); } |
| 135 | + }); |
| 136 | + |
| 137 | + fastify.post('/graph/render', async (request, reply) => { |
| 138 | + try { |
| 139 | + fastify[clients][request.query.id].graph = |
| 140 | + await renderGraph('default', fastify[clients][request.query.id].data); |
| 141 | + reply.code(200).send('successfully rendered graph'); |
| 142 | + } catch (err) { reply.code(500).send(err); } |
| 143 | + }) |
| 144 | + |
| 145 | + fastify.get('/dataframe/read', async (request, reply) => { |
| 146 | + try { |
| 147 | + const pageIndex = parseInt(request.query.pageIndex); |
| 148 | + const pageSize = parseInt(request.query.pageSize); |
| 149 | + const dataframe = request.query.dataframe; //{'nodes', 'edges'} |
| 150 | + const [arrowTable, numRows] = |
| 151 | + await getPaginatedRows(fastify[clients][request.query.id].data[dataframe].dataframe, |
| 152 | + pageIndex, |
| 153 | + pageSize, |
| 154 | + fastify[clients][request.query.id].state.selectedInfo[dataframe]); |
| 155 | + |
| 156 | + arrowTable.schema.metadata.set('numRows', numRows); |
| 157 | + RecordBatchStreamWriter.writeAll(arrowTable).pipe(reply.stream()); |
| 158 | + } catch (err) { |
| 159 | + request.log.error({err}, '/run_query error'); |
| 160 | + reply.code(500).send(err); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + done(); |
| 165 | +} |
0 commit comments