github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/tabulator/filter.go (about)

     1  /*
     2  Copyright 2022 The TestGrid 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 tabulator
    18  
    19  import (
    20  	"fmt"
    21  	"net/url"
    22  	"regexp"
    23  
    24  	statepb "github.com/GoogleCloudPlatform/testgrid/pb/state"
    25  )
    26  
    27  const (
    28  	includeFilter = "include-filter-by-regex"
    29  	excludeFilter = "exclude-filter-by-regex"
    30  )
    31  
    32  // filterGrid filters the grid to rows matching the include/exclude list in the base options
    33  func filterGrid(baseOptions string, rows []*statepb.Row) ([]*statepb.Row, error) {
    34  
    35  	vals, err := url.ParseQuery(baseOptions)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("parse %q: %v", baseOptions, err)
    38  	}
    39  
    40  	for _, include := range vals[includeFilter] {
    41  		if rows, err = includeRows(rows, include); err != nil {
    42  			return nil, fmt.Errorf("bad %s=%s: %v", includeFilter, include, err)
    43  		}
    44  	}
    45  
    46  	for _, exclude := range vals[excludeFilter] {
    47  		if rows, err = excludeRows(rows, exclude); err != nil {
    48  			return nil, fmt.Errorf("bad %s=%s: %v", excludeFilter, exclude, err)
    49  		}
    50  	}
    51  
    52  	// TODO(chases2): drop columns that are now empty due to filters
    53  	// TODO(fejta): grouping, which is not used by testgrid.k8s.io
    54  	// TODO(fejta): sorting, unused by testgrid.k8s.io
    55  	// TODO(fejta): graph, unused by testgrid.k8s.io
    56  	// TODO(fejta): tabuluar, unused by testgrid.k8s.io
    57  	return rows, nil
    58  }
    59  
    60  // includeRows returns the subset of rows that match the regex
    61  func includeRows(in []*statepb.Row, include string) ([]*statepb.Row, error) {
    62  	re, err := regexp.Compile(include)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	var rows []*statepb.Row
    67  	for _, r := range in {
    68  		if !re.MatchString(r.Name) {
    69  			continue
    70  		}
    71  		rows = append(rows, r)
    72  	}
    73  	return rows, nil
    74  }
    75  
    76  // excludeRows returns the subset of rows that do not match the regex
    77  func excludeRows(in []*statepb.Row, exclude string) ([]*statepb.Row, error) {
    78  	re, err := regexp.Compile(exclude)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	var rows []*statepb.Row
    83  	for _, r := range in {
    84  		if re.MatchString(r.Name) {
    85  			continue
    86  		}
    87  		rows = append(rows, r)
    88  	}
    89  	return rows, nil
    90  }