📄 metricstats.pm
字号:
$count = $topic->get_topic_size_in_lines(); } push @row, $count; } # Get the list of users for this review. my @users = $metrics->get_complete_list_of_topic_participants(); my @user_metrics = $metrics->get_user_metrics_totals(@users); for (my $index = 0; $index < scalar(@{$headers->{user}}); ++$index) { my $count = ""; foreach my $metric ( @user_metrics ) { $count = $metric->{value} if ($metric->{name} eq $headers->{user}->[$index]); } if ($headers->{user}->[$index] eq $total_participants_header) { # Add the total number of participants in the topic. $count = scalar(@users); } push @row, $count; } # Close the connection, and check for any database errors. Codestriker::DB::DBI->release_connection($dbh, 1); return @row;}# Returns 12 months of comment metrics data.## returns a collection of the following hash references# {# name = the comment metric name# results = collection ref to# {# name = the comment value name.# counts = array ref to metric counts per month# monthnames = array ref to month names# }# }sub get_comment_metrics { # Stores the collection results. my @results = (); # Obtain a database connection. my $dbh = Codestriker::DB::DBI->get_connection(); # Get the comment metric totals. foreach my $metric_config (@{ $Codestriker::comment_state_metrics }) { my $metric_name = $metric_config->{name}; my $query = "SELECT commentstatemetric.value, COUNT(commentstate.id) " . "FROM commentstate, commentstatemetric " . "WHERE commentstatemetric.id = commentstate.id AND " . "commentstatemetric.name = " . $dbh->quote($metric_name) . " AND " . "commentstate.creation_ts > ? AND " . "commentstate.creation_ts <= ? " . "GROUP BY commentstatemetric.value " . "ORDER BY commentstatemetric.value"; my @metrics = _get_monthly_metrics(12, $query); my $months_ref = $metrics[0]->{monthnames}; # Make sure all enumerated values are catered for. my %handled_value = (); foreach my $value (@metrics) { $handled_value{$value->{name}} = 1; } foreach my $value (@{ $metric_config->{values} }) { if (! defined $handled_value{$value}) { push @metrics, { name => $value, counts => [0,0,0,0,0,0,0,0,0,0,0,0], monthnames => $months_ref }; } } my $result = { name => $metric_name, results => \@metrics }; push @results, $result; } # Get comment thread totals. my @total_metrics = (); my @thread_total = _get_monthly_metrics(12, 'SELECT \'Comment Threads\', COUNT(commentstate.id) FROM commentstate WHERE commentstate.creation_ts > ? AND commentstate.creation_ts <= ?'); push @total_metrics, @thread_total; # Get submitted comment totals. my @submitted_total = _get_monthly_metrics(12, 'SELECT \'Submitted Comments\', COUNT(commentstate.id) FROM commentstate, commentdata WHERE commentstate.id = commentdata.commentstateid AND commentstate.creation_ts > ? AND commentstate.creation_ts <= ?'); push @total_metrics, @submitted_total; my $result = { name => 'Total', results => \@total_metrics }; push @results, $result; # Close the connection, and check for any database errors. Codestriker::DB::DBI->release_connection($dbh, 1); return @results;}# Returns 12 months of data with a break down of topic metrics.## Returns a collection of the following hash references:# {# name = the metric name# counts = array ref to metric counts per month# monthnames = array ref to month names# }sub get_topic_metrics { my @metrics; # Get total. my @total = _get_monthly_metrics(12, 'SELECT \'Total Topics\', COUNT(topic.id) FROM topic WHERE topic.creation_ts > ? AND topic.creation_ts <= ?'); push @metrics, @total; # Get totals for the topic metrics. @total = _get_monthly_metrics(12, 'SELECT topicmetric.metric_name, SUM(topicmetric.value) FROM topicmetric,topic WHERE topic.creation_ts > ? AND topic.creation_ts <= ? AND topicmetric.topicid = topic.id GROUP BY topicmetric.metric_name ORDER BY topicmetric.metric_name'); push @metrics, @total; # Get totals for the topic user metrics. @total = _get_monthly_metrics(12, 'SELECT topicusermetric.metric_name, SUM(topicusermetric.value) FROM topicusermetric,topic WHERE topic.creation_ts > ? AND topic.creation_ts <= ? AND topicusermetric.topicid = topic.id GROUP BY topicusermetric.metric_name ORDER BY topicusermetric.metric_name'); push @metrics, @total; return @metrics;}# Returns $total_months of data for the given query. The query must return# name, count collection of rows between two times.## returns a collection of the following hash references# {# name = the metric name# counts = array ref to metric counts per month# monthnames = array ref to month names# }sub _get_monthly_metrics { my ($total_months, $dbi_query_string) = @_; # Obtain a database connection. my $dbh = Codestriker::DB::DBI->get_connection(); my @month_dbi_times = _dbi_month_time_stamp($total_months); my @month_names = _user_month_name_list($total_months); my @metrics; # For the past year, get the metrics counts for each month. for (my $month_count = 0; $month_count+1 < @month_dbi_times; ++$month_count) { # Do the db query. my $comment_counts = $dbh->selectall_arrayref( $dbi_query_string, { }, $month_dbi_times[$month_count], $month_dbi_times[$month_count+1]); foreach my $row (@$comment_counts) { my ($db_metric_name, $db_count) = @$row; my $found = 0; # See if we can find the metric. foreach my $metric (@metrics) { if ($metric->{name} eq $db_metric_name) { push @{$metric->{counts}}, $db_count; $found = 1; last; } } if ($found == 0) { my $metric = { name=>$db_metric_name, counts=>[], monthnames=>\@month_names }; # Catch up the collection of counts on any missed months. for ( my $missingmonths = 0; $missingmonths < $month_count; ++$missingmonths) { push @{$metric->{counts}}, 0; } push @{$metric->{counts}}, $db_count; push @metrics, $metric; } } # Add zero's to any metrics not present. foreach my $metric (@metrics) { if (@{$metric->{counts}} eq $month_count) { push @{$metric->{counts}}, 0; } } } # Close the connection, and check for any database errors. Codestriker::DB::DBI->release_connection($dbh, 1); return @metrics;}# Return a list of dbi time stamp on 1 month boundaries back for n months. This# is used to do db queries to get data cut up by month.sub _dbi_month_time_stamp { my ($number_months_back) = @_; # Get the start time of this month my @month = _add_month(-$number_months_back+1,localtime(time())); my @month_dbi_ts; for (my $count = 0; $count < $number_months_back +1; ++$count) { # Calculate the start of this month dbi string. my $month_start = sprintf("%04d-%02d-01 00:00:00", $month[5]+1900, $month[4]+1); push @month_dbi_ts, $month_start; @month = _add_month(1, @month); } return @month_dbi_ts;}# Return a list of user displayable time stamps on 1 month bondaries# back for n months.sub _user_month_name_list { my ($number_months_back) = @_; # Get the start time of this month. my @month = _add_month(-$number_months_back+1,localtime(time())); my @month_names; for (my $count = 0; $count < $number_months_back; ++$count) { my $month_name = $Codestriker::short_months[$month[4]] . " " . ($month[5]+1900); push @month_names, $month_name; @month = _add_month(1, @month); } return @month_names;}# Add or substracts count months on to a time array.sub _add_month { my ($count,@time) = @_; my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = @time; if ($count > 0) { for (my $i = 0; $i < $count; ++$i) { # Calculate the end of this month dbi string. ++$mon; if ($mon >= 12) { $mon = 0; ++$year; } } } elsif ($count < 0) { for (my $i = $count; $i < 0; ++$i) { # Calculate the end of this month dbi string. --$mon; if ($mon < 0) { $mon = 11; --$year; } } } $time[4] = $mon; $time[5] = $year; return @time;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -