summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/status.py
blob: 84cf21e947b8b12f435ecacd7fd921fe2e73d4fe (plain)
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Nick Lanham <nick@afternight.org>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import logging

from twisted.internet import defer

import deluge.component as component
from deluge.common import TORRENT_STATE, fspeed
from deluge.ui.client import client

from . import BaseCommand

log = logging.getLogger(__name__)


class Command(BaseCommand):
    """Shows various status information from the daemon"""

    def add_arguments(self, parser):
        parser.add_argument(
            '-r',
            '--raw',
            action='store_true',
            default=False,
            dest='raw',
            help=_(
                'Raw values for upload/download rates (without KiB/s suffix)'
                '(useful for scripts that want to do their own parsing)'
            ),
        )
        parser.add_argument(
            '-n',
            '--no-torrents',
            action='store_false',
            default=True,
            dest='show_torrents',
            help=_('Do not show torrent status (Improves command speed)'),
        )

    def handle(self, options):
        self.console = component.get('ConsoleUI')
        self.status = None
        self.torrents = 1 if options.show_torrents else 0
        self.raw = options.raw

        def on_session_status(status):
            self.status = status

        def on_torrents_status(status):
            self.torrents = status

        def on_torrents_status_fail(reason):
            log.warning('Failed to retrieve session status: %s', reason)
            self.torrents = -2

        deferreds = []

        ds = client.core.get_session_status(
            ['num_peers', 'payload_upload_rate', 'payload_download_rate', 'dht_nodes']
        )
        ds.addCallback(on_session_status)
        deferreds.append(ds)

        if options.show_torrents:
            dt = client.core.get_torrents_status({}, ['state'])
            dt.addCallback(on_torrents_status)
            dt.addErrback(on_torrents_status_fail)
            deferreds.append(dt)

        return defer.DeferredList(deferreds).addCallback(self.print_status)

    def print_status(self, *args):
        self.console.set_batch_write(True)
        if self.raw:
            self.console.write(
                '{!info!}Total upload: %f' % self.status['payload_upload_rate']
            )
            self.console.write(
                '{!info!}Total download: %f' % self.status['payload_download_rate']
            )
        else:
            self.console.write(
                '{!info!}Total upload: %s' % fspeed(self.status['payload_upload_rate'])
            )
            self.console.write(
                '{!info!}Total download: %s'
                % fspeed(self.status['payload_download_rate'])
            )
        self.console.write('{!info!}DHT Nodes: %i' % self.status['dht_nodes'])

        if isinstance(self.torrents, int):
            if self.torrents == -2:
                self.console.write('{!error!}Error getting torrent info')
        else:
            self.console.write('{!info!}Total torrents: %i' % len(self.torrents))
            state_counts = {}
            for state in TORRENT_STATE:
                state_counts[state] = 0
            for t in self.torrents:
                s = self.torrents[t]
                state_counts[s['state']] += 1
            for state in TORRENT_STATE:
                self.console.write('{!info!} %s: %i' % (state, state_counts[state]))

        self.console.set_batch_write(False)