日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

Slackのメッセージ数の増減をgrowthforecastで見てみる。

この記事は Dark - Developers at Real Kommunity Advent Calendar 2015 - Adventar の16日目として書かれています。

あと少しで埋まるはずや!

DarkのコミュニティではSlackを利用してるのですが、 ray というruby製のbotが現れた途端みんなで殺そうとしたりして 突発的にメッセージの投稿がたくさんあったりします。

github.com

あの時どれくらい盛り上がったんだろうって、なんか見たくなるじゃないですか。
なので今回はそれを頑張ってみました。

概要

GrowthForecastとfluentdを利用します。

GrowthForecast

GrowthForecast - Lightning fast Graphing / Visualization

利用したfluentdのplugin

ara-ta3/fluent-plugin-slackrtm · GitHub

tagomoris/fluent-plugin-growthforecast · GitHub

tagomoris/fluent-plugin-forest · GitHub

fluent/fluent-plugin-rewrite-tag-filter · GitHub

GrowthForecastのインストール

こちらを参考にインストールします。
cpanm -n GrowthForecast で結構時間かかって不安になりますが、ちゃんと待てば終わります。

GrowthForecast - Lightning fast Graphing / Visualization

ちょっとハマったこと

nginxでproxyさせるとjsやcssのURLが正しくなく読めなかった。

=> 2つやれば解決されます。

  • nginx側
server {
  listen 80;
  server_name www.example.com;

  location / {
    proxy_pass http://localhost:5125;
    proxy_set_header Host $host;
    # proxy_set_headerを追加します。
  }
}
  • GrowthForecast側
$growthforecast.pl --data-dir /path/to/data/growthforecast --front-proxy=127.0.0.1

のように--front-proxyオプションを追加して起動する。

fluentdの設定

最終的な設定ファイルがこんな感じです。

<source>
    type slackrtm
    token xxxx-xxxx...
    tag slack.rtm
</source>

<filter slack.**>
    @type grep
    regexp1 type ^message$
</filter>

<match slack.**>
    type rewrite_tag_filter
    capitalize_regex_backreference yes
    rewriterule1 channel C012ABCDE filtered.yami
    rewriterule2 channel .+ clear
</match>

<match clear>
    type null
</match>

<match filtered.**>
    type forest
    subtype flowcounter
    <template>
        count_keys type
        unit       minute
        aggregate  all
        tag channel.${tag_parts[1]}
    </template>
</match>

<match channel.**>
    type growthforecast
    gfapi_url /url/for/api
    graph_path dark/slack/${tag}
    name_keys count
    remove_prefix test
</match>

流れ

1. slackrtm -> filter-grep -> rewrite_tag_filter

まずSlackのmessageイベント以外をfilterします。
そしてrewrite_tag_filterでchannel名をtagに付与します。
Slack Real Time Messageの場合Channel IDしかメッセージに書いてないので、手でChannel IDとChannel名をマッピングしています。

TODO Slack APIに初め問い合わせて、ID to 名前のMapを作って、来たもの全てを変換するみたいなfilter pluginでも書こうかな

2. rewrite_tag_filter -> flowcounter

flowcounter pluginを利用して1分毎のメッセージイベント数をgrowthforecastにforwardします。

3. flowcounter -> growthforecast

growthforecast pluginを利用して自前のgrowthforecastにPOSTしています。
graph_pathに${tag}を含めることでどこのチャンネルのグラフなのかがわかります。

${tag}が使えるようになったからforestいらなくねって思ったけどちょっと良くわからない感じにハマった上に体調が最高に悪かったので調査やめました。
そのうち修正します。

結果

Darkの #yami チャンネルのメッセージ数の推移です。
(まあそんな多くないですよね)
昼ぐらいから増え始めて、2時間毎に投稿数が増えて、夕方16時頃に爆発してます。
また、なにかネタが出た時に面白い感じにトゲってくれたらいいなぁって感じですね。

f:id:arata3da4:20151216204401p:plain

Dark とは ?

Dark - Developers at Real Kommunity | Doorkeeper