Skip to content

Commit 646d318

Browse files
committed
feat: 道場の新規開設数を計算するメソッドを追加
統計グラフで「開設数」を正確に表示するため、各年に新規開設された 道場数を集計するannual_new_dojos_countメソッドを実装。 Changes: - annual_new_dojos_count: created_atから各年の新規道場数を集計 - annual_dojos_chart: 新旧両方のデータ(累積数と開設数)を渡す形式に変更 これにより、道場が閉鎖された年でも負の値ではなく、 実際の新規開設数(0以上)を表示できるようになる。
1 parent 521cce6 commit 646d318

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

app/models/stat.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ def annual_sum_of_participants
3434
def annual_dojos_chart(lang = 'ja')
3535
# 各年末時点でアクティブだったDojoを集計(過去の非アクティブDojoも含む)
3636
# YAMLマスターデータには既にinactivated_atが含まれているため、常にこの方式を使用
37-
HighChartsBuilder.build_annual_dojos(annual_dojos_with_historical_data, lang)
37+
data = {
38+
active_dojos: annual_dojos_with_historical_data,
39+
new_dojos: annual_new_dojos_count
40+
}
41+
HighChartsBuilder.build_annual_dojos(data, lang)
3842
end
3943

4044
# 各年末時点でアクティブだったDojo数を集計(過去の非アクティブDojoも含む)
@@ -46,6 +50,17 @@ def annual_dojos_with_historical_data
4650
hash[year.to_s] = count
4751
end
4852
end
53+
54+
# 各年に新規開設されたDojo数を集計
55+
def annual_new_dojos_count
56+
(@period.first.year..@period.last.year).each_with_object({}) do |year, hash|
57+
start_of_year = Time.zone.local(year).beginning_of_year
58+
end_of_year = Time.zone.local(year).end_of_year
59+
# その年に作成されたDojoの数を集計
60+
count = Dojo.where(created_at: start_of_year..end_of_year).sum(:counter)
61+
hash[year.to_s] = count
62+
end
63+
end
4964

5065
def annual_event_histories_chart(lang = 'ja')
5166
HighChartsBuilder.build_annual_event_histories(annual_count_of_event_histories, lang)

0 commit comments

Comments
 (0)