|
| 1 | +/* |
| 2 | +Copyright 2025 The KodeRover Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package service |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "sort" |
| 22 | + |
| 23 | + "go.uber.org/zap" |
| 24 | + |
| 25 | + "github.com/koderover/zadig/v2/pkg/microservice/aslan/config" |
| 26 | + commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb" |
| 27 | + "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/stat/repository/mongodb" |
| 28 | + e "github.com/koderover/zadig/v2/pkg/tool/errors" |
| 29 | +) |
| 30 | + |
| 31 | +func GetTestCount(projects []string, logger *zap.SugaredLogger) (int, error) { |
| 32 | + // if projects is empty, get all testings |
| 33 | + if len(projects) == 0 { |
| 34 | + testings, err := commonrepo.NewTestingColl().List(&commonrepo.ListTestOption{}) |
| 35 | + if err != nil { |
| 36 | + logger.Errorf("TestingModule.List error: %v", err) |
| 37 | + return 0, e.ErrListTestModule.AddDesc(err.Error()) |
| 38 | + } |
| 39 | + return len(testings), nil |
| 40 | + } |
| 41 | + |
| 42 | + // otherwise, get testings for the specified projects |
| 43 | + testings, err := commonrepo.NewTestingColl().List(&commonrepo.ListTestOption{ProductNames: projects}) |
| 44 | + if err != nil { |
| 45 | + logger.Errorf("TestingModule.List error: %v", err) |
| 46 | + return 0, e.ErrListTestModule.AddDesc(err.Error()) |
| 47 | + } |
| 48 | + return len(testings), nil |
| 49 | +} |
| 50 | + |
| 51 | +type DailyTestHealthStat struct { |
| 52 | + Date string `json:"date"` |
| 53 | + TotalSuccess int `json:"totalSuccess"` |
| 54 | + TotalFailure int `json:"totalFailure"` |
| 55 | + SuccessRate float64 `json:"successRate"` |
| 56 | +} |
| 57 | + |
| 58 | +func GetDailyTestHealthTrend(startDate, endDate int64, projects []string, logger *zap.SugaredLogger) ([]*DailyTestHealthStat, error) { |
| 59 | + // Get test stats from database |
| 60 | + testStats, err := mongodb.NewTestStatColl().ListTestStat(&mongodb.TestStatOption{ |
| 61 | + StartDate: startDate, |
| 62 | + EndDate: endDate, |
| 63 | + IsAsc: true, |
| 64 | + ProductNames: projects, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + logger.Errorf("ListTestStat error: %v", err) |
| 68 | + return nil, fmt.Errorf("ListTestStat error: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Group by date |
| 72 | + dailyStatMap := make(map[string]*DailyTestHealthStat) |
| 73 | + for _, testStat := range testStats { |
| 74 | + dateKey := testStat.Date |
| 75 | + |
| 76 | + if _, exists := dailyStatMap[dateKey]; !exists { |
| 77 | + dailyStatMap[dateKey] = &DailyTestHealthStat{ |
| 78 | + Date: dateKey, |
| 79 | + TotalSuccess: 0, |
| 80 | + TotalFailure: 0, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + dailyStatMap[dateKey].TotalSuccess += testStat.TotalSuccess |
| 85 | + dailyStatMap[dateKey].TotalFailure += testStat.TotalFailure |
| 86 | + } |
| 87 | + |
| 88 | + // Convert map to sorted array |
| 89 | + dateKeys := make([]string, 0, len(dailyStatMap)) |
| 90 | + for dateKey := range dailyStatMap { |
| 91 | + dateKeys = append(dateKeys, dateKey) |
| 92 | + } |
| 93 | + sort.Strings(dateKeys) |
| 94 | + |
| 95 | + result := make([]*DailyTestHealthStat, 0, len(dateKeys)) |
| 96 | + for _, dateKey := range dateKeys { |
| 97 | + stat := dailyStatMap[dateKey] |
| 98 | + totalTests := stat.TotalSuccess + stat.TotalFailure |
| 99 | + if totalTests > 0 { |
| 100 | + stat.SuccessRate = float64(stat.TotalSuccess) / float64(totalTests) * 100 |
| 101 | + } |
| 102 | + result = append(result, stat) |
| 103 | + } |
| 104 | + |
| 105 | + return result, nil |
| 106 | +} |
| 107 | + |
| 108 | +type RecentTestTask struct { |
| 109 | + TaskID int64 `json:"task_id"` |
| 110 | + TaskCreator string `json:"task_creator"` |
| 111 | + ProductName string `json:"product_name"` |
| 112 | + TestName string `json:"test_name"` |
| 113 | + WorkflowName string `json:"workflow_name"` |
| 114 | + Status config.Status `json:"status"` |
| 115 | + CreateTime int64 `json:"create_time"` |
| 116 | + StartTime int64 `json:"start_time"` |
| 117 | + EndTime int64 `json:"end_time"` |
| 118 | +} |
| 119 | + |
| 120 | +func GetRecentTestTask(projects []string, number int, logger *zap.SugaredLogger) ([]*RecentTestTask, error) { |
| 121 | + // Default to 10 if number is not specified or invalid |
| 122 | + if number <= 0 { |
| 123 | + number = 10 |
| 124 | + } |
| 125 | + |
| 126 | + // Query workflow tasks filtered by testing type and projects at database level |
| 127 | + option := &commonrepo.ListWorkflowTaskV4Option{ |
| 128 | + Type: config.WorkflowTaskTypeTesting, // Filter by testing type in database |
| 129 | + Limit: number, |
| 130 | + IsSort: true, |
| 131 | + } |
| 132 | + |
| 133 | + // If projects are specified, filter by them |
| 134 | + if len(projects) > 0 { |
| 135 | + option.ProjectNames = projects |
| 136 | + } |
| 137 | + |
| 138 | + workflowTasks, _, err := commonrepo.NewworkflowTaskv4Coll().List(option) |
| 139 | + if err != nil { |
| 140 | + logger.Errorf("failed to list workflow tasks, error: %s", err) |
| 141 | + return nil, fmt.Errorf("failed to list workflow tasks, error: %s", err) |
| 142 | + } |
| 143 | + |
| 144 | + // Build response |
| 145 | + recentTasks := make([]*RecentTestTask, 0, len(workflowTasks)) |
| 146 | + for _, task := range workflowTasks { |
| 147 | + // Extract test name from workflow display name |
| 148 | + testName := task.WorkflowDisplayName |
| 149 | + if testName == "" && task.WorkflowArgs != nil { |
| 150 | + testName = task.WorkflowArgs.DisplayName |
| 151 | + } |
| 152 | + if testName == "" { |
| 153 | + testName = task.WorkflowName |
| 154 | + } |
| 155 | + |
| 156 | + recentTasks = append(recentTasks, &RecentTestTask{ |
| 157 | + TaskID: task.TaskID, |
| 158 | + TaskCreator: task.TaskCreator, |
| 159 | + ProductName: task.ProjectName, |
| 160 | + TestName: testName, |
| 161 | + WorkflowName: task.WorkflowName, |
| 162 | + Status: task.Status, |
| 163 | + CreateTime: task.CreateTime, |
| 164 | + StartTime: task.StartTime, |
| 165 | + EndTime: task.EndTime, |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + return recentTasks, nil |
| 170 | +} |
0 commit comments