-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store all nodes of a topology to the database. New queries - simple GraphQL interface to query all nodes, or a subset of nodes filtered by ids. - new `nodeCount` field to the `summary` endpoint. ## Refactoring `ConnectionPool#queryPaginated` helper method and `createSqlQuery` utility method. ## Future improvements Should we store new nodes and neighbors which we find when we crawl a single newly created stream.
- Loading branch information
Showing
15 changed files
with
266 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Arg, Int, Query, Resolver } from 'type-graphql' | ||
import { Inject, Service } from 'typedi' | ||
import { Nodes } from '../entities/Node' | ||
import { NodeRepository } from '../repository/NodeRepository' | ||
|
||
@Resolver() | ||
@Service() | ||
export class NodeResolver { | ||
|
||
private repository: NodeRepository | ||
|
||
constructor( | ||
@Inject() repository: NodeRepository | ||
) { | ||
this.repository = repository | ||
} | ||
|
||
@Query(() => Nodes) | ||
async nodes( | ||
@Arg("ids", () => [String], { nullable: true }) ids?: string[], | ||
@Arg("pageSize", () => Int, { nullable: true }) pageSize?: number, | ||
@Arg("cursor", { nullable: true }) cursor?: string, | ||
): Promise<Nodes> { | ||
return this.repository.getNodes(ids, pageSize, cursor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Field, ObjectType } from 'type-graphql' | ||
|
||
/* eslint-disable indent */ | ||
@ObjectType() | ||
export class Node { | ||
@Field() | ||
id!: string | ||
@Field(() => String, { nullable: true }) | ||
ipAddress!: string | null | ||
} | ||
|
||
/* eslint-disable indent */ | ||
@ObjectType() | ||
export class Nodes { | ||
@Field(() => [Node]) | ||
items!: Node[] | ||
@Field(() => String, { nullable: true }) | ||
cursor!: string | null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Logger } from '@streamr/utils' | ||
import { RowDataPacket } from 'mysql2' | ||
import { Inject, Service } from 'typedi' | ||
import { Topology } from '../crawler/Topology' | ||
import { Nodes } from '../entities/Node' | ||
import { createSqlQuery } from '../utils' | ||
import { ConnectionPool } from './ConnectionPool' | ||
|
||
interface NodeRow extends RowDataPacket { | ||
id: string | ||
ipAddress: string | null | ||
} | ||
|
||
const logger = new Logger(module) | ||
|
||
@Service() | ||
export class NodeRepository { | ||
|
||
private readonly connectionPool: ConnectionPool | ||
|
||
constructor( | ||
@Inject() connectionPool: ConnectionPool | ||
) { | ||
this.connectionPool = connectionPool | ||
} | ||
|
||
async getNodes( | ||
ids?: string[], | ||
pageSize?: number, | ||
cursor?: string | ||
): Promise<Nodes> { | ||
logger.info('Query: getNodes', { ids, pageSize, cursor }) | ||
const whereClauses = [] | ||
const params = [] | ||
if (ids !== undefined) { | ||
whereClauses.push('id in (?)') | ||
params.push(ids) | ||
} | ||
const sql = createSqlQuery( | ||
`SELECT id, ipAddress FROM nodes`, | ||
whereClauses | ||
) | ||
return this.connectionPool.queryPaginated<NodeRow[]>(sql, params) | ||
} | ||
|
||
async replaceNetworkTopology(topology: Topology): Promise<void> { | ||
const nodes = topology.getNodes().map((node) => { | ||
return [node.id, node.ipAddress] | ||
}) | ||
const connection = await this.connectionPool.getConnection() | ||
try { | ||
await connection.beginTransaction() | ||
await connection.query('DELETE FROM nodes') | ||
await connection.query('INSERT INTO nodes (id, ipAddress) VALUES ?', [nodes]) | ||
await connection.commit() | ||
} catch (e) { | ||
connection.rollback() | ||
throw e | ||
} finally { | ||
connection.release() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.