File: Stats.pm

package info (click to toggle)
libtransmission-client-perl 0.0806-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 3,004; makefile: 2
file content (120 lines) | stat: -rw-r--r-- 2,300 bytes parent folder | download | duplicates (2)
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
# ex:ts=4:sw=4:sts=4:et
package Transmission::Stats;
# See Transmission::Client for copyright statement.

=head1 NAME

Transmission::Stats - Transmission session statistics

=head1 DESCRIPTION

See "4.2 Sesion statistics" from
L<https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt>

=cut

use Moose;
use Transmission::Types ':all';

with 'Transmission::AttributeRole';

=head1 ATTRIBUTES

=head2 active_torrent_count

 $num = $self->active_torrent_count;

=head2 download_speed

 $num = $self->download_speed;

=head2 paused_torrent_count

 $num = $self->paused_torrent_count;

=head2 torrent_count

 $num = $self->torrent_count;

=head2 upload_speed

 $num = $self->upload_speed;

=head1 METHODS

=head2 read_all

Initialize/update stats attributes using Transmission RPC (C<session-stats>).
Also returns all attributes as a hash reference (named as per the attributes of
the class).

=cut

my %both = (
    activeTorrentCount  => number,
    downloadSpeed       => number,
    pausedTorrentCount  => number,
    torrentCount        => number,
    uploadSpeed         => number,
);

for my $camel (keys %both) {
    (my $name = $camel) =~ s/([A-Z]+)/{ "_" .lc($1) }/ge;

    has $name => (
        is      => 'ro',
        isa     => $both{$camel},
        coerce  => 1,
        writer  => "_set_$name",
        clearer => "clear_$name",
        lazy    => 1,
        default => sub {
            my $self = shift;
            my $val = delete $self->_tmp_store->{$name};

            return $val if defined $val;

            $self->_clear_tmp_store;
            return delete $self->_tmp_store->{$name};
        },
    );
}

has _tmp_store => (
    is      => 'ro',
    isa     => 'HashRef',
    lazy    => 1,
    builder => 'read_all',
    clearer => '_clear_tmp_store',
);

sub read_all {
    my $self = shift;
    my $lazy = $self->lazy_write;
    my(%res, $rpc);

    $rpc = $self->client->rpc('session-stats') or return;

    $self->lazy_write(1);

    for my $camel (keys %both) {
        my $name = __PACKAGE__->_camel2Normal($camel);
        my $writer = "_set_$name";
        $res{$name} = $rpc->{$camel};
        $self->$writer($rpc->{$camel});
    }

    $self->lazy_write($lazy);

    return \%res;
}

=head1 LICENSE

=head1 AUTHOR

See L<Transmission::Client>

=cut

1;