Initial commit

This commit is contained in:
Thomas Nordquist
2018-12-28 15:25:25 +01:00
commit 0af3a2ede3
21 changed files with 2892 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
node_modules
coverage
build
.nyc_output
test.dot
test.png

2353
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

54
package.json Normal file
View File

@@ -0,0 +1,54 @@
{
"name": "mqtt-explorer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"test-inspect": "mocha --inspect-brk",
"coverage": "nyc mocha",
"debug": "ts-node --inspect ./src/index.ts"
},
"author": "",
"license": "ISC",
"nyc": {
"include": [
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"src/**/spec/*.spec.ts"
],
"extension": [
".ts",
".tsx"
],
"require": [
"ts-node/register"
],
"reporter": [
"text-summary",
"html"
],
"sourceMap": true,
"instrument": true
},
"dependencies": {
"@types/sha1": "^1.1.1",
"mqtt": "^2.18.8",
"sha1": "^1.1.1",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"chai": "^4.2.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"source-map-support": "^0.5.9",
"ts-node": "^7.0.1",
"tslint-strict-null-checks": "^1.0.1"
}
}

63
src/DotExport.ts Normal file
View File

@@ -0,0 +1,63 @@
import { Tree, TreeNode } from './Model'
export class DotExport {
public static renderNodeInformation(node: TreeNode): string {
return `\t${node.sourceEdge.hash()} [label=${this.renderLabel(node.value)}]`
}
public static toDot(tree: Tree): string {
let i = 1
let leaveEdges = Object.values(tree.edges)
.map(e => e.node)
.map(node => node.leaves())
.reduce((a, b) => a.concat(b), [])
.map(leave => leave.branch())
const allEdges: Array<string> = []
const nodeInformation: {[s: string]: string} = {}
leaveEdges.map(edges => edges.reduce( (prev, current) => {
let currentHash = current.sourceEdge.hash()
nodeInformation[currentHash] = this.renderNodeInformation(current)
if (current && prev) {
allEdges.push(`\t${prev.sourceEdge.hash()} -> ${currentHash} [label="${current.sourceEdge.name}"]`)
}
return current
}))
return `strict digraph ethane {
${
[this.renderNodeInformation(tree)]
.concat(Object.values(nodeInformation))
.concat(allEdges)
.join('\n')
}
}`;
}
private static renderLabel(value: any): string {
let str;
if(!isNaN(value)) {
str = value
} else {
str = JSON.stringify(value)
console.log(str)
if(str && str.length > 0) {
str = str.slice(1, -1)
console.log(str)
}
}
if (!str) {
return '""'
}
if(str.length > 20) {
str = str.slice(0, 20)+'…'
}
if (str[0] !== '"') {
str = `"${str}"`
}
return str
}
}

37
src/Model/Edge.ts Normal file
View File

@@ -0,0 +1,37 @@
import { Hashable, TreeNode, TopicProperties } from './'
const sha1 = require('sha1')
export class Edge {
public name: string
public node!: TreeNode
public source: TreeNode | undefined
public target: TreeNode | undefined
constructor(name: string) {
this.name = name
}
public edges() {
return this.node ? Object.values(this.node.edges) : []
}
public hash(): string {
let previousHash = this.source ? this.source.sourceEdge.hash() : ''
return 'H' + sha1(previousHash + this.name)
}
public firstEdge(): Edge {
if (this.source) {
return this.source.sourceEdge.firstEdge()
} else {
return this
}
}
// public merge(edge: Edge) {
// if (this.node && edge.node) {
// this.node.updateWithNode(edge.node)
// }
// }
}

3
src/Model/Hashable.ts Normal file
View File

@@ -0,0 +1,3 @@
export interface Hashable {
hash(): string
}

View File

@@ -0,0 +1,4 @@
export class TopicProperties {
public topicSeparator: string = '/'
public multilevelWildcard: string | null = '#'
}

9
src/Model/Tree.ts Normal file
View File

@@ -0,0 +1,9 @@
import { Edge, TreeNode } from './'
export class Tree extends TreeNode {
constructor() {
const rootEdge = new Edge('')
super(rootEdge, undefined)
rootEdge.node = this
}
}

65
src/Model/TreeNode.ts Normal file
View File

@@ -0,0 +1,65 @@
import { Edge } from './'
export class TreeNode {
public sourceEdge: Edge
public value: any | null | undefined = undefined
public edges: {[s: string]: Edge} = {}
constructor(sourceEdge: Edge, value: any) {
this.sourceEdge = sourceEdge
sourceEdge.target = this
this.value = value
}
public firstNode(): TreeNode {
return this.sourceEdge.firstEdge().node
}
private previous(): TreeNode | undefined {
return this.sourceEdge.source || undefined
}
public addEdge(edge: Edge) {
this.edges[edge.name] = edge
edge.source = this
}
public branch(): Array<TreeNode> {
let previous = this.previous()
if (!previous) {
return [this]
}
return previous.branch().concat([this])
}
public updateWithNode(node: TreeNode) {
debugger
if (node.value !== undefined) {
this.value = node.value
}
this.mergeEdges(node)
}
public leaves(): Array<TreeNode> {
if (Object.values(this.edges).length === 0) {
return [this]
}
return Object.values(this.edges)
.map(e => e.node.leaves())
.reduce((a, b) => a.concat(b), [])
}
private mergeEdges(node: TreeNode) {
let edgeKeys = Object.keys(node.edges)
for (let edgeKey of edgeKeys) {
let matchingEdge = this.edges[edgeKey]
if (matchingEdge) {
matchingEdge.node.updateWithNode(node.edges[edgeKey].node)
} else {
this.addEdge(node.edges[edgeKey])
}
}
}
}

View File

@@ -0,0 +1,32 @@
import { Edge, Tree, TreeNode } from './'
export abstract class TreeNodeFactory {
public static fromEdgesAndValue(edgeNames: Array<string>, value: any): TreeNode {
const lastEdgeIndex = edgeNames.length - 1
var edges = edgeNames
.map((name, idx) => {
const edge = new Edge(name)
const nodeValue = lastEdgeIndex == idx ? value : undefined
const node = new TreeNode(edge, nodeValue)
edge.node = node
return edge
})
let reversed: Array<Edge> = edges.reverse()
let previous: Edge | undefined = undefined;
for (let edge of reversed) {
if (previous) {
edge.node.addEdge(previous)
}
previous = edge;
}
let leaf = reversed[0].node
let sourceTree = new Tree()
sourceTree.addEdge(leaf.firstNode().sourceEdge)
return leaf
}
}

10
src/Model/index.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Edge } from './Edge'
import { TreeNode } from './TreeNode'
import { TreeNodeFactory } from './TreeNodeFactory'
import { Tree } from './Tree'
import { TopicProperties } from './TopicProperties'
import { Hashable } from './Hashable'
export {
Edge, TreeNode, TreeNodeFactory, Tree, TopicProperties, Hashable
}

View File

@@ -0,0 +1,31 @@
import { Edge, TreeNode } from '../'
import { expect } from 'chai';
import 'mocha';
describe('Edge', () => {
it('should contain a name', () => {
let e = new Edge('foo')
expect(e.name).to.equal('foo')
});
it('hash should not be empty', () => {
let e = new Edge('bar')
expect(e.hash().length).to.be.gt(0)
});
it('hash should be stable', () => {
let e = new Edge('bar')
let previousHash = e.hash()
expect(e.hash()).to.eq(previousHash)
});
it('hash should change when parent is present', () => {
let foo = new Edge('foo')
let bar = new Edge('bar')
var previousHash = bar.hash()
bar.source = new TreeNode(foo, undefined)
expect(bar.hash()).to.not.eq(previousHash)
});
});

View File

@@ -0,0 +1,18 @@
import { Edge, Tree, TreeNode, TreeNodeFactory } from '../'
import { expect } from 'chai';
import 'mocha';
import './TreeNode.findNode'
describe('Tree', () => {
it('node can be merged into a tree', () => {
const tree = new Tree()
const topics = 'foo/bar'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 3)
debugger
tree.updateWithNode(leaf.firstNode())
let expectedNode = tree.findNode('foo/bar')
expect(expectedNode).to.eq(leaf)
})
});

View File

@@ -0,0 +1,29 @@
import { TreeNode, TreeNodeFactory } from '../'
import { expect } from 'chai';
import 'mocha';
import './TreeNode.findNode'
describe('TreeNode.findNode', () => {
it('findNode should retrieve node', () => {
const topics = 'foo/bar/baz'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 5)
let root = leaf.firstNode()
expect(root.sourceEdge.name).to.eq('')
let barNode = root.findNode('foo/bar')
if (!barNode) {
expect.fail('did not find node')
return
}
expect(barNode.sourceEdge.name).to.eq('bar')
let bazNode = root.findNode('foo/bar/baz')
if (!bazNode) {
expect.fail('did not find node')
return
}
expect(bazNode.sourceEdge.name).to.eq('baz')
})
})

View File

@@ -0,0 +1,20 @@
import { TreeNode } from '../'
declare module "../" {
interface TreeNode {
findNode(path: String): TreeNode | undefined
}
}
TreeNode.prototype.findNode = function(path: String): TreeNode | undefined {
const topics = path.split('/')
let edge = this.edges[topics[0]]
let remainingTopics = topics.slice(1, topics.length)
if (edge && remainingTopics.length === 0) {
return edge.node
} else if (edge) {
return edge.node.findNode(remainingTopics.join('/'))
}
return undefined
}

View File

@@ -0,0 +1,51 @@
import { Edge, Tree, TreeNode, TreeNodeFactory } from '../'
import { expect } from 'chai';
import 'mocha';
import './TreeNode.findNode'
describe('TreeNode', () => {
it('updateWithNode should update value', () => {
const topics = 'foo/bar'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 3)
expect(leaf.value).to.eq(3)
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics, 5)
leaf.firstNode().updateWithNode(updateLeave.firstNode())
expect(leaf.firstNode().sourceEdge.name).to.eq(updateLeave.firstNode().sourceEdge.name)
expect(leaf.value).to.eq(5)
})
it('updateWithNode should update intermediate nodes', () => {
const topics1 = 'foo/bar/baz'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics1, 3)
expect(leaf.value).to.eq(3)
const topics2 = 'foo/bar'.split('/')
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics2, 5)
leaf.firstNode().updateWithNode(updateLeave.firstNode())
let barNode = leaf.firstNode().findNode('foo/bar')
expect(barNode && barNode.sourceEdge.name).to.eq('bar')
expect(barNode && barNode.value).to.eq(5)
expect(leaf.sourceEdge.name).to.eq('baz')
expect(leaf.value).to.eq(3)
})
it('updateWithNode should add nodes to the tree', () => {
const topics1 = 'foo/bar'.split('/')
const leaf1 = TreeNodeFactory.fromEdgesAndValue(topics1, 3)
const topics2 = 'foo/bar/baz'.split('/')
const leaf2 = TreeNodeFactory.fromEdgesAndValue(topics2, 5)
leaf1.firstNode().updateWithNode(leaf2.firstNode())
let expectedNode = leaf1.firstNode().findNode('foo/bar/baz')
if (!expectedNode) {
expect.fail('merge seems to have failed')
return
}
})
});

View File

@@ -0,0 +1,46 @@
import { Edge, TreeNode, TreeNodeFactory } from '../'
import { expect } from 'chai';
import 'mocha';
describe('TreeNodeFactory', () => {
it('should create node', () => {
let topic = 'foo/bar'
let edges = topic.split('/')
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
expect(node).to.not.eq(undefined)
expect(node.sourceEdge.name).to.eq('bar')
expect(node.value).to.eq(5)
if (!node.sourceEdge.source) {
expect.fail('should not happen')
return
}
let foo = node.sourceEdge.source.sourceEdge
expect(foo.name).to.eq('foo')
});
it('node should contain edges in order', () => {
let topic = 'foo/bar/baz'
let edges = topic.split('/')
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
expect(node.value).to.eq(5)
expect(node.sourceEdge.name).to.eq('baz')
const barNode = node.sourceEdge.source
if (!barNode) {
expect.fail('should not fail')
return
}
expect(barNode.sourceEdge.name).to.eq('bar')
const fooNode = barNode.sourceEdge.source
if (!fooNode) {
expect.fail('should not fail')
return
}
expect(fooNode.sourceEdge.name).to.eq('foo')
});
});

41
src/index.ts Normal file
View File

@@ -0,0 +1,41 @@
import { connect as mqttConnect } from 'mqtt'
import { TopicProperties, Tree, TreeNodeFactory } from './Model'
import { DotExport } from './DotExport'
import { writeFileSync } from 'fs'
import { spawn } from 'child_process'
var client = mqttConnect('mqtt://test.mosquitto.org')
const topicSeparator = '/'
client.on('connect', function () {
console.log('connected')
client.subscribe('#', (err: Error) => {
if (!err) {}
})
})
let tree = new Tree()
client.on('message', function (topic, message) {
// message is Buffer
const edges = topic.split(topicSeparator)
let value = message.toString()
let node = TreeNodeFactory.fromEdgesAndValue(edges, value)
tree.updateWithNode(node.firstNode())
})
function writeTree() {
writeFileSync('./test.dot', DotExport.toDot(tree))
let p = spawn('dot', '-Tpng test.dot -o test.png'.split(' '))
}
setInterval(() => {
writeTree()
console.log(tree)
}, 2000)
setTimeout(() => {
console.log(tree)
client.end()
}, 1000000)

3
test/mocha.opts Normal file
View File

@@ -0,0 +1,3 @@
--require ts-node/register
--require source-map-support/register
--recursive ./src/**/*.spec.ts

11
tsconfig.json Normal file
View File

@@ -0,0 +1,11 @@
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"outDir": "./build",
"strict": true,
"lib": ["es2017"],
"sourceMap": true
}
}

5
tslint.json Normal file
View File

@@ -0,0 +1,5 @@
{
"rules": {
"member-access": true
}
}