Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Average Node XP per Month

by choroba (Cardinal)
on Sep 18, 2013 at 13:15 UTC ( [id://1054649]=perlmeditation: print w/replies, xml ) Need Help??

Are you a regular monk? If yes, you might be interested in how the quality of your posts changes in time. At least I was curious about it, so I created a script to get the data from the appropriate XML ticker and display it as a graph in gnuplot. It shows the average XP per node for each month.
#!/usr/bin/perl use warnings; use strict; use WWW::Mechanize; use XML::LibXML; use constant { PM_URL => '', # Node ids: LOGIN => 109, XP => 32704, }; print "User? "; chomp(my $user = <>); print "Pass? "; chomp(my $pass = <>); my $mech = 'WWW::Mechanize'->new; $mech->get(PM_URL . LOGIN); $mech->submit_form( form_number => 1, fields => { user => $user, passwd => $pass, }, ); my %out; my $limit = 500; my $offset = 0; while (1) { $mech->get(PM_URL . XP . ";limit=$limit;offset=$offset"); my $xml = 'XML::LibXML'->load_xml(string => $mech->content); my @nodes = $xml->findnodes('/USERNODES/NODE'); last unless @nodes; for my $node (@nodes) { my ($date, $xp) = map $node->getAttribute($_), qw(createtime r +eputation); substr $date, 7, length($date), q{}; # Group by month. $out{$date}{count}++; $out{$date}{sum} += $xp; } $offset += $limit; } open my $OUT, '>', 'xp.out' or die $!; for my $date (sort keys %out) { print {$OUT} "$date\t", $out{$date}{sum} / $out{$date}{count}, "\n +"; } close $OUT or die $!; =for gnuplot set xdata time set timefmt '%Y-%m' set xtics format '%Y-%m' plot 'xp.out' using 1:2:(2e6) with boxes fs solid =cut

Please, do not run the script too often, as it might lag our already panting servers :-)

The script produces a file xp.out (I use a fixed name, so I can just copy&paste the POD section to gnuplot). If you do not happen to have gnuplot installed, you can use the following Perl programme (using Tk) to view the graph. Its input file defaults to xp.out, but you can specify any other file name on the command line.

#!/usr/bin/perl use warnings; use strict; use 5.010; # // use Tk; use constant MESSAGE => 'Point a mouse over a bar to see the details'; my $width = 25; my $height = 200; my $space = 10; my $input = shift // 'xp.out'; open my $IN, '<', $input or die $!; my @rectangles; my ($oldest, $max, $min) = (0, 0, 0); while (my $line = <$IN>) { my ($date, $xp) = split ' ', $line; my ($year, $month) = split /-/, $date; my $x = $year * 12 + $month; $oldest ||= $x; $max = $xp if $xp > $max; $min = $xp if $xp < $min; push @rectangles, [ $x - $oldest, $xp, $date ]; } my $step = $height / $max; my $mw = 'MainWindow'->new(-title => 'Average XP per month'); my $info = MESSAGE; $mw->Label(-textvariable => \$info, -foreground => 'black', -background => 'white', -relief => 'ridge', )->pack(-pady => 5); my $canvas = $mw->Canvas(-background => 'white', -width => $width * (1 + $rectangles[-1][ +0]), -height => $height + 2 * $space + $step * + abs $min, )->pack(-padx => 10, -pady => 10); for my $rect (@rectangles) { $canvas->createRectangle( $rect->[0] * $width, $space + $step * ($max - $rect->[1] +), ++$rect->[0] * $width, $space + $height, -fill => (qw/blue cyan green yellow orange red/) [($rect->[1] + abs $min) / ($max + abs $min) * + 5], -outline => 'black', -tag => "bar$rect->[0]", ); $canvas->bind("bar$rect->[0]", '<Any-Enter>' => sub { $info = "$rect->[2]: $rect->[ +1]" }); } $canvas->bind('all', '<Any-Leave>' => sub { $info = MESSAGE }); $mw->Button(-text => 'Quit', -command => sub { exit }, )->pack; MainLoop();

Related: Re: Zen and the art of ignoring XP.

Updated: Fixed the second script (line 41).

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re: Average Node XP per Month
by 5mi11er (Deacon) on Sep 22, 2013 at 16:42 UTC
    Perhaps this would be better placed on a public accessible page with results for the past cached and only posts from the previous month or so recalculated? Would save from having to beat the system for each monk that was interested...

    I'm having a vague memory that maybe I've seen something like that in the distant past.


      Even the nodes older than one month can still be voted for.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Yes, but I believe the amount of new XP points on nearly all nodes over a month old to be a tiny percentage of the votes cast.

        You could cache those older than a month results, and maybe recalculated them occasionally, maybe once every few months?


Re: Average Node XP per Month -- oneliner
by Discipulus (Canon) on Apr 26, 2017 at 12:30 UTC
    ..using directly the xp.out file you can have an yearly overview with:
    # win double quotes! perl -F"/\-|\s/" -lane "$m{$F[0]}[0]++;$m{$F[0]}[1]+=$F[2]}{map{print +join' ',$_,$m{$_}[1]/$m{$_}[0]}sort keys %m" xp.out

    PS thanks choroba for such a tool, to use not so often!


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1054649]
Approved by keszler
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-07-19 09:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.