summaryrefslogtreecommitdiffstats
path: root/static/new_status-dnbd3.html
blob: b4049bbd76dba3385ec0dbd907df5c1f9ff1aee5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
  <style type="text/css">
  
  body { margin: 0; }
  
  .container {
    display: flex;
  }
  
  .server {
    border-spacing: 5px;
    background-color: #eee;
    border: 1px solid #666;
  }
  
  .first-row {
    font-weight: bold;
  }
  
  .server-ip {
    text-align: center;
  }
  
  .client-label, .client-speed {
    border: 1px solid black;
    text-align: center;
  }
  
  .client-speed {
    width: 240px;
  }
  
  </style>
</head>
<body>

  <div id="app">
    <div class="container">
      <table v-for="server in servers" class="server">
        <tbody>
          <tr class="first-row"><td>Server IP:</td><td class="server-ip">{{ server.address }}</td></tr>
          <tr><td>Uptime:</td><td>{{ formatSeconds(server.uptime) }}</td></tr>
          <tr><td>Upload speed:</td><td>{{ formatBytes(server.uploadSpeed) +'/s' }}</td></tr>
          <tr><td>Download speed:</td><td>{{ formatBytes(server.downloadSpeed) +'/s' }}</td></tr>
          <tr><td>Total sent:</td><td>{{ formatBytes(server.bytesSent) }}</td></tr>
          <tr><td>Total received:</td><td>{{ formatBytes(server.bytesReceived) }}</td></tr>
          <tr v-for="client in server.clients">
            <td class="client-label">{{ client.address.split( ":" )[0] }}</td>
            <td class="client-speed" :style="calcBackgroundStyle(client.uploadSpeed)"><span>{{ formatBytes(client.uploadSpeed) + '/s' }}</span></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  
<script src="vue.min.js"></script>
<script>

var app = new Vue({
  el: '#app',
  data: {
    data: {},
    log: { server: {}, client: {} },
    timespan: 120
  },
  computed: {
    servers () {
      if (!this.data.servers) return []
      const servers = this.data.servers.slice(0)
      servers.sort((a, b) => b.clients.length - a.clients.length)
      servers.forEach(server => {
        this.calcSpeed('server', server)
        server.clients.forEach(client => {
          this.calcSpeed('client', client, server.timestamp)
        })
        server.clients = server.clients.filter(client => client.uploadSpeed > 0)
        server.clients.sort((a, b) => b.uploadSpeed - a.uploadSpeed)
      })
      return servers
    }
  },
  methods: {
    async updateData () {
      const response = await fetch('/data2.json')
      this.data = await response.json()
      setTimeout(this.updateData, 2000)
    },
    calcSpeed (type, obj, timestamp) {
      var log = this.log[type][obj.address]
      log = this.log[type][obj.address] = log ? log.filter(x => x.timestamp > this.data.timestamp - this.timespan * 1000) : []
      if (timestamp) obj.timestamp = timestamp
      log.push(obj)
      if (log.length > 0) {
        const a = log[0]
        const b = log[log.length - 1]
        const time = (b.timestamp - a.timestamp) / 1000
        obj.uploadSpeed = (b.bytesSent - a.bytesSent) / time
        if (type === 'server') obj.downloadSpeed = (b.bytesReceived - a.bytesReceived) / time
      }
    },
    formatBytes (bytes) {
      if (bytes < 1024) return bytes.toFixed(2) + ' Bytes'
      else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + ' KiB'
      else if (bytes < 1073741824) return (bytes / 1048576).toFixed(2) + ' MiB'
      else if (bytes < 1099511627776) return (bytes / 1073741824).toFixed(2) + ' GiB'
      else return (bytes / 1099511627776).toFixed(2) + ' TiB'
    },
    formatSeconds (seconds) {
      return ( Math.floor(seconds / ( 3600 * 24 ) ) + 'd '
             + Math.floor(seconds / 3600 ) % 24 + 'h '
             + Math.floor(seconds / 60 ) % 60 + 'min' )
    },
    calcBackgroundStyle (speed) {
      const percent = Math.round(Math.max(0, Math.min(100, Math.log(1 + speed * (100000 / 1073741824)) / Math.log(100000) * 100)))
      var color = '#056d00'
      if (speed < 1048576) color = '#8adb8b'
      else if (speed < 10485760) color = '#5cdb55'
      else if (speed < 104857600) color = '#26a81f'
      return { background: `linear-gradient(90deg, ${color} ${percent}%, #e0e0e0 ${percent}%)` }
    }
  },
  created () {
    const urlParams = new URLSearchParams(window.location.search)
    this.timespan = parseInt(urlParams.get('timespan')) || 120
    this.updateData()
  }
})

</script>
</body>
</html>