[LON-CAPA-cvs] cvs: modules /raeburn HS_usage.pl

raeburn raeburn at source.lon-capa.org
Fri Oct 21 17:37:16 EDT 2011


raeburn		Fri Oct 21 21:37:16 2011 EDT

  Modified files:              
    /modules/raeburn	HS_usage.pl 
  Log:
  - Script used to retrieve usage data had not been updated in a while.
    - %ENV -> %env.
    - owner now username:domain
    - Courses require activity in past year, creation within past six months,
      student count is only for students with no end date, or end dates in 
      the future.
    - lowercase extensions for resources found in course.
    - print output to file/directories in /home/www/usage/.
    -run as www user
  
  
-------------- next part --------------
Index: modules/raeburn/HS_usage.pl
diff -u modules/raeburn/HS_usage.pl:1.3 modules/raeburn/HS_usage.pl:1.4
--- modules/raeburn/HS_usage.pl:1.3	Mon Mar 21 16:39:16 2005
+++ modules/raeburn/HS_usage.pl	Fri Oct 21 21:37:15 2011
@@ -6,11 +6,11 @@
 use Apache::lonuserstate;
 use Apache::lonnavmaps;
 use LONCAPA::Configuration;
+use LONCAPA;
 use GDBM_File;
                                                                               
 # Determine the library server's domain and hostID
 my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
-my $logfile = $$perlvarref{'lonDaemons'}.'/logs/usage.log';
 my @domains = &Apache::lonnet::current_machine_domains();
 my @hostids = &Apache::lonnet::current_machine_ids();
 my @allowners = ();
@@ -31,11 +31,20 @@
 
 # Determine the present time;
 my $timenow = time();
+my $yearago = $timenow - 31104000;
+my $sixmonthsago = $timenow - 15552000;
 
 # For each domain ......
 foreach my $dom (@domains) {
-    my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.',1,\@hostids);
+    my $fh;
+    my $output;
+   my $domdesc = &Apache::lonnet::domain($dom);
+   my %courses = &Apache::lonnet::courseiddump($dom,'.',$yearago,'.','.','.',1,\@hostids,'.');
     foreach my $key (sort keys %courses) {
+        if (ref($courses{$key}) eq 'HASH') {
+            my $created = $courses{$key}{'created'};
+            next if ($created < $sixmonthsago);
+        }
         my ($cdom,$crs) = split/_/,$key;
         my $lastvisit = 0;
 # get timestamp for activity log
@@ -54,11 +63,16 @@
         if (-e "$classlist") {
             my %classlist = &Apache::lonnet::dump('classlist',$cdom,$crs);
             foreach my $item (sort keys %classlist) {
-                $stucount ++;
+                my $value = &unescape($classlist{$item});
+                my ($end,$start) = split(/:/,$value,3);
+                if (($end == 0 || $end > $timenow) && ($start == 0 || $start <$timenow)) {
+                    $stucount ++;
+                }
             }
         }
         my %crsenv = &Apache::lonnet::dump('environment',$cdom,$crs);
         my $owner;
+        my $owneruname;
         my $ownerdom;
         my $ownername;
         my $ownermail;
@@ -73,13 +87,21 @@
         foreach my $role (sort keys %roles) {
             if ($role eq 'Course Coordinator') {
                 my @ccroles = split/,/,$roles{$role};
-                foreach (@ccroles) {
-                    my ($uname,$udom) = split/:/,$_;
-                    if ($uname eq $owner) {
+                foreach my $cc (@ccroles) {
+                    my ($uname,$udom) = split/:/,$cc;
+                    if ($cc eq $owner) {  
                         $ownerdom = $udom;
+                        $owneruname = $uname;
+                    } elsif ($owner !~ /\:/) {
+                        if (($uname ne '') && ($uname eq $owner)) {
+                            $ownerdom = $udom;
+                            $owneruname = $uname;
+                            $owner .= ':'.$udom;
+                        }
                     } elsif ($uname ne 'felicia' && $uname ne 'raeburn' && $uname ne 'albertelli') {
                         if (!$owner) {
-                            $owner = $uname;
+                            $owner = $cc;
+                            $owneruname = $uname;
                             $ownerdom = $udom;
                         }
                     }
@@ -90,28 +112,28 @@
             if (!$ownerdom) {
                 $ownerdom = $cdom;
             }
-            unless (grep/^$owner:$ownerdom/, at allowners) {
-                push @allowners, $owner.':'.$ownerdom;
+            unless (grep/^$owner/, at allowners) {
+                push(@allowners,$owner);
             }
-            unless (defined($ownercourses{$owner.':'.$ownerdom})) {
-                $ownercourses{$owner.':'.$ownerdom} = 0;
+            unless (defined($ownercourses{$owner})) {
+                $ownercourses{$owner} = 0;
             }
-            unless (defined($external{$owner.':'.$ownerdom})) {
-                $external{$owner.':'.$ownerdom} = 0;
+            unless (defined($external{$owner})) {
+                $external{$owner} = 0;
             }
-            unless (defined($homegrown{$owner.':'.$ownerdom})) {
-                $homegrown{$owner.':'.$ownerdom} = 0;
+            unless (defined($homegrown{$owner})) {
+                $homegrown{$owner} = 0;
             }
-            unless (defined($uploaded{$owner.':'.$ownerdom})) {
-                $uploaded{$owner.':'.$ownerdom} = 0;
+            unless (defined($uploaded{$owner})) {
+                $uploaded{$owner} = 0;
             }
-            unless (defined($uploadtotal{$owner.':'.$ownerdom})) {
-                $uploaded{$owner.':'.$ownerdom} = 0;
+            unless (defined($uploadtotal{$owner})) {
+                $uploaded{$owner} = 0;
             }
-            unless (defined($crscount{$owner.':'.$ownerdom})) {
-                %{$crscount{$owner.':'.$ownerdom}} = ();
+            unless (defined($crscount{$owner})) {
+                %{$crscount{$owner}} = ();
             }
-            my %ownerhash =  &Apache::lonnet::dump('environment',$ownerdom,$owner);
+            my %ownerhash =  &Apache::lonnet::dump('environment',$ownerdom,$owneruname);
             foreach my $key (sort keys %ownerhash) {
                 if ($key eq 'firstname') {
                     $ownername = $ownerhash{$key};
@@ -119,29 +141,34 @@
                     $ownername .= ' '.$ownerhash{$key};
                 } elsif ($key =~ /notification/) {
                     $ownermail = $ownerhash{$key};
-                    $owneremails{$owner.':'.$ownerdom} = $ownermail;
+                    $owneremails{$owner} = $ownermail;
                 }
             }
-            $ownernames{$owner.':'.$ownerdom} = $ownername;
-            my %emailstatus = &Apache::lonnet::dump('email_status',$ownerdom,$owner);
+            $ownernames{$owner} = $ownername;
+            my %emailstatus = &Apache::lonnet::dump('email_status',$ownerdom,$owneruname);
             foreach my $key (sort keys %emailstatus) {
                 if ($key eq 'logout') {
-                    $lastactivity{$owner.':'.$ownerdom} = $emailstatus{$key};
+                    $lastactivity{$owneruname.':'.$ownerdom} = $emailstatus{$key};
                 }
             }
         }
 
         if ($stucount > 0 && $lastvisit > 0) {
             my $crsitems = 0;
-            $ENV{'user.name'} = $owner;
-            $ENV{'user.home'} = &Apache::lonnet::homeserver($owner,$ownerdom);
-            $ENV{'user.domain'} = $ownerdom;
-
-            print "$dom -- Found $key -- has value ".&Apache::lonnet::unescape($courses{$key})."\n";
-            print "lastvisit ".localtime($lastvisit)." count is $stucount owner is $ownername, e-mail is $ownermail\n";
-            $ownercourses{$owner.':'.$ownerdom} ++;
+            $env{'user.name'} = $owneruname;
+            $env{'user.home'} = &Apache::lonnet::homeserver($owneruname,$ownerdom);
+            $env{'user.domain'} = $ownerdom;
+
+            $output.= "$domdesc,";
+            if (ref($courses{$key}) eq 'HASH') {
+                my $cdesc = &Apache::lonnet::unescape($courses{$key}{'description'});
+                $cdesc =~ s/,/-/g;
+                $output .= $cdesc;
+            }
+            $output .= ",".localtime($lastvisit).",$stucount,$ownername,$ownermail,$owneruname,$ownerdom,";
+            $ownercourses{$owner} ++;
             my ($furl,$ferr) = &Apache::lonuserstate::readmap($cdom.'/'.$crs);
-            my $fn = $ENV{"request.course.fn"};
+            my $fn = $env{"request.course.fn"};
             my %hash = ();
             if (-e "$fn.db") {
               if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
@@ -152,45 +179,51 @@
                          my $extension;
                          $src =~ s#^/adm/wrapper##;
                          if ($src =~ m-^/res/([^/]+)/([^/]+)/-) {
-                             if ($1 eq $ownerdom && $2 eq $owner) {
-                                 $homegrown{$owner.':'.$ownerdom} ++;
+                             if ($1 eq $ownerdom && $2 eq $owneruname) {
+                                 $homegrown{$owner} ++;
                              } else {
                                  if ($1 eq 'lib' && $2 eq 'templates') {
                                      if ($src =~ m-/([^/]+)\.problem$-) {
-                                          $crscount{$owner.':'.$ownerdom}{$1} ++;
+                                         $crscount{$owner}{$1} ++;
                                      }
                                  } else {
-                                     $external{$owner.':'.$ownerdom} ++;
+                                     $external{$owner} ++;
                                  }
                              }
                              ($extension) = ($src =~ m/\.([^\.]+)$/);
-                             $crscount{$owner.':'.$ownerdom}{$extension} ++;
+                             $extension = lc($extension);
+                             $crscount{$owner}{$extension} ++;
                          } elsif ($src =~ m-/adm/-) {
                              $src =~ s/\?[^\?]*$//g;
                              ($extension) = ($src =~ m-/([^/]+)$-);
-                             $crscount{$owner.':'.$ownerdom}{$extension} ++;
+                             if ($extension =~ /\.([^.]+)$/) {
+                                  $extension = $1;
+                             }
+                             $extension = lc($extension);
+                             $crscount{$owner}{$extension} ++;
                          } elsif ($src =~ m-/uploaded/-) {
                              ($extension) = ($src =~ m-\.([^\.]+)$-);
-                             $crscount{$owner.':'.$ownerdom}{$extension} ++;
+                             $extension = lc($extension);
+                             $crscount{$owner}{$extension} ++;
                              if ($extension eq 'sequence') {
-                                 $folders{$owner.':'.$ownerdom} ++;
+                                 $folders{$owner} ++;
                              } elsif ($extension eq 'page') {
-                                 $pages{$owner.':'.$ownerdom} ++;
+                                 $pages{$owner} ++;
                              } else {
-                                 $uploaded{$owner.':'.$ownerdom} ++;
+                                 $uploaded{$owner} ++;
                                  my $filepath = $src; 
                                  $filepath =~ s#^/uploaded/$cdom/$crs#$crsdir/userfiles/#;
                                  if (-e "$filepath") {
                                      my @fileinfo = stat($filepath);
-                                     $uploadtotal{$owner.':'.$ownerdom} += $fileinfo[7];
+                                     $uploadtotal{$owner} += $fileinfo[7];
                                  }  
                              }
                          } elsif ($src =~ m-^/public/-) {
                              $src =~ s/\?[^\?]*$//g;
                              if ($src =~ m-syllabus$-) {
-                                 $crscount{$owner.':'.$ownerdom}{syllabus} ++;
+                                 $crscount{$owner}{syllabus} ++;
                              } else {
-                                 $crscount{$owner.':'.$ownerdom}{public} ++;
+                                 $crscount{$owner}{public} ++;
                              }          
                          } 
                      }
@@ -198,10 +231,19 @@
                  untie %hash;
                }
             }
-            print "total items = $crsitems\n";
-            delete($ENV{'user.name'});
-            delete($ENV{'user.home'});
-            delete($ENV{'user.domain'});
+            $output .= "$crsitems\n";
+            delete($env{'user.name'});
+            delete($env{'user.home'});
+            delete($env{'user.domain'});
+        }
+    }
+    if ($output ne '') {
+        if (!-d "/home/www/usage/$dom") {
+            mkdir("/home/www/usage/$dom",0755);
+        }
+        if (open($fh,">/home/www/usage/$dom.txt")) {
+            print $fh $output;
+            close($fh);
         }
     }
 }
@@ -209,15 +251,27 @@
 # For each course owner..
 
 foreach my $cc (@allowners) {
-    print "Imported total = $external{$cc}\n";
-    print "Homegrown total = $homegrown{$cc}\n";
-    print "Uploaded total = $uploaded{$cc}\n";
-    print "Uploaded total bytes = $uploadtotal{$cc}\n";
-    foreach my $key (sort keys %{$crscount{$cc}}) {
-        print "Course item -- $key, value is $crscount{$cc}{$key}\n";
+    my ($uname,$udom) = split/:/,$cc;
+    if ($udom ne '') {
+        if (!-d "/home/www/usage/$udom") {
+            mkdir("/home/www/usage/$udom",0755); 
+        }
+        if (open(my $fh,">/home/www/usage/$udom/$uname.txt")) {
+            print $fh "Owner: $cc\n";
+            print $fh "Imported total = $external{$cc}\n";
+            print $fh "Homegrown total = $homegrown{$cc}\n";
+            print $fh "Uploaded total = $uploaded{$cc}\n";
+            print $fh "Uploaded total bytes = $uploadtotal{$cc}\n";
+            foreach my $key (sort keys %{$crscount{$cc}}) {
+                print $fh "Course item -- $key, value is $crscount{$cc}{$key}\n";
+            }
+            print $fh "\n";
+            close($fh);
+        } else {
+            print "Could not open /home/www/usage/$udom.txt for writing\n";
+        }
     }
 
-    my ($uname,$udom) = split/:/,$cc;
     %{$totals{$cc}} = ();
     %{$sizes{$cc}} = ();
     %{$publish{$cc}} = ();


More information about the LON-CAPA-cvs mailing list