github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/state.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  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  	"k8s.io/test-infra/velodrome/sql"
    24  )
    25  
    26  // StatePlugin records age percentiles of issues in InfluxDB
    27  type StatePlugin struct {
    28  	states      BundledStates
    29  	desc        string
    30  	percentiles []int
    31  }
    32  
    33  var _ Plugin = &StatePlugin{}
    34  
    35  // AddFlags adds "state" and "percentiles" to the command help
    36  func (s *StatePlugin) AddFlags(cmd *cobra.Command) {
    37  	cmd.Flags().StringVar(&s.desc, "state", "", "Description of the state (eg: `opened,!merged,labeled:cool`)")
    38  	cmd.Flags().IntSliceVar(&s.percentiles, "percentiles", []int{}, "Age percentiles for state")
    39  }
    40  
    41  // CheckFlags configures which states to monitor
    42  func (s *StatePlugin) CheckFlags() error {
    43  	s.states = NewBundledStates(s.desc)
    44  	return nil
    45  }
    46  
    47  // ReceiveIssue is needed to implement a Plugin
    48  func (s *StatePlugin) ReceiveIssue(issue sql.Issue) []Point {
    49  	return nil
    50  }
    51  
    52  // ReceiveIssueEvent computes age percentiles and saves them to InfluxDB
    53  func (s *StatePlugin) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    54  	label := ""
    55  	if event.Label != nil {
    56  		label = *event.Label
    57  	}
    58  
    59  	if !s.states.ReceiveEvent(event.IssueID, event.Event, label, event.EventCreatedAt) {
    60  		return nil
    61  	}
    62  
    63  	total, sum := s.states.Total(event.EventCreatedAt)
    64  	values := map[string]interface{}{
    65  		"count": total,
    66  		"sum":   int(sum),
    67  	}
    68  	for _, percentile := range s.percentiles {
    69  		values[fmt.Sprintf("%d%%", percentile)] = int(s.states.Percentile(event.EventCreatedAt, percentile))
    70  	}
    71  
    72  	return []Point{
    73  		{
    74  			Values: values,
    75  			Date:   event.EventCreatedAt,
    76  		},
    77  	}
    78  }
    79  
    80  // ReceiveComment is needed to implement a Plugin
    81  func (s *StatePlugin) ReceiveComment(comment sql.Comment) []Point {
    82  	return nil
    83  }