Skip to content

Conversation

@mathsuky
Copy link
Contributor

@mathsuky mathsuky commented Oct 13, 2025

User description

複数の時間台にまたがる(例14時台,15時台にまたがる(14:15-15:20))様なものを分離して記録するロジックをうまくSQLで表現できず,取ってきてGoでごちゃごちゃ処理する様な形になってしまいました。その点について,うまいやり方があればご教示いただきたいです:pray:
(#1393 をみたのですが,これは一方の時間帯のログとして処理していそう?)


PR Type

Enhancement, Tests


Description

  • Edition別プレイ統計の実装

  • 進行中ログの時間分割集計

  • 存在確認と未検出時のエラー対応

  • 包括的な統計テスト追加


Diagram Walkthrough

flowchart LR
  repo["GetEditionPlayStats 実装"] -- "DB取得・存在確認" --> edition["Edition取得"]
  repo -- "期間交差ログ取得" --> logs["PlayLogs抽出 (start/end)"]
  logs -- "時間帯ごとに分割" --> hourly["Hourly集計 (count/time)"]
  logs -- "ゲーム別集計" --> perGame["Game別集計 (count/time)"]
  hourly -- "ソート" --> sortHourly["Hourlyソート"]
  perGame -- "配列化" --> gameStats["GameStats配列"]
  sortHourly -- "統計生成" --> result["EditionPlayStats返却"]
  tests["包括テスト追加"] -- "正常/進行中/未存在" --> repo
Loading

File Walkthrough

Relevant files
Enhancement
v2_game_play_log.go
Edition別プレイ統計取得処理を実装                                                                         

src/repository/gorm2/v2_game_play_log.go

  • GetEditionPlayStatsを実装
  • Edition存在チェックとErrRecordNotFound対応
  • 期間交差・進行中ログの時間帯分割集計
  • Hourly/ゲーム別統計の生成とソート
+130/-3 
Tests
v2_game_play_log_test.go
Edition別統計の包括テストを追加                                                                           

src/repository/gorm2/v2_game_play_log_test.go

  • GetEditionPlayStatsのスキップ解除とテスト追加
  • 正常系・進行中ログ・未存在Editionのケース
  • 期待統計の詳細検証とアサート拡充
  • テストデータの作成とクリーンアップ
+278/-15

@mathsuky mathsuky requested a review from a team as a code owner October 13, 2025 13:36
@github-actions
Copy link

Migrate lint ✅

Lint output
Notice: Starting with v0.38, 'atlas migrate lint' will only be available in Atlas Pro.
The command and existing code remain open source and available in the Community Edition.
Learn more: https://atlasgo.io/blog-v037#deprecate-lint

1 similar comment
@github-actions
Copy link

github-actions bot commented Oct 13, 2025

Migrate lint ✅

Lint output
Notice: Starting with v0.38, 'atlas migrate lint' will only be available in Atlas Pro.
The command and existing code remain open source and available in the Community Edition.
Learn more: https://atlasgo.io/blog-v037#deprecate-lint

@codecov
Copy link

codecov bot commented Oct 13, 2025

Codecov Report

❌ Patch coverage is 87.96296% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.51%. Comparing base (3acfa53) to head (ab1ce26).
⚠️ Report is 35 commits behind head on main.

Files with missing lines Patch % Lines
src/repository/gorm2/v2_game_play_log.go 87.96% 9 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1403      +/-   ##
==========================================
+ Coverage   50.00%   50.51%   +0.51%     
==========================================
  Files         135      136       +1     
  Lines       11810    11979     +169     
==========================================
+ Hits         5905     6051     +146     
- Misses       5601     5620      +19     
- Partials      304      308       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mathsuky mathsuky requested a review from ikura-hamu October 13, 2025 18:19
Copy link
Member

@ikura-hamu ikura-hamu left a comment

Choose a reason for hiding this comment

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

ありがとう、とてもよく書けていると思います。
SQLで書くのもたぶんできなくはないんですけど難しいので、今回は読みやすさと実装しやすさを優先して全件取得からGoで集計の今の方針でいいと思います。

(#1393 をみたのですが,これは一方の時間帯のログとして処理していそう?)

これがよくわからなかったんですが、説明をお願いしてもいいですか?
このPRのロジックは多分これで合っていると思います。

))
}

sort.Slice(hourlyStats, func(i, j int) bool {
Copy link
Member

Choose a reason for hiding this comment

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

sortパッケージはジェネリクスが入る前の機能なので、代わりにslices.SortFuncを使うようにしてください。

hourStart := current
nextHour := current.Add(time.Hour)

playStart := logStart
Copy link
Member

Choose a reason for hiding this comment

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

playStartplayEndは、そのhourの間のプレイ開始・終了時刻を表すと思うのですが、hour要素が無くてわかりにくい気がします。hourlyPlayStarthourlyPlayEndのような感じにするのはどうでしょうか

gameStats.playTime += playDuration
gameStatsMap[playLog.GameID] = gameStats

current := time.Date(logStart.Year(), logStart.Month(), logStart.Day(), logStart.Hour(), 0, 0, 0, logStart.Location())
Copy link
Member

Choose a reason for hiding this comment

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

time.Time型のcurrentは現在時刻感が非常に強くなってしまうので、違う名前にしたいです。hourlyRangeStartとかはどうでしょうか

current := time.Date(logStart.Year(), logStart.Month(), logStart.Day(), logStart.Hour(), 0, 0, 0, logStart.Location())
isFirstHour := true

for current.Before(logEnd) {
Copy link
Member

Choose a reason for hiding this comment

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

forの3つ並べるやつでやると、横に長くなっちゃうけど、意味がとらえやすいのとhourlyRangeStartのスコープが狭まっていい感じになると思います。

for hourlyRangeStart := time.Date(...); hourlyRangeStart.Before(logEnd); hourlyRangeStart = hourlyRangeStart.Add(time.Hour) {
  ...
}

err = db.Model(&schema.GamePlayLogTable{}).
Where("edition_id = ?", uuid.UUID(editionID)).
Where("start_time <= ?", end).
Where("(end_time >= ? OR end_time IS NULL)", start).
Copy link
Member

Choose a reason for hiding this comment

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

仕様が明確になってなかった気がしますが、ここの不等号のイコールはいらないかもです。
仕様としては、重複が発生してしまうので、[start,end)の範囲でいいんじゃないかなと思っています。

return
}

require.NoError(t, err)
Copy link
Member

Choose a reason for hiding this comment

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

テストの結果としてエラー出ないことを知りたいので、assertにしてください。

Suggested change
require.NoError(t, err)
assert.NoError(t, err)

assert.ErrorIs(t, err, testCase.err)
assert.Error(t, err)
if testCase.err != nil {
assert.Equal(t, testCase.err, err)
Copy link
Member

Choose a reason for hiding this comment

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

エラーの比較にはEqualではなくErrorsIsを使うようにしてください

Suggested change
assert.Equal(t, testCase.err, err)
assert.ErrorsIs(t, testCase.err, err)

@mathsuky
Copy link
Contributor Author

(#1393 をみたのですが,これは一方の時間帯のログとして処理していそう?)

これがよくわからなかったんですが、説明をお願いしてもいいですか?
このPRのロジックは多分これで合っていると思います。

こちらは自分の勘違いでした(仕様をごっちゃにしていました)。すみません:pray:

@mathsuky mathsuky requested a review from ikura-hamu October 17, 2025 13:21
Copy link
Member

@ikura-hamu ikura-hamu left a comment

Choose a reason for hiding this comment

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

ありがとう。issueもcloseしちゃってください

@mathsuky mathsuky merged commit 8e01197 into main Oct 19, 2025
11 checks passed
@mathsuky mathsuky deleted the feat/imp-repository-GetEditionPlayStats branch October 19, 2025 04:28
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