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方式输入。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据