Changeset 36

Show
Ignore:
Timestamp:
10/07/06 16:15:12 (7 years ago)
Author:
max
Message:

Some more intelli-random grabber selection order.

Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • shepherd

    r35 r36  
    119119my $config_file =   "$CWD/$progname.conf"; 
    120120my $channels_file = "$CWD/channels.conf"; 
    121 my $days; 
     121my $days = 7; 
    122122 
    123123# postprocessing 
     
    322322    my $total = score_grabbers(); 
    323323 
    324     print "Scoring unused valid grabbers:\n" . Dumper($gscore); 
     324    return undef unless ($total); 
     325 
     326    if ($debug) 
     327    { 
     328        print "Grabber selection probabilities:\n"; 
     329        foreach (keys %$gscore) 
     330        { 
     331            printf "%15s %6.1f%% %12s\n",  
     332                   $_, 100 * $gscore->{$_} / $total, "($gscore->{$_} pts)"; 
     333        } 
     334    } 
    325335 
    326336    return undef unless (scalar keys %$gscore); 
     
    332342    foreach (keys %$gscore) 
    333343    { 
     344        next if (!$gscore->{$_}); 
    334345        if ($r >= $c and $r < ($c + $gscore->{$_})) 
    335346        { 
    336347            delete $gscore->{$_}; 
     348            print "Selected $_.\n" if ($debug); 
    337349            return $_; 
    338350        } 
     
    344356sub score_grabbers 
    345357{ 
    346     my ($score, $total); 
    347     foreach (keys %$gscore) 
    348     { 
    349         $score = 10; 
    350         $gscore->{$_} = $score; 
     358    my ($score, $total, $niceness, $granularity, $m); 
     359 
     360    my $missing = detect_missing_data(); 
     361 
     362    foreach my $grabber (keys %$gscore) 
     363    { 
     364        # for each slot, say whether we can fill it or not -- that is, 
     365        # whether we support this channel and this day #. 
     366         
     367        my $hits = 0; 
     368        foreach (@$missing) 
     369        { 
     370            my ($ch, $day) = split(/:/); 
     371            my $result = (can_support_channel($grabber, $ch) 
     372                          and 
     373                          can_support_day($grabber, $day)); 
     374            $hits += $result; 
     375#           print "$grabber vs $ch:$day: " . ($result ? "OK" : "no") . "\n"; 
     376        } 
     377        $niceness = $grabbers->{$grabber}->{config}->{niceness}; 
     378        unless ($niceness) 
     379        { 
     380            print "WARNING: Grabber $grabber has no niceness support " . 
     381                  "specified in config.\n"; 
     382            $niceness = 5; 
     383        } 
     384        $granularity = $grabbers->{$grabber}->{config}->{granularity}; 
     385        unless (defined $granularity) 
     386        { 
     387            print "WARNING: Grabber $grabber has no granularity support " . 
     388                "specified in config.\n"; 
     389            $granularity = ''; 
     390        } 
     391        # TODO: use granularity more intelligently ('c' vs 'd') -- Max. 
     392        $granularity = length ($granularity); 
     393        my $total_channeldays = $days * scalar (keys %$channels); 
     394        $granularity *= int((($total_channeldays - 1 ) / scalar(@$missing))/2); 
     395 
     396        $score = $hits * ($niceness + $granularity); 
     397        print "Grabber $grabber can fill $hits empty slots with $niceness niceness and $granularity granularity: scoring $score pts.\n"; 
     398        $gscore->{$grabber} = $score; 
    351399        $total += $score; 
    352400    } 
    353401    return $total; 
     402} 
     403 
     404sub can_support_channel 
     405{ 
     406    my ($grabber, $ch) = @_; 
     407 
     408    my $channels_supported = $grabbers->{$grabber}->{config}->{channels}; 
     409    unless (defined $channels_supported) 
     410    { 
     411        print "WARNING: Grabber $grabber has no channel support " . 
     412              "specified in config.\n"; 
     413        $channels_supported = ''; 
     414    } 
     415 
     416    return 1 unless ($channels_supported); # Empty string means we support all 
     417     
     418    my $match = ($channels_supported =~ /\b$ch\b/); 
     419#    $match ||= 0; 
     420    my $exceptions = ($channels_supported =~/^-/); 
     421#    $exceptions ||= 0; 
     422#       print "M: " . Dumper ($match) . "E:" . Dumper ($exceptions) . "\n"; 
     423#    print "Can $grabber support channel $ch: " . ($match != $exceptions ? 1 : 0) .  
     424        #      " (match $match, exceptions: $exceptions).\n"; 
     425    return ($match != $exceptions); 
     426} 
     427 
     428sub can_support_day 
     429{ 
     430    my ($grabber, $day) = @_; 
     431 
     432    my $days_supported = $grabbers->{$grabber}->{config}->{max_days}; 
     433    unless ($days_supported) 
     434    { 
     435        print "WARNING: Grabber $grabber has no max_days support " . 
     436              "specified in config.\n"; 
     437        $days_supported = 2; 
     438    } 
     439#    print "Can $grabber support day $day: " . ($day <= $days_supported) . ".\n"; 
     440    return $day <= $days_supported; 
     441} 
     442 
     443#  
     444# Build a little hash of what channel/day data we're currently missing. 
     445# I think granularity of one day is good for now; could possibly be 
     446# made more fine-grained if we think grabbers will support that. 
     447# 
     448sub detect_missing_data 
     449{ 
     450    my @missing; 
     451 
     452    my $timeslots_per_day = (24 * 60 * 60) / $timeslot_size; 
     453 
     454    foreach my $ch (keys %$channels) 
     455    { 
     456        if (defined $channel_data->{$ch}) 
     457        { 
     458            my $slotnum; 
     459            for ($slotnum = 0; $slotnum < $num_timeslots-1; $slotnum++)  
     460            { 
     461                if (!@{$channel_data->{$ch}->{timeslots}}[$slotnum]) 
     462                { 
     463                    my $day = int($slotnum / $timeslots_per_day) + 1; 
     464                    push @missing, "$ch:$day"; 
     465                    $slotnum += $timeslots_per_day - 
     466                                ($slotnum % $timeslots_per_day); 
     467 
     468                } 
     469            } 
     470        } 
     471        else 
     472        { 
     473            for my $i (1 .. $days) 
     474            { 
     475                push (@missing, "$ch:$i"); 
     476            } 
     477        } 
     478 
     479    } 
     480 
     481    print "Need data for @missing.\n" if ($debug); 
     482    return \@missing; 
    354483} 
    355484