|
|
@ -5,6 +5,7 @@ |
|
|
|
package models |
|
|
|
package models |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
"github.com/go-xorm/xorm" |
|
|
@ -25,6 +26,30 @@ type ActivityStats struct { |
|
|
|
PublishedReleaseAuthorCount int64 |
|
|
|
PublishedReleaseAuthorCount int64 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetActivityStats return stats for repository at given time range
|
|
|
|
|
|
|
|
func GetActivityStats(repoID int64, timeFrom time.Time, releases, issues, prs bool) (*ActivityStats, error) { |
|
|
|
|
|
|
|
stats := &ActivityStats{} |
|
|
|
|
|
|
|
if releases { |
|
|
|
|
|
|
|
if err := stats.FillReleases(repoID, timeFrom); err != nil { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("FillReleases: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if prs { |
|
|
|
|
|
|
|
if err := stats.FillPullRequests(repoID, timeFrom); err != nil { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("FillPullRequests: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if issues { |
|
|
|
|
|
|
|
if err := stats.FillIssues(repoID, timeFrom); err != nil { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("FillIssues: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := stats.FillUnresolvedIssues(repoID, timeFrom, issues, prs); err != nil { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("FillUnresolvedIssues: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return stats, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ActivePRCount returns total active pull request count
|
|
|
|
// ActivePRCount returns total active pull request count
|
|
|
|
func (stats *ActivityStats) ActivePRCount() int { |
|
|
|
func (stats *ActivityStats) ActivePRCount() int { |
|
|
|
return stats.OpenedPRCount() + stats.MergedPRCount() |
|
|
|
return stats.OpenedPRCount() + stats.MergedPRCount() |
|
|
@ -85,13 +110,13 @@ func (stats *ActivityStats) PublishedReleaseCount() int { |
|
|
|
return len(stats.PublishedReleases) |
|
|
|
return len(stats.PublishedReleases) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FillPullRequestsForActivity returns pull request information for activity page
|
|
|
|
// FillPullRequests returns pull request information for activity page
|
|
|
|
func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error { |
|
|
|
func (stats *ActivityStats) FillPullRequests(repoID int64, fromTime time.Time) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
var count int64 |
|
|
|
var count int64 |
|
|
|
|
|
|
|
|
|
|
|
// Merged pull requests
|
|
|
|
// Merged pull requests
|
|
|
|
sess := pullRequestsForActivityStatement(baseRepoID, fromTime, true) |
|
|
|
sess := pullRequestsForActivityStatement(repoID, fromTime, true) |
|
|
|
sess.OrderBy("pull_request.merged_unix DESC") |
|
|
|
sess.OrderBy("pull_request.merged_unix DESC") |
|
|
|
stats.MergedPRs = make(PullRequestList, 0) |
|
|
|
stats.MergedPRs = make(PullRequestList, 0) |
|
|
|
if err = sess.Find(&stats.MergedPRs); err != nil { |
|
|
|
if err = sess.Find(&stats.MergedPRs); err != nil { |
|
|
@ -102,14 +127,14 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Merged pull request authors
|
|
|
|
// Merged pull request authors
|
|
|
|
sess = pullRequestsForActivityStatement(baseRepoID, fromTime, true) |
|
|
|
sess = pullRequestsForActivityStatement(repoID, fromTime, true) |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil { |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
stats.MergedPRAuthorCount = count |
|
|
|
stats.MergedPRAuthorCount = count |
|
|
|
|
|
|
|
|
|
|
|
// Opened pull requests
|
|
|
|
// Opened pull requests
|
|
|
|
sess = pullRequestsForActivityStatement(baseRepoID, fromTime, false) |
|
|
|
sess = pullRequestsForActivityStatement(repoID, fromTime, false) |
|
|
|
sess.OrderBy("issue.created_unix ASC") |
|
|
|
sess.OrderBy("issue.created_unix ASC") |
|
|
|
stats.OpenedPRs = make(PullRequestList, 0) |
|
|
|
stats.OpenedPRs = make(PullRequestList, 0) |
|
|
|
if err = sess.Find(&stats.OpenedPRs); err != nil { |
|
|
|
if err = sess.Find(&stats.OpenedPRs); err != nil { |
|
|
@ -120,7 +145,7 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Opened pull request authors
|
|
|
|
// Opened pull request authors
|
|
|
|
sess = pullRequestsForActivityStatement(baseRepoID, fromTime, false) |
|
|
|
sess = pullRequestsForActivityStatement(repoID, fromTime, false) |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil { |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -129,8 +154,8 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func pullRequestsForActivityStatement(baseRepoID int64, fromTime time.Time, merged bool) *xorm.Session { |
|
|
|
func pullRequestsForActivityStatement(repoID int64, fromTime time.Time, merged bool) *xorm.Session { |
|
|
|
sess := x.Where("pull_request.base_repo_id=?", baseRepoID). |
|
|
|
sess := x.Where("pull_request.base_repo_id=?", repoID). |
|
|
|
Join("INNER", "issue", "pull_request.issue_id = issue.id") |
|
|
|
Join("INNER", "issue", "pull_request.issue_id = issue.id") |
|
|
|
|
|
|
|
|
|
|
|
if merged { |
|
|
|
if merged { |
|
|
@ -144,13 +169,13 @@ func pullRequestsForActivityStatement(baseRepoID int64, fromTime time.Time, merg |
|
|
|
return sess |
|
|
|
return sess |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FillIssuesForActivity returns issue information for activity page
|
|
|
|
// FillIssues returns issue information for activity page
|
|
|
|
func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error { |
|
|
|
func (stats *ActivityStats) FillIssues(repoID int64, fromTime time.Time) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
var count int64 |
|
|
|
var count int64 |
|
|
|
|
|
|
|
|
|
|
|
// Closed issues
|
|
|
|
// Closed issues
|
|
|
|
sess := issuesForActivityStatement(baseRepoID, fromTime, true, false) |
|
|
|
sess := issuesForActivityStatement(repoID, fromTime, true, false) |
|
|
|
sess.OrderBy("issue.updated_unix DESC") |
|
|
|
sess.OrderBy("issue.updated_unix DESC") |
|
|
|
stats.ClosedIssues = make(IssueList, 0) |
|
|
|
stats.ClosedIssues = make(IssueList, 0) |
|
|
|
if err = sess.Find(&stats.ClosedIssues); err != nil { |
|
|
|
if err = sess.Find(&stats.ClosedIssues); err != nil { |
|
|
@ -158,14 +183,14 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Closed issue authors
|
|
|
|
// Closed issue authors
|
|
|
|
sess = issuesForActivityStatement(baseRepoID, fromTime, true, false) |
|
|
|
sess = issuesForActivityStatement(repoID, fromTime, true, false) |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil { |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
stats.ClosedIssueAuthorCount = count |
|
|
|
stats.ClosedIssueAuthorCount = count |
|
|
|
|
|
|
|
|
|
|
|
// New issues
|
|
|
|
// New issues
|
|
|
|
sess = issuesForActivityStatement(baseRepoID, fromTime, false, false) |
|
|
|
sess = issuesForActivityStatement(repoID, fromTime, false, false) |
|
|
|
sess.OrderBy("issue.created_unix ASC") |
|
|
|
sess.OrderBy("issue.created_unix ASC") |
|
|
|
stats.OpenedIssues = make(IssueList, 0) |
|
|
|
stats.OpenedIssues = make(IssueList, 0) |
|
|
|
if err = sess.Find(&stats.OpenedIssues); err != nil { |
|
|
|
if err = sess.Find(&stats.OpenedIssues); err != nil { |
|
|
@ -173,7 +198,7 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Opened issue authors
|
|
|
|
// Opened issue authors
|
|
|
|
sess = issuesForActivityStatement(baseRepoID, fromTime, false, false) |
|
|
|
sess = issuesForActivityStatement(repoID, fromTime, false, false) |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil { |
|
|
|
if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -182,13 +207,13 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FillUnresolvedIssuesForActivity returns unresolved issue and pull request information for activity page
|
|
|
|
// FillUnresolvedIssues returns unresolved issue and pull request information for activity page
|
|
|
|
func FillUnresolvedIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time, issues, prs bool) error { |
|
|
|
func (stats *ActivityStats) FillUnresolvedIssues(repoID int64, fromTime time.Time, issues, prs bool) error { |
|
|
|
// Check if we need to select anything
|
|
|
|
// Check if we need to select anything
|
|
|
|
if !issues && !prs { |
|
|
|
if !issues && !prs { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
sess := issuesForActivityStatement(baseRepoID, fromTime, false, true) |
|
|
|
sess := issuesForActivityStatement(repoID, fromTime, false, true) |
|
|
|
if !issues || !prs { |
|
|
|
if !issues || !prs { |
|
|
|
sess.And("issue.is_pull = ?", prs) |
|
|
|
sess.And("issue.is_pull = ?", prs) |
|
|
|
} |
|
|
|
} |
|
|
@ -197,8 +222,8 @@ func FillUnresolvedIssuesForActivity(stats *ActivityStats, baseRepoID int64, fro |
|
|
|
return sess.Find(&stats.UnresolvedIssues) |
|
|
|
return sess.Find(&stats.UnresolvedIssues) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func issuesForActivityStatement(baseRepoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session { |
|
|
|
func issuesForActivityStatement(repoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session { |
|
|
|
sess := x.Where("issue.repo_id = ?", baseRepoID). |
|
|
|
sess := x.Where("issue.repo_id = ?", repoID). |
|
|
|
And("issue.is_closed = ?", closed) |
|
|
|
And("issue.is_closed = ?", closed) |
|
|
|
|
|
|
|
|
|
|
|
if !unresolved { |
|
|
|
if !unresolved { |
|
|
@ -212,13 +237,13 @@ func issuesForActivityStatement(baseRepoID int64, fromTime time.Time, closed, un |
|
|
|
return sess |
|
|
|
return sess |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FillReleasesForActivity returns release information for activity page
|
|
|
|
// FillReleases returns release information for activity page
|
|
|
|
func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error { |
|
|
|
func (stats *ActivityStats) FillReleases(repoID int64, fromTime time.Time) error { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
var count int64 |
|
|
|
var count int64 |
|
|
|
|
|
|
|
|
|
|
|
// Published releases list
|
|
|
|
// Published releases list
|
|
|
|
sess := releasesForActivityStatement(baseRepoID, fromTime) |
|
|
|
sess := releasesForActivityStatement(repoID, fromTime) |
|
|
|
sess.OrderBy("release.created_unix DESC") |
|
|
|
sess.OrderBy("release.created_unix DESC") |
|
|
|
stats.PublishedReleases = make([]*Release, 0) |
|
|
|
stats.PublishedReleases = make([]*Release, 0) |
|
|
|
if err = sess.Find(&stats.PublishedReleases); err != nil { |
|
|
|
if err = sess.Find(&stats.PublishedReleases); err != nil { |
|
|
@ -226,7 +251,7 @@ func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime ti |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Published releases authors
|
|
|
|
// Published releases authors
|
|
|
|
sess = releasesForActivityStatement(baseRepoID, fromTime) |
|
|
|
sess = releasesForActivityStatement(repoID, fromTime) |
|
|
|
if _, err = sess.Select("count(distinct release.publisher_id) as `count`").Table("release").Get(&count); err != nil { |
|
|
|
if _, err = sess.Select("count(distinct release.publisher_id) as `count`").Table("release").Get(&count); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -235,8 +260,8 @@ func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime ti |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func releasesForActivityStatement(baseRepoID int64, fromTime time.Time) *xorm.Session { |
|
|
|
func releasesForActivityStatement(repoID int64, fromTime time.Time) *xorm.Session { |
|
|
|
return x.Where("release.repo_id = ?", baseRepoID). |
|
|
|
return x.Where("release.repo_id = ?", repoID). |
|
|
|
And("release.is_draft = ?", false). |
|
|
|
And("release.is_draft = ?", false). |
|
|
|
And("release.created_unix >= ?", fromTime.Unix()) |
|
|
|
And("release.created_unix >= ?", fromTime.Unix()) |
|
|
|
} |
|
|
|
} |
|
|
|