Skip to content

Commit 47ad46f

Browse files
committed
Pushed first public release of dDatabase Web UI
0 parents  commit 47ad46f

File tree

9 files changed

+338
-0
lines changed

9 files changed

+338
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
example/bundle.js
3+
bundle.js
4+
example/npm-debug.log

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# [MIT License](https://benchx.io/license/third-party/MIT)
2+
3+
Copyright (c) Distributed Webs Project, LLC. <legal@distributedwebs.org>
4+
Copyright (c) 2016 Mathias Buus
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[![dDatabase](https://raw.githubusercontent.com/DistributedWeb/dweb-design/master/repo-headers/ddatabase-header.png)](https://ddatabase.io)<br>
2+
3+
# @ddatabase/ui
4+
5+
## Official Documentation
6+
7+
[http://docs.ddatabase.io/ui](http://docs.ddatabase.io/ui)
8+
9+
## GitHub Development Bounty
10+
11+
- Get involved with dWeb and dDrive development and earn BEX coins : https://bounty.bex.life
12+
13+
## Security
14+
15+
If you discover a security vulnerability within this package, please send an e-mail to security@ddatabase.io. All security vulnerabilities will be promptly addressed.
16+
17+
## Credits
18+
19+
- [Jared Rice Sr.](https://github.com/jaredricesr)
20+
- [Mathias Buus](https://github.com/mafintosh)
21+
- [Karissa McKelvey](https://github.com/karissa)
22+
23+
## License
24+
25+
[MIT](LICENSE.md) © [Distributed Webs Project, LLC](https://distributedwebs.org)

example/app.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var Stats = require('../')
2+
var ess = require('event-source-stream')
3+
4+
var stats = Stats(document.body)
5+
6+
ess('http://' + window.location.host + '/events')
7+
.on('data', function (data) {
8+
data = JSON.parse(data)
9+
switch (data.type) {
10+
case 'key': return stats.onkey(data)
11+
case 'peer-update': return stats.onpeerupdate(data)
12+
case 'ddb': return stats.onddb(data)
13+
case 'update': return stats.onupdate(data)
14+
case 'download': return stats.ondownload(data)
15+
case 'upload': return stats.onupload(data)
16+
}
17+
})

example/cli.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
3+
var stats = require('./server.js')
4+
var http = require('http')
5+
var flockRevelation = require('@flockcore/revelation')
6+
var minimist = require('minimist')
7+
8+
var argv = minimist(process.argv.slice(2), {
9+
alias: {port: 'p', '@ddrive/core': 'd', wait: 'w'},
10+
boolean: ['@ddrive/core']
11+
})
12+
13+
var ddatabase = require('@ddatabase/core')
14+
var ddrive = require('@ddrive/core')
15+
var dwRem = require('@dwcore/rem')
16+
17+
var key = argv._[0]
18+
if (!key) {
19+
console.error(
20+
`Usage: node cli [--port=<port>] [--ddrive] \n` +
21+
` [--wait=<seconds>] <key>\n`
22+
)
23+
process.exit(1)
24+
}
25+
26+
var target = argv.ddrive ? ddrive(dwRem, key) : ddatabase(dwRem, key)
27+
28+
var server = http.createServer(stats(target))
29+
30+
server.on('listening', function () {
31+
target.ready(function () {
32+
console.log('Ddb/vault:', target.key.toString('hex'))
33+
console.log('Stats listening on port ' + server.address().port)
34+
35+
if (argv.wait) setTimeout(join, Number(argv.wait) * 1000)
36+
else join()
37+
})
38+
})
39+
40+
server.listen(argv.port || process.env.PORT || 10000)
41+
server.once('error', function () {
42+
server.listen(0)
43+
})
44+
45+
function join () {
46+
var sw = flockRevelation(target)
47+
sw.on('connection', function (peer, type) {
48+
console.log('connected to', sw.connections.length, 'peers')
49+
peer.on('close', function () {
50+
console.log('peer disconnected')
51+
})
52+
})
53+
}

example/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<style>
6+
body {
7+
font-family: monospace;
8+
}
9+
10+
#template {
11+
display: none;
12+
}
13+
14+
.upload {
15+
background-color: green;
16+
}
17+
18+
.clearfix {
19+
clear: both;
20+
}
21+
22+
.dot {
23+
border: 1px solid black;
24+
width: 5px;
25+
height: 5px;
26+
margin: 1px;
27+
float: left;
28+
}
29+
</style>
30+
</head>
31+
<body>
32+
<h3 id="key"></h3>
33+
<h4><span id="peers">Connected to 0 peers</span>
34+
<div id="template">
35+
<h3 class="name"></h3>
36+
<h4><span class="overview"></span><span class="download-speed"></span> / ↑ <span class="upload-speed"></span></h4>
37+
<div class="blocks"></div>
38+
<div class="clearfix"></div>
39+
</div>
40+
<script src="bundle.js"></script>
41+
</body>
42+
</html>

example/server.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var corsify = require('corsify')
2+
var fs = require('fs')
3+
var path = require('path')
4+
var server = require('@ddatabase/stats-server')
5+
6+
var cors = corsify({
7+
'Access-Control-Allow-Origin': '*',
8+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
9+
'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Authorization'
10+
})
11+
12+
module.exports = function (vault) {
13+
return cors(function (req, res) {
14+
if (req.url === '/') return file('index.html', 'text/html', res)
15+
if (req.url === '/bundle.js') return file('bundle.js', 'text/javascript', res)
16+
server(vault, res)
17+
})
18+
}
19+
20+
function file (name, type, res) {
21+
res.setHeader('Content-Type', type + '; charset=utf-8')
22+
fs.readFile(path.join(__dirname, name), function (err, buf) {
23+
if (err) return res.end()
24+
res.end(buf)
25+
})
26+
}

index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
var dweb-netspeed = require('@dwcore/netspeed')
2+
var prettyBytes = require('pretty-bytes')
3+
4+
var $ = document.querySelector.bind(document)
5+
6+
module.exports = Stats
7+
8+
function Stats (el, interval) {
9+
if (!(this instanceof Stats)) return new Stats(el, interval)
10+
var self = this
11+
self.ddbs = {}
12+
self.el = el
13+
14+
setInterval(function () {
15+
var keys = Object.keys(self.ddbs)
16+
for (var i = 0; i < keys.length; i++) {
17+
var st = self.ddbs[keys[i]]
18+
self.$$(keys[i], '.upload-speed').innerText = prettyBytes(st.uploadSpeed()) + '/s'
19+
self.$$(keys[i], '.download-speed').innerText = prettyBytes(st.downloadSpeed()) + '/s'
20+
}
21+
}, interval || 500)
22+
}
23+
24+
Stats.prototype.$$ = function (name, sel) {
25+
return this.el.querySelector('#' + (name || 'unknown') + ' ' + sel)
26+
}
27+
28+
Stats.prototype._get = function (name) {
29+
var self = this
30+
if (!name) name = 'unknown'
31+
var st = self.ddbs[name]
32+
if (!st) {
33+
var div = document.createElement('div')
34+
div.id = name
35+
div.innerHTML = self.el.querySelector('#template').innerHTML
36+
div.className = 'ddb'
37+
38+
if (name !== 'unknown') {
39+
div.querySelector('.name').innerText = name
40+
}
41+
42+
self.el.appendChild(div)
43+
44+
st = self.ddbs[name] = {
45+
blocks: 0,
46+
uploadSpeed: dweb-netspeed(),
47+
downloadSpeed: dweb-netspeed(),
48+
div: div
49+
}
50+
}
51+
52+
return st
53+
}
54+
55+
Stats.prototype._update = function () {
56+
var self = this
57+
var keys = Object.keys(self.ddbs)
58+
for (var i = 0; i < keys.length; i++) {
59+
var st = self.ddbs[keys[i]]
60+
self.$$(keys[i], '.upload-speed').innerText = prettyBytes(st.uploadSpeed()) + '/s'
61+
self.$$(keys[i], '.download-speed').innerText = prettyBytes(st.downloadSpeed()) + '/s'
62+
}
63+
}
64+
65+
Stats.prototype.onkey = function (data) {
66+
var self = this
67+
$('#key').innerText = data.key
68+
69+
while ($('.ddb')) self.el.removeChild($('.ddb'))
70+
self.ddbs = {}
71+
}
72+
73+
Stats.prototype.updateHeader = function (data) {
74+
this.$$(data.name, '.overview').innerText = data.blocks.length + ' blocks (' + prettyBytes(data.bytes) + ')'
75+
}
76+
77+
Stats.prototype.onpeerupdate = function (data) {
78+
$('#peers').innerText = 'Connected to ' + data.peers + ' peer' + (data.peers === 1 ? '' : 's')
79+
}
80+
81+
Stats.prototype.ondownload = function (data) {
82+
this._get(data.name).downloadSpeed(data.bytes)
83+
this.$$(data.name, '.block-' + data.index).style.backgroundColor = 'gray'
84+
}
85+
86+
Stats.prototype.onupload = function (data) {
87+
var self = this
88+
this._get(data.name).uploadSpeed(data.bytes)
89+
this.$$(data.name, '.block-' + data.index).style.backgroundColor = '#35b44f'
90+
setTimeout(function () {
91+
self.$$(data.name, '.block-' + data.index).style.backgroundColor = 'gray'
92+
}, 500)
93+
}
94+
95+
Stats.prototype.onddb = function (data) {
96+
var self = this
97+
var blocks = this._get(data.name).blocks = data.blocks.length
98+
self.updateHeader(data)
99+
100+
for (var i = 0; i < blocks; i++) {
101+
self._appendDot(data.name, i)
102+
if (data.blocks[i]) self.$$(data.name, '.block-' + i).style.backgroundColor = 'gray'
103+
}
104+
}
105+
106+
Stats.prototype.onupdate = function (data) {
107+
var self = this
108+
self.updateHeader(data)
109+
110+
for (var i = self._get(data.name).blocks; i < data.blocks.length; i++) {
111+
self._get(data.name).blocks++
112+
self._appendDot(data.name, i)
113+
if (data.blocks[i]) self.$$(data.name, '.block-' + i).style.backgroundColor = 'gray'
114+
}
115+
}
116+
117+
Stats.prototype._appendDot = function (name, i) {
118+
var div = document.createElement('div')
119+
div.className = 'dot block-' + i
120+
this.$$(name, '.blocks').appendChild(div)
121+
}

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@ddatabase/ui",
3+
"version": "0.1.1",
4+
"description": "UI For dDrives and dDatabase Management and Statistics.",
5+
"contributors": [
6+
"Jared Rice Sr. <jared@dwebs.io>",
7+
"Distributed Webs Project, LLC. <labs@distributedwebs.org>"
8+
],
9+
"license": "MIT",
10+
"main": "index.js",
11+
"dependencies": {
12+
"@dwcore/netspeed": "dwcore/netspeed#master",
13+
"browserify": "^16.1.1",
14+
"corsify": "^2.1.0",
15+
"event-source-stream": "^1.5.0",
16+
"pretty-bytes": "^4.0.2"
17+
},
18+
"devDependencies": {
19+
"@ddatabase/core": "ddatabase/core#master",
20+
"@ddrive/core": "ddrives/core#master",
21+
"@dwcore/rem": "dwcore/rem#master",
22+
"@ddatabase/stats-server": "ddatabase/stats-server#master",
23+
"@flockcore/revelation": "flockcore/revelation#master",
24+
"minimist": "^1.2.0"
25+
},
26+
"publishConfig": {
27+
"access": "public"
28+
}
29+
}

0 commit comments

Comments
 (0)