目次

HRForecast

公式

http://blog.nomadscafe.jp/2013/02/hrforecast--.html
https://github.com/kazeburo/HRForecast

インストール

cpanm

依存モジュールのインストールにcpanmが必要なので、事前にインストールします。

# cd /root
# mkdir bin
# cd /bin
# curl -LOk http://xrl.us/cpanm
# chmod 755 cpanm

HRForecast

# cd /etc/
# git clone git://github.com/kazeburo/HRForecast.git
Initialized empty Git repository in /etc/HRForecast/.git/
remote: Counting objects: 414, done.
remote: Compressing objects: 100% (218/218), done.
remote: Total 414 (delta 225), reused 374 (delta 194)
Receiving objects: 100% (414/414), 288.08 KiB | 256 KiB/s, done.
Resolving deltas: 100% (225/225), done.

依存モジュール

# cd HRForecast
# cpanm -n -lextlib --installdeps .
--> Working on .
~省略~
69 distributions installed

MySQLにテーブル作成

mysql -u root -p

mysql> create database hrforecast;
mysql> use hrforecast;
mysql> SOURCE /etc/HRForecast/schema.sql
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes
Query OK, 0 rows affected, 2 warnings (0.01 sec)

ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

付属のテーブル作成スクリプトファイルを実行すると上記のようにエラーになるので、スクリプトファイルのテーブル定義を編集する。

# cp schema.sql schema.sql.org
# vi schema.sql
:%s/255/111/g

編集後、再度実行。

mysql> SOURCE /etc/HRForecast/schema.sql
Query OK, 0 rows affected, 2 warnings (0.00 sec)

Query OK, 0 rows affected, 3 warnings (0.01 sec)

Query OK, 0 rows affected, 2 warnings (0.00 sec)

初期設定

# vi config.pl

{
    dsn => 'dbi:mysql:hrforecast;hostname=127.0.0.1',
    username => 'root',
    password => 'password',
    port => '5127',
    host => '0.0.0.0',
    front_proxy => [],
    allow_from => [],
};

起動確認

以下コマンド実行後、

# perl hrforecast.pl --config config.pl >> /var/log/hrforecast.log 2>&1 &

ブラウザから以下にアクセスできることを確認。

http://サーバ:5127/

起動スクリプト

起動スクリプトは付属していないので、作成する必要がある。

# vi /etc/init.d/hrforecast
#!/bin/sh
# chkconfig: - 80 20
# description: HRForecast

# Source function library.
. /etc/rc.d/init.d/functions

### Default variables
PERLBREW_ROOT="/usr/bin"
PERLBREW_HOME=$PERLBREW_ROOT/.perl
LOG_FILE="/var/log/hrforecast.log"
SYSCONFIG="/etc/sysconfig/hrforecast"

#source $SYSCONFIG
#source $PERLBREW_ROOT/etc/bashrc
PROG_ROOT="/etc/HRForecast"
PROG_NAME="hrforecast.pl"
PROG_ARGS="--config $PROG_ROOT/config.pl"
PID_FILE=/var/run/hrforecast.pid

start() {
  PID=`pgrep -fo "$PROG_NAME"`
  if [ -z "$PID" ]; then
    if [ -f $PID_FILE ]; then rm -f $PID_FILE; fi
  else
    echo "hrforecast already started."
    exit 1
  fi
  echo -n "Starting hrforecast: "
  su $USER -c "perl $PROG_ROOT/$PROG_NAME $PROG_ARGS >>$LOG_FILE 2>&1 &"
  RETVAL=$?
  echo `pgrep -fo "$PROG_NAME"` > $PID_FILE
  if [ $RETVAL -eq 0 ] ; then
    echo_success
    touch /var/lock/subsys/$prog
  else
    echo_failure
  fi
  echo
  return $RETVAL
}

stop() {
  PID=`pgrep -f "$PROG_NAME"`
  if [ -z "$PID" ]; then
    echo "hrforecast already stopped."
    exit 0
  fi
  echo -n "Stopping hrforecast: "
  rm -f $PID_FILE
  pkill -TERM -f "$PROG_NAME"
  RETVAL=$?
  if [ $RETVAL -eq 0 ] ; then
    echo_success
  else
    echo_failure
  fi
  echo
  return $RETVAL
}

status() {
  PID=`pgrep -f "$PROG_NAME"`
  if [ -z "$PID" ]; then
    echo "hrforecast stopped."
  else
    echo "hrforecast running."
  fi
}

usage() {
  echo "Usage: `basename $0` {start|stop|restart|status}"
}

case $1 in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 1
    start
    ;;
  status)
    status
    ;;
  *)
    usage
    ;;
esac
# chmod +x /etc/init.d/hrforecast
# /etc/init.d/hrforecast start

データ登録

numberとdatetimeにパラメータをつけて、サーバのAPIにリクエストを投げる。

# curl -F number=33 -F datetime=20130728T00:00:00 http://localhost:5127/api/mona/regist/all

Tips

レンジの追加

グラフのレンジに「1日間」を追加

diff -ir /etc/HRForecast/lib/HRForecast/Web.pm /etc/HRForecast.org/lib/HRForecast/Web.pm
47,50d46
<     elsif ( $term eq 'x' ) {
<         $from = time - 86400 * 1;
<         $to = time;
<     }
161c157
<             [['CHOICE',qw/w m y x c range/],'invalid browse term'],
---
>             [['CHOICE',qw/w m y c range/],'invalid browse term'],
diff -ir /etc/HRForecast/t/HRForecast/Web/02_calc_term.t /etc/HRForecast.org/t/HRForecast/Web/02_calc_term.t
29,33d28
< subtest 't => x' => sub {
<   my ($from, $to) = $obj->calc_term(t => 'x');
<   is($to->epoch, int(time / 3600) * 3600);
<   is(($to - $from), (1 * 24 * 60 * 60));
< };
diff -ir /etc/HRForecast/views/list.tx /etc/HRForecast.org/views/list.tx
13d12
< <li class="<: ($valid.valid('t') == 'x') ? 'active' : '' :>"><a href="<: $c.req.uri_for($path_info, $merge_params({t=>'x'})) :>">1日間</a></li>