|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/grafana/github-datasource/pkg/dfutil" |
| 10 | + "github.com/grafana/github-datasource/pkg/models" |
| 11 | + "github.com/grafana/grafana-plugin-sdk-go/data" |
| 12 | +) |
| 13 | + |
| 14 | +// CopilotMetricsResponse represents the response from GitHub's Copilot metrics API |
| 15 | +type CopilotMetricsResponse []models.CopilotMetrics |
| 16 | + |
| 17 | +// GetCopilotMetrics retrieves Copilot metrics for an organization |
| 18 | +func GetCopilotMetrics(ctx context.Context, client models.Client, opts models.ListCopilotMetricsOptions) (dfutil.Framer, error) { |
| 19 | + metrics, _, err := client.GetCopilotMetrics(ctx, opts.Organization, opts) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + return copilotMetricsToDataFrame(CopilotMetricsResponse(metrics), "copilot_metrics") |
| 25 | +} |
| 26 | + |
| 27 | +// GetCopilotMetricsTeam retrieves Copilot metrics for a team |
| 28 | +func GetCopilotMetricsTeam(ctx context.Context, client models.Client, opts models.ListCopilotMetricsTeamOptions) (dfutil.Framer, error) { |
| 29 | + metrics, _, err := client.GetCopilotMetricsTeam(ctx, opts.Organization, opts.TeamSlug, opts) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + return copilotMetricsToDataFrame(CopilotMetricsResponse(metrics), "copilot_metrics_team") |
| 35 | +} |
| 36 | + |
| 37 | +// copilotMetricsToDataFrame converts Copilot metrics to a Grafana data frame |
| 38 | +func copilotMetricsToDataFrame(metrics CopilotMetricsResponse, name string) (dfutil.Framer, error) { |
| 39 | + return metrics, nil |
| 40 | +} |
| 41 | + |
| 42 | +// Frames converts the list of copilot metrics to a Grafana DataFrame |
| 43 | +func (c CopilotMetricsResponse) Frames() data.Frames { |
| 44 | + frame := data.NewFrame("copilot_metrics") |
| 45 | + |
| 46 | + if len(c) == 0 { |
| 47 | + return data.Frames{frame} |
| 48 | + } |
| 49 | + |
| 50 | + // Create time series for the main metrics |
| 51 | + dates := make([]time.Time, len(c)) |
| 52 | + totalActiveUsers := make([]int64, len(c)) |
| 53 | + totalEngagedUsers := make([]int64, len(c)) |
| 54 | + ideCompletionUsers := make([]int64, len(c)) |
| 55 | + ideChatUsers := make([]int64, len(c)) |
| 56 | + dotcomChatUsers := make([]int64, len(c)) |
| 57 | + dotcomPRUsers := make([]int64, len(c)) |
| 58 | + |
| 59 | + for i, metric := range c { |
| 60 | + date, err := time.Parse("2006-01-02", metric.Date) |
| 61 | + if err != nil { |
| 62 | + // If date parsing fails, use a default date |
| 63 | + date = time.Now().AddDate(0, 0, -i) |
| 64 | + } |
| 65 | + |
| 66 | + dates[i] = date |
| 67 | + totalActiveUsers[i] = int64(metric.TotalActiveUsers) |
| 68 | + totalEngagedUsers[i] = int64(metric.TotalEngagedUsers) |
| 69 | + ideCompletionUsers[i] = int64(metric.CopilotIDECodeCompletions.TotalEngagedUsers) |
| 70 | + ideChatUsers[i] = int64(metric.CopilotIDEChat.TotalEngagedUsers) |
| 71 | + dotcomChatUsers[i] = int64(metric.CopilotDotcomChat.TotalEngagedUsers) |
| 72 | + dotcomPRUsers[i] = int64(metric.CopilotDotcomPullRequests.TotalEngagedUsers) |
| 73 | + } |
| 74 | + |
| 75 | + // Add fields to the frame |
| 76 | + frame.Fields = append(frame.Fields, data.NewField("time", nil, dates)) |
| 77 | + frame.Fields = append(frame.Fields, data.NewField("total_active_users", nil, totalActiveUsers)) |
| 78 | + frame.Fields = append(frame.Fields, data.NewField("total_engaged_users", nil, totalEngagedUsers)) |
| 79 | + frame.Fields = append(frame.Fields, data.NewField("ide_completion_users", nil, ideCompletionUsers)) |
| 80 | + frame.Fields = append(frame.Fields, data.NewField("ide_chat_users", nil, ideChatUsers)) |
| 81 | + frame.Fields = append(frame.Fields, data.NewField("dotcom_chat_users", nil, dotcomChatUsers)) |
| 82 | + frame.Fields = append(frame.Fields, data.NewField("dotcom_pr_users", nil, dotcomPRUsers)) |
| 83 | + |
| 84 | + // Add language breakdown data if available |
| 85 | + if len(c) > 0 && len(c[0].CopilotIDECodeCompletions.Languages) > 0 { |
| 86 | + langData := make(map[string][]int64) |
| 87 | + for _, metric := range c { |
| 88 | + for _, lang := range metric.CopilotIDECodeCompletions.Languages { |
| 89 | + if langData[lang.Name] == nil { |
| 90 | + langData[lang.Name] = make([]int64, len(c)) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + for i, metric := range c { |
| 96 | + for langName := range langData { |
| 97 | + found := false |
| 98 | + for _, lang := range metric.CopilotIDECodeCompletions.Languages { |
| 99 | + if lang.Name == langName { |
| 100 | + langData[langName][i] = int64(lang.TotalEngagedUsers) |
| 101 | + found = true |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + if !found { |
| 106 | + langData[langName][i] = 0 |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for langName, users := range langData { |
| 112 | + fieldName := fmt.Sprintf("language_%s_users", langName) |
| 113 | + frame.Fields = append(frame.Fields, data.NewField(fieldName, nil, users)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Add editor breakdown data if available |
| 118 | + if len(c) > 0 && len(c[0].CopilotIDECodeCompletions.Editors) > 0 { |
| 119 | + editorData := make(map[string][]int64) |
| 120 | + for _, metric := range c { |
| 121 | + for _, editor := range metric.CopilotIDECodeCompletions.Editors { |
| 122 | + if editorData[editor.Name] == nil { |
| 123 | + editorData[editor.Name] = make([]int64, len(c)) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + for i, metric := range c { |
| 129 | + for editorName := range editorData { |
| 130 | + found := false |
| 131 | + for _, editor := range metric.CopilotIDECodeCompletions.Editors { |
| 132 | + if editor.Name == editorName { |
| 133 | + editorData[editorName][i] = int64(editor.TotalEngagedUsers) |
| 134 | + found = true |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + if !found { |
| 139 | + editorData[editorName][i] = 0 |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + for editorName, users := range editorData { |
| 145 | + fieldName := fmt.Sprintf("editor_%s_users", editorName) |
| 146 | + frame.Fields = append(frame.Fields, data.NewField(fieldName, nil, users)) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // Add detailed JSON for complex nested data |
| 151 | + detailedData := make([]string, len(c)) |
| 152 | + for i, metric := range c { |
| 153 | + jsonData, err := json.Marshal(metric) |
| 154 | + if err != nil { |
| 155 | + detailedData[i] = "" |
| 156 | + } else { |
| 157 | + detailedData[i] = string(jsonData) |
| 158 | + } |
| 159 | + } |
| 160 | + frame.Fields = append(frame.Fields, data.NewField("detailed_metrics", nil, detailedData)) |
| 161 | + |
| 162 | + return data.Frames{frame} |
| 163 | +} |
0 commit comments