Changeset 113
Legend:
- Unmodified
- Added
- Removed
-
shepherd
r112 r113 118 118 my $days = 7; 119 119 my $timeslice; 120 my $grabbed; 120 121 my $gmt_offset; 121 122 my $grabber_found_all_data; … … 276 277 { 277 278 print "CAT1 grabber: grabbing timeslice.\n" if ($debug); 279 280 record_requested_chandays($grabber, $timeslice); 281 278 282 if ($timeslice->{start} != 1) 279 283 { … … 342 346 343 347 my $missing_after = convert_chandays_to_list(detect_missing_data()); 344 #print "Missing before: " . Dumper($missing_before) . "\nMissing after:" . Dumper($missing_after);348 # print "Missing before: " . Dumper($missing_before) . "\nMissing after:" . Dumper($missing_after); 345 349 my $list = List::Compare->new($missing_before, $missing_after); 346 350 my @grabbed = $list->get_symmetric_difference(); … … 393 397 # score grabbers 394 398 my $total = score_grabbers(); 395 return undef unless ($total);396 399 397 400 if ($debug) 398 401 { 399 402 print "Grabber selection probabilities:\n"; 403 my $perc; 400 404 foreach (sort { $gscore->{$b} <=> $gscore->{$a} } keys %$gscore) 401 405 { 406 $perc = 100 * ($total ? $gscore->{$_} / $total : 0); 402 407 printf "%25s %6.1f%% %12s\n", 403 $_, 100 * $gscore->{$_} / $total, "($gscore->{$_} pts)";404 } 405 } 406 407 return undef unless ( scalar keys %$gscore);408 $_, $perc, "($gscore->{$_} pts)"; 409 } 410 } 411 412 return undef unless ($total); 408 413 409 414 my $r = int(rand($total)); … … 415 420 if ($r >= $c and $r < ($c + $gscore->{$grabber})) 416 421 { 417 delete $gscore->{$grabber};418 422 print "Selected $grabber.\n" if ($debug); 419 423 if ($grabber =~ /(.*) \[cache\]/) … … 454 458 my $missing = detect_missing_data(); 455 459 456 calculate_best_timeslice($missing);460 $timeslice = calculate_best_timeslice($missing); 457 461 458 462 print "Best timeslice: " . Dumper($timeslice) . "\n" if ($debug); … … 476 480 if ($cat == 1) 477 481 { 478 $key = $missing_slice;482 $key = cut_down($grabber, $missing_slice); 479 483 print "Grabber $grabber is Category 1: comparing capability to best timeslice.\n" if ($debug); 480 484 } … … 591 595 my $hits = 0; 592 596 597 # print "Find cache hits in: " . Dumper($missing); 593 598 foreach my $day (keys %$missing) 594 599 { … … 596 601 foreach my $ch (@{$missing->{$day}}) 597 602 { 598 $hits++ if (grep(/^$date:$ch$/, @{$components->{$grabber}->{cached}})); 603 if (grep(/^$date:$ch$/, @{$components->{$grabber}->{cached}})) 604 { 605 $hits++; 606 print "Hit: $day:$ch\n" if ($debug); 607 } 599 608 } 600 609 } … … 616 625 my $timeslots_per_day = (24 * 60 * 60) / $policy{timeslot_size}; 617 626 618 # print "Channel data:\n" . Dumper($channel_data);619 627 foreach my $ch (keys %$channels) 620 628 { … … 656 664 } 657 665 666 # Takes a hash of what's missing in the format: 667 # { '1' => [ 'ABC', 'SBS' ], '2' => [ 'ABC' ], ... } 668 # and sets $timeslice to the biggest 'slice' of that. 669 # For example, the biggest slice of the above is days 1 -2 of ABC. 658 670 sub calculate_best_timeslice 659 671 { … … 661 673 662 674 my ($overlap, $a); 663 $timeslice = { 'chandays' => 0 };675 my $slice = { 'chandays' => 0 }; 664 676 foreach my $day (1 .. $days) 665 677 { 666 consider_slice($ day, $day, @{$missing->{$day}});678 consider_slice($slice, $day, $day, @{$missing->{$day}}); 667 679 $overlap = $missing->{$day}; 668 680 foreach my $nextday (($day + 1) .. $days) … … 670 682 last unless ($missing->{$nextday}); 671 683 $a = Algorithm::Diff::LCS($overlap, $missing->{$nextday}); 672 # print "Overlap: " . Dumper($a);673 684 last unless ($a and @{$a}); 674 consider_slice($ day, $nextday, @{$a});685 consider_slice($slice, $day, $nextday, @{$a}); 675 686 $overlap = $a; 676 687 } 677 688 } 689 return $slice; 678 690 } 679 691 680 692 sub consider_slice 681 693 { 682 my ($s tartday, $stopday, @chans) = @_;694 my ($slice, $startday, $stopday, @chans) = @_; 683 695 684 696 my $challenger = ($stopday - $startday + 1) * scalar(@chans); 685 return unless ($challenger > $ timeslice->{chandays});697 return unless ($challenger > $slice->{chandays}); 686 698 687 699 # We have a winner! 688 $timeslice->{start} = $startday; 689 $timeslice->{stop} = $stopday; 690 $timeslice->{chans} = [ @chans ]; 691 $timeslice->{chandays} = $challenger; 692 } 693 700 $slice->{start} = $startday; 701 $slice->{stop} = $stopday; 702 $slice->{chans} = [ @chans ]; 703 $slice->{chandays} = $challenger; 704 } 705 706 # Turns the current $timeslice into a missing-style hash 707 # (i.e. { '1' => [ 'ABC', 'SBS' ], '2' => ... 694 708 sub create_missing_slice 695 709 { … … 703 717 } 704 718 719 # Record what a cacheable C1 grabber has just retrieved for us, 720 # so we know next time that this data can be grabbed quickly. 705 721 sub record_cached 706 722 { … … 732 748 } 733 749 734 # Takes a 'missing'hash and returns it as a list like this:750 # Takes a missing-style hash and returns it as a list like this: 735 751 # ( "20061018:ABC", "20061018:Seven", ... ) 736 752 sub convert_chandays_to_list … … 751 767 } 752 768 769 # If we're about to re-try a grabber, make sure that we're not asking 770 # it for the same data. That is, prevent a broken C1 grabber causing 771 # an infinite loop. 772 sub record_requested_chandays 773 { 774 my ($grabber, $slice) = @_; 775 776 print "Recording timeslice request; will not request these chandays from $grabber again.\n" if ($debug); 777 778 my @requested; 779 for my $day ($slice->{start} .. $slice->{stop}) 780 { 781 foreach my $ch (@{$slice->{chans}}) 782 { 783 push @requested, "$day:$ch"; 784 } 785 } 786 if ($grabbed->{$grabber}) 787 { 788 push @{$grabbed->{$grabber}}, @requested; 789 } 790 else 791 { 792 $grabbed->{$grabber} = [ @requested ]; 793 } 794 } 795 796 # If this grabber has been called previously, remove those chandays 797 # from the current request -- we don't want to ask it over and over 798 # for a timeslice that it has already failed to provide. 799 sub cut_down 800 { 801 my ($grabber, $slice) = @_; 802 803 return $slice unless ($grabbed->{$grabber}); 804 805 if ($debug) 806 { 807 print "Grabber $grabber has been called previously; removing " . 808 "requested chandays from timeslice.\n"; 809 } 810 811 my $newslice = {}; 812 foreach my $day (keys %$slice) 813 { 814 my @chans; 815 foreach my $ch (@{$slice->{$day}}) 816 { 817 unless (grep(/$day:$ch/, @{$grabbed->{$grabber}})) 818 { 819 push (@chans, $ch); 820 } 821 } 822 if (@chans) 823 { 824 $newslice->{$day} = [ @chans ]; 825 } 826 } 827 return $newslice; 828 } 753 829 754 830 # interpret xmltv data from this grabber/postprocessor
