X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=clearadm%2Fviewager.cgi;h=150237a06a641f332101e9e5fc1392059f5e50dc;hb=901870f2fc61575bff838a8556a9fec652ec7d0b;hp=c283ebec0960a6a145994bf24f1200eb6e609644;hpb=ceb7db8c45226caebec04a0406521ff3911e59fc;p=clearscm.git
diff --git a/clearadm/viewager.cgi b/clearadm/viewager.cgi
old mode 100755
new mode 100644
index c283ebe..150237a
--- a/clearadm/viewager.cgi
+++ b/clearadm/viewager.cgi
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/local/bin/perl
=pod
@@ -59,7 +59,8 @@ including old ones.
Where:
-u|sage: Displays usage
- -region : Region to use when looking for the view
+ -region : Region to use when looking for views (Default
+ for generate action: all)
-e|mail: Send email to owners of old views
-ag|eThreshold: Number of days before a view is considered old
(Default: 180)
@@ -119,7 +120,7 @@ use warnings;
use FindBin;
use Getopt::Long;
-use CGI qw (:standard :cgi-lib *table start_Tr end_Tr);
+use CGI qw(:standard :cgi-lib *table start_Tr end_Tr);
use CGI::Carp 'fatalsToBrowser';
use File::stat;
use Time::localtime;
@@ -140,11 +141,11 @@ use User;
my $VERSION = '$Revision: 1.11 $';
($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
-my %opts = Vars;
+my %opts;
my $clearadm;
-$opts{sortby} ||= 'age';
-$opts{region} ||= $Clearcase::CC->region;
+$opts{sortby} ||= 'age';
+$opts{ageThreshold} = 180; # Default number of days a view must be older than
my $subtitle = 'View Aging Report';
my $email;
@@ -158,14 +159,13 @@ my $script = 'http://'
. $port
. $scriptName;
-my (%total, $action);
-my $ageThreshold = 180; # Default number of days a view must be older than
+my %total;
my $nbrThreshold; # Number of views threshold - think top 10
-sub GenerateRegion ($) {
+sub GenerateRegion($) {
my ($region) = @_;
- verbose "Processing $region";
+ verbose "Processing region $region";
$total{Regions}++;
my $views = Clearcase::Views->new ($region);
@@ -176,7 +176,7 @@ sub GenerateRegion ($) {
my $i = 0;
- foreach my $name (@Views) {
+ for my $name (@Views) {
$total{Views}++;
if (++$i % 100 == 0) {
@@ -185,7 +185,7 @@ sub GenerateRegion ($) {
verbose_nolf '.';
}# if
- my $view = Clearcase::View->new ($name, $region);
+ my $view = Clearcase::View->new($name, $region);
my $gpath;
@@ -207,20 +207,24 @@ sub GenerateRegion ($) {
# Note if the view server is unreachable (e.g. user puts view on laptop and
# the laptop is powered off), then these fields will be undef. Change them
# to Unknown. (Should Clearcase::View.pm do this instead?).
- my $type = $view->type;
- $type ||= 'Unknown';
+ my $type = $view->type;
+ $type ||= 'dynamic';
+ my $ownerid = $view->owner;
+ $ownerid ||= 'Unknown';
my $user;
- my $ownerid = $view->owner;
+ if ($ownerid =~ /^\w+(\\|\/)(\w+)/) {
+ # TODO: Handle user identification better
+ #$user = User->new ($ownerid);
- if ($ownerid) {
- $user = User->new ($ownerid);
-
- $user->{name} ||= 'Unknown';
+ $ownerid = $2;
+ $user->{name} = $2;
+ $user->{email} = "$2\@gddsi.com";
} else {
$ownerid = 'Unknown';
$user->{name} = 'Unknown';
+ $user->{email} = 'unknown@gddsi.com';
} # if
my $age = 0;
@@ -235,29 +239,39 @@ sub GenerateRegion ($) {
# Compute age
$age = Age ($modified_date);
$ageSuffix = $age != 1 ? 'days' : 'day';
- } else {
- $modified_date = 'Unknown';
} # if
- my ($err, $msg) = $clearadm->AddView (
- system => $view->shost,
- region => $view->region,
- tag => $view->tag,
- owner => $ownerid,
- ownerName => $user->{name},
- email => $user->{email},
- type => $type,
- gpath => $gpath,
- modified_date => $modified_date,
- age => $age,
- ageSuffix => $ageSuffix,
+ my %oldView = $clearadm->GetView($view->tag, $view->region);
+
+ my ($err, $msg);
+
+ my %viewRec = (
+ region => $view->region,
+ tag => $view->tag,
+ owner => $ownerid,
+ ownerName => $user->{name},
+ email => $user->{email},
+ type => $type,
+ gpath => $gpath,
+ age => $age,
+ ageSuffix => $ageSuffix,
);
- error "Unable to add view $name to Clearadm\n$msg", $err
- if $err;
- } # foreach
+ # Some views have not yet been modified
+ $viewRec{modified} = $modified_date if $modified_date;
+
+ if (%oldView) {
+ ($err, $msg) = $clearadm->UpdateView(%viewRec);
+
+ error "Unable to update view $name in Clearadm\n$msg", $err if $err;
+ } else {
+ ($err, $msg) = $clearadm->AddView(%viewRec);
+
+ error "Unable to add view $name to Clearadm\n$msg", $err if $err;
+ } # if
+ } # for
- verbose "\nProcessed $region";
+ verbose "\nProcessed region $region";
return;
} # GenerateRegion
@@ -265,12 +279,10 @@ sub GenerateRegion ($) {
sub Generate ($) {
my ($region) = @_;
- if ($region =~ /all/i) {
- foreach ($Clearcase::CC->regions) {
- GenerateRegion $_;
- } # foreach
- } else {
+ if ($region) {
GenerateRegion $region;
+ } else {
+ GenerateRegion $_ for $Clearcase::CC->regions;
} # if
return;
@@ -283,21 +295,21 @@ sub Report (@) {
my @sortedViews;
- if ($opts{sort} eq 'age') {
+ if ($opts{sortby} eq 'age') {
# Sort by age numerically decending
@sortedViews = sort { $$b{$opts{sortby}} <=> $$a{$opts{sortby}} } @views;
} else {
- @sortedViews = sort { $$a{$opts{sort}} cmp $$b{$opts{sort}} } @views;
+ @sortedViews = sort { $$a{$opts{sortby}} cmp $$b{$opts{sortby}} } @views;
} # if
$total{Reported} = 0;
- foreach (@sortedViews) {
+ for (@sortedViews) {
my %view = %{$_};
last
if ($nbrThreshold and $total{Reported} + 1 > $nbrThreshold) or
- ($view{age} < $ageThreshold);
+ ($view{age} < $opts{ageThreshold});
$total{Reported}++;
@@ -321,16 +333,16 @@ format STDOUT_TOP =
.
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<<<<<< @>>>> @<<<<
-$view{tag},$view{owner},$view{type},$view{modified_date},$view{age},$view{ageSuffix}
+$view{tag},$view{owner},$view{type},$view{modified},$view{age},$view{ageSuffix}
.
write;
- } # foreach
+ } # for
return;
} # Report
-sub FormatTable ($@) {
+sub FormatTable($@) {
my ($style, @views) = @_;
my $table;
@@ -349,12 +361,12 @@ sub FormatTable ($@) {
my $caption;
- my $regionDropdown = start_form (
+ my $regionDropdown = start_form(
-action => $script,
);
$regionDropdown .= font {-class => 'captionLabel'}, 'Region: ';
- $regionDropdown .= popup_menu (
+ $regionDropdown .= popup_menu(
-name => 'region',
-values => [$Clearcase::CC->regions],
-default => $Clearcase::CC->region,
@@ -369,17 +381,12 @@ sub FormatTable ($@) {
width => '100%',
};
- my $registryHost = $Clearcase::CC->registry_host;
-
- $registryHost = font {class => 'unknown'}, 'Unknown'
- unless $registryHost;
-
$caption .= start_Tr;
$caption .= td {
-align => 'left',
-width => '30%',
}, font ({-class => 'label'}, 'Registry: '),
- $registryHost, '
',
+ setField($Clearcase::CC->registry_host), '
',
font ({-class => 'label'}, 'Views: '),
$nbrViews;
$caption .= td {
@@ -454,18 +461,20 @@ sub FormatTable ($@) {
# Sort by age numerically decending
@views = $opts{reverse} == 1
? sort { $$a{$opts{sortby}} <=> $$b{$opts{sortby}} } @views
- : sort { $$b{$opts{sortby}} <=> $$a{$opts{sortby}} } @views
+ : sort { $$b{$opts{sortby}} <=> $$a{$opts{sortby}} } @views;
} else {
@views = $opts{reverse} == 1
? sort { $$b{$opts{sortby}} cmp $$a{$opts{sortby}} } @views
- : sort { $$a{$opts{sortby}} cmp $$b{$opts{sortby}} } @views
+ : sort { $$a{$opts{sortby}} cmp $$b{$opts{sortby}} } @views;
} # if
my $i;
- foreach (@views) {
+ for (@views) {
my %view = %{$_};
+ next if $view{region} ne $opts{region};
+
my $owner = $view{owner};
if ($view{owner} =~ /\S+(\\|\/)(\S+)/) {
@@ -474,7 +483,9 @@ sub FormatTable ($@) {
$owner = $view{ownerName} ? $view{ownerName} : 'Unknown';
- my $rowClass= $view{age} > $ageThreshold ? 'oldview' : 'view';
+ next if $opts{user} and $owner ne $opts{user};
+
+ my $rowClass= $view{age} > $opts{ageThreshold} ? 'oldview' : 'view';
$table .= start_Tr {
class => $rowClass
@@ -503,7 +514,7 @@ sub FormatTable ($@) {
class => $view{type}
}, $view{age}, ' ', $view{ageSuffix});
$table .= end_Tr;
- } # foreach
+ } # for
$table .= end_table;
@@ -512,19 +523,19 @@ sub FormatTable ($@) {
# TODO: Add an option to remove views older than a certain date
-sub EmailUser ($@) {
+sub EmailUser($@) {
my ($emailTo, @oldViews) = @_;
@oldViews = sort { $$b{age} <=> $$a{age} } @oldViews;
- my $msg = '';
+ my $msg = '';
$msg .= <<"END";
You have old Clearcase Views
Won't you take a moment to review this message and clean up any views you no
longer need?
-The following views are owned by you and have not been modified in $ageThreshold
+
The following views are owned by you and have not been modified in $opts{ageThreshold}
days:
END
@@ -568,7 +579,7 @@ which will stop it from being reported as old.
Your friendly Clearcase Administrator
END
- mail (
+ mail(
to => $emailTo,
# to => 'Andrew@DeFaria.com',
mode => 'html',
@@ -579,7 +590,7 @@ END
return
} # EmailUser
-sub EmailUsers (@) {
+sub EmailUsers(@) {
my (@views) = @_;
@views = sort { $$a{ownerName} cmp $$b{ownerName} } @views;
@@ -587,26 +598,24 @@ sub EmailUsers (@) {
my @userViews;
my $currUser = $views [0]->{ownerName};
- foreach (@views) {
+ for (@views) {
my %view = %{$_};
- next
- unless $view{email};
+ next unless $view{email};
if ($currUser ne $view{ownerName}) {
- EmailUser $view{email}, @userViews
- if @userViews;
+ EmailUser $view{email}, @userViews if @userViews;
$currUser = $view{ownerName};
- @userViews =();
+ @userViews = ();
} else {
- if ($view{age} > $ageThreshold) {
+ if ($view{age} > $opts{ageThreshold}) {
push @userViews, \%view
if !-f "$view{gpath}/ageless";
} # if
} # if
- } # foreach
+ } # for
display"Done";
@@ -614,7 +623,7 @@ sub EmailUsers (@) {
} # EmailUsers
# Main
-GetOptions (
+GetOptions(
\%opts,
'usage' => sub { Usage },
'verbose' => sub { set_verbose },
@@ -627,18 +636,21 @@ GetOptions (
'nbrThreshold=i',
) or Usage "Invalid parameter";
-local $| = 1;
+# Get options from CGI
+my %CGIOpts = Vars;
-$opts{region} ||= '';
+$opts{$_} = $CGIOpts{$_} for keys %CGIOpts;
+
+local $| = 1;
# Announce ourselves
verbose "$FindBin::Script v$VERSION";
$clearadm = Clearadm->new;
-if ($action and $action eq 'generate') {
+if ($opts{action} and $opts{action} eq 'generate') {
Generate $opts{region};
- Stats \%total;
+ Stats \%total if $opts{verbose};
} else {
if ($opts{region} and ($opts{region} eq 'Clearcase not installed')) {
heading;
@@ -647,14 +659,15 @@ if ($action and $action eq 'generate') {
exit 1;
} # if
- my @views = $clearadm->FindView (
- 'all',
- $opts{region},
+ $opts{region} ||= $Clearcase::CC->region;
+
+ my @views = $clearadm->FindView(
$opts{tag},
+ $opts{region},
$opts{user}
);
- if ($action and $action eq 'report') {
+ if ($opts{action} and $opts{action} eq 'report') {
Report @views;
Stats \%total;
} elsif ($email) {
@@ -743,4 +756,4 @@ Please report problems to Andrew DeFaria .
Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
-=cut
\ No newline at end of file
+=cut