Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation to aggregate statistical information #12 #128

Merged
merged 37 commits into from Aug 21, 2017

Conversation

nalabjp
Copy link
Member

@nalabjp nalabjp commented Jul 30, 2017

#12

統計情報の集計のために以下の機能を実装しました。

Features

  • event_historiesテーブルの作成
  • dojo_event_servicesテーブルの作成
  • APIクライアントの実装
    • Connpass
    • Doorkeeper
  • API経由のイベント開催実績をevent_historiesテーブルに流し込む実装
    • Runner
    • Rake Task
  • group_idを調べるためのRake Task

運用イメージ

前提

  • dojo毎のdojo_event_serviceレコードを作成する
    • connpassのseries.id / doorkeeperのgroupを調べる必要がある
    • rake statistics:search[keyword]でキーワード検索 or なんとかして調べる

月次バッチ

  • cronもしくはスケジューラ等で毎月月初に前月のバッチ処理を行う
    • rake statistics:aggregation[yyyymm]を叩く
    • yyyymmは省略可能(省略すると前月分を実行)

過去分

  • 年月を指定して過去分の月次バッチ処理を行う
    • rake statistics:aggregation[yyyymm]を叩く
    • yyyymmに集計したい年月を指定する

@yasulab
Copy link
Member

yasulab commented Jul 31, 2017

うぉぉぉぉ、PR ありがとうございます...!! 🙏 ✨ まだ WIP とのことですが、何か気づいたことがあれば随時共有して行きますね! ;)

@nalabjp nalabjp force-pushed the statistics branch 2 times, most recently from 7394a32 to ec719e1 Compare August 13, 2017 13:17
@nalabjp nalabjp changed the title [WIP] Implementation to aggregate statistical information #12 Implementation to aggregate statistical information #12 Aug 13, 2017
@nalabjp
Copy link
Member Author

nalabjp commented Aug 13, 2017

一通り実装し終えたのでレビューをお願いします。

なお、CIが回せていないのは SCRIVITO_TENANTSCRIVITO_API_KEYあたりが必要なためです。
https://docs.travis-ci.com/user/environment-variables/#Defining-encrypted-variables-in-.travis.yml
ここに載っている通りセキュアな文字列にしてしまえば良さそうなんですが、
やってもよいか念のため確認してからにしようと思いコミットしていません。
問題なければセキュアな文字列にしてコミットするのでご確認をお願いします😌

@yasulab
Copy link
Member

yasulab commented Aug 14, 2017

ありがとうございます! 後ほど拝見します! 👀 💨

.travis.yml Outdated
@@ -12,4 +12,10 @@ deploy:
run:
- "bundle exec rails db:migrate"
- "bundle exec rails dojos:update_db_by_yaml"

rvm:
- 2.4.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

travisのログみた感じだと.ruby-versionがあればここ指定する必要はないみたいです。

$ rvm use $(< .ruby-version) --install --binary --fuzzy

https://travis-ci.org/coderdojo-japan/coderdojo.jp/jobs/260468747

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。
fixed 563bd68

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

複数バージョンのRubyに対してテスト実行したいときは便利そうですが今は使ってるRuby1つしかないので消しちゃって大丈夫そうですね 👍

.travis.yml Outdated
cache:
- bundler
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

travisだとデフォルトでRAILS_ENV=testなのでここで環境変数指定する必要はなさそうです
https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅
fixed f44f78e


private

def connection_for(endpoint, &block)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&blockつかわれてなさそう

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらで使っています〜
db0afac#diff-eaf50acb3e83c4f729603033a19f20c4R54

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blockの変数自体は使ってなさそうに見えました。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、確かに。
失礼しました。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 754cf52

@client = Client.new(ENDPOINT)
end

def fetch_series_id(**params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ハッシュ受け取るだけなら単にparamsでよさそうな気も

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インタフェースが変わって消えてしまいました〜

def fetch_series_id(**params)
@client.get('event/', params.merge(count: 1))
.fetch('events')
.first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配列の1番目取るなら.dig(0, 'series', 'id')でよさそう

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配列の要素がないときエラーにしたい感じなのかな?

[4] pry(main)> [{foo: 1}].dig(0, :foo)
=> 1
[5] pry(main)> [].dig(0, :foo)
=> nil
[6] pry(main)> [].first.dig(:foo)
NoMethodError: undefined method `dig' for nil:NilClass
from (pry):6:in `__pry__'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おぉ、indexも引数に渡せるからそういう使い方できるんですね。💡
勉強になりました。


# Timezone
config.time_zone = 'Asia/Tokyo'
ENV['TZ'] = 'Asia/Tokyo'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heroku、travis-ciのほうで設定した方がいいかも?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Herokuのenvはどなたかにお願いするとして、そうしましょうか。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heroku の env 確認しましたが、既に入ってました ;) ✅

$ heroku config
...
TZ:                       Asia/Tokyo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご確認ありがとうございます!😁

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご確認ありがとうございます!😁

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 0366800

@@ -11,5 +11,9 @@ class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Timezone
config.time_zone = 'Asia/Tokyo'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


break if part['results_returned'].zero?

events.push(*part.fetch('events'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こことL99、count: 100なので大丈夫そうですがevents個数がものすごく多いとスタック溢れそうですね

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですねー。
一応connpassはcount: 100がAPIの上限値なのでまぁ良いかなぁと判断しました。
doorkeeperは25固定です。

@@ -15,6 +15,9 @@ namespace :statistics do

raise ArgumentError, "Invalid format: #{args[:yyyymm]}" if date.nil?


EventHistory.where(evented_at: date.beginning_of_month..date.end_of_month).delete_all
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

その月の分まるごと作り直す感じなんですねー。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あまり無いとは思いますがたまにやり直したいケースがあったりするのでそのようにしました。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いいと思います!

class Aggregation
class << self
def run(date:)
cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: 'connpass' }).to_a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここでnameに渡している文字列、マジックナンバーになっていますね。このカラムをEnumか何かにしたいですね

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘のとおりですね。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed d392c68

@yasulab
Copy link
Member

yasulab commented Aug 15, 2017

なお、CIが回せていないのは SCRIVITO_TENANT や SCRIVITO_API_KEYあたりが必要なためです。
https://docs.travis-ci.com/user/environment-variables/#Defining-encrypted-variables-in-.travis.yml
ここに載っている通りセキュアな文字列にしてしまえば良さそうなんですが、
やってもよいか念のため確認してからにしようと思いコミットしていません。
問題なければセキュアな文字列にしてコミットするのでご確認をお願いします😌

@nalabjp Travis の encrypt ですね! はい、そちらもコミットしてもらえると助かります 😆

別件ですが、以前 coderdojo-japan/dojopaas を実装している時に、Travis CI のログにトークンが流れるというミスをしてしまったことがあります 😅

cf. クライアントのdebugがtrueだとアクセストークンが出力されてしまう
sakura-internet/node-sacloud#9

今回のケースでは特に問題にならないと思いますが、念のためチェックしておくと良さそうですね ;)

@yasulab
Copy link
Member

yasulab commented Aug 15, 2017

encrypt したトークンを追加したり、指摘した点の対応が終わったら、CIが通ってるのを確認してマージみたいな流れかなという認識です! 🙆 何か他に懸念点などあれば、随時共有する形で進められたらと考えています😆

In heroku, it is already defined.
@nalabjp
Copy link
Member Author

nalabjp commented Aug 20, 2017

travisのencrypted env vars、PRからだと読み込まれなくなっているようですね。
以下のログが出ていました。

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#Pull-Requests-and-Security-Restrictions

https://docs.travis-ci.com/user/pull-requests/#Pull-Requests-and-Security-Restrictions
一部を引用すると

Pull requests sent from forked repositories do not have access to encrypted variables or data.

ということらしいです。
これはもしかして回避する方法がないってことなんでしょうかね?🤔

(ところで、travisコマンドでymlにsecretを追加しなくても、既にsettingsに環境変数として設定されていたんですね、見ていませんでした😅)

@nalabjp
Copy link
Member Author

nalabjp commented Aug 20, 2017

travisの件以外はレビューのフィードバックに対応したつもりです 💪 😄

Copy link
Contributor

@hanachin hanachin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正確認しました〜! よさそうです。
travisの件、回避する方法はなさそうですね。

@yasulab
Copy link
Member

yasulab commented Aug 21, 2017

Pull requests sent from forked repositories do not have access to encrypted variables or data.

なるほど 🤔 そういう理由だったんですね 😅 ナラさんにコミット権は既に付与してたと思うので、branch 間の PR でも大丈夫そうです! 😸

@yasulab
Copy link
Member

yasulab commented Aug 21, 2017

僕の方でもあとで確認して、問題なければマージしようと思います! 📑 👀

@yasulab
Copy link
Member

yasulab commented Aug 21, 2017

確認しました! こちらのPRは一旦マージした後に集計を行い、その結果 (数字) をトップページに出すところまで持っていきたいですね 📈 ✨ (デザインや見せ方はさておき、数字をトップページに置くだけでもインパクトはあると思うので...!!)

@yasulab yasulab merged commit f3a83d3 into coderdojo-japan:master Aug 21, 2017
@nalabjp nalabjp deleted the statistics branch August 21, 2017 04:57
@nalabjp
Copy link
Member Author

nalabjp commented Aug 21, 2017

レビューありがとうございました〜😌

@yasulab yasulab added the 統計情報 Tracking event record function via APIs: https://coderdojo.jp/stats label Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
統計情報 Tracking event record function via APIs: https://coderdojo.jp/stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants