Something like:

use strict; use warnings; my %urls = ( google => {min => 0, max => 20}, yahoo => {min => -3, max => 10}, msn => {min => -5, max => 5}, ); my @units = ( {unit => 's', tipAt => 60, updateAt => 20}, {unit => 'min', tipAt => 60, updateAt => 1}, {unit => 'h', tipAt => 24, updateAt => 1}, {unit => 'day', tipAt => 0, updateAt => 0}, ); my %stats; buildStructure (\%stats, \%urls, @units); srand (1); for my $second (1 .. 24 * 60 * 60) { my %hits = genHits (%urls); for my $url (keys %hits) { my $hit = $hits{$url}; my $bump = 1; my $updateHit = 0; my $updateCount = 0; my $urlStats = $stats{$url} ||= {}; $urlStats->{total} += $hit; for my $unit (@units) { my $unitStats = $urlStats->{$unit->{unit}}; if ($updateCount) { $unitStats->{extraHits} = $updateHit; $unitStats->{extraCount} = $updateCount; } last unless $bump; $bump = 0; $unitStats->{lhits} ||= 0; $hit = $unitStats->{hits} += $hit; ++$unitStats->{count}; if ($unit->{updateAt} and ! ($unitStats->{count} % $unit-> +{updateAt})) { $updateHit = $unitStats->{hits}; $updateCount = $unitStats->{count} / $unit->{tipAt}; } next if ! $unit->{tipAt} or $unitStats->{count} < $unit->{ +tipAt}; # Tipping time $unitStats->{lhits} = $unitStats->{hits}; $unitStats->{hits} = 0; $unitStats->{count} = 0; $unitStats->{extraHits} = 0; $unitStats->{extraCount} = 0; $bump = 1; } } next if int rand (1000); # Gen header printf "%-10s" . ((' %7s %7s') x @units) . "\n", 'URL', map {("avg/$_->{unit}", "cur/$_->{unit}")} @units; # Show stats for my $url (keys %hits) { my $urlStats = $stats{$url}; my $total = $urlStats->{total}; my $scale = 1; printf "%-10s", $url; for my $unit (@units) { my $unitStats = $urlStats->{$unit->{unit}}; my $avg = $total / $second * $scale; my $cur = $unitStats->{hits} + $unitStats->{lhits} + $unit +Stats->{extraHits}; my $curScale = $unitStats->{count} + $unitStats->{extraCou +nt}; $scale *= $unit->{tipAt} if $unit->{tipAt}; $curScale += $unit->{tipAt} if $second > $scale; $cur /= $curScale if $curScale; printf ' %7d %7d', $avg, $cur; } print "\n"; } } exit; sub buildStructure { my ($stats, $urls, @units) = @_; for my $url (keys %urls) { my $urlStats = $stats->{$url} = {}; for my $unit (@units) { my $unitStats = $urlStats->{$unit->{unit}} = {}; $unitStats->{$_} ||= 0 for qw(count hits lhits extraCount extraHits); } } } sub genHits { my (%urls) = @_; my %hits; for my $url (keys %urls) { my $hits = rand ($urls{$url}{max} - $urls{$url}{min}) + $urls{$url}{m +in}; $hits = 0 if $hits < 0; $hits{$url} = int $hits; } return %hits; }

Prints:

... URL avg/s cur/s avg/min cur/min avg/h cur/h avg/day cur +/day google 9 9 573 571 34407 34409 825779 82 +6904 yahoo 3 3 208 210 12503 12502 300079 29 +9128 msn 1 0 60 59 3617 3617 86809 8 +6696 URL avg/s cur/s avg/min cur/min avg/h cur/h avg/day cur +/day google 9 8 572 569 34365 34384 824771 82 +6904 yahoo 3 3 208 211 12524 12517 300588 29 +9128 msn 1 0 60 58 3601 3603 86427 8 +6696 ...

Perl reduces RSI - it saves typing

In reply to Re^3: Data structure for statistics by GrandFather
in thread Data structure for statistics by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.