From e3fd895780ec865572a1421ecc0058f58890183e Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 05:12:29 +0900 Subject: [PATCH 01/37] Gem `faraday` --- Gemfile | 1 + Gemfile.lock | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index d08710a37..7527d27dc 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem 'secure_headers' # Rendering legal documents gem 'kramdown' +gem 'faraday' # TODO: Delete this if the following issue is fixed # https://github.com/bundler/bundler/issues/5332 gem 'faraday_middleware', '0.10' diff --git a/Gemfile.lock b/Gemfile.lock index 79d49cc69..a353d148a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -284,6 +284,7 @@ DEPENDENCIES bootstrap-sass capybara coffee-rails + faraday faraday_middleware (= 0.10) font-awesome-rails jbuilder @@ -315,4 +316,4 @@ RUBY VERSION ruby 2.4.0p0 BUNDLED WITH - 1.13.7 + 1.15.3 From db0afac193f38762f3cd88ea790239e2d42fbd07 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 12:59:31 +0900 Subject: [PATCH 02/37] Draft implementaion of API client --- lib/statistics.rb | 3 ++ lib/statistics/client.rb | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 lib/statistics.rb create mode 100644 lib/statistics/client.rb diff --git a/lib/statistics.rb b/lib/statistics.rb new file mode 100644 index 000000000..b70d35d2b --- /dev/null +++ b/lib/statistics.rb @@ -0,0 +1,3 @@ +module Statistics; end + +require_relative 'statistics/client' diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb new file mode 100644 index 000000000..d1653c21a --- /dev/null +++ b/lib/statistics/client.rb @@ -0,0 +1,69 @@ +module Statistics + class Client + class_attribute :debug + self.debug = false + + def initialize(endpoint, &block) + @conn = connection_for(endpoint, &block) + end + + def get(path, params) + @conn.get(path, params).body + end + + private + + def connection_for(endpoint, &block) + Faraday.new(endpoint) do |f| + f.response :logger if self.debug + f.response :json, :content_type => /\bjson$/ + f.response :raise_error + + yield f if block_given? + + f.adapter Faraday.default_adapter + end + end + + class Connpass + ENDPOINT = 'https://connpass.com/api/v1'.freeze + + def initialize + @client = Client.new(ENDPOINT) + end + + def fetch_series_id(**params) + @client.get('event/', params.merge(count: 1)) + .fetch('events') + .first + .dig('series', 'id') + end + + def fetch_events(series_id:) + @client.get('event/', series_id: series_id, count: 100) + .fetch('events') + end + end + + class Doorkeeper + ENDPOINT = 'https://api.doorkeeper.jp'.freeze + DEFAULT_SINCE = Date.parse('2010-07-01') + + def initialize + @client = Client.new(ENDPOINT) do |c| + c.authorization(:Bearer, ENV.fetch('DOORKEEPER_API_TOKEN')) + end + end + + def fetch_group_id(keyword:) + @client.get('events', q: keyword, since: DEFAULT_SINCE) + .first + .dig('event', 'group') + end + + def fetch_events(group_id:, offset: 1, since: DEFAULT_SINCE) + @client.get("groups/#{group_id}/events", offset: offset, since: since) + end + end + end +end From 04f645ce4c4c755dea14feeecfab2406254d9973 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 17:30:23 +0900 Subject: [PATCH 03/37] Create a table named `event_histories` --- .../20170730050122_create_event_histories.rb | 18 ++++++++++++++++++ db/schema.rb | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20170730050122_create_event_histories.rb diff --git a/db/migrate/20170730050122_create_event_histories.rb b/db/migrate/20170730050122_create_event_histories.rb new file mode 100644 index 000000000..acf2adbbb --- /dev/null +++ b/db/migrate/20170730050122_create_event_histories.rb @@ -0,0 +1,18 @@ +class CreateEventHistories < ActiveRecord::Migration[5.0] + def change + create_table :event_histories do |t| + t.references :dojo, foreign_key: true, index: true, null: false + t.string :dojo_name, null: false + t.string :service_name, null: false + t.integer :service_group_id, null: false + t.integer :event_id, null: false + t.string :event_url, unique: true, null: false + t.integer :participants, null: false + t.datetime :evented_at, null: false + t.timestamps + + t.index [:service_name, :event_id], unique: true + t.index [:evented_at, :dojo_id] + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b9235ccbc..bb9c44291 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161217063010) do +ActiveRecord::Schema.define(version: 20170730050122) do create_table "dojos", force: :cascade do |t| t.string "name" @@ -24,4 +24,20 @@ t.datetime "updated_at", null: false end + create_table "event_histories", force: :cascade do |t| + t.integer "dojo_id", null: false + t.string "dojo_name", null: false + t.string "service_name", null: false + t.integer "service_group_id", null: false + t.integer "event_id", null: false + t.string "event_url", null: false + t.integer "participants", null: false + t.datetime "evented_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["dojo_id"], name: "index_event_histories_on_dojo_id" + t.index ["evented_at", "dojo_id"], name: "index_event_histories_on_evented_at_and_dojo_id" + t.index ["service_name", "event_id"], name: "index_event_histories_on_service_name_and_event_id", unique: true + end + end From 6ff7f214a26186983e8a1f5b582cc248bb1a9eb7 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 17:30:46 +0900 Subject: [PATCH 04/37] Create a model named `EventHistory` --- app/models/event_history.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 app/models/event_history.rb diff --git a/app/models/event_history.rb b/app/models/event_history.rb new file mode 100644 index 000000000..7de8982cd --- /dev/null +++ b/app/models/event_history.rb @@ -0,0 +1,11 @@ +class EventHistory < ApplicationRecord + belongs_to :dojo + + validates :dojo_name, presence: true + validates :service_name, presence: true, uniqueness: { scope: :event_id } + validates :service_group_id, presence: true + validates :event_id, presence: true + validates :event_url, presence: true, uniqueness: true, allow_nil: true + validates :participants, presence: true + validates :evented_at, presence: true +end From 453d28f48158b43c611f6d7b7ff6d706bd10d10b Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 23:36:55 +0900 Subject: [PATCH 05/37] Create a table named `dojo_event_services` --- .../20170730114150_create_dojo_event_services.rb | 11 +++++++++++ db/schema.rb | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20170730114150_create_dojo_event_services.rb diff --git a/db/migrate/20170730114150_create_dojo_event_services.rb b/db/migrate/20170730114150_create_dojo_event_services.rb new file mode 100644 index 000000000..b07663483 --- /dev/null +++ b/db/migrate/20170730114150_create_dojo_event_services.rb @@ -0,0 +1,11 @@ +class CreateDojoEventServices < ActiveRecord::Migration[5.0] + def change + create_table :dojo_event_services do |t| + t.references :dojo, foreign_key: true, index: true, null: false + t.string :name + t.string :url + t.integer :group_id + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index bb9c44291..8000106a9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170730050122) do +ActiveRecord::Schema.define(version: 20170730114150) do + + create_table "dojo_event_services", force: :cascade do |t| + t.integer "dojo_id", null: false + t.string "name" + t.string "url" + t.integer "group_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["dojo_id"], name: "index_dojo_event_services_on_dojo_id" + end create_table "dojos", force: :cascade do |t| t.string "name" From 3669f2aacb7fe6ab53df1b16b2509ff1cf0b163a Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 30 Jul 2017 23:39:17 +0900 Subject: [PATCH 06/37] Create a model named `DojoEventService` --- app/models/dojo_event_service.rb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 app/models/dojo_event_service.rb diff --git a/app/models/dojo_event_service.rb b/app/models/dojo_event_service.rb new file mode 100644 index 000000000..d8692f84d --- /dev/null +++ b/app/models/dojo_event_service.rb @@ -0,0 +1,3 @@ +class DojoEventService < ApplicationRecord + belongs_to :dojo +end From b3d39bcf7c8bb57caa8ccd0ece3c7401951c608b Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 31 Jul 2017 00:23:36 +0900 Subject: [PATCH 07/37] Add associations --- app/models/dojo.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/dojo.rb b/app/models/dojo.rb index 3571c760e..909ffc03b 100644 --- a/app/models/dojo.rb +++ b/app/models/dojo.rb @@ -3,6 +3,9 @@ class Dojo < ApplicationRecord NUM_OF_WHOLE_DOJOS = "1,250" NUM_OF_JAPAN_DOJOS = Dojo.count.to_s + has_one :dojo_event_service + has_many :event_history + serialize :tags default_scope -> { order(order: :asc) } before_save { self.email = self.email.downcase } From f3580de3a34430e4387f0bab74e4eaf37f15fda2 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 31 Jul 2017 00:24:52 +0900 Subject: [PATCH 08/37] Add timezone as `Asia/Tokyo` --- config/application.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/application.rb b/config/application.rb index 06b42023f..efd250489 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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' + ENV['TZ'] = 'Asia/Tokyo' end end From 3bc50c1278171ca3bbd73acc5cfa11dd98041b05 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 31 Jul 2017 01:37:00 +0900 Subject: [PATCH 09/37] Draft implementation of importing data via API to `event_histories` table --- lib/statistics.rb | 1 + lib/statistics/runner.rb | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 lib/statistics/runner.rb diff --git a/lib/statistics.rb b/lib/statistics.rb index b70d35d2b..f68457b26 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -1,3 +1,4 @@ module Statistics; end require_relative 'statistics/client' +require_relative 'Statistics/runner' diff --git a/lib/statistics/runner.rb b/lib/statistics/runner.rb new file mode 100644 index 000000000..0e84dff63 --- /dev/null +++ b/lib/statistics/runner.rb @@ -0,0 +1,57 @@ +module Statistics + class Runner + class << self + def run + cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'connpass' }).to_a + drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'doorkeeper' }).to_a + + Connpass.run(cnps_dojos) + Doorkeeper.run(drkp_dojos) + end + end + + class Connpass + class << self + def run(dojos) + cnps = Cilient::Connpass.new + dojos.each do |dojo| + cnps.fetch_events(series_id: dojo.dojo_event_service.group_id).each do |e| + next unless e.dig('series', 'id') == dojo.dojo_event_service.group_id + + EventHistory.create!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['event_id'], + event_url: e['event_url'], + participants: e['accepted'], + evented_at: Time.zone.parse(e['started_at'])) + end + end + end + end + end + + class Doorkeeper + class << self + def run(dojos) + drkp = Client::Doorkeeper.new + dojos.each do |dojo| + drkp.fetch_events(group_id: dojo.dojo_event_service.group_id).each do |e| + next unless e['group'] == dojo.dojo_event_service.group_id + + EventHistory.create!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['id'], + event_url: e['public_url'], + participants: e['participants'], + evented_at: Time.zone.parse(e['starts_at'])) + end + end + end + end + end + end +end From dfdc16cf99247091e372c76125ea1cdfc1145cc4 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 00:06:49 +0900 Subject: [PATCH 10/37] Fix --- lib/statistics/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index d1653c21a..168f7f5c9 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -15,7 +15,7 @@ def get(path, params) def connection_for(endpoint, &block) Faraday.new(endpoint) do |f| - f.response :logger if self.debug + f.response :logger if self.class.debug f.response :json, :content_type => /\bjson$/ f.response :raise_error From c93dfb82bcfb69296cd8c2ca357aa1af395e0e4d Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 02:10:03 +0900 Subject: [PATCH 11/37] Support multiple requests --- lib/statistics/client.rb | 53 +++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index 168f7f5c9..efd3f849c 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -39,15 +39,37 @@ def fetch_series_id(**params) .dig('series', 'id') end - def fetch_events(series_id:) - @client.get('event/', series_id: series_id, count: 100) - .fetch('events') + def fetch_events(series_id:, yyyymm: nil) + params = { + series_id: series_id, + start: 1, + count: 100 + } + params[:ym] = yyyymm if yyyymm + events = [] + + loop do + part = @client.get('event/', params) + + break if part['results_returned'].zero? + + events.push(*part.fetch('events')) + + break if part.size < params[:count] + + break if params[:start] + params[:count] > part['results_available'] + + params[:start] += params[:count] + end + + events end end class Doorkeeper ENDPOINT = 'https://api.doorkeeper.jp'.freeze - DEFAULT_SINCE = Date.parse('2010-07-01') + DEFAULT_SINCE = Time.zone.parse('2010-07-01') + DEFAULT_UNTIL = Time.zone.now.end_of_day def initialize @client = Client.new(ENDPOINT) do |c| @@ -61,8 +83,27 @@ def fetch_group_id(keyword:) .dig('event', 'group') end - def fetch_events(group_id:, offset: 1, since: DEFAULT_SINCE) - @client.get("groups/#{group_id}/events", offset: offset, since: since) + def fetch_events(group_id:, since_at: DEFAULT_SINCE, until_at: DEFAULT_UNTIL) + params = { + page: 1, + since: since_at, + until: until_at + } + events = [] + + loop do + part = @client.get("groups/#{group_id}/events", params) + + break if part.size.zero? + + events.push(*part) + + break if part.size < 25 # 25 items / 1 request + + params[:page] += 1 + end + + events end end end From 9eb61aa2bf645971fec9a9ee3fac72448181f6b2 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:04:06 +0900 Subject: [PATCH 12/37] Support a period for search conditions --- lib/statistics/runner.rb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/statistics/runner.rb b/lib/statistics/runner.rb index 0e84dff63..f79de3bf5 100644 --- a/lib/statistics/runner.rb +++ b/lib/statistics/runner.rb @@ -1,21 +1,25 @@ module Statistics class Runner class << self - def run + def run(date:) cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'connpass' }).to_a drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'doorkeeper' }).to_a - Connpass.run(cnps_dojos) - Doorkeeper.run(drkp_dojos) + Connpass.run(cnps_dojos, date) + Doorkeeper.run(drkp_dojos, date) end end class Connpass class << self - def run(dojos) + def run(dojos, date) cnps = Cilient::Connpass.new + params = { + yyyymm: "#{date.year}#{date.month}" + } + dojos.each do |dojo| - cnps.fetch_events(series_id: dojo.dojo_event_service.group_id).each do |e| + cnps.fetch_events(params.merge(series_id: dojo.dojo_event_service.group_id)).each do |e| next unless e.dig('series', 'id') == dojo.dojo_event_service.group_id EventHistory.create!(dojo_id: dojo.id, @@ -34,10 +38,15 @@ def run(dojos) class Doorkeeper class << self - def run(dojos) + def run(dojos, date) drkp = Client::Doorkeeper.new + params = { + since_at: date.beginning_of_month, + until_at: date.end_of_month + } + dojos.each do |dojo| - drkp.fetch_events(group_id: dojo.dojo_event_service.group_id).each do |e| + drkp.fetch_events(params.merge(group_id: dojo.dojo_event_service.group_id)).each do |e| next unless e['group'] == dojo.dojo_event_service.group_id EventHistory.create!(dojo_id: dojo.id, From a4cb3ec2fea22d7d89683013a3e53ee17c295d3b Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:14:37 +0900 Subject: [PATCH 13/37] Support idempotency in re-running with same conditions --- lib/statistics/runner.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/statistics/runner.rb b/lib/statistics/runner.rb index f79de3bf5..53365594d 100644 --- a/lib/statistics/runner.rb +++ b/lib/statistics/runner.rb @@ -22,14 +22,14 @@ def run(dojos, date) cnps.fetch_events(params.merge(series_id: dojo.dojo_event_service.group_id)).each do |e| next unless e.dig('series', 'id') == dojo.dojo_event_service.group_id - EventHistory.create!(dojo_id: dojo.id, - dojo_name: dojo.name, - service_name: dojo.dojo_event_service.name, - service_group_id: dojo.dojo_event_service.group_id, - event_id: e['event_id'], - event_url: e['event_url'], - participants: e['accepted'], - evented_at: Time.zone.parse(e['started_at'])) + EventHistory.find_or_create_by!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['event_id'], + event_url: e['event_url'], + participants: e['accepted'], + evented_at: Time.zone.parse(e['started_at'])) end end end @@ -49,14 +49,14 @@ def run(dojos, date) drkp.fetch_events(params.merge(group_id: dojo.dojo_event_service.group_id)).each do |e| next unless e['group'] == dojo.dojo_event_service.group_id - EventHistory.create!(dojo_id: dojo.id, - dojo_name: dojo.name, - service_name: dojo.dojo_event_service.name, - service_group_id: dojo.dojo_event_service.group_id, - event_id: e['id'], - event_url: e['public_url'], - participants: e['participants'], - evented_at: Time.zone.parse(e['starts_at'])) + EventHistory.find_or_create_by!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['id'], + event_url: e['public_url'], + participants: e['participants'], + evented_at: Time.zone.parse(e['starts_at'])) end end end From d75d2fedc02010190c5a08a112d84cd2cb3d93ae Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:23:56 +0900 Subject: [PATCH 14/37] Rename a class name --- lib/statistics.rb | 2 +- lib/statistics/{runner.rb => aggregation.rb} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/statistics/{runner.rb => aggregation.rb} (99%) diff --git a/lib/statistics.rb b/lib/statistics.rb index f68457b26..d232edc09 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -1,4 +1,4 @@ module Statistics; end require_relative 'statistics/client' -require_relative 'Statistics/runner' +require_relative 'Statistics/aggregation' diff --git a/lib/statistics/runner.rb b/lib/statistics/aggregation.rb similarity index 99% rename from lib/statistics/runner.rb rename to lib/statistics/aggregation.rb index 53365594d..a60a95412 100644 --- a/lib/statistics/runner.rb +++ b/lib/statistics/aggregation.rb @@ -1,5 +1,5 @@ module Statistics - class Runner + class Aggregation class << self def run(date:) cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'connpass' }).to_a From 8ba45061365144cd932a12e9ca908bc4fbdaaa29 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:41:57 +0900 Subject: [PATCH 15/37] Use instance variables instead of constants --- lib/statistics/client.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index efd3f849c..24ead926b 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -68,13 +68,13 @@ def fetch_events(series_id:, yyyymm: nil) class Doorkeeper ENDPOINT = 'https://api.doorkeeper.jp'.freeze - DEFAULT_SINCE = Time.zone.parse('2010-07-01') - DEFAULT_UNTIL = Time.zone.now.end_of_day def initialize @client = Client.new(ENDPOINT) do |c| c.authorization(:Bearer, ENV.fetch('DOORKEEPER_API_TOKEN')) end + @default_since = Time.zone.parse('2010-07-01') + @default_until = Time.zone.now.end_of_day end def fetch_group_id(keyword:) @@ -83,7 +83,7 @@ def fetch_group_id(keyword:) .dig('event', 'group') end - def fetch_events(group_id:, since_at: DEFAULT_SINCE, until_at: DEFAULT_UNTIL) + def fetch_events(group_id:, since_at: @default_since, until_at: @default_until) params = { page: 1, since: since_at, From b1a5ad12a3af2b1d19a0907a5e17fc8354687377 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:43:40 +0900 Subject: [PATCH 16/37] Fix typo --- lib/statistics/aggregation.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/statistics/aggregation.rb b/lib/statistics/aggregation.rb index a60a95412..de6b21b0f 100644 --- a/lib/statistics/aggregation.rb +++ b/lib/statistics/aggregation.rb @@ -2,8 +2,8 @@ module Statistics class Aggregation class << self def run(date:) - cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'connpass' }).to_a - drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_service: { name: 'doorkeeper' }).to_a + cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: 'connpass' }).to_a + drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: 'doorkeeper' }).to_a Connpass.run(cnps_dojos, date) Doorkeeper.run(drkp_dojos, date) @@ -13,7 +13,7 @@ def run(date:) class Connpass class << self def run(dojos, date) - cnps = Cilient::Connpass.new + cnps = Client::Connpass.new params = { yyyymm: "#{date.year}#{date.month}" } From 719755818fcd27c989567f81ae76225a960069a3 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 7 Aug 2017 03:43:53 +0900 Subject: [PATCH 17/37] Create a task for the monthly aggregation --- lib/tasks/statistics.rake | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/tasks/statistics.rake diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake new file mode 100644 index 000000000..24316f587 --- /dev/null +++ b/lib/tasks/statistics.rake @@ -0,0 +1,20 @@ +require_relative '../statistics.rb' + +namespace :statistics do + desc '月次のイベント履歴を集計します' + task :aggregation, [:yyyymm] => :environment do |tasks, args| + date = Time.current.prev_month.beginning_of_month + if args[:yyyymm].present? + date = %w(%Y%m %Y/%m %Y-%m).map do |fmt| + begin + Time.zone.strptime(args[:yyyymm], fmt) + rescue ArgumentError + end + end.compact.first + end + + raise ArgumentError, "Invalid format: #{args[:yyyymm]}" if date.nil? + + Statistics::Aggregation.run(date: date) + end +end From 983d8288737869ab24b01f43cc361fc0362e6060 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 18:50:46 +0900 Subject: [PATCH 18/37] Want to see some search results --- lib/statistics/client.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index 24ead926b..3790950fc 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -32,11 +32,8 @@ def initialize @client = Client.new(ENDPOINT) end - def fetch_series_id(**params) - @client.get('event/', params.merge(count: 1)) - .fetch('events') - .first - .dig('series', 'id') + def search(keyword:) + @client.get('event/', { keyword: keyword, count: 100 }) end def fetch_events(series_id:, yyyymm: nil) @@ -77,10 +74,8 @@ def initialize @default_until = Time.zone.now.end_of_day end - def fetch_group_id(keyword:) - @client.get('events', q: keyword, since: DEFAULT_SINCE) - .first - .dig('event', 'group') + def search(keyword:) + @client.get('events', q: keyword, since: @default_since) end def fetch_events(group_id:, since_at: @default_since, until_at: @default_until) From cf033f8f58096be0d188a5bc955c155305bbe546 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 19:01:11 +0900 Subject: [PATCH 19/37] Rake task for searching --- lib/tasks/statistics.rake | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake index 24316f587..e26066bd7 100644 --- a/lib/tasks/statistics.rake +++ b/lib/tasks/statistics.rake @@ -17,4 +17,16 @@ namespace :statistics do Statistics::Aggregation.run(date: date) end + + task :search, [:keyword] => :environment do |tasks, args| + raise ArgumentError, 'Require the keyword' if args[:keyword].nil? + + require 'pp' + + puts 'Searching Connpass' + pp Statistics::Client::Connpass.new.search(keyword: args[:keyword]) + + puts 'Searching Doorkeeper' + pp Statistics::Client::Doorkeeper.new.search(keyword: args[:keyword]) + end end From 70ef722385f87554f62eb584bba1e7776c7af748 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 19:26:14 +0900 Subject: [PATCH 20/37] Fix typo --- lib/statistics.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/statistics.rb b/lib/statistics.rb index d232edc09..c6723f50d 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -1,4 +1,4 @@ module Statistics; end require_relative 'statistics/client' -require_relative 'Statistics/aggregation' +require_relative 'statistics/aggregation' From 5168879bc53b964c821c62009c993c69ad4b535c Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 19:35:45 +0900 Subject: [PATCH 21/37] Migrate database on Travis CI --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7588079b0..110efa3d7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,4 +12,5 @@ deploy: run: - "bundle exec rails db:migrate" - "bundle exec rails dojos:update_db_by_yaml" - +script: + - RAILS_ENV=test bundle exec rake db:migrate --trace From ba6fa8f3ecbed13a9e2f596b743187f5c9d5ca5b Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 21:31:14 +0900 Subject: [PATCH 22/37] Run RSpec on Travis CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 110efa3d7..c5e526017 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,3 +14,4 @@ deploy: - "bundle exec rails dojos:update_db_by_yaml" script: - RAILS_ENV=test bundle exec rake db:migrate --trace + - bundle exec rake From 59fe612a35f148de1cff1cd12a5110d209979cbe Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 21:35:50 +0900 Subject: [PATCH 23/37] Ruby 2.4.0 on Travis CI --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index c5e526017..bd1e216ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,8 @@ deploy: run: - "bundle exec rails db:migrate" - "bundle exec rails dojos:update_db_by_yaml" +rvm: + - 2.4.0 script: - RAILS_ENV=test bundle exec rake db:migrate --trace - bundle exec rake From ec719e1652c5baff96042d193903b168c43dd228 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 13 Aug 2017 21:38:23 +0900 Subject: [PATCH 24/37] Cache bundler on Travis CI --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index bd1e216ef..cbeebb9c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,8 @@ deploy: - "bundle exec rails dojos:update_db_by_yaml" rvm: - 2.4.0 +cache: + - bundler script: - RAILS_ENV=test bundle exec rake db:migrate --trace - bundle exec rake From 0024192bbce8838679cb358c07183b01e827646e Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 02:57:27 +0900 Subject: [PATCH 25/37] Shared context for API stubs --- spec/rails_helper.rb | 2 +- spec/support/shared_contexts/statistics.rb | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spec/support/shared_contexts/statistics.rb diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6f1ab1463..438342967 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -20,7 +20,7 @@ # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } # Checks for pending migration and applies them before tests are run. # If you are not using ActiveRecord, you can remove this line. diff --git a/spec/support/shared_contexts/statistics.rb b/spec/support/shared_contexts/statistics.rb new file mode 100644 index 000000000..fb303cb39 --- /dev/null +++ b/spec/support/shared_contexts/statistics.rb @@ -0,0 +1,35 @@ +RSpec.shared_context 'Use stubs for Faraday' do + let(:connpass_response) do + [ + 200, + { 'Content-Type' => 'application/json' }, + '{"results_returned": 1, "events": [{"event_url": "https://coderdojo-okutama.connpass.com/event/12345/", "event_type": "participation", "owner_nickname": "nalabjp", "series": {"url": "https://coderdojo-okutama.connpass.com/", "id": 9876, "title": "CoderDojo series"}, "updated_at": "2017-04-29T14:59:30+09:00", "lat": "35.801763000000", "started_at": "2017-05-07T10:00:00+09:00", "hash_tag": "CoderDojo", "title": "CoderDojo title", "event_id": 12345, "lon": "139.087656000000", "waiting": 2, "limit": 10, "owner_id": 2525, "owner_display_name": "nalabjp", "description": "CoderDojo description", "address": "Okutama-cho Tokyo", "catch": "CoderDojo catch", "accepted": 10, "ended_at": "2017-05-07T12:00:00+09:00", "place": "Tokyo"}], "results_start": 200, "results_available": 518}' + ] + end + + let(:doorkeeper_response) do + [ + 200, + { 'Content-Type' => 'application/json' }, + '[{"event":{"title":"CoderDojo title","id":1234,"starts_at":"2017-05-28T01:00:00.000Z","ends_at":"2017-05-28T04:00:00.000Z","venue_name":"奥多摩町","address":"奥多摩町","lat":"35.801763000000","long":"139.087656000000","ticket_limit":30,"published_at":"2017-04-22T03:43:04.000Z","updated_at":"2017-05-10T11:31:21.810Z","group":5555,"banner":null,"description":"CoderDojo description","public_url":"https://coderdojo-okutama.doorkeeper.jp/events/8888","participants":12,"waitlisted":0}}]' + ] + end + + let(:stub_connection) do + Faraday.new do |f| + f.response :json, :content_type => /\bjson$/ + f.adapter :test, Faraday::Adapter::Test::Stubs.new do |stub| + # connpass + stub.get('/event/') { connpass_response } + + # doorkeeper + stub.get('/events') { doorkeeper_response } + stub.get('/groups/5555/events') { doorkeeper_response } + end + end + end + + before do + allow_any_instance_of(Statistics::Client).to receive(:connection_for).and_return(stub_connection) + end +end From b7e821366cd57e0bcb5ca1ec0e69664dcd777d07 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 03:29:51 +0900 Subject: [PATCH 26/37] Remove nested Hash key --- lib/statistics/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index 3790950fc..aa9df9e78 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -91,7 +91,7 @@ def fetch_events(group_id:, since_at: @default_since, until_at: @default_until) break if part.size.zero? - events.push(*part) + events.push(*part.map { |e| e['event'] }) break if part.size < 25 # 25 items / 1 request From 58bfebde4794c02d1aa549ff03344496433e344f Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 03:30:48 +0900 Subject: [PATCH 27/37] Add testing for `Statistics::Client` --- spec/lib/statistics/client_spec.rb | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 spec/lib/statistics/client_spec.rb diff --git a/spec/lib/statistics/client_spec.rb b/spec/lib/statistics/client_spec.rb new file mode 100644 index 000000000..f5313b72b --- /dev/null +++ b/spec/lib/statistics/client_spec.rb @@ -0,0 +1,57 @@ +require 'rails_helper' +require 'statistics' + +RSpec.describe Statistics::Client do + include_context 'Use stubs for Faraday' + + context 'Connpass' do + describe '#search' do + subject { Statistics::Client::Connpass.new.search(keyword: 'coderdojo') } + + it do + expect(subject).to be_instance_of(Hash) + expect(subject['results_returned']).to eq 1 + expect(subject['events'].size).to eq 1 + expect(subject['events'].first['event_id']).to eq 12345 + expect(subject['events'].first['series']['url']).to eq 'https://coderdojo-okutama.connpass.com/' + expect(subject['events'].first['series']['id']).to eq 9876 + end + end + + describe '#fetch_events' do + subject { Statistics::Client::Connpass.new.fetch_events(series_id: 9876) } + + it do + expect(subject).to be_instance_of(Array) + expect(subject.size).to eq 1 + expect(subject.first['event_id']).to eq 12345 + expect(subject.first['series']['url']).to eq 'https://coderdojo-okutama.connpass.com/' + expect(subject.first['series']['id']).to eq 9876 + end + end + end + + context 'Doorkeeper' do + describe '#search' do + subject { Statistics::Client::Doorkeeper.new.search(keyword: 'coderdojo') } + + it do + expect(subject).to be_instance_of(Array) + expect(subject.size).to eq 1 + expect(subject.first['event']['id']).to eq 1234 + expect(subject.first['event']['group']).to eq 5555 + end + end + + describe '#fetch_events' do + subject { Statistics::Client::Doorkeeper.new.fetch_events(group_id: 5555) } + + it do + expect(subject).to be_instance_of(Array) + expect(subject.size).to eq 1 + expect(subject.first['id']).to eq 1234 + expect(subject.first['group']).to eq 5555 + end + end + end +end From ab9736dad9135fb56926f8bb4c611d6e8d7fc3c9 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 03:31:08 +0900 Subject: [PATCH 28/37] Add testing for `Statistics::Aggregation` --- spec/lib/statistics/aggregation_spec.rb | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 spec/lib/statistics/aggregation_spec.rb diff --git a/spec/lib/statistics/aggregation_spec.rb b/spec/lib/statistics/aggregation_spec.rb new file mode 100644 index 000000000..768b8b486 --- /dev/null +++ b/spec/lib/statistics/aggregation_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' +require 'statistics' + +RSpec.describe Statistics::Aggregation do + include_context 'Use stubs for Faraday' + + before(:all) do + Dojo.delete_all + DojoEventService.delete_all + EventHistory.delete_all + end + + after do + Dojo.delete_all + DojoEventService.delete_all + EventHistory.delete_all + end + + describe '.run' do + before do + d1 = Dojo.create(name: 'Dojo1', email: 'info@dojo1.com', description: 'CoderDojo1', tags: %w(CoderDojo1), url: 'https://dojo1.com') + d2 = Dojo.create(name: 'Dojo2', email: 'info@dojo2.com', description: 'CoderDojo2', tags: %w(CoderDojo2), url: 'https://dojo2.com') + DojoEventService.create(dojo_id: d1.id, name: 'connpass', group_id: 9876) + DojoEventService.create(dojo_id: d2.id, name: 'doorkeeper', group_id: 5555) + end + + subject { Statistics::Aggregation.run(date: Time.current) } + + it do + expect{ subject }.to change{EventHistory.count}.from(0).to(2) + end + end +end From 740ca73a1e654faf18612cc5e041bc2a9592a0e5 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 03:40:55 +0900 Subject: [PATCH 29/37] Remove records of the target period before executing the aggregation --- lib/statistics/aggregation.rb | 32 ++++++++++++++++---------------- lib/tasks/statistics.rake | 3 +++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/statistics/aggregation.rb b/lib/statistics/aggregation.rb index de6b21b0f..ab586319e 100644 --- a/lib/statistics/aggregation.rb +++ b/lib/statistics/aggregation.rb @@ -22,14 +22,14 @@ def run(dojos, date) cnps.fetch_events(params.merge(series_id: dojo.dojo_event_service.group_id)).each do |e| next unless e.dig('series', 'id') == dojo.dojo_event_service.group_id - EventHistory.find_or_create_by!(dojo_id: dojo.id, - dojo_name: dojo.name, - service_name: dojo.dojo_event_service.name, - service_group_id: dojo.dojo_event_service.group_id, - event_id: e['event_id'], - event_url: e['event_url'], - participants: e['accepted'], - evented_at: Time.zone.parse(e['started_at'])) + EventHistory.create!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['event_id'], + event_url: e['event_url'], + participants: e['accepted'], + evented_at: Time.zone.parse(e['started_at'])) end end end @@ -49,14 +49,14 @@ def run(dojos, date) drkp.fetch_events(params.merge(group_id: dojo.dojo_event_service.group_id)).each do |e| next unless e['group'] == dojo.dojo_event_service.group_id - EventHistory.find_or_create_by!(dojo_id: dojo.id, - dojo_name: dojo.name, - service_name: dojo.dojo_event_service.name, - service_group_id: dojo.dojo_event_service.group_id, - event_id: e['id'], - event_url: e['public_url'], - participants: e['participants'], - evented_at: Time.zone.parse(e['starts_at'])) + EventHistory.create!(dojo_id: dojo.id, + dojo_name: dojo.name, + service_name: dojo.dojo_event_service.name, + service_group_id: dojo.dojo_event_service.group_id, + event_id: e['id'], + event_url: e['public_url'], + participants: e['participants'], + evented_at: Time.zone.parse(e['starts_at'])) end end end diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake index e26066bd7..cbf483717 100644 --- a/lib/tasks/statistics.rake +++ b/lib/tasks/statistics.rake @@ -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 + Statistics::Aggregation.run(date: date) end From 86b1f6c1caadb2d5657035edcc82a34c08af9e83 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 14 Aug 2017 04:08:21 +0900 Subject: [PATCH 30/37] Add description for `statistics:search` --- lib/tasks/statistics.rake | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake index cbf483717..ba502d200 100644 --- a/lib/tasks/statistics.rake +++ b/lib/tasks/statistics.rake @@ -21,6 +21,7 @@ namespace :statistics do Statistics::Aggregation.run(date: date) end + desc 'キーワードからイベント情報を検索します' task :search, [:keyword] => :environment do |tasks, args| raise ArgumentError, 'Require the keyword' if args[:keyword].nil? From 563bd684b157236e461b4582e115fb6c40fa002e Mon Sep 17 00:00:00 2001 From: nalabjp Date: Tue, 15 Aug 2017 01:41:26 +0900 Subject: [PATCH 31/37] Remove needless configuration --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index cbeebb9c8..f018dc500 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,8 +12,6 @@ deploy: run: - "bundle exec rails db:migrate" - "bundle exec rails dojos:update_db_by_yaml" -rvm: - - 2.4.0 cache: - bundler script: From f44f78e23d52710f90769f09eebe412401993d15 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Tue, 15 Aug 2017 01:43:21 +0900 Subject: [PATCH 32/37] Remove needless configuration --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f018dc500..aa737071f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,5 +15,5 @@ deploy: cache: - bundler script: - - RAILS_ENV=test bundle exec rake db:migrate --trace + - bundle exec rake db:migrate --trace - bundle exec rake From 0d7230206e3e0f6692dc9e12ce2f67201c0239e5 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Tue, 15 Aug 2017 02:06:49 +0900 Subject: [PATCH 33/37] Fix association name --- app/models/dojo.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/dojo.rb b/app/models/dojo.rb index 909ffc03b..3a4001daf 100644 --- a/app/models/dojo.rb +++ b/app/models/dojo.rb @@ -4,7 +4,7 @@ class Dojo < ApplicationRecord NUM_OF_JAPAN_DOJOS = Dojo.count.to_s has_one :dojo_event_service - has_many :event_history + has_many :event_histories serialize :tags default_scope -> { order(order: :asc) } From 754cf521677b0e8d185b6bc553d806fef80250eb Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 20 Aug 2017 17:05:07 +0900 Subject: [PATCH 34/37] Remove needless argument --- lib/statistics/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index aa9df9e78..9ec627d52 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -13,7 +13,7 @@ def get(path, params) private - def connection_for(endpoint, &block) + def connection_for(endpoint) Faraday.new(endpoint) do |f| f.response :logger if self.class.debug f.response :json, :content_type => /\bjson$/ From 0366800d473911449ae6a3a1e0bfcabb9e002d01 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 20 Aug 2017 17:46:34 +0900 Subject: [PATCH 35/37] Move to `.travis.yml` In heroku, it is already defined. --- .travis.yml | 3 +++ config/application.rb | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aa737071f..2651fe9fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,3 +17,6 @@ cache: script: - bundle exec rake db:migrate --trace - bundle exec rake +env: + global: + - TZ='Asia/Tokyo' diff --git a/config/application.rb b/config/application.rb index efd250489..c8ed62753 100644 --- a/config/application.rb +++ b/config/application.rb @@ -14,6 +14,5 @@ class Application < Rails::Application # Timezone config.time_zone = 'Asia/Tokyo' - ENV['TZ'] = 'Asia/Tokyo' end end From 9adb714930dcbadd2dad4cea5f4e07077928e942 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 20 Aug 2017 17:54:46 +0900 Subject: [PATCH 36/37] Add `NOT NULL` restriction --- .../20170820085052_add_not_null_to_dojo_event_services.rb | 6 ++++++ db/schema.rb | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20170820085052_add_not_null_to_dojo_event_services.rb diff --git a/db/migrate/20170820085052_add_not_null_to_dojo_event_services.rb b/db/migrate/20170820085052_add_not_null_to_dojo_event_services.rb new file mode 100644 index 000000000..2c4d42252 --- /dev/null +++ b/db/migrate/20170820085052_add_not_null_to_dojo_event_services.rb @@ -0,0 +1,6 @@ +class AddNotNullToDojoEventServices < ActiveRecord::Migration[5.0] + def change + change_column_null :dojo_event_services, :name, false + change_column_null :dojo_event_services, :group_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 8000106a9..3507e5368 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170730114150) do +ActiveRecord::Schema.define(version: 20170820085052) do create_table "dojo_event_services", force: :cascade do |t| t.integer "dojo_id", null: false - t.string "name" + t.string "name", null: false t.string "url" - t.integer "group_id" + t.integer "group_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["dojo_id"], name: "index_dojo_event_services_on_dojo_id" From d392c6848815d0db40becb062445a4dfc8681700 Mon Sep 17 00:00:00 2001 From: nalabjp Date: Sun, 20 Aug 2017 18:31:15 +0900 Subject: [PATCH 37/37] Change the column type of `dojo_event_services.name` to `integer` from `string` --- app/models/dojo_event_service.rb | 1 + ...70820090605_change_column_type_dojo_event_services.rb | 9 +++++++++ db/schema.rb | 4 ++-- lib/statistics/aggregation.rb | 4 ++-- spec/lib/statistics/aggregation_spec.rb | 4 ++-- 5 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20170820090605_change_column_type_dojo_event_services.rb diff --git a/app/models/dojo_event_service.rb b/app/models/dojo_event_service.rb index d8692f84d..61e07b3ca 100644 --- a/app/models/dojo_event_service.rb +++ b/app/models/dojo_event_service.rb @@ -1,3 +1,4 @@ class DojoEventService < ApplicationRecord belongs_to :dojo + enum name: %i( connpass doorkeeper ) end diff --git a/db/migrate/20170820090605_change_column_type_dojo_event_services.rb b/db/migrate/20170820090605_change_column_type_dojo_event_services.rb new file mode 100644 index 000000000..ffe421e5d --- /dev/null +++ b/db/migrate/20170820090605_change_column_type_dojo_event_services.rb @@ -0,0 +1,9 @@ +class ChangeColumnTypeDojoEventServices < ActiveRecord::Migration[5.0] + def up + change_column :dojo_event_services, :name, :integer, null: false + end + + def down + change_column :dojo_event_services, :name, :string, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 3507e5368..c4ba02606 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170820085052) do +ActiveRecord::Schema.define(version: 20170820090605) do create_table "dojo_event_services", force: :cascade do |t| t.integer "dojo_id", null: false - t.string "name", null: false + t.integer "name", null: false t.string "url" t.integer "group_id", null: false t.datetime "created_at", null: false diff --git a/lib/statistics/aggregation.rb b/lib/statistics/aggregation.rb index ab586319e..f1c1b4109 100644 --- a/lib/statistics/aggregation.rb +++ b/lib/statistics/aggregation.rb @@ -2,8 +2,8 @@ module Statistics class Aggregation class << self def run(date:) - cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: 'connpass' }).to_a - drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: 'doorkeeper' }).to_a + cnps_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: :connpass }).to_a + drkp_dojos = Dojo.joins(:dojo_event_service).where(dojo_event_services: { name: :doorkeeper }).to_a Connpass.run(cnps_dojos, date) Doorkeeper.run(drkp_dojos, date) diff --git a/spec/lib/statistics/aggregation_spec.rb b/spec/lib/statistics/aggregation_spec.rb index 768b8b486..37a20aa23 100644 --- a/spec/lib/statistics/aggregation_spec.rb +++ b/spec/lib/statistics/aggregation_spec.rb @@ -20,8 +20,8 @@ before do d1 = Dojo.create(name: 'Dojo1', email: 'info@dojo1.com', description: 'CoderDojo1', tags: %w(CoderDojo1), url: 'https://dojo1.com') d2 = Dojo.create(name: 'Dojo2', email: 'info@dojo2.com', description: 'CoderDojo2', tags: %w(CoderDojo2), url: 'https://dojo2.com') - DojoEventService.create(dojo_id: d1.id, name: 'connpass', group_id: 9876) - DojoEventService.create(dojo_id: d2.id, name: 'doorkeeper', group_id: 5555) + DojoEventService.create(dojo_id: d1.id, name: :connpass, group_id: 9876) + DojoEventService.create(dojo_id: d2.id, name: :doorkeeper, group_id: 5555) end subject { Statistics::Aggregation.run(date: Time.current) }