- 
                Notifications
    You must be signed in to change notification settings 
- Fork 66
Adding copilot metrics #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            austenstone
  wants to merge
  8
  commits into
  grafana:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
austenstone:copilot
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b5c7797
              
                Adding copilot metrics
              
              
                austenstone a707c74
              
                refactor: unify Copilot metrics handling for organizations and teams
              
              
                austenstone f7f9378
              
                fix: correct spelling in cspell configuration and add "neovim" to wor…
              
              
                austenstone d156b2a
              
                lock files
              
              
                austenstone c73c084
              
                cleanup
              
              
                austenstone 7519cfd
              
                fix ui
              
              
                austenstone b6f499d
              
                fix ui
              
              
                austenstone 9b99859
              
                Merge branch 'copilot' of github.com:austenstone/github-datasource in…
              
              
                austenstone File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package github | ||
|  | ||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|  | ||
| "github.com/grafana/github-datasource/pkg/dfutil" | ||
| "github.com/grafana/github-datasource/pkg/models" | ||
| "github.com/grafana/grafana-plugin-sdk-go/data" | ||
| ) | ||
|  | ||
| // CopilotMetricsResponse represents the response from GitHub's Copilot metrics API | ||
| type CopilotMetricsResponse []models.CopilotMetrics | ||
|  | ||
| // GetCopilotMetrics retrieves Copilot metrics for an organization or team | ||
| func GetCopilotMetrics(ctx context.Context, client models.Client, opts models.ListCopilotMetricsOptions) (dfutil.Framer, error) { | ||
| metrics, _, err := client.GetCopilotMetrics(ctx, opts.Organization, opts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| frameName := "copilot_metrics" | ||
| if opts.TeamSlug != "" { | ||
| frameName = "copilot_metrics_team" | ||
| } | ||
|  | ||
| return copilotMetricsToDataFrame(CopilotMetricsResponse(metrics), frameName) | ||
| } | ||
|  | ||
| // copilotMetricsToDataFrame converts Copilot metrics to a Grafana data frame | ||
| func copilotMetricsToDataFrame(metrics CopilotMetricsResponse, name string) (dfutil.Framer, error) { | ||
| return metrics, nil | ||
| } | ||
|  | ||
| // Frames converts the list of copilot metrics to a Grafana DataFrame | ||
| func (c CopilotMetricsResponse) Frames() data.Frames { | ||
| frame := data.NewFrame("copilot_metrics") | ||
|  | ||
| if len(c) == 0 { | ||
| return data.Frames{frame} | ||
| } | ||
|  | ||
| // Create time series for the main metrics | ||
| dates := make([]time.Time, len(c)) | ||
| totalActiveUsers := make([]int64, len(c)) | ||
| totalEngagedUsers := make([]int64, len(c)) | ||
| ideCompletionUsers := make([]int64, len(c)) | ||
| ideChatUsers := make([]int64, len(c)) | ||
| dotcomChatUsers := make([]int64, len(c)) | ||
| dotcomPRUsers := make([]int64, len(c)) | ||
|  | ||
| for i, metric := range c { | ||
| date, err := time.Parse("2006-01-02", metric.Date) | ||
| if err != nil { | ||
| // If date parsing fails, use a default date | ||
| date = time.Now().AddDate(0, 0, -i) | ||
| } | ||
|  | ||
| dates[i] = date | ||
| totalActiveUsers[i] = int64(metric.TotalActiveUsers) | ||
| totalEngagedUsers[i] = int64(metric.TotalEngagedUsers) | ||
| ideCompletionUsers[i] = int64(metric.CopilotIDECodeCompletions.TotalEngagedUsers) | ||
| ideChatUsers[i] = int64(metric.CopilotIDEChat.TotalEngagedUsers) | ||
| dotcomChatUsers[i] = int64(metric.CopilotDotcomChat.TotalEngagedUsers) | ||
| dotcomPRUsers[i] = int64(metric.CopilotDotcomPullRequests.TotalEngagedUsers) | ||
| } | ||
|  | ||
| // Add fields to the frame | ||
| frame.Fields = append(frame.Fields, data.NewField("time", nil, dates)) | ||
| frame.Fields = append(frame.Fields, data.NewField("total_active_users", nil, totalActiveUsers)) | ||
| frame.Fields = append(frame.Fields, data.NewField("total_engaged_users", nil, totalEngagedUsers)) | ||
| frame.Fields = append(frame.Fields, data.NewField("ide_completion_users", nil, ideCompletionUsers)) | ||
| frame.Fields = append(frame.Fields, data.NewField("ide_chat_users", nil, ideChatUsers)) | ||
| frame.Fields = append(frame.Fields, data.NewField("dotcom_chat_users", nil, dotcomChatUsers)) | ||
| frame.Fields = append(frame.Fields, data.NewField("dotcom_pr_users", nil, dotcomPRUsers)) | ||
|  | ||
| // Add language breakdown data if available | ||
| if len(c) > 0 && len(c[0].CopilotIDECodeCompletions.Languages) > 0 { | ||
| langData := make(map[string][]int64) | ||
| for _, metric := range c { | ||
| for _, lang := range metric.CopilotIDECodeCompletions.Languages { | ||
| if langData[lang.Name] == nil { | ||
| langData[lang.Name] = make([]int64, len(c)) | ||
| } | ||
| } | ||
| } | ||
|  | ||
| for i, metric := range c { | ||
| for langName := range langData { | ||
| found := false | ||
| for _, lang := range metric.CopilotIDECodeCompletions.Languages { | ||
| if lang.Name == langName { | ||
| langData[langName][i] = int64(lang.TotalEngagedUsers) | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| langData[langName][i] = 0 | ||
| } | ||
| } | ||
| } | ||
|  | ||
| for langName, users := range langData { | ||
| fieldName := fmt.Sprintf("language_%s_users", langName) | ||
| frame.Fields = append(frame.Fields, data.NewField(fieldName, nil, users)) | ||
| } | ||
| } | ||
|  | ||
| // Add editor breakdown data if available | ||
| if len(c) > 0 && len(c[0].CopilotIDECodeCompletions.Editors) > 0 { | ||
| editorData := make(map[string][]int64) | ||
| for _, metric := range c { | ||
| for _, editor := range metric.CopilotIDECodeCompletions.Editors { | ||
| if editorData[editor.Name] == nil { | ||
| editorData[editor.Name] = make([]int64, len(c)) | ||
| } | ||
| } | ||
| } | ||
|  | ||
| for i, metric := range c { | ||
| for editorName := range editorData { | ||
| found := false | ||
| for _, editor := range metric.CopilotIDECodeCompletions.Editors { | ||
| if editor.Name == editorName { | ||
| editorData[editorName][i] = int64(editor.TotalEngagedUsers) | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| editorData[editorName][i] = 0 | ||
| } | ||
| } | ||
| } | ||
|  | ||
| for editorName, users := range editorData { | ||
| fieldName := fmt.Sprintf("editor_%s_users", editorName) | ||
| frame.Fields = append(frame.Fields, data.NewField(fieldName, nil, users)) | ||
| } | ||
| } | ||
|  | ||
| // Add detailed JSON for complex nested data | ||
| detailedData := make([]string, len(c)) | ||
| for i, metric := range c { | ||
| jsonData, err := json.Marshal(metric) | ||
| if err != nil { | ||
| detailedData[i] = "" | ||
| } else { | ||
| detailedData[i] = string(jsonData) | ||
| } | ||
| } | ||
| frame.Fields = append(frame.Fields, data.NewField("detailed_metrics", nil, detailedData)) | ||
|  | ||
| return data.Frames{frame} | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package github | ||
|  | ||
| import ( | ||
| "context" | ||
|  | ||
| "github.com/grafana/github-datasource/pkg/dfutil" | ||
| "github.com/grafana/github-datasource/pkg/models" | ||
| "github.com/grafana/grafana-plugin-sdk-go/backend" | ||
| ) | ||
|  | ||
| func (s *QueryHandler) handleCopilotMetricsQuery(ctx context.Context, q backend.DataQuery) backend.DataResponse { | ||
| query := &models.CopilotMetricsQuery{} | ||
| if err := UnmarshalQuery(q.JSON, query); err != nil { | ||
| return *err | ||
| } | ||
| return dfutil.FrameResponseWithError(s.Datasource.HandleCopilotMetricsQuery(ctx, query, q)) | ||
| } | ||
|  | ||
| // HandleCopilotMetrics handles the plugin query for GitHub Copilot metrics for an organization or team | ||
| func (s *QueryHandler) HandleCopilotMetrics(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { | ||
| return &backend.QueryDataResponse{ | ||
| Responses: processQueries(ctx, req, s.handleCopilotMetricsQuery), | ||
| }, nil | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package github | ||
|  | ||
| import ( | ||
| "testing" | ||
|  | ||
| "github.com/grafana/github-datasource/pkg/models" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|  | ||
| func TestCopilotMetricsResponse_Frames(t *testing.T) { | ||
| // Test empty response | ||
| t.Run("empty response", func(t *testing.T) { | ||
| response := CopilotMetricsResponse{} | ||
| frames := response.Frames() | ||
| assert.Len(t, frames, 1) | ||
| assert.Equal(t, "copilot_metrics", frames[0].Name) | ||
| assert.Len(t, frames[0].Fields, 0) | ||
| }) | ||
|  | ||
| // Test response with data | ||
| t.Run("response with data", func(t *testing.T) { | ||
| response := CopilotMetricsResponse{ | ||
| { | ||
| Date: "2025-01-01", | ||
| TotalActiveUsers: 100, | ||
| TotalEngagedUsers: 75, | ||
| CopilotIDECodeCompletions: models.CopilotIDECodeCompletions{ | ||
| TotalEngagedUsers: 50, | ||
| Languages: []models.CopilotLanguageMetrics{ | ||
| {Name: "go", TotalEngagedUsers: 25}, | ||
| {Name: "typescript", TotalEngagedUsers: 20}, | ||
| }, | ||
| Editors: []models.CopilotEditorMetrics{ | ||
| {Name: "vscode", TotalEngagedUsers: 45}, | ||
| {Name: "neovim", TotalEngagedUsers: 5}, | ||
| }, | ||
| }, | ||
| CopilotIDEChat: models.CopilotIDEChat{ | ||
| TotalEngagedUsers: 30, | ||
| }, | ||
| CopilotDotcomChat: models.CopilotDotcomChat{ | ||
| TotalEngagedUsers: 25, | ||
| }, | ||
| CopilotDotcomPullRequests: models.CopilotDotcomPullRequests{ | ||
| TotalEngagedUsers: 15, | ||
| }, | ||
| }, | ||
| } | ||
|  | ||
| frames := response.Frames() | ||
| assert.Len(t, frames, 1) | ||
| frame := frames[0] | ||
|  | ||
| assert.Equal(t, "copilot_metrics", frame.Name) | ||
|  | ||
| // Check that we have the expected fields | ||
| fieldNames := make([]string, len(frame.Fields)) | ||
| for i, field := range frame.Fields { | ||
| fieldNames[i] = field.Name | ||
| } | ||
|  | ||
| expectedFields := []string{ | ||
| "time", | ||
| "total_active_users", | ||
| "total_engaged_users", | ||
| "ide_completion_users", | ||
| "ide_chat_users", | ||
| "dotcom_chat_users", | ||
| "dotcom_pr_users", | ||
| "language_go_users", | ||
| "language_typescript_users", | ||
| "editor_vscode_users", | ||
| "editor_neovim_users", | ||
| "detailed_metrics", | ||
| } | ||
|  | ||
| for _, expected := range expectedFields { | ||
| assert.Contains(t, fieldNames, expected, "Field %s should be present", expected) | ||
| } | ||
|  | ||
| // Check that all fields have the correct length | ||
| for _, field := range frame.Fields { | ||
| assert.Equal(t, 1, field.Len(), "Field %s should have length 1", field.Name) | ||
| } | ||
| }) | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need these I believe. Just returning
metricswould be enough.Frames()method will handle this part. So we don't needcopilotMetricsToDataFrametoo