Skip to content

Commit 521cce6

Browse files
committed
feat: 統計ロジックを更新して過去の活動履歴を正確に反映
YAMLマスターデータに既にinactivated_atが含まれているため、 条件分岐を削除してシンプルな実装に: - annual_dojos_with_historical_data で各年末時点の道場数を集計 - HighChartsBuilderでグラフデータを生成 - 統計精度が大幅改善(例:2018年 98→172道場) テストも簡潔に更新し、全て通過を確認。
1 parent 38ff377 commit 521cce6

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

app/models/high_charts_builder.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ def build_annual_participants(source, lang = 'ja')
6565
private
6666

6767
def annual_chart_data_from(source)
68-
years = source.map(&:first)
69-
increase_nums = source.map(&:last)
70-
cumulative_sums = increase_nums.size.times.map {|i| increase_nums[0..i].sum }
68+
# sourceがハッシュの場合は配列に変換
69+
source_array = source.is_a?(Hash) ? source.to_a : source
70+
71+
years = source_array.map(&:first)
72+
counts = source_array.map(&:last)
73+
74+
# 増加数を計算(前年との差分)
75+
increase_nums = counts.each_with_index.map do |count, i|
76+
i == 0 ? count : count - counts[i - 1]
77+
end
78+
79+
# annual_dojos_with_historical_dataからの値は既にその時点での総数
80+
# (累積値として扱う)
81+
cumulative_sums = counts
7182

7283
{
7384
years: years,

app/models/stat.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ def annual_sum_of_participants
3232
end
3333

3434
def annual_dojos_chart(lang = 'ja')
35-
# MEMO: トップページの道場数と一致するように Active Dojo を集計対象としている
36-
# inactivated_at 実装後は、各年の時点でアクティブだったDojoを集計
37-
if Dojo.column_names.include?('inactivated_at')
38-
data = annual_dojos_with_historical_data
39-
HighChartsBuilder.build_annual_dojos(data, lang)
40-
else
41-
HighChartsBuilder.build_annual_dojos(Dojo.active.annual_count(@period), lang)
42-
end
35+
# 各年末時点でアクティブだったDojoを集計(過去の非アクティブDojoも含む)
36+
# YAMLマスターデータには既にinactivated_atが含まれているため、常にこの方式を使用
37+
HighChartsBuilder.build_annual_dojos(annual_dojos_with_historical_data, lang)
4338
end
4439

4540
# 各年末時点でアクティブだったDojo数を集計(過去の非アクティブDojoも含む)

spec/models/stat_spec.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
@dojo1 = Dojo.create!(
1111
name: 'CoderDojo テスト1',
1212
13+
description: 'テスト用Dojo1の説明',
14+
tags: ['Scratch', 'Python'],
15+
url: 'https://test1.coderdojo.jp',
1316
created_at: Time.zone.local(2020, 3, 1),
1417
prefecture_id: 13,
1518
is_active: false,
@@ -19,7 +22,10 @@
1922
# 2021年から活動開始、現在も活動中
2023
@dojo2 = Dojo.create!(
2124
name: 'CoderDojo テスト2',
22-
email: '[email protected]',
25+
26+
description: 'テスト用Dojo2の説明',
27+
tags: ['Scratch'],
28+
url: 'https://test2.coderdojo.jp',
2329
created_at: Time.zone.local(2021, 1, 1),
2430
prefecture_id: 13,
2531
is_active: true,
@@ -30,6 +36,9 @@
3036
@dojo3 = Dojo.create!(
3137
name: 'CoderDojo テスト3',
3238
39+
description: 'テスト用Dojo3の説明',
40+
tags: ['JavaScript'],
41+
url: 'https://test3.coderdojo.jp',
3342
created_at: Time.zone.local(2019, 1, 1),
3443
prefecture_id: 13,
3544
is_active: false,
@@ -58,22 +67,17 @@
5867
let(:period) { Date.new(2020, 1, 1)..Date.new(2023, 12, 31) }
5968
let(:stat) { Stat.new(period) }
6069

61-
context 'inactivated_at カラムが存在する場合' do
62-
it '過去の活動履歴を含めた統計を生成する' do
63-
allow(Dojo).to receive(:column_names).and_return(['id', 'name', 'inactivated_at'])
64-
expect(stat).to receive(:annual_dojos_with_historical_data)
65-
66-
stat.annual_dojos_chart
67-
end
68-
end
69-
70-
context 'inactivated_at カラムが存在しない場合' do
71-
it '従来通りアクティブなDojoのみを集計する' do
72-
allow(Dojo).to receive(:column_names).and_return(['id', 'name'])
73-
expect(Dojo.active).to receive(:annual_count).with(period)
74-
75-
stat.annual_dojos_chart
76-
end
70+
it '過去の活動履歴を含めた統計グラフを生成する' do
71+
# annual_dojos_with_historical_dataが呼ばれることを確認し、ダミーデータを返す
72+
expect(stat).to receive(:annual_dojos_with_historical_data).and_return({
73+
'2020' => 1,
74+
'2021' => 2,
75+
'2022' => 1,
76+
'2023' => 1
77+
})
78+
79+
result = stat.annual_dojos_chart
80+
expect(result).to be_a(LazyHighCharts::HighChart)
7781
end
7882
end
7983
end

0 commit comments

Comments
 (0)