|
- package models
-
- import (
- "fmt"
- "sort"
- "strconv"
- "time"
-
- "code.gitea.io/gitea/modules/git"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/timeutil"
- "xorm.io/builder"
- "xorm.io/xorm"
- )
-
- const (
- PAGE_SIZE = 2000
- BATCH_INSERT_SIZE = 50
- )
-
- type UserBusinessAnalysisAll struct {
- ID int64 `xorm:"pk"`
-
- CountDate int64 `xorm:"pk"`
-
- //action :ActionMergePullRequest // 11
- CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //action :ActionCommitRepo // 5
- CommitCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //action :ActionCreateIssue // 10
- IssueCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //comment table current date
- CommentCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //watch table current date
- FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //star table current date
- StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //follow table
- WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
-
- // user table
- GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
-
- //
- CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
-
- //attachement table
- CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
-
- //0
- CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //issue, issueassignees
- SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //baike
- EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //user
- RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
-
- //repo
- CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //login count, from elk
- LoginCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //openi index
- OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
-
- //user
- Email string `xorm:"NOT NULL"`
-
- //user
- Name string `xorm:"NOT NULL"`
-
- DataDate string `xorm:"NULL"`
- }
-
- type UserBusinessAnalysis struct {
- ID int64 `xorm:"pk"`
-
- CountDate int64 `xorm:"pk"`
-
- //action :ActionMergePullRequest // 11
- CodeMergeCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //action :ActionCommitRepo // 5
- CommitCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //action :ActionCreateIssue // 6
- IssueCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //comment table current date
- CommentCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //watch table current date
- FocusRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //star table current date
- StarRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //follow table
- WatchedCount int `xorm:"NOT NULL DEFAULT 0"`
-
- // user table
- GiteaAgeMonth int `xorm:"NOT NULL DEFAULT 0"`
-
- //
- CommitCodeSize int `xorm:"NOT NULL DEFAULT 0"`
-
- //attachement table
- CommitDatasetSize int `xorm:"NOT NULL DEFAULT 0"`
-
- //0
- CommitModelCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //issue, issueassignees
- SolveIssueCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //baike
- EncyclopediasCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //user
- RegistDate timeutil.TimeStamp `xorm:"NOT NULL"`
-
- //repo
- CreateRepoCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //login count, from elk
- LoginCount int `xorm:"NOT NULL DEFAULT 0"`
-
- //openi index
- OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"`
-
- //user
- Email string `xorm:"NOT NULL"`
-
- //user
- Name string `xorm:"NOT NULL"`
-
- DataDate string `xorm:"NULL"`
- }
-
- type UserBusinessAnalysisQueryOptions struct {
- ListOptions
- UserName string
- SortType string
- StartTime int64
- EndTime int64
- IsAll bool
- }
-
- type UserBusinessAnalysisList []*UserBusinessAnalysis
-
- func (ulist UserBusinessAnalysisList) Swap(i, j int) { ulist[i], ulist[j] = ulist[j], ulist[i] }
- func (ulist UserBusinessAnalysisList) Len() int { return len(ulist) }
- func (ulist UserBusinessAnalysisList) Less(i, j int) bool {
- return ulist[i].ID > ulist[j].ID
- }
-
- func getLastCountDate() int64 {
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
- statictisSess.Limit(1, 0)
- userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
- if err := statictisSess.Table("user_business_analysis").OrderBy("count_date desc").Limit(1, 0).
- Find(&userBusinessAnalysisList); err == nil {
- for _, userRecord := range userBusinessAnalysisList {
- return userRecord.CountDate - 10000
- }
- } else {
- log.Info("query error." + err.Error())
- }
- currentTimeNow := time.Now()
- pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
- return pageStartTime.Unix()
- }
-
- func QueryUserStaticDataByTableName(start int, pageSize int, tableName string, queryObj interface{}, userName string) ([]*UserBusinessAnalysisAll, int64) {
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
- var cond = builder.NewCond()
- if len(userName) > 0 {
- cond = cond.And(
- builder.Like{"name", userName},
- )
- }
- allCount, err := statictisSess.Where(cond).Count(queryObj)
- if err != nil {
- log.Info("query error." + err.Error())
- return nil, 0
- }
- log.Info("query return total:" + fmt.Sprint(allCount))
- userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
- if err := statictisSess.Table(tableName).Where(cond).OrderBy("commit_count desc,id desc").Limit(pageSize, start).
- Find(&userBusinessAnalysisAllList); err != nil {
- return nil, 0
- }
- return userBusinessAnalysisAllList, allCount
- }
-
- func QueryUserStaticDataAll(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysisAll, int64) {
- log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
-
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
-
- allCount, err := statictisSess.Count(new(UserBusinessAnalysisAll))
- if err != nil {
- log.Info("query error." + err.Error())
- return nil, 0
- }
- log.Info("query return total:" + fmt.Sprint(allCount))
-
- pageSize := PAGE_SIZE
- totalPage := int(allCount) / pageSize
- userBusinessAnalysisReturnList := make([]*UserBusinessAnalysisAll, 0)
- for i := 0; i <= int(totalPage); i++ {
- userBusinessAnalysisAllList := make([]*UserBusinessAnalysisAll, 0)
- if err := statictisSess.Table("user_business_analysis_all").OrderBy("id desc").Limit(pageSize, i*pageSize).
- Find(&userBusinessAnalysisAllList); err != nil {
- return nil, 0
- }
- log.Info("query " + fmt.Sprint(i+1) + " result size=" + fmt.Sprint(len(userBusinessAnalysisAllList)))
- for _, userRecord := range userBusinessAnalysisAllList {
- userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, userRecord)
- }
- }
-
- log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
- return userBusinessAnalysisReturnList, allCount
- }
-
- func QueryUserStaticDataPage(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) {
-
- log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll))
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
-
- currentTimeNow := time.Now()
- pageStartTime := getLastCountDate()
- pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).Unix()
-
- var cond = builder.NewCond()
- if len(opts.UserName) > 0 {
- cond = cond.And(
- builder.Like{"name", opts.UserName},
- )
- }
- cond = cond.And(
- builder.Gte{"count_date": pageStartTime},
- )
- cond = cond.And(
- builder.Lte{"count_date": pageEndTime},
- )
-
- count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
- if err != nil {
- log.Info("query error." + err.Error())
- return nil, 0
- }
-
- if opts.Page >= 0 && opts.PageSize > 0 {
- var start int
- if opts.Page == 0 {
- start = 0
- } else {
- start = (opts.Page - 1) * opts.PageSize
- }
- statictisSess.Limit(opts.PageSize, start)
- }
-
- userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
- if err := statictisSess.Table("user_business_analysis").Where(cond).OrderBy("id desc").
- Find(&userBusinessAnalysisList); err != nil {
- return nil, 0
- }
-
- resultMap := make(map[int64]*UserBusinessAnalysis)
-
- if len(userBusinessAnalysisList) > 0 {
- var newAndCond = builder.NewCond()
- var newOrCond = builder.NewCond()
- for _, userRecord := range userBusinessAnalysisList {
- newOrCond = newOrCond.Or(
- builder.Eq{"id": userRecord.ID},
- )
- }
- newAndCond = newAndCond.And(
- newOrCond,
- )
- if !opts.IsAll {
- newAndCond = newAndCond.And(
- builder.Gte{"count_date": opts.StartTime},
- )
- newAndCond = newAndCond.And(
- builder.Lte{"count_date": opts.EndTime},
- )
- }
-
- allCount, err := statictisSess.Where(newAndCond).Count(new(UserBusinessAnalysis))
- if err != nil {
- log.Info("query error." + err.Error())
- return nil, 0
- }
-
- pageSize := 1000
- totalPage := int(allCount) / pageSize
-
- for i := 0; i <= int(totalPage); i++ {
- userBusinessAnalysisList = make([]*UserBusinessAnalysis, 0)
- if err := statictisSess.Table("user_business_analysis").Where(newAndCond).OrderBy("count_date desc").Limit(pageSize, i*pageSize).
- Find(&userBusinessAnalysisList); err != nil {
- return nil, 0
- }
- log.Info("query result size=" + fmt.Sprint(len(userBusinessAnalysisList)))
- for _, userRecord := range userBusinessAnalysisList {
- if _, ok := resultMap[userRecord.ID]; !ok {
- resultMap[userRecord.ID] = userRecord
- } else {
- resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount
- resultMap[userRecord.ID].CommitCount += userRecord.CommitCount
- resultMap[userRecord.ID].IssueCount += userRecord.IssueCount
- resultMap[userRecord.ID].CommentCount += userRecord.CommentCount
- resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount
- resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount
- resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount
- resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize
- resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize
- resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount
- resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount
- resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount
- resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount
- resultMap[userRecord.ID].LoginCount += userRecord.LoginCount
- }
- }
- }
- }
-
- userBusinessAnalysisReturnList := UserBusinessAnalysisList{}
- for _, v := range resultMap {
- userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v)
- }
- sort.Sort(userBusinessAnalysisReturnList)
- log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList)))
- return userBusinessAnalysisReturnList, count
- }
-
- func refreshUserStaticTable(wikiCountMap map[string]int, CommitCodeSizeMap map[string]*git.UserKPIStats, tableName string, pageStartTime time.Time, pageEndTime time.Time) {
- sess := x.NewSession()
- defer sess.Close()
-
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
- log.Info("truncate all data from table: " + tableName)
- statictisSess.Exec("TRUNCATE TABLE " + tableName)
-
- log.Info("pageStartTime:" + pageStartTime.Format("2006-01-02 15:04:05"))
- log.Info("pageEndTime time:" + pageEndTime.Format("2006-01-02 15:04:05"))
-
- start_unix := pageStartTime.Unix()
- end_unix := pageEndTime.Unix()
-
- currentTimeNow := time.Now()
- startTime := currentTimeNow.AddDate(0, 0, -1)
-
- CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
- CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
- IssueCountMap := queryCreateIssue(start_unix, end_unix)
-
- CommentCountMap := queryComment(start_unix, end_unix)
- FocusRepoCountMap := queryWatch(start_unix, end_unix)
- StarRepoCountMap := queryStar(start_unix, end_unix)
- WatchedCountMap := queryFollow(start_unix, end_unix)
-
- CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
- SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
- CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
- LoginCountMap := queryLoginCount(start_unix, end_unix)
-
- OpenIIndexMap := queryUserRepoOpenIIndex(startTime.Unix(), end_unix)
-
- DataDate := currentTimeNow.Format("2006-01-02") + " 00:01"
-
- cond := "type != 1 and is_active=true"
- count, err := sess.Where(cond).Count(new(User))
- if err != nil {
- log.Info("query user error. return.")
- return
- }
- var indexTotal int64
- indexTotal = 0
- insertCount := 0
- dateRecordBatch := make([]UserBusinessAnalysisAll, 0)
- for {
- sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- userList := make([]*User, 0)
- sess.Find(&userList)
-
- for _, userRecord := range userList {
- var dateRecordAll UserBusinessAnalysisAll
- dateRecordAll.ID = userRecord.ID
- dateRecordAll.Email = userRecord.Email
- dateRecordAll.RegistDate = userRecord.CreatedUnix
- dateRecordAll.Name = userRecord.Name
- dateRecordAll.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
- dateRecordAll.DataDate = DataDate
-
- if _, ok := CodeMergeCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.CodeMergeCount = 0
- } else {
- dateRecordAll.CodeMergeCount = CodeMergeCountMap[dateRecordAll.ID]
- }
-
- if _, ok := CommitCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.CommitCount = 0
- } else {
- dateRecordAll.CommitCount = CommitCountMap[dateRecordAll.ID]
- }
-
- if _, ok := IssueCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.IssueCount = 0
- } else {
- dateRecordAll.IssueCount = IssueCountMap[dateRecordAll.ID]
- }
-
- if _, ok := CommentCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.CommentCount = 0
- } else {
- dateRecordAll.CommentCount = CommentCountMap[dateRecordAll.ID]
- }
-
- if _, ok := FocusRepoCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.FocusRepoCount = 0
- } else {
- dateRecordAll.FocusRepoCount = FocusRepoCountMap[dateRecordAll.ID]
- }
-
- if _, ok := StarRepoCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.StarRepoCount = 0
- } else {
- dateRecordAll.StarRepoCount = StarRepoCountMap[dateRecordAll.ID]
- }
-
- if _, ok := WatchedCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.WatchedCount = 0
- } else {
- dateRecordAll.WatchedCount = WatchedCountMap[dateRecordAll.ID]
- }
-
- if _, ok := CommitCodeSizeMap[dateRecordAll.Email]; !ok {
- dateRecordAll.CommitCodeSize = 0
- } else {
- dateRecordAll.CommitCodeSize = int(CommitCodeSizeMap[dateRecordAll.Email].CommitLines)
- }
-
- if _, ok := CommitDatasetSizeMap[dateRecordAll.ID]; !ok {
- dateRecordAll.CommitDatasetSize = 0
- } else {
- dateRecordAll.CommitDatasetSize = CommitDatasetSizeMap[dateRecordAll.ID]
- }
-
- if _, ok := SolveIssueCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.SolveIssueCount = 0
- } else {
- dateRecordAll.SolveIssueCount = SolveIssueCountMap[dateRecordAll.ID]
- }
-
- if _, ok := wikiCountMap[dateRecordAll.Name]; !ok {
- dateRecordAll.EncyclopediasCount = 0
- } else {
- dateRecordAll.EncyclopediasCount = wikiCountMap[dateRecordAll.Name]
- }
-
- if _, ok := CreateRepoCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.CreateRepoCount = 0
- } else {
- dateRecordAll.CreateRepoCount = CreateRepoCountMap[dateRecordAll.ID]
- }
-
- if _, ok := LoginCountMap[dateRecordAll.ID]; !ok {
- dateRecordAll.LoginCount = 0
- } else {
- dateRecordAll.LoginCount = LoginCountMap[dateRecordAll.ID]
- }
-
- if _, ok := OpenIIndexMap[dateRecordAll.ID]; !ok {
- dateRecordAll.OpenIIndex = 0
- } else {
- dateRecordAll.OpenIIndex = OpenIIndexMap[dateRecordAll.ID]
- }
-
- dateRecordAll.CommitModelCount = 0
-
- dateRecordBatch = append(dateRecordBatch, dateRecordAll)
- if len(dateRecordBatch) >= BATCH_INSERT_SIZE {
- insertTable(dateRecordBatch, tableName, statictisSess)
- insertCount += BATCH_INSERT_SIZE
- if err != nil {
- log.Info("insert all data failed." + err.Error())
- }
- dateRecordBatch = make([]UserBusinessAnalysisAll, 0)
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- if len(dateRecordBatch) > 0 {
- insertTable(dateRecordBatch, tableName, statictisSess)
- insertCount += len(dateRecordBatch)
- if err != nil {
- log.Info("insert all data failed." + err.Error())
- }
- }
-
- log.Info("refresh data finished.tableName=" + tableName + " total record:" + fmt.Sprint(insertCount))
- }
-
- func insertTable(dateRecords []UserBusinessAnalysisAll, tableName string, statictisSess *xorm.Session) {
-
- insertBatchSql := "INSERT INTO public." + tableName +
- "(id, count_date, code_merge_count, commit_count, issue_count, comment_count, focus_repo_count, star_repo_count, watched_count, gitea_age_month, commit_code_size, commit_dataset_size, " +
- "commit_model_count, solve_issue_count, encyclopedias_count, regist_date, create_repo_count, login_count, open_i_index, email, name, data_date) " +
- "VALUES"
-
- for i, record := range dateRecords {
- insertBatchSql += "(" + fmt.Sprint(record.ID) + ", " + fmt.Sprint(record.CountDate) + ", " + fmt.Sprint(record.CodeMergeCount) + ", " + fmt.Sprint(record.CommitCount) +
- ", " + fmt.Sprint(record.IssueCount) + ", " + fmt.Sprint(record.CommentCount) + ", " + fmt.Sprint(record.FocusRepoCount) + ", " + fmt.Sprint(record.StarRepoCount) +
- ", " + fmt.Sprint(record.WatchedCount) + ", " + fmt.Sprint(record.GiteaAgeMonth) + ", " + fmt.Sprint(record.CommitCodeSize) + ", " + fmt.Sprint(record.CommitDatasetSize) +
- ", " + fmt.Sprint(record.CommitModelCount) + ", " + fmt.Sprint(record.SolveIssueCount) + ", " + fmt.Sprint(record.EncyclopediasCount) + ", " + fmt.Sprint(record.RegistDate) +
- ", " + fmt.Sprint(record.CreateRepoCount) + ", " + fmt.Sprint(record.LoginCount) + ", " + fmt.Sprint(record.OpenIIndex) + ", '" + record.Email + "', '" + record.Name + "', '" + record.DataDate + "')"
- if i < (len(dateRecords) - 1) {
- insertBatchSql += ","
- }
- }
-
- statictisSess.Exec(insertBatchSql)
- }
-
- func RefreshUserStaticAllTabel(wikiCountMap map[string]int, CommitCodeSizeMap map[string]*git.UserKPIStats) {
- currentTimeNow := time.Now()
- pageStartTime := time.Date(2021, 11, 5, 0, 0, 0, 0, currentTimeNow.Location())
- pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_all", pageStartTime, pageEndTime)
- log.Info("refresh all data finished.")
-
- pageStartTime = time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location())
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_current_year", pageStartTime, pageEndTime)
-
- thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_current_month", thisMonth, pageEndTime)
-
- offset := int(time.Monday - currentTimeNow.Weekday())
- if offset > 0 {
- offset = -6
- }
- pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_current_week", pageStartTime, pageEndTime)
-
- pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30)
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_last30_day", pageStartTime, pageEndTime)
-
- pageStartTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
- pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_yesterday", pageStartTime, pageEndTime)
-
- pageStartTime = thisMonth.AddDate(0, -1, 0)
- pageEndTime = time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
- refreshUserStaticTable(wikiCountMap, CommitCodeSizeMap, "user_business_analysis_last_month", pageStartTime, pageEndTime)
-
- }
-
- func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, endTime time.Time, isReCount bool) error {
-
- log.Info("start to count other user info data")
- sess := x.NewSession()
- defer sess.Close()
-
- currentTimeNow := time.Now()
- log.Info("current time:" + currentTimeNow.Format("2006-01-02 15:04:05"))
-
- start_unix := startTime.Unix()
- log.Info("DB query time:" + startTime.Format("2006-01-02 15:04:05"))
-
- end_unix := endTime.Unix()
- CountDate := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 1, 0, 0, currentTimeNow.Location())
- if isReCount {
- CountDate = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 1, 0, 0, currentTimeNow.Location())
- }
-
- DataDate := startTime.Format("2006-01-02")
- CodeMergeCountMap := queryPullRequest(start_unix, end_unix)
- CommitCountMap := queryCommitAction(start_unix, end_unix, 5)
- IssueCountMap := queryCreateIssue(start_unix, end_unix)
-
- CommentCountMap := queryComment(start_unix, end_unix)
- FocusRepoCountMap := queryWatch(start_unix, end_unix)
- StarRepoCountMap := queryStar(start_unix, end_unix)
- WatchedCountMap := queryFollow(start_unix, end_unix)
-
- CommitCodeSizeMap, err := GetAllUserKPIStats()
- if err != nil {
- log.Info("query commit code errr.")
- } else {
- log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
- }
- CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix)
- SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
- CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix)
- LoginCountMap := queryLoginCount(start_unix, end_unix)
- OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix)
-
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
-
- cond := "type != 1 and is_active=true"
- count, err := sess.Where(cond).Count(new(User))
- if err != nil {
- log.Info("query user error. return.")
- return err
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- userList := make([]*User, 0)
- sess.Find(&userList)
-
- for i, userRecord := range userList {
- var dateRecord UserBusinessAnalysis
- dateRecord.ID = userRecord.ID
- log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
- dateRecord.CountDate = CountDate.Unix()
-
- statictisSess.Delete(&dateRecord)
-
- dateRecord.Email = userRecord.Email
- dateRecord.RegistDate = userRecord.CreatedUnix
- dateRecord.Name = userRecord.Name
- dateRecord.GiteaAgeMonth = subMonth(currentTimeNow, userRecord.CreatedUnix.AsTime())
- dateRecord.DataDate = DataDate
- if _, ok := CodeMergeCountMap[dateRecord.ID]; !ok {
- dateRecord.CodeMergeCount = 0
- } else {
- dateRecord.CodeMergeCount = CodeMergeCountMap[dateRecord.ID]
- }
-
- if _, ok := CommitCountMap[dateRecord.ID]; !ok {
- dateRecord.CommitCount = 0
- } else {
- dateRecord.CommitCount = CommitCountMap[dateRecord.ID]
- }
-
- if _, ok := IssueCountMap[dateRecord.ID]; !ok {
- dateRecord.IssueCount = 0
- } else {
- dateRecord.IssueCount = IssueCountMap[dateRecord.ID]
- }
-
- if _, ok := CommentCountMap[dateRecord.ID]; !ok {
- dateRecord.CommentCount = 0
- } else {
- dateRecord.CommentCount = CommentCountMap[dateRecord.ID]
- }
-
- if _, ok := FocusRepoCountMap[dateRecord.ID]; !ok {
- dateRecord.FocusRepoCount = 0
- } else {
- dateRecord.FocusRepoCount = FocusRepoCountMap[dateRecord.ID]
- }
-
- if _, ok := StarRepoCountMap[dateRecord.ID]; !ok {
- dateRecord.StarRepoCount = 0
- } else {
- dateRecord.StarRepoCount = StarRepoCountMap[dateRecord.ID]
- }
-
- if _, ok := WatchedCountMap[dateRecord.ID]; !ok {
- dateRecord.WatchedCount = 0
- } else {
- dateRecord.WatchedCount = WatchedCountMap[dateRecord.ID]
- }
-
- if _, ok := CommitCodeSizeMap[dateRecord.Email]; !ok {
- dateRecord.CommitCodeSize = 0
- } else {
- dateRecord.CommitCodeSize = int(CommitCodeSizeMap[dateRecord.Email].CommitLines)
- }
-
- if _, ok := CommitDatasetSizeMap[dateRecord.ID]; !ok {
- dateRecord.CommitDatasetSize = 0
- } else {
- dateRecord.CommitDatasetSize = CommitDatasetSizeMap[dateRecord.ID]
- }
-
- if _, ok := SolveIssueCountMap[dateRecord.ID]; !ok {
- dateRecord.SolveIssueCount = 0
- } else {
- dateRecord.SolveIssueCount = SolveIssueCountMap[dateRecord.ID]
- }
-
- if _, ok := wikiCountMap[dateRecord.Name]; !ok {
- dateRecord.EncyclopediasCount = 0
- } else {
- dateRecord.EncyclopediasCount = wikiCountMap[dateRecord.Name]
- }
-
- if _, ok := CreateRepoCountMap[dateRecord.ID]; !ok {
- dateRecord.CreateRepoCount = 0
- } else {
- dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID]
- }
-
- if _, ok := LoginCountMap[dateRecord.ID]; !ok {
- dateRecord.LoginCount = 0
- } else {
- dateRecord.LoginCount = LoginCountMap[dateRecord.ID]
- }
-
- if _, ok := OpenIIndexMap[dateRecord.ID]; !ok {
- dateRecord.OpenIIndex = 0
- } else {
- dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID]
- }
-
- dateRecord.CommitModelCount = 0
-
- _, err = statictisSess.Insert(&dateRecord)
- if err != nil {
- log.Info("insert daterecord failed." + err.Error())
- return err
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- RefreshUserStaticAllTabel(wikiCountMap, CommitCodeSizeMap)
-
- return nil
- }
-
- func getInt(str string) int {
- re, err := strconv.ParseInt(str, 10, 32)
- if err != nil {
- return 0
- }
- return int(re)
- }
-
- func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime time.Time) {
- CounDataByDateAndReCount(wikiCountMap, startTime, endTime, false)
- }
-
- func querySolveIssue(start_unix int64, end_unix int64) map[int64]int {
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
- cond := "issue.is_closed=true and issue.closed_unix>=" + fmt.Sprint(start_unix) + " and issue.closed_unix<=" + fmt.Sprint(end_unix)
-
- count, err := sess.Table("issue_assignees").Join("inner", "issue", "issue.id=issue_assignees.issue_id").Where(cond).Count(new(IssueAssignees))
- if err != nil {
- log.Info("query issue error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- issueAssigneesList := make([]*IssueAssignees, 0)
- sess.Select("issue_assignees.*").Table("issue_assignees").
- Join("inner", "issue", "issue.id=issue_assignees.issue_id").
- Where(cond).OrderBy("issue_assignees.id asc").Limit(PAGE_SIZE, int(indexTotal))
-
- sess.Find(&issueAssigneesList)
-
- log.Info("query IssueAssignees size=" + fmt.Sprint(len(issueAssigneesList)))
- for _, issueAssigneesRecord := range issueAssigneesList {
- if _, ok := resultMap[issueAssigneesRecord.AssigneeID]; !ok {
- resultMap[issueAssigneesRecord.AssigneeID] = 1
- } else {
- resultMap[issueAssigneesRecord.AssigneeID] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
- }
-
- func queryPullRequest(start_unix int64, end_unix int64) map[int64]int {
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
- cond := "pull_request.merged_unix>=" + fmt.Sprint(start_unix) + " and pull_request.merged_unix<=" + fmt.Sprint(end_unix)
- count, err := sess.Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Count(new(Issue))
- if err != nil {
- log.Info("query issue error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- issueList := make([]*Issue, 0)
- sess.Select("issue.*").Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).OrderBy("issue.id asc").Limit(PAGE_SIZE, int(indexTotal))
- sess.Find(&issueList)
- log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList)))
- for _, issueRecord := range issueList {
- if _, ok := resultMap[issueRecord.PosterID]; !ok {
- resultMap[issueRecord.PosterID] = 1
- } else {
- resultMap[issueRecord.PosterID] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- return resultMap
- }
-
- func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
-
- cond := "user_id=act_user_id and op_type=" + fmt.Sprint(actionType) + " and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
-
- count, err := sess.Where(cond).Count(new(Action))
- if err != nil {
- log.Info("query action error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- actionList := make([]*Action, 0)
- sess.Find(&actionList)
-
- log.Info("query action size=" + fmt.Sprint(len(actionList)))
- for _, actionRecord := range actionList {
- if _, ok := resultMap[actionRecord.UserID]; !ok {
- resultMap[actionRecord.UserID] = 1
- } else {
- resultMap[actionRecord.UserID] += 1
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
- }
-
- func queryCreateIssue(start_unix int64, end_unix int64) map[int64]int {
-
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
- cond := "is_pull=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
-
- count, err := sess.Where(cond).Count(new(Issue))
- if err != nil {
- log.Info("query Issue error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,poster_id").Table("issue").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- issueList := make([]*Issue, 0)
- sess.Find(&issueList)
- log.Info("query issue size=" + fmt.Sprint(len(issueList)))
- for _, issueRecord := range issueList {
- if _, ok := resultMap[issueRecord.PosterID]; !ok {
- resultMap[issueRecord.PosterID] = 1
- } else {
- resultMap[issueRecord.PosterID] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- return resultMap
-
- }
-
- func queryComment(start_unix int64, end_unix int64) map[int64]int {
-
- sess := x.NewSession()
- defer sess.Close()
- cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
- resultMap := make(map[int64]int)
- count, err := sess.Where(cond).Count(new(Comment))
- if err != nil {
- log.Info("query Comment error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,type,poster_id").Table("comment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- commentList := make([]*Comment, 0)
- sess.Find(&commentList)
- log.Info("query Comment size=" + fmt.Sprint(len(commentList)))
- for _, commentRecord := range commentList {
- if _, ok := resultMap[commentRecord.PosterID]; !ok {
- resultMap[commentRecord.PosterID] = 1
- } else {
- resultMap[commentRecord.PosterID] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- return resultMap
- }
-
- func queryWatch(start_unix int64, end_unix int64) map[int64]int {
-
- sess := x.NewSession()
- defer sess.Close()
-
- cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
-
- resultMap := make(map[int64]int)
- count, err := sess.Where(cond).Count(new(Watch))
- if err != nil {
- log.Info("query issue error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- watchList := make([]*Watch, 0)
- sess.Select("id,user_id,repo_id").Table("watch").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- sess.Find(&watchList)
-
- log.Info("query Watch size=" + fmt.Sprint(len(watchList)))
- for _, watchRecord := range watchList {
- if _, ok := resultMap[watchRecord.UserID]; !ok {
- resultMap[watchRecord.UserID] = 1
- } else {
- resultMap[watchRecord.UserID] += 1
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
-
- }
-
- func queryStar(start_unix int64, end_unix int64) map[int64]int {
-
- sess := x.NewSession()
- defer sess.Close()
-
- cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
- resultMap := make(map[int64]int)
-
- count, err := sess.Where(cond).Count(new(Star))
- if err != nil {
- log.Info("query star error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,uid,repo_id").Table("star").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- starList := make([]*Star, 0)
- sess.Find(&starList)
-
- log.Info("query Star size=" + fmt.Sprint(len(starList)))
- for _, starRecord := range starList {
- if _, ok := resultMap[starRecord.UID]; !ok {
- resultMap[starRecord.UID] = 1
- } else {
- resultMap[starRecord.UID] += 1
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- return resultMap
- }
-
- func queryFollow(start_unix int64, end_unix int64) map[int64]int {
-
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
- cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
-
- count, err := sess.Where(cond).Count(new(Follow))
- if err != nil {
- log.Info("query follow error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,user_id,follow_id").Table("follow").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- followList := make([]*Follow, 0)
- sess.Find(&followList)
-
- log.Info("query Follow size=" + fmt.Sprint(len(followList)))
- for _, followRecord := range followList {
- if _, ok := resultMap[followRecord.FollowID]; !ok {
- resultMap[followRecord.FollowID] = 1
- } else {
- resultMap[followRecord.FollowID] += 1
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
- }
-
- func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int {
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
- cond := " created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
-
- count, err := sess.Where(cond).Count(new(Attachment))
- if err != nil {
- log.Info("query attachment error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,uploader_id,size").Table("attachment").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- attachmentList := make([]*Attachment, 0)
- sess.Find(&attachmentList)
-
- log.Info("query Attachment size=" + fmt.Sprint(len(attachmentList)))
- for _, attachRecord := range attachmentList {
- if _, ok := resultMap[attachRecord.UploaderID]; !ok {
- resultMap[attachRecord.UploaderID] = int(attachRecord.Size / (1024 * 1024)) //MB
- } else {
- resultMap[attachRecord.UploaderID] += int(attachRecord.Size / (1024 * 1024)) //MB
- }
- }
-
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
- }
-
- func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int {
- sess := x.NewSession()
- defer sess.Close()
- resultMap := make(map[int64]int)
-
- cond := "is_fork=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
- count, err := sess.Where(cond).Count(new(Repository))
- if err != nil {
- log.Info("query Repository error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- sess.Select("id,owner_id,name").Table("repository").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- repoList := make([]*Repository, 0)
- sess.Find(&repoList)
- log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
- for _, repoRecord := range repoList {
- if _, ok := resultMap[repoRecord.OwnerID]; !ok {
- resultMap[repoRecord.OwnerID] = 1
- } else {
- resultMap[repoRecord.OwnerID] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
-
- return resultMap
- }
-
- func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 {
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
-
- statictisSess.Select("id,repo_id,radar_total").Table("repo_statistic").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)).OrderBy("id desc")
- repoStatisticList := make([]*RepoStatistic, 0)
- statictisSess.Find(&repoStatisticList)
- repoOpenIIndexMap := make(map[int64]float64)
- log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList)))
- for _, repoRecord := range repoStatisticList {
- if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok {
- repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal
- }
- }
-
- sess := x.NewSession()
- defer sess.Close()
- sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false")
- repoList := make([]*Repository, 0)
- sess.Find(&repoList)
-
- userMap := make(map[int64]float64)
-
- log.Info("query Repository size=" + fmt.Sprint(len(repoList)))
- for _, repoRecord := range repoList {
- if _, ok := userMap[repoRecord.OwnerID]; !ok {
- if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok {
- userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID]
- }
- }
- }
-
- //query collaboration
- sess.Select("repo_id,user_id,mode").Table("collaboration")
- collaborationList := make([]*Collaboration, 0)
- sess.Find(&collaborationList)
-
- log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList)))
-
- for _, collaborationRecord := range collaborationList {
- if _, ok := userMap[collaborationRecord.UserID]; !ok {
- if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
- userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID]
- }
- } else {
- if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok {
- userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID]
- }
- }
- }
-
- log.Info("user openi index size=" + fmt.Sprint(len(userMap)))
-
- return userMap
- }
-
- func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
- statictisSess := xStatistic.NewSession()
- defer statictisSess.Close()
-
- resultMap := make(map[int64]int)
- cond := "created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)
- count, err := statictisSess.Where(cond).Count(new(UserLoginLog))
- if err != nil {
- log.Info("query UserLoginLog error. return.")
- return resultMap
- }
- var indexTotal int64
- indexTotal = 0
- for {
- statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
- userLoginLogList := make([]*UserLoginLog, 0)
- statictisSess.Find(&userLoginLogList)
- log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList)))
- for _, loginRecord := range userLoginLogList {
- if _, ok := resultMap[loginRecord.UId]; !ok {
- resultMap[loginRecord.UId] = 1
- } else {
- resultMap[loginRecord.UId] += 1
- }
- }
- indexTotal += PAGE_SIZE
- if indexTotal >= count {
- break
- }
- }
- log.Info("user login size=" + fmt.Sprint(len(resultMap)))
- return resultMap
- }
-
- func subMonth(t1, t2 time.Time) (month int) {
- y1 := t1.Year()
- y2 := t2.Year()
- m1 := int(t1.Month())
- m2 := int(t2.Month())
- d1 := t1.Day()
- d2 := t2.Day()
-
- yearInterval := y1 - y2
- // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
- if m1 < m2 || m1 == m2 && d1 < d2 {
- yearInterval--
- }
- // 获取月数差值
- monthInterval := (m1 + 12) - m2
- if d1 < d2 {
- monthInterval--
- }
- monthInterval %= 12
- month = yearInterval*12 + monthInterval
- if month == 0 {
- month = 1
- }
- return month
- }
|