Skip to content

直近のイベント情報を集計するtaskの追加(仕切り直し) #459

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

Merged
merged 8 commits into from
May 31, 2019

Conversation

chicaco
Copy link
Contributor

@chicaco chicaco commented May 26, 2019

背景

直近の CoderDojo 開催情報を表示したい

やりたいこと

  • 直近の CoderDojo 開催情報を収集する task を追加する

cf #258

※ 使用する EventService モジュールの I/F を変更したため、PR #297 から引き継いで仕切り直す

このPRでやること

  • 近日開催イベント(upcoming_events)T の属性見直し (event_histories T に準じる)
  • 直近の CoderDojo 開催情報を収集する task を追加する
  • rake タスクの rspec を追加する
  • 直近の CoderDojo 開催情報収集手順のドキュメントを作成する

やらなかったこと

この PR では、対象 issue (#258) の以下は行わない (別 PR とする)

  • 直近の CoderDojo 開催情報を CoderDojo サイトに表示すること

レビューポイント

  • 直近の CoderDojo 開催情報収集手順のドキュメント > @yasulab
  • 直近の CoderDojo 開催情報を収集する rake タスクの新設

困ってること

特になし

@chicaco chicaco self-assigned this May 26, 2019
@yasulab yasulab temporarily deployed to coderdojo-japan-pr-459 May 26, 2019 15:48 Inactive
@yasulab yasulab temporarily deployed to coderdojo-japan-pr-459 May 27, 2019 15:19 Inactive
@chicaco chicaco requested review from nalabjp and yasulab May 28, 2019 00:39
・関連見直し
・リファクタ
・不要なコメント削除
@yasulab yasulab temporarily deployed to coderdojo-japan-pr-459 May 28, 2019 16:00 Inactive
Copy link
Member

@nalabjp nalabjp left a comment

Choose a reason for hiding this comment

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

コメントしました

db/schema.rb Outdated
t.string "event_id", null: false
t.string "event_url", null: false
t.datetime "event_at", null: false
t.integer "dojo_event_service_id", default: 1, null: false
Copy link
Member

Choose a reason for hiding this comment

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

default: 1 🤔
これは不要そうな気がしました〜

Copy link
Contributor Author

Choose a reason for hiding this comment

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

migrate <-> rollback したときのゴミでした...。申し訳ありません。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

こちら、差し替えました。

remove_index :upcoming_events, :event_at

add_column :upcoming_events, :dojo_name, :string, null: false
add_column :upcoming_events, :service_name, :string, null: false
Copy link
Member

Choose a reason for hiding this comment

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

ちょっと思慮不足で見落としていたのでいまさらで申し訳ないんですが、
dojo_nameservice_nameが非正規化されているのってどういう意図でしょうか?
正規化のほうがされている方が良さそうに思ったので、お聞きしたいです〜。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dojo_name を持っているは直近のイベント一覧を表示するときに、dojo レコードまで辿らなくても表示できるかな、という目論見によります。
service_name は service_name + event_id でユニーク制約を付けたかったので持ちました。

後者については、関連する(親の)レコードの attribute (今回の場合は service_name or service_id)と自レコードの attribute (今回の場合は event_id) でのユニーク制約を設定する方法があれば、その方がよいと思っています。

Copy link
Member

Choose a reason for hiding this comment

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

履歴テーブルの場合は過去の不変の情報なので非正規化、未来の予定に関するテーブルの場合は変更に強い情報の持ち方で正規化、というのが良いかなと思っています。
このテーブルで扱ってる情報としてはあまりないユースケースになるとは思いますがdojo_nameが変わってしまうケースは変更に追従できなさそうに思います。

service_name は service_name + event_id でユニーク制約を付けたかったので持ちました。

こちらはこれからユニーク制約が追加されるという事になりますか?

後者については〜

についてはちょっと理解が追いつきませんでした。
ユニーク制約がないと困るケースが想像できなかったので、例を挙げて貰えると助かります。

Copy link
Contributor Author

@chicaco chicaco May 30, 2019

Choose a reason for hiding this comment

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

dojo_name については、ご指摘の通りです。
少し楽をしようと思ってしまいましたが、正しい姿ではありませんよね。
となると、毎週 Update していく UpcomingEvent のレコードよりも、一旦収集したらそのまま残りそうな EventHistory のレコードの方が気になってしまいました...。

service_name + event_id でのユニーク制約は、ActiveRecord レベルでは設定していたのですが、SQL レベルでは設定できていないという不十分な状態でした。
対応漏れですので、追記します。

connpass や doorkeeper という service_name + event_id で UpcomingEvent が一意になるという前提で 2 ヶ月後までのイベント情報を upsert していますので、ユニーク制約は必要であると考えました。

Copy link
Member

Choose a reason for hiding this comment

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

event_historiesは履歴なのでそのままで大丈夫です。
過去の事実を記録しているので、仮に現在のdojo_nameと差異があったとしてもそれを最新に更新すると事実と異なる事が履歴にあることになってしまいます。

あ、event_idだけだと、重複しないと言い切れないからってことですか。
event_urlでも事足りませんかね、ユニーク制約🤔
まあ、service_nameが変わることはほぼ無いと思うので、問題があるわけではないですが。

Copy link
Contributor Author

@chicaco chicaco May 30, 2019

Choose a reason for hiding this comment

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

「event_historiesは履歴」なのですが、イベントが開催されたときの Dojo名 ではなく、統計情報を収集したときの Dojo 名ですよね。イベント開催時でも現在でもないので、気になりました。

connpass, doorkeeper のみでしたら event_url でユニーク制約でよいのですが、
static_yaml や現 event_histories でやむなく採用している facebook の yaml になりますと URL を持たないので断念しました。
URL は「直近のイベント一覧」で表示したいので、そのまま URL として有効な値を設定しておきたいと考え、yaml 用にダミーでユニークになりそうな文字列を生成するような対応も採りませんでした。
yaml からの更新の場合は、event_id も独自採番になるのですが... (ここ、あまり考慮できていなかったです)

Copy link
Member

Choose a reason for hiding this comment

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

イベントが開催されたときの Dojo名 ではなく、統計情報を収集したときの Dojo 名ですよね。イベント開催時でも現在でもないので、気になりました。

そこまでハンドリングするのはさすがに難しそうな印象ですねぇ🤔

facebook の yaml になりますと URL を持たないので断念しました。

なるほど、そういう事情なんですね、理解しました。
ありがとうございます〜。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ひとつ訂正です。
facebook イベントの yaml からの読み込みの場合、event_id は facebook 側のイベントから拾えますし、イベントの URL もあります。
static_yaml の場合だけが URL も event_id もありません。直近のイベント情報収集で static_yaml に対応するときに注意が必要になります。

@client.fetch_events(@params.merge(series_id: dojo_event_service.group_id)).each do |e|
next unless e.dig('series', 'id').to_s == dojo_event_service.group_id

record = dojo_event_service.upcoming_events.find_by(event_id: e['event_id'])
Copy link
Member

Choose a reason for hiding this comment

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

[nits]
find_or_initialize_byじゃなくなったのってどういう意図でしょうか?
そんなに強い主張もないんですがあえて冗長になったのが気になったので〜。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

has_many で追加される関連のメソッドに find_or_initialize_by はないと思い込んでいたのですが、試してはいませんでした。
使えるのであれば find_or_initialize_by の方がよいですね。
確認してみます。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

関連に対しても find_or_initialize_by が使用でき、RSpec もコマンド実動も問題なかったので修正しました。

chicaco added 2 commits May 29, 2019 23:50
・schema.rb の差し替え
・関連に対して find_or_initialize_by が使えたのでリファクタ
@yasulab yasulab temporarily deployed to coderdojo-japan-pr-459 May 29, 2019 14:51 Inactive
chicaco added 2 commits May 30, 2019 23:56
・UpcomingEvent から dojo_name カラムを削除
・UpcomingEvent に service_name + event_id でユニーク制約を追加
Copy link
Member

@nalabjp nalabjp left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Member

@yasulab yasulab left a comment

Choose a reason for hiding this comment

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

PR ありがとうございます!! 👏👏👏😻🆒✨

ドキュメントの方はあとで追記・修正できるので、ひとまずは @chicaco さんの都合の良いタイミングでマージ&デプロイして頂けると嬉しいです...!! (>人< )💖

@chicaco chicaco merged commit 6d13362 into master May 31, 2019
@chicaco chicaco deleted the add_upcoming_events_task branch May 31, 2019 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants