Skip to content

Commit

Permalink
status
Browse files Browse the repository at this point in the history
  • Loading branch information
simon300000 committed Oct 23, 2019
1 parent 38e38e2 commit 33654b0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
50 changes: 47 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ <h1 class="title">
</h1>
<h2 class="subtitle">DD@Home</h2>
<br>
<nav class="level">
<div class="level-item has-text-centered">
<nav class="level has-text-centered">
<div class="level-item">
<div>
<p class="heading">已处理请求</p>
<p class="title">{{state.completeNum}}</p>
</div>
</div>
<div class="level-item has-text-centered">
<div class="level-item">
<div>
<p class="heading">运行时间</p>
<p class="title">{{uptime}}</p>
Expand All @@ -62,6 +62,50 @@ <h2 class="subtitle">DD@Home</h2>

<hr>

<section class="section">
<div class="container">
<h1 class="title">状态</h1>
<h2 class="subtitle">Service</h2>
</div>
<br>
<nav class="level has-text-centered">
<div class="level-item">
<div>
<p class="heading">目前在线</p>
<p class="title">{{state.online}}</p>
</div>
</div>
<div class="level-item">
<div>
<p class="heading">任务</p>
<p class="title">{{state.pending}}</p>
</div>
</div>
<div class="level-item">
<div>
<p class="heading">Pulls</p>
<p class="title">{{state.pulls}}</p>
</div>
</div>
</nav>

<div class="table-container">
<table class="table is-striped is-hoverable is-fullwidth">
<tbody>
<tr v-for="home in homes" :key="home.id">
<td>{{home.name}}@{{home.runtime}}</td>
<td>{{home.resolves}}/{{home.sum}}</td>
<td>{{home.platform}}</td>
<td>{{home.version}}</td>
</tr>
</tbody>
</table>
</div>

</section>

<hr>

<section class="section">
<div class="container">
<h1 class="title">设置</h1>
Expand Down
19 changes: 18 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ new Vue({
updateProgress: undefined,
updateDownloaded: undefined,
nickname: undefined,
log: undefined
log: undefined,
pending: undefined,
pulls: undefined,
online: undefined,
homes: []
},
logs: [],
uptime: undefined,
Expand Down Expand Up @@ -60,6 +64,19 @@ new Vue({
computed: {
intervalWarning() {
return this.interval && Number(this.interval) < 400
},
homes() {
return this.state.homes
.map(({ runtime = 'Home', version = '', docker, platform = '', name = 'DD', resolves, rejects, id }) => ({
id,
name,
sum: resolves + rejects,
resolves,
runtime,
platform: docker || platform,
version
}))
.sort(({ resolves: a }, { resolves: b }) => b - a)
}
},
async created() {
Expand Down
4 changes: 4 additions & 0 deletions src/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = ({ getWin, state, stateEmitter, getWs, updateInterval, quitAndI
subscribe('update')
subscribe('updateProgress')
subscribe('updateDownloaded')
subscribe('pending')
subscribe('pulls')
subscribe('online')
subscribe('homes')

ipcMain.on('get', (e, channel, key, ...args) => {
const route = router[channel]
Expand Down
63 changes: 44 additions & 19 deletions src/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ const wait = ms => new Promise(resolve => setTimeout(resolve, ms))

const parse = string => {
try {
const json = JSON.parse(string)
if (json) {
const { key, data: { type, url } } = json
if (type === 'http') {
return { key, url }
}
}
return JSON.parse(string)
} catch (_) {
return undefined
}
Expand All @@ -29,7 +23,21 @@ module.exports = async ({ state, db }) => {
const PARALLEL = 128
let INTERVAL = await db.get('INTERVAL').catch(() => 680)
let nickname = await db.get('nickname').catch(() => undefined)
let ws
let ws = {}
const queryTable = new Map()

const secureSend = data => {
if (ws.readyState === 1) {
ws.send(data)
return true
}
}

const ask = query => new Promise(resolve => {
const key = String(Math.random())
queryTable.set(key, resolve)
secureSend(JSON.stringify({ key, query }))
})

const connect = () => new Promise(resolve => {
const url = new URL('wss://cluster.vtbs.moe')
Expand All @@ -49,22 +57,23 @@ module.exports = async ({ state, db }) => {

ws = new WebSocket(url)

const secureSend = data => {
if (ws.readyState === 1) {
ws.send(data)
return true
}
}

const pending = []

ws.on('message', async message => {
const json = parse(message)
if (json) {
const { key, data } = parse(message)
const { type } = data
if (type === 'http') {
const { url } = data
const resolve = pending.shift()
if (resolve) {
console.log('job received', json.url)
resolve(json)
console.log('job received', url)
resolve({ key, url })
}
} else if (type === 'query') {
if (queryTable.has(key)) {
const { result } = data
queryTable.get(key)(result)
queryTable.delete(key)
}
}
})
Expand Down Expand Up @@ -107,6 +116,7 @@ module.exports = async ({ state, db }) => {

ws.on('close', (n, reason) => {
state.log = `closed, ${n}, ${reason}`
queryTable.clear()
console.log('closed', n, reason)
if (reason === 'User Reload') {
resolve()
Expand All @@ -124,6 +134,21 @@ module.exports = async ({ state, db }) => {
}
})()

setInterval(async () => {
if (ws.readyState === 1) {
['pending', 'pulls', 'online'].map(async key => {
state[key] = await ask(key)
})
state.homes = await ask('homes')
}
}, 1000)

setInterval(async () => {
if (ws.readyState === 1) {
// state.homes = await ask('homes')
}
}, 1000 * 15)

const getWs = () => ws
const updateInterval = interval => {
db.put('INTERVAL', interval)
Expand Down

0 comments on commit 33654b0

Please sign in to comment.