[Bio] / MGRAST / Statistics.pm Repository:
ViewVC logotype

Annotation of /MGRAST/Statistics.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (view) (download) (as text)

1 : dsouza 1.1 package MGRAST::Statistics;
2 :    
3 :     use strict;
4 :     use warnings;
5 :     use Data::Dumper;
6 :    
7 :     use FIG_Config;
8 :     use DBI;
9 :    
10 :     1;
11 :    
12 :     # need to check if db handles passed in and create if necessary
13 :     # use WebApplicationDBHandle;
14 :     # my ($dbmaster, $error) = WebApplicationDBHandle->new();
15 :     # fail if error found
16 :    
17 :     sub new {
18 :     my($class, $mgrast_dbh, $user_dbh) = @_;
19 :    
20 :     my $self = {
21 :     mgrast_dbh => $mgrast_dbh,
22 :     user_dbh => $user_dbh,
23 :     };
24 :    
25 :     # eval {
26 :     # $self->{_handle} = DBMaster->new( -database => $FIG_Config::mgrast_metadata_db || 'MGRASTMetadata',
27 :     # -host => $FIG_Config::mgrast_metadata_host,
28 :     # -user => $FIG_Config::mgrast_metadata_user,
29 :     # -password => $FIG_Config::mgrast_metadata_password || "");
30 :     # };
31 :     # if ($@) {
32 :     # warn "Unable to connect to MGRAST metadata db: $@\n";
33 :     # $self->{_handle} = undef;
34 :     # }
35 :    
36 :     bless ($self, $class);
37 :     return $self;
38 :     }
39 :    
40 :     sub mgrast_dbh {
41 :     my ($self) = @_;
42 :     return $self->{mgrast_dbh};
43 :     }
44 :    
45 :     sub user_dbh {
46 :     my ($self) = @_;
47 :     return $self->{user_dbh};
48 :     }
49 :    
50 :     sub registered_users {
51 :     my($self, $time) = @_;
52 :    
53 :     my $sql = "select user, current_page from UserSession where timestamp > '$time' and user is not null";
54 :     my $users = $self->user_dbh->selectall_arrayref($sql);
55 :     return $users;
56 :     }
57 :    
58 :     sub number_of_users {
59 :     my($self, $time) = @_;
60 :    
61 :     my $sql = "select count(*) from UserSession where timestamp > '$time'";
62 :     my $users = $self->user_dbh->selectall_arrayref($sql);
63 :     return $users;
64 :     }
65 :    
66 :     sub job_count {
67 :     my($self) = @_;
68 :    
69 :     my $sql = "select substring(created_on,1,7) as Date, count(job_id) as Jobs from Job where job_id is not NULL group by Date";
70 :     my $count = $self->mgrast_dbh->selectall_arrayref($sql);
71 :     return $count;
72 :     }
73 :    
74 :     sub get_funding_users {
75 :     my($self) = @_;
76 :    
77 :     my $funding_users = $self->user_dbh->selectall_arrayref("select value, count(*) from Preferences where name = 'funding_source' group by value");
78 :     return $funding_users;
79 :     }
80 :    
81 :     sub get_funding_user_jobs {
82 :     my ($self, $mapping) = @_;
83 :    
84 :     my $lf = {};
85 :     my $job_data = $self->mgrast_dbh->selectall_arrayref("select owner, _id from Job where owner is not NULL");
86 :     my $user_data = $self->user_dbh->selectall_arrayref("select _id, email from User");
87 :     my $fund_data = $self->user_dbh->selectall_arrayref("select user, value from Preferences where name = 'funding_source'");
88 :     my $stat_data = $self->mgrast_dbh->selectall_arrayref("select job, value from JobStatistics where tag = 'bp_count_raw' and value is not NULL and value > 0");
89 :     unless ($user_data && $fund_data && $job_data) { return $lf; }
90 :    
91 :     my %user_jobs = ();
92 :     my %user_email = map { $_->[0], $_->[1] } @$user_data;
93 :     my %user_fund = map { $_->[0], uc($_->[1]) } @$fund_data;
94 :     my %job_stats = ($stat_data && (@$stat_data > 0)) ? map { $_->[0], $_->[1] } @$stat_data : ();
95 :    
96 :     map { push @{ $user_jobs{$_->[0]} }, $_->[1] } @$job_data;
97 :    
98 :     foreach my $user (keys %user_email) {
99 :     if (($user == 122) || ($user == 7232)) { next; } # skip Wilke
100 :    
101 :     my $fund = exists($user_fund{$user}) ? $user_fund{$user} : '';
102 :     my $jobs = exists($user_jobs{$user}) ? $user_jobs{$user} : [];
103 :     my $bps = 0;
104 :     map { $bps += $job_stats{$_} } grep { exists $job_stats{$_} } @$jobs;
105 :    
106 :     my $has_fund = 0;
107 :     if ($fund) {
108 :     $lf->{$fund}{users}++;
109 :     $lf->{$fund}{jobs} += scalar(@$jobs);
110 :     $lf->{$fund}{bp_count_raw} += $bps;
111 :     $has_fund = 1;
112 :     }
113 :     else {
114 :     while ( my ($ext, $code) = each %$mapping ) {
115 :     if ( $user_email{$user} =~ /$ext$/ ) {
116 :     $lf->{$code}{users}++;
117 :     $lf->{$code}{jobs} += scalar(@$jobs);
118 :     $lf->{$code}{bp_count_raw} += $bps;
119 :     $has_fund = 1;
120 :     }
121 :     }
122 :     }
123 :     }
124 :    
125 :     my @res = map { [ $_, $lf->{$_}{users}, $lf->{$_}{jobs}, $lf->{$_}{bp_count_raw} ] } sort keys %$lf;
126 :     return \@res;
127 :     }
128 :    
129 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3