github.com/timstclair/heapster@v0.20.0-alpha1/metrics/util/util.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"strings"
    21  	"time"
    22  )
    23  
    24  // Concatenates a map of labels into a comma-separated key=value pairs.
    25  func LabelsToString(labels map[string]string, separator string) string {
    26  	output := make([]string, 0, len(labels))
    27  	for key, value := range labels {
    28  		output = append(output, fmt.Sprintf("%s:%s", key, value))
    29  	}
    30  
    31  	// Sort to produce a stable output.
    32  	sort.Strings(output)
    33  	return strings.Join(output, separator)
    34  }
    35  
    36  func CopyLabels(labels map[string]string) map[string]string {
    37  	c := make(map[string]string, len(labels))
    38  	for key, val := range labels {
    39  		c[key] = val
    40  	}
    41  	return c
    42  }
    43  
    44  func GetLatest(a, b time.Time) time.Time {
    45  	if a.After(b) {
    46  		return a
    47  	}
    48  	return b
    49  }