summaryrefslogtreecommitdiffstats
path: root/frikanalen/bin/frikanalen-tonostats
blob: 1acdcef4123b5a8e50d8d9c3e787a5f6453c7781 (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
#!/usr/bin/perl

use strict;
use warnings;
use vars qw(%opts %orgs);
use Data::Dumper;

use Getopt::Std;

use JSON;
use LWP::Simple;

sub get_tono_stats_page {
    my $url = shift;
    my $jsonstr = get($url);
    my $json = decode_json( $jsonstr );
#    print Dumper($json);
    unless ($json->{'results'}) {
        return;
    }
    my $tono = 0;
    my $notono = 0;
    foreach my $video (@{$json->{'results'}}) {
        if (exists $video->{'has_tono_records'}
            && $video->{'has_tono_records'}) {
            $tono++;
            $orgs{$video->{'organization'}}{tono}++;
            push(@{$orgs{$video->{'organization'}}{tonoids}},
                 $video->{id});
#            print Dumper($video);
#            exit 0;
        } else {
            $notono++;
            $orgs{$video->{'organization'} || ""}{notono}++;
        }
    }
    if (defined $json->{'next'}) {
        my ($newtono, $newnotono) = get_tono_stats_page($json->{'next'});
        $tono += $newtono;
        $notono += $newnotono;
    }
    return ($tono, $notono);
}

sub get_tono_stats {
    my $url = 'http://beta.frikanalen.tv/ws/videos/';
    return get_tono_stats_page($url);
}
sub print_tono_stats {
    my ($with, $without) = get_tono_stats();
    printf("Innslag med tono-musikk:   %d (%.1f%%)\n",   $with, 100 * $with / ($with+$without));
    printf("Innslag uten tono-musikk:  %d (%.1f%%)\n",$without, 100 *$without/($with+$without));

    print "Andel\tmed\tuten\tnavn\n";
    for my $org (sort tonoorder keys %orgs) {
        my $tono = $orgs{$org}{tono} || 0;
        my $notono = $orgs{$org}{notono} || 0;
        my $frac = 0;
        $frac = 100 * $tono / $with if $with;
        printf "%4.1f\t%d\t%d\t%s\n", $frac, $tono, $notono, $org;
        print join(" ", "  ", @{$orgs{$org}{tonoids}}), "\n"
            if $opts{'i'} && exists $orgs{$org}{tonoids};
    }
}

sub tonoorder {
   my $order = ($orgs{$b}{tono} || 0) <=> ($orgs{$a}{tono} || 0);
   if (0 == $order) {
      $order = ($orgs{$b}{notono} || 0) <=> ($orgs{$a}{notono} || 0);
   }
   return $order;
}

sub munin_plugin {
    my @ARGV = @_;
    if (defined $ARGV[0] && "config" eq $ARGV[0]) {
        print <<EOF;
graph_title Fraction of events with Tono-related music
graph_category Frikanalen
tono_frac.label fraction
EOF
    } else {
        my ($with, $without) = get_tono_stats();
        printf "tono_frac.value %.3f\n", ($with / ($with + $without));
    }
}

getopts("im", \%opts);

if ($opts{'m'}) {
    munin_plugin(@ARGV);
} else {
    print_tono_stats();
}