github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logcli/query/utils.go (about)

     1  package query
     2  
     3  import (
     4  	"github.com/grafana/loki/pkg/loghttp"
     5  )
     6  
     7  // return commonLabels labels between given labels set
     8  func commonLabels(streams loghttp.Streams) loghttp.LabelSet {
     9  	if len(streams) == 0 {
    10  		return nil
    11  	}
    12  
    13  	result := streams[0].Labels
    14  	for i := 1; i < len(streams); i++ {
    15  		result = intersect(result, streams[i].Labels)
    16  	}
    17  	return result
    18  }
    19  
    20  // intersect two labels set
    21  func intersect(a, b loghttp.LabelSet) loghttp.LabelSet {
    22  	set := loghttp.LabelSet{}
    23  
    24  	for ka, va := range a {
    25  		if vb, ok := b[ka]; ok {
    26  			if vb == va {
    27  				set[ka] = va
    28  			}
    29  		}
    30  	}
    31  	return set
    32  }
    33  
    34  // subtract labels set b from labels set a
    35  func subtract(a, b loghttp.LabelSet) loghttp.LabelSet {
    36  	set := loghttp.LabelSet{}
    37  
    38  	for ka, va := range a {
    39  		if vb, ok := b[ka]; ok {
    40  			if vb == va {
    41  				continue
    42  			}
    43  		}
    44  		set[ka] = va
    45  	}
    46  	return set
    47  }
    48  
    49  func matchLabels(on bool, l loghttp.LabelSet, names []string) loghttp.LabelSet {
    50  	ret := loghttp.LabelSet{}
    51  
    52  	nameSet := map[string]struct{}{}
    53  	for _, n := range names {
    54  		nameSet[n] = struct{}{}
    55  	}
    56  
    57  	for k, v := range l {
    58  		if _, ok := nameSet[k]; on == ok {
    59  			ret[k] = v
    60  		}
    61  	}
    62  
    63  	return ret
    64  }