rtmstat

使用RTM有些时日了,很想知道自己到底完成了多少个任务,以怎样的频率,然而RTM本身并不提供这些功能,幸运的是,它提供API,且CPAN模块WebService::RTMAgent实现了这个API。

编写基于RTM API的程序,首先需要申请API key,然后还需要一个认证的过程,认证辅助脚本如下:

#!/usr/bin/perl

use WebService::RTMAgent;

use strict;
use warnings;

my $ua = new WebService::RTMAgent;

$ua->api_key("your api key here");
$ua->api_secret("your secret key here");

$ua->init;

my $res = $ua->tasks_getList("filter=status:completed");
die $ua->error unless defined $res;

## Collect data
my %stat;
foreach my $list (@{$res->{tasks}}) {
  foreach my $taskseries (@{$list->{list}}) {
    my $list_id = $taskseries->{id};
    next unless exists $taskseries->{taskseries};
    foreach my $task (@{$taskseries->{taskseries}}) {
      my %task_stat;

      foreach my $t (@{$task->{task}}) {
        my ($year, $month, undef) = split /-/, $t->{completed};
        ++$task_stat{$year}{$month};
      }

      # only count taskseries
      foreach my $y (keys %task_stat) {
        foreach my $m (keys %{$task_stat{$y}}) {
          ++$stat{$y}{$m};
        }
      }
    }
  }
}


## Output

sub simple_cvs_output {
  my $input = shift;

  for my $year (sort keys %$input) {
    for my $month (sort keys %{$input->{$year}}) {
      print "$year,$month,$input->{$year}{$month}n";
    }
  }
}

sub chart_stackedbars_output {
  my $input = shift;
  my $filename = shift;

  require Chart::StackedBars;
  my $chart = Chart::StackedBars->new(400, 300);

  my %stat = %$input;
  my @years = sort keys %stat;
  $chart->add_dataset(@years);

  my @months = map { sprintf "%02d", $_ } (1 .. 12);
  for my $m (@months) {
    my @count_per_month = map { (exists $stat{$_}->{$m}) ? $stat{$_}->{$m} : 0 } @years;
    $chart->add_dataset(@count_per_month);
  }

  my %properties =
    (
     title => "statistics of netcasper's RTM accomplishment",
     text_space => 5,
     precision => 0,
     x_label => 'Year',
     y_label => 'Number of Task Series',
     y_grid_lines => 'true',
     legend => 'right',
     legend_labels => ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                       'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    );
  $chart->set(%properties);
  $chart->png($filename);
}

# simple_cvs_output(%stat);
chart_stackedbars_output(%stat, 'taskseries.png');

该脚本提供两种方式输出统计数据,默认是图形方式(参见下图),也可以使用csv方式输入。

Plug your own filter into Catalyst

Template::Manual::Filters里面提供了好多很好用的filter,当它们不够用的时候,还可以提供自己写的filter函数。但是怎么告诉Catalyst呢?没有找到文档,只好又去看代码。方法是在MyApp.pm里面__PACKAGE__->setup;一行之前添加如下代码:

__PACKAGE__->config( 'View::HTML' => {
    FILTERS => {
        'myown' => &myown_filter,
    },
});

View::HTML换成你自己的View,然后写一个名为myown_filter的函数来完成真正的filter功能。