github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  type StatePlugin struct {
    27  	states      BundledStates
    28  	desc        string
    29  	percentiles []int
    30  }
    31  
    32  var _ Plugin = &StatePlugin{}
    33  
    34  func (s *StatePlugin) AddFlags(cmd *cobra.Command) {
    35  	cmd.Flags().StringVar(&s.desc, "state", "", "Description of the state (eg: `opened,!merged,labeled:cool`)")
    36  	cmd.Flags().IntSliceVar(&s.percentiles, "percentiles", []int{}, "Age percentiles for state")
    37  }
    38  
    39  func (s *StatePlugin) CheckFlags() error {
    40  	s.states = NewBundledStates(s.desc)
    41  	return nil
    42  }
    43  
    44  func (s *StatePlugin) ReceiveIssue(issue sql.Issue) []Point {
    45  	return nil
    46  }
    47  
    48  func (s *StatePlugin) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    49  	label := ""
    50  	if event.Label != nil {
    51  		label = *event.Label
    52  	}
    53  
    54  	if !s.states.ReceiveEvent(event.IssueId, event.Event, label, event.EventCreatedAt) {
    55  		return nil
    56  	}
    57  
    58  	total, sum := s.states.Total(event.EventCreatedAt)
    59  	values := map[string]interface{}{
    60  		"count": total,
    61  		"sum":   int(sum),
    62  	}
    63  	for _, percentile := range s.percentiles {
    64  		values[fmt.Sprintf("%d%%", percentile)] = int(s.states.Percentile(event.EventCreatedAt, percentile))
    65  	}
    66  
    67  	return []Point{
    68  		{
    69  			Values: values,
    70  			Date:   event.EventCreatedAt,
    71  		},
    72  	}
    73  }
    74  
    75  func (s *StatePlugin) ReceiveComment(comment sql.Comment) []Point {
    76  	return nil
    77  }