github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/metricsender/wireformat/wireformat.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package wireformat defines the format that will be used to send metric
     5  // batches to the collector and receive updates.
     6  package wireformat
     7  
     8  import (
     9  	"time"
    10  
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  // MetricBatch is a batch of metrics that will be sent to
    15  // the metric collector
    16  type MetricBatch struct {
    17  	UUID        string    `json:"uuid"`
    18  	EnvUUID     string    `json:"env-uuid"`
    19  	UnitName    string    `json:"unit-name"`
    20  	CharmUrl    string    `json:"charm-url"`
    21  	Created     time.Time `json:"created"`
    22  	Metrics     []Metric  `json:"metrics"`
    23  	Credentials []byte    `json:"credentials"`
    24  }
    25  
    26  // Metric represents a single Metric.
    27  type Metric struct {
    28  	Key   string    `json:"key"`
    29  	Value string    `json:"value"`
    30  	Time  time.Time `json:"time"`
    31  }
    32  
    33  // ToWire converts the state.MetricBatch into a type
    34  // that can be sent over the wire to the collector.
    35  func ToWire(mb *state.MetricBatch) *MetricBatch {
    36  	metrics := make([]Metric, len(mb.Metrics()))
    37  	for i, m := range mb.Metrics() {
    38  		metrics[i] = Metric{
    39  			Key:   m.Key,
    40  			Value: m.Value,
    41  			Time:  m.Time.UTC(),
    42  		}
    43  	}
    44  	return &MetricBatch{
    45  		UUID:        mb.UUID(),
    46  		EnvUUID:     mb.EnvUUID(),
    47  		UnitName:    mb.Unit(),
    48  		CharmUrl:    mb.CharmURL(),
    49  		Created:     mb.Created().UTC(),
    50  		Metrics:     metrics,
    51  		Credentials: mb.Credentials(),
    52  	}
    53  }
    54  
    55  // Response represents the response from the metrics collector.
    56  type Response struct {
    57  	UUID           string               `json:"uuid"`
    58  	EnvResponses   EnvironmentResponses `json:"env-responses"`
    59  	NewGracePeriod time.Duration        `json:"new-grace-period"`
    60  }
    61  
    62  type EnvironmentResponses map[string]EnvResponse
    63  
    64  // Ack adds the specified the batch UUID to the list of acknowledged batches
    65  // for the specified environment.
    66  func (e EnvironmentResponses) Ack(envUUID, batchUUID string) {
    67  	env := e[envUUID]
    68  
    69  	env.AcknowledgedBatches = append(env.AcknowledgedBatches, batchUUID)
    70  	e[envUUID] = env
    71  }
    72  
    73  func (e EnvironmentResponses) SetStatus(envUUID, unitName, status, info string) {
    74  	s := UnitStatus{
    75  		Status: status,
    76  		Info:   info,
    77  	}
    78  
    79  	env := e[envUUID]
    80  
    81  	if env.UnitStatuses == nil {
    82  		env.UnitStatuses = map[string]UnitStatus{
    83  			unitName: s,
    84  		}
    85  	} else {
    86  		env.UnitStatuses[unitName] = s
    87  	}
    88  	e[envUUID] = env
    89  
    90  }
    91  
    92  // EnvResponse contains the response data relevant to a concrete environment.
    93  type EnvResponse struct {
    94  	AcknowledgedBatches []string              `json:"acks,omitempty"`
    95  	UnitStatuses        map[string]UnitStatus `json:"unit-statuses,omitempty"`
    96  }
    97  
    98  type UnitStatus struct {
    99  	Status string `json:"status"`
   100  	Info   string `json:"info"`
   101  }