github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/author_logger_wrapper.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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 plugins
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  	"k8s.io/test-infra/velodrome/sql"
    22  )
    23  
    24  // AuthorLoggerPluginWrapper logs the author on all the Points returned. This is enabled by command-line.
    25  type AuthorLoggerPluginWrapper struct {
    26  	plugin  Plugin
    27  	enabled bool
    28  }
    29  
    30  var _ Plugin = &AuthorLoggerPluginWrapper{}
    31  
    32  // NewAuthorLoggerPluginWrapper is the constructor for AuthorLoggerPluginWrapper
    33  func NewAuthorLoggerPluginWrapper(plugin Plugin) *AuthorLoggerPluginWrapper {
    34  	return &AuthorLoggerPluginWrapper{
    35  		plugin: plugin,
    36  	}
    37  }
    38  
    39  // AddFlags adds "log-authors" <authors> to the command help
    40  func (a *AuthorLoggerPluginWrapper) AddFlags(cmd *cobra.Command) {
    41  	cmd.Flags().BoolVar(&a.enabled, "log-author", false, "Log the author for each metric")
    42  }
    43  
    44  // ReceiveIssue is a wrapper on plugin.ReceiveIssue() logging the author
    45  func (a *AuthorLoggerPluginWrapper) ReceiveIssue(issue sql.Issue) []Point {
    46  	points := a.plugin.ReceiveIssue(issue)
    47  	if a.enabled {
    48  		for i := range points {
    49  			if points[i].Values == nil {
    50  				points[i].Values = map[string]interface{}{}
    51  			}
    52  			points[i].Values["author"] = issue.User
    53  		}
    54  	}
    55  
    56  	return points
    57  }
    58  
    59  // ReceiveIssueEvent is a wrapper on plugin.ReceiveIssueEvent() logging the author
    60  func (a *AuthorLoggerPluginWrapper) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    61  	points := a.plugin.ReceiveIssueEvent(event)
    62  
    63  	if a.enabled {
    64  		for i := range points {
    65  			if points[i].Values == nil {
    66  				points[i].Values = map[string]interface{}{}
    67  			}
    68  			if event.Actor != nil {
    69  				points[i].Values["author"] = *event.Actor
    70  			}
    71  		}
    72  	}
    73  
    74  	return points
    75  }
    76  
    77  // ReceiveComment is a wrapper on plugin.ReceiveComment() logging the author
    78  func (a *AuthorLoggerPluginWrapper) ReceiveComment(comment sql.Comment) []Point {
    79  	points := a.plugin.ReceiveComment(comment)
    80  
    81  	if a.enabled {
    82  		for i := range points {
    83  			if points[i].Values == nil {
    84  				points[i].Values = map[string]interface{}{}
    85  			}
    86  			points[i].Values["author"] = comment.User
    87  		}
    88  	}
    89  
    90  	return points
    91  }