statistic.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package models
  2. // Statistic is the representation of a statistic used by the monitoring service.
  3. type Statistic struct {
  4. Name string `json:"name"`
  5. Tags map[string]string `json:"tags"`
  6. Values map[string]interface{} `json:"values"`
  7. }
  8. // NewStatistic returns an initialized Statistic.
  9. func NewStatistic(name string) Statistic {
  10. return Statistic{
  11. Name: name,
  12. Tags: make(map[string]string),
  13. Values: make(map[string]interface{}),
  14. }
  15. }
  16. // StatisticTags is a map that can be merged with others without causing
  17. // mutations to either map.
  18. type StatisticTags map[string]string
  19. // Merge creates a new map containing the merged contents of tags and t.
  20. // If both tags and the receiver map contain the same key, the value in tags
  21. // is used in the resulting map.
  22. //
  23. // Merge always returns a usable map.
  24. func (t StatisticTags) Merge(tags map[string]string) map[string]string {
  25. // Add everything in tags to the result.
  26. out := make(map[string]string, len(tags))
  27. for k, v := range tags {
  28. out[k] = v
  29. }
  30. // Only add values from t that don't appear in tags.
  31. for k, v := range t {
  32. if _, ok := tags[k]; !ok {
  33. out[k] = v
  34. }
  35. }
  36. return out
  37. }