Skip to content

Commit

Permalink
perf: only update changed data + code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
WDaan committed Apr 4, 2021
1 parent 89cf6ba commit 664e4db
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 77 deletions.
14 changes: 14 additions & 0 deletions src/Helpers/ArrayHelper.js
@@ -0,0 +1,14 @@
import * as _ from 'lodash'

export class ArrayHelper {
static remove(array, item) {
const toRemove = Array.isArray(item) ? item : [item]
_.remove(array, item => toRemove.indexOf(item) > -1)

return array
}

static concat(a, b) {
return _.concat(a, b)
}
}
13 changes: 13 additions & 0 deletions src/Helpers/Hostname.js
@@ -0,0 +1,13 @@
export class Hostname {
static get(url) {
const match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i)
if (match != null &&
match.length > 2 &&
typeof match[2] === 'string' &&
match[2].length > 0) {
return match[2]
} else {
return ''
}
}
}
6 changes: 6 additions & 0 deletions src/Helpers/index.js
@@ -0,0 +1,6 @@
import { ArrayHelper } from './ArrayHelper'
import { Hostname } from './Hostname'

export {
ArrayHelper, Hostname
}
22 changes: 13 additions & 9 deletions src/actions/DocumentTitle.js
@@ -1,29 +1,33 @@
import { formatBytes } from '@/helpers'
import store from '../store'


export class DocumentTitle {
static setDefault() {
this.set('VueTorrent')
}

static setGlobalSpeed(speeds) {
if (!speeds || speeds.length !== 2) return
this.set(`[D: ${formatBytes(speeds[0])}/s, U: ${formatBytes(speeds[1])}/s] VueTorrent`)
static setGlobalSpeed() {
const status = store.getters.getStatus()
this.set(`[D: ${formatBytes(status.upspeed)}/s, U: ${formatBytes(status.dlspeed)}/s] VueTorrent`)
}

static setFirstTorrentStatus(torrent) {
if (!torrent) return
static setFirstTorrentStatus() {
const torrents = store.getters.getTorrents()
if (!torrents && !torrents.length) return
const torrent = torrents[0]
this.set(`[D: ${formatBytes(torrent.dlspeed)}/s, U: ${formatBytes(torrent.upspeed)}/s] ${torrent.progress}%`)
}

static updateTitle(mode, speeds, torrent) {
if (!mode || !speeds.length || !torrent) return
static update() {
const mode = store.getters.getWebuiSettings().title
switch (mode) {
case 'Default':
return this.setDefault()
case 'Global Speed':
return this.setGlobalSpeed(speeds)
return this.setGlobalSpeed()
case 'First Torrent Status':
return this.setFirstTorrentStatus(torrent)
return this.setFirstTorrentStatus()
default:
return this.setDefault()
}
Expand Down
11 changes: 11 additions & 0 deletions src/actions/Graph.js
@@ -0,0 +1,11 @@
import store from '../store'

export class Graph {
static update() {
const state = store.state
state.download_data.shift()
state.download_data.push(state.status.dlspeedRaw || 0)
state.upload_data.shift()
state.upload_data.push(state.status.upspeedRaw || 0)
}
}
10 changes: 10 additions & 0 deletions src/actions/ServerStatus.js
@@ -0,0 +1,10 @@
import store from '../store'
import Status from '@/models/Status'

export class ServerStatus {
static update(response) {
if (response.server_state) {
store.state.status = new Status(response.server_state)
}
}
}
24 changes: 24 additions & 0 deletions src/actions/Tags.js
@@ -0,0 +1,24 @@
import store from '../store'
import { ArrayHelper } from '@/Helpers'

export class Tags {
static update(response) {
if (response?.full_update === true) {
store.state.tags = response.tags

return
}

if (response.tags_removed) {
store.state.tags = ArrayHelper.remove(store.state.tags, response.tags_removed)

return
}

if (response.tags) {
store.state.tags = ArrayHelper.concat(store.state.tags, response.tags)


}
}
}
17 changes: 17 additions & 0 deletions src/actions/Torrents.js
@@ -0,0 +1,17 @@
import store from '../store'
import { Hostname } from '@/Helpers'
import Torrent from '@/models/Torrent'

export class Torrents {
static update(data) {
if (store.state.webuiSettings.showTrackerFilter) { // dont calculate trackers when disabled

if (store.state.sort_options.tracker !== null) {
data = data.filter(d => Hostname.get(d.tracker) === store.state.sort_options.tracker)
}
}

// update torrents
store.state.torrents = data.map(t => new Torrent(t))
}
}
13 changes: 13 additions & 0 deletions src/actions/Trackers.js
@@ -0,0 +1,13 @@
import store from '../store'
import { Hostname } from '@/Helpers'

export class Trackers {
static update(data) {
if (store.state.webuiSettings.showTrackerFilter) {
store.state.trackers = data.map(t => t.tracker)
.map(url => Hostname.get(url))
.filter((domain, index, self) => index === self.indexOf(domain) && domain)
.sort()
}
}
}
8 changes: 7 additions & 1 deletion src/actions/index.js
@@ -1,5 +1,11 @@
import { DocumentTitle } from './DocumentTitle'
import { Tags } from './Tags'
import { Torrents } from './Torrents'
import { Trackers } from './Trackers'
import { Graph } from './Graph'
import { ServerStatus } from './ServerStatus'

export {
DocumentTitle
DocumentTitle, Tags, Torrents, Trackers,
Graph, ServerStatus
}
39 changes: 25 additions & 14 deletions src/models/Status.js
@@ -1,18 +1,29 @@
import store from '../store'

export default class Status {
constructor(data, tags) {
if (data) {
this.status = data.connection_status
this.downloaded = data.dl_info_data
this.uploaded = data.up_info_data
this.dlspeed = data.dl_info_speed
this.upspeed = data.up_info_speed
this.freeDiskSpace = data.free_space_on_disk
this.altSpeed = data.use_alt_speed_limits
this.dlspeedRaw = this.formatSpeed(data.dl_info_speed)
this.upspeedRaw = this.formatSpeed(data.up_info_speed)
this.tags = tags
Object.freeze(this)
}
constructor({
connection_status,
dl_info_data,
up_info_data,
dl_info_speed,
up_info_speed,
free_space_on_disk,
use_alt_speed_limits
}) {

const previous = store.state.status

this.status = connection_status || previous.status
this.downloaded = dl_info_data || previous.downloaded
this.uploaded = up_info_data || previous.uploaded
this.dlspeed = dl_info_speed || previous.dlspeed
this.upspeed = up_info_speed || previous.upspeed
this.freeDiskSpace = free_space_on_disk || previous.freeDiskSpace
this.altSpeed = use_alt_speed_limits || previous.altSpeed
this.dlspeedRaw = this.formatSpeed(dl_info_speed) || previous.dlspeedRaw
this.upspeedRaw = this.formatSpeed(up_info_speed) || previous.upspeedRaw
Object.freeze(this)

}

formatSpeed(value) {
Expand Down
29 changes: 15 additions & 14 deletions src/services/qbit.js
Expand Up @@ -12,7 +12,7 @@ class Qbit {
execute(method, action, params) {
if (method === 'post') {
const data = new URLSearchParams(params)

return this.axios.post(action, data).then(res => res.data)
}
}
Expand All @@ -35,7 +35,7 @@ class Qbit {
return status === 200 || status === 403
}
})

return data
}

Expand All @@ -62,6 +62,7 @@ class Qbit {
getMainData(rid) {
return this.axios.get(
'/sync/maindata', { params: { rid } })
.then(res => res.data)
}

switchToOldUi() {
Expand Down Expand Up @@ -121,7 +122,7 @@ class Qbit {
hash,
name
}

return this.axios.get('/torrents/rename', { params })
}

Expand Down Expand Up @@ -167,7 +168,7 @@ class Qbit {
} else {
data = new URLSearchParams(params)
}

return this.axios.post('/torrents/add', data)
}

Expand All @@ -177,13 +178,13 @@ class Qbit {
id: idList.join('|'),
priority
}

return this.execute('post', '/torrents/filePrio', params)
}

deleteTorrents(hashes, deleteFiles) {
if (!hashes.length) return

return this.torrentAction('delete', hashes, { deleteFiles })
}

Expand Down Expand Up @@ -248,16 +249,16 @@ class Qbit {
hash,
urls: trackers
}

return this.execute('post', '/torrents/addTrackers', params)
}

removeTorrentTrackers(hash, trackers) {
const params = {
hash,
urls: trackers.join('|')
urls: trackers.join('|')
}

return this.execute('post', '/torrents/removeTrackers', params)
}

Expand All @@ -266,7 +267,7 @@ class Qbit {
hashes: hashes.length ? hashes.join('|') : 'all',
...extra
}

return this.execute('post', `/torrents/${action}`, params)
}

Expand All @@ -276,7 +277,7 @@ class Qbit {
id,
name
}

return this.execute('post', '/torrents/renameFile', params)
}

Expand Down Expand Up @@ -344,7 +345,7 @@ class Qbit {
category: cat.name,
savePath: cat.savePath
}

return this.execute('post', '/torrents/editCategory', params)
}

Expand All @@ -359,7 +360,7 @@ class Qbit {
names: plugins.join('|'),
enable
}

return this.execute('post', '/search/enablePlugin', params)
}

Expand All @@ -369,7 +370,7 @@ class Qbit {
plugins: Array.isArray(plugins) ? plugins.join('|') : 'all',
category: 'all'
}

return this.execute('post', '/search/start', params)
}

Expand Down
2 changes: 1 addition & 1 deletion src/store/getters.js
Expand Up @@ -9,7 +9,7 @@ export default {
getTorrent: state => hash =>
state.torrents.filter(el => el.hash === hash)[0],
getWebuiSettings: state => () => state.webuiSettings,
getAvailableTags: state => () => state.status.tags,
getAvailableTags: state => () => state.tags,
getCategories: state => () => state.categories,
getModals: state => () => state.modals,
getTorrents: state => () => state.torrents,
Expand Down
1 change: 1 addition & 0 deletions src/store/index.js
Expand Up @@ -97,6 +97,7 @@ export default new Vuex.Store({
},
categories: [],
trackers: [],
tags: [],
filteredTorrentsCount: 0,
latestSelectedTorrent: null,
selectMode: false,
Expand Down

1 comment on commit 664e4db

@m4ximuel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 'scr/models/Status.js'
It should be replaced as instructed below.

this.dlspeed = dl_info_speed || previous.dlspeed
this.upspeed = up_info_speed || previous.upspeed

this.dlspeedRaw = this.formatSpeed(dl_info_speed) || previous.dlspeedRaw
this.upspeedRaw = this.formatSpeed(up_info_speed) || previous.upspeedRaw

this.dlspeed = dl_info_speed || 0
this.upspeed = up_info_speed || 0

this.dlspeedRaw = this.formatSpeed(dl_info_speed) || 0
this.upspeedRaw = this.formatSpeed(up_info_speed) || 0

Because those 4 items are delivered even if the values are the same unless they are requested faster than Qbittorrent's internal system update timer, and if the value is 0, no reply is made.

Please sign in to comment.