| @@ -7,6 +7,7 @@ | |||||
| package models | package models | ||||
| import ( | import ( | ||||
| "encoding/binary" | |||||
| "fmt" | "fmt" | ||||
| "strings" | "strings" | ||||
| @@ -1016,3 +1017,12 @@ func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID | |||||
| }) | }) | ||||
| return err | return err | ||||
| } | } | ||||
| func GetCommentCountByRepoID(repoID int64) (int64, error) { | |||||
| sql := fmt.Sprintf("select count(1) from comment where issue_id in (select id from issue where repo_id = %d) and type = %d;", repoID, CommentTypeComment) | |||||
| res, err := x.Query(sql) | |||||
| if err != nil { | |||||
| return 0, err | |||||
| } | |||||
| return int64(binary.BigEndian.Uint64(res[0]["count"])), nil | |||||
| } | |||||
| @@ -1,25 +1,23 @@ | |||||
| package models | package models | ||||
| import ( | import ( | ||||
| "fmt" | |||||
| "time" | |||||
| "code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
| "fmt" | |||||
| ) | ) | ||||
| // RepoStatistic statistic info of all repository | // RepoStatistic statistic info of all repository | ||||
| type RepoStatistic struct { | type RepoStatistic struct { | ||||
| ID int64 `xorm:"pk autoincr"` | |||||
| RepoID int64 `xorm:"unique(s) NOT NULL"` | |||||
| Date time.Time `xorm:"unique(s) NOT NULL"` | |||||
| NumWatches int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumStars int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumForks int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumDownloads int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumComments int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumVisits int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumClosedIssues int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumVersions int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| ID int64 `xorm:"pk autoincr"` | |||||
| RepoID int64 `xorm:"unique(s) NOT NULL"` | |||||
| Date string `xorm:"unique(s) NOT NULL"` | |||||
| NumWatches int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumStars int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumForks int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumDownloads int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumComments int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumVisits int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumClosedIssues int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| NumVersions int64 `xorm:"NOT NULL DEFAULT 0"` | |||||
| //develop months | //develop months | ||||
| NumDevMonths int64 `xorm:"NOT NULL DEFAULT 0"` | NumDevMonths int64 `xorm:"NOT NULL DEFAULT 0"` | ||||
| RepoSize int64 `xorm:"NOT NULL DEFAULT 0"` | RepoSize int64 `xorm:"NOT NULL DEFAULT 0"` | ||||
| @@ -37,7 +35,7 @@ type RepoStatistic struct { | |||||
| UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` | UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` | ||||
| } | } | ||||
| func DeleteRepoStatDaily(date time.Time) error { | |||||
| func DeleteRepoStatDaily(date string) error { | |||||
| sess := xStatistic.NewSession() | sess := xStatistic.NewSession() | ||||
| defer sess.Close() | defer sess.Close() | ||||
| if err := sess.Begin(); err != nil { | if err := sess.Begin(); err != nil { | ||||
| @@ -43,6 +43,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Post("/manager/restart", Restart) | m.Post("/manager/restart", Restart) | ||||
| m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues) | m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues) | ||||
| m.Post("/cmd/update_all_repo_commit_cnt", UpdateAllRepoCommitCnt) | m.Post("/cmd/update_all_repo_commit_cnt", UpdateAllRepoCommitCnt) | ||||
| m.Post("/tool/repo_stat", RepoStatisticManually) | |||||
| }, CheckInternalToken) | }, CheckInternalToken) | ||||
| } | } | ||||
| @@ -5,11 +5,13 @@ | |||||
| package private | package private | ||||
| import ( | import ( | ||||
| "gitea.com/macaron/macaron" | |||||
| "net/http" | "net/http" | ||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| "code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
| "code.gitea.io/gitea/routers/repo" | |||||
| "gitea.com/macaron/macaron" | |||||
| ) | ) | ||||
| func UpdateAllRepoCommitCnt(ctx *macaron.Context) { | func UpdateAllRepoCommitCnt(ctx *macaron.Context) { | ||||
| @@ -35,3 +37,8 @@ func UpdateAllRepoCommitCnt(ctx *macaron.Context) { | |||||
| "error_msg": "", | "error_msg": "", | ||||
| }) | }) | ||||
| } | } | ||||
| func RepoStatisticManually(ctx *macaron.Context) { | |||||
| date := ctx.Query("date") | |||||
| repo.RepoStatisticDaily(date) | |||||
| } | |||||
| @@ -1,6 +1,7 @@ | |||||
| package repo | package repo | ||||
| import ( | import ( | ||||
| "code.gitea.io/gitea/modules/repository" | |||||
| "time" | "time" | ||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| @@ -8,12 +9,15 @@ import ( | |||||
| ) | ) | ||||
| //auto daily or manually | //auto daily or manually | ||||
| func RepoStatisticDaily() { | |||||
| //delete all yesterday | |||||
| func RepoStatisticAuto() { | |||||
| log.Info("", time.Now()) | log.Info("", time.Now()) | ||||
| yesterday := time.Now().AddDate(0, 0, -1) | |||||
| log.Info("", yesterday) | |||||
| if err := models.DeleteRepoStatDaily(yesterday); err != nil { | |||||
| yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02") | |||||
| RepoStatisticDaily(yesterday) | |||||
| } | |||||
| func RepoStatisticDaily(date string) { | |||||
| log.Info("%s", date) | |||||
| if err := models.DeleteRepoStatDaily(date); err != nil { | |||||
| log.Error("DeleteRepoStatDaily failed: %v", err.Error()) | log.Error("DeleteRepoStatDaily failed: %v", err.Error()) | ||||
| return | return | ||||
| } | } | ||||
| @@ -52,28 +56,38 @@ func RepoStatisticDaily() { | |||||
| continue | continue | ||||
| } | } | ||||
| repoStat := models.RepoStatistic{ | |||||
| RepoID: repo.ID, | |||||
| Date:yesterday, | |||||
| NumWatches:int64(repo.NumWatches), | |||||
| NumStars:int64(repo.NumStars), | |||||
| NumDownloads:repo.CloneCnt, | |||||
| NumComments:0, | |||||
| NumVisits:0, | |||||
| NumClosedIssues:int64(repo.NumClosedIssues), | |||||
| NumVersions:numVersions, | |||||
| NumDevMonths:repoGitStat.DevelopAge, | |||||
| RepoSize:repo.Size, | |||||
| DatasetSize:datasetSize, | |||||
| NumModels:0, | |||||
| NumWikiViews:0, | |||||
| NumCommits:repo.NumCommit, | |||||
| NumIssues:int64(repo.NumIssues), | |||||
| NumPulls:int64(repo.NumPulls), | |||||
| IssueFixedRate:issueFixedRate, | |||||
| NumContributor:repoGitStat.Contributors, | |||||
| NumKeyContributor:repoGitStat.KeyContributors, | |||||
| numComments, err := models.GetCommentCountByRepoID(repo.ID) | |||||
| if err != nil { | |||||
| log.Error("GetCommentCountByRepoID failed: %s", repo.Name) | |||||
| log.Error("failed statistic: %s", repo.Name) | |||||
| continue | |||||
| } | |||||
| log.Info("%s", repo.OwnerName) | |||||
| beginTime, endTime := getStatTime(date) | |||||
| numVisits := repository.AppointProjectView(repo.OwnerName, repo.Name, beginTime, endTime) | |||||
| repoStat := models.RepoStatistic{ | |||||
| RepoID: repo.ID, | |||||
| Date: date, | |||||
| NumWatches: int64(repo.NumWatches), | |||||
| NumStars: int64(repo.NumStars), | |||||
| NumDownloads: repo.CloneCnt, | |||||
| NumComments: numComments, | |||||
| NumVisits: int64(numVisits), | |||||
| NumClosedIssues: int64(repo.NumClosedIssues), | |||||
| NumVersions: numVersions, | |||||
| NumDevMonths: repoGitStat.DevelopAge, | |||||
| RepoSize: repo.Size, | |||||
| DatasetSize: datasetSize, | |||||
| NumModels: 0, | |||||
| NumWikiViews: repoGitStat.WikiPages, | |||||
| NumCommits: repo.NumCommit, | |||||
| NumIssues: int64(repo.NumIssues), | |||||
| NumPulls: int64(repo.NumPulls), | |||||
| IssueFixedRate: issueFixedRate, | |||||
| NumContributor: repoGitStat.Contributors, | |||||
| NumKeyContributor: repoGitStat.KeyContributors, | |||||
| } | } | ||||
| if _, err = models.InsertRepoStat(&repoStat); err != nil { | if _, err = models.InsertRepoStat(&repoStat); err != nil { | ||||
| @@ -95,3 +109,15 @@ func getDatasetSize(repo *models.Repository) (int64, error) { | |||||
| return models.GetAttachmentSizeByDatasetID(dataset.ID) | return models.GetAttachmentSizeByDatasetID(dataset.ID) | ||||
| } | } | ||||
| func getStatTime(timeStr string) (string, string) { | |||||
| t, _ := time.Parse("2006-01-02", timeStr) | |||||
| timeNumber := t.Unix() | |||||
| beginTimeNumber := timeNumber - 8*60*60 | |||||
| endTimeNumber := timeNumber + 16*60*60 | |||||
| beginTime := time.Unix(beginTimeNumber, 0).Format(time.RFC3339) | |||||
| endTime := time.Unix(endTimeNumber, 0).Format(time.RFC3339) | |||||
| log.Info("%s, %s", beginTime, endTime) | |||||
| return beginTime, endTime | |||||
| } | |||||