github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  // 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  func NewAuthorLoggerPluginWrapper(plugin Plugin) *AuthorLoggerPluginWrapper {
    33  	return &AuthorLoggerPluginWrapper{
    34  		plugin: plugin,
    35  	}
    36  }
    37  
    38  func (a *AuthorLoggerPluginWrapper) AddFlags(cmd *cobra.Command) {
    39  	cmd.Flags().BoolVar(&a.enabled, "log-author", false, "Log the author for each metric")
    40  }
    41  
    42  func (a *AuthorLoggerPluginWrapper) ReceiveIssue(issue sql.Issue) []Point {
    43  	points := a.plugin.ReceiveIssue(issue)
    44  	if a.enabled {
    45  		for i := range points {
    46  			if points[i].Values == nil {
    47  				points[i].Values = map[string]interface{}{}
    48  			}
    49  			points[i].Values["author"] = issue.User
    50  		}
    51  	}
    52  
    53  	return points
    54  }
    55  
    56  func (a *AuthorLoggerPluginWrapper) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    57  	points := a.plugin.ReceiveIssueEvent(event)
    58  
    59  	if a.enabled {
    60  		for i := range points {
    61  			if points[i].Values == nil {
    62  				points[i].Values = map[string]interface{}{}
    63  			}
    64  			if event.Actor != nil {
    65  				points[i].Values["author"] = *event.Actor
    66  			}
    67  		}
    68  	}
    69  
    70  	return points
    71  }
    72  
    73  func (a *AuthorLoggerPluginWrapper) ReceiveComment(comment sql.Comment) []Point {
    74  	points := a.plugin.ReceiveComment(comment)
    75  
    76  	if a.enabled {
    77  		for i := range points {
    78  			if points[i].Values == nil {
    79  				points[i].Values = map[string]interface{}{}
    80  			}
    81  			points[i].Values["author"] = comment.User
    82  		}
    83  	}
    84  
    85  	return points
    86  }