Skip to content

Commit

Permalink
fix sql
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Feb 2, 2025
1 parent d924de1 commit 7843cbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 5 additions & 5 deletions models/organization/org_worktime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetWorktimeByRepos(org *Organization, unitFrom, unixTo int64) (results []Wo
And(builder.Eq{"tracked_time.deleted": false}).
And(builder.Gte{"tracked_time.created_unix": unitFrom}).
And(builder.Lte{"tracked_time.created_unix": unixTo}).
GroupBy("repository.id").
GroupBy("repository.name").
OrderBy("repository.name").
Find(&results)
return results, err
Expand All @@ -49,7 +49,7 @@ func GetWorktimeByMilestones(org *Organization, unitFrom, unixTo int64) (results
And(builder.Eq{"tracked_time.deleted": false}).
And(builder.Gte{"tracked_time.created_unix": unitFrom}).
And(builder.Lte{"tracked_time.created_unix": unixTo}).
GroupBy("repository.id, milestone.id").
GroupBy("repository.name, milestone.name, milestone.deadline_unix, milestone.id").
OrderBy("repository.name, milestone.deadline_unix, milestone.id").
Find(&results)
// Show only the first RepoName, for nicer output.
Expand All @@ -71,16 +71,16 @@ type WorktimeSumByMembers struct {

func GetWorktimeByMembers(org *Organization, unitFrom, unixTo int64) (results []WorktimeSumByMembers, err error) {
err = db.GetEngine(db.DefaultContext).
Select("user.name AS user_name, SUM(tracked_time.time) AS sum_time").
Select("`user`.name AS user_name, SUM(tracked_time.time) AS sum_time").
Table("tracked_time").
Join("INNER", "issue", "tracked_time.issue_id = issue.id").
Join("INNER", "repository", "issue.repo_id = repository.id").
Join("INNER", "user", "tracked_time.user_id = user.id").
Join("INNER", "`user`", "tracked_time.user_id = `user`.id").
Where(builder.Eq{"repository.owner_id": org.ID}).
And(builder.Eq{"tracked_time.deleted": false}).
And(builder.Gte{"tracked_time.created_unix": unitFrom}).
And(builder.Lte{"tracked_time.created_unix": unixTo}).
GroupBy("user.id").
GroupBy("`user`.name").
OrderBy("sum_time DESC").
Find(&results)
return results, err
Expand Down
14 changes: 8 additions & 6 deletions tests/integration/org_worktime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/unittest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestTimesByRepos tests TimesByRepos functionality
Expand Down Expand Up @@ -191,10 +192,11 @@ func testTimesByMilestones(t *testing.T) {
for _, kase := range kases {
t.Run(kase.name, func(t *testing.T) {
org, err := organization.GetOrgByID(db.DefaultContext, kase.orgname)
assert.NoError(t, err)
require.NoError(t, err)
results, err := organization.GetWorktimeByMilestones(org, kase.unixfrom, kase.unixto)
assert.NoError(t, err)
assert.Equal(t, kase.expected, results)
if assert.NoError(t, err) {
assert.Equal(t, kase.expected, results)
}
})
}
}
Expand Down Expand Up @@ -285,7 +287,7 @@ func testTimesByMembers(t *testing.T) {
func TestOrgWorktime(t *testing.T) {
// we need to run these tests in integration test because there are complex SQL queries
assert.NoError(t, unittest.PrepareTestDatabase())
testTimesByRepos(t)
testTimesByMilestones(t)
testTimesByMembers(t)
t.Run("ByRepos", testTimesByRepos)
t.Run("ByMilestones", testTimesByMilestones)
t.Run("ByMembers", testTimesByMembers)
}

0 comments on commit 7843cbb

Please sign in to comment.