Copyright 2013 The Prometheus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package model

import (
	
	
	
	
	
	
)

ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a non-existing sample pair. It is a SamplePair with timestamp Earliest and value 0.0. Note that the natural zero value of SamplePair has a timestamp of 0, which is possible to appear in a real SamplePair and thus not suitable to signal a non-existing SamplePair.
ZeroSample is the pseudo zero-value of Sample used to signal a non-existing sample. It is a Sample with timestamp Earliest, value 0.0, and metric nil. Note that the natural zero value of Sample has a timestamp of 0, which is possible to appear in a real Sample and thus not suitable to signal a non-existing Sample.
A SampleValue is a representation of a value for a given sample at a given time.
MarshalJSON implements json.Marshaler.
func ( SampleValue) () ([]byte, error) {
	return json.Marshal(.String())
}
UnmarshalJSON implements json.Unmarshaler.
func ( *SampleValue) ( []byte) error {
	if len() < 2 || [0] != '"' || [len()-1] != '"' {
		return fmt.Errorf("sample value must be a quoted string")
	}
	,  := strconv.ParseFloat(string([1:len()-1]), 64)
	if  != nil {
		return 
	}
	* = SampleValue()
	return nil
}
Equal returns true if the value of v and o is equal or if both are NaN. Note that v==o is false if both are NaN. If you want the conventional float behavior, use == to compare two SampleValues.
func ( SampleValue) ( SampleValue) bool {
	if  ==  {
		return true
	}
	return math.IsNaN(float64()) && math.IsNaN(float64())
}

func ( SampleValue) () string {
	return strconv.FormatFloat(float64(), 'f', -1, 64)
}
SamplePair pairs a SampleValue with a Timestamp.
MarshalJSON implements json.Marshaler.
func ( SamplePair) () ([]byte, error) {
	,  := json.Marshal(.Timestamp)
	if  != nil {
		return nil, 
	}
	,  := json.Marshal(.Value)
	if  != nil {
		return nil, 
	}
	return []byte(fmt.Sprintf("[%s,%s]", , )), nil
}
UnmarshalJSON implements json.Unmarshaler.
func ( *SamplePair) ( []byte) error {
	 := [...]json.Unmarshaler{&.Timestamp, &.Value}
	return json.Unmarshal(, &)
}
Equal returns true if this SamplePair and o have equal Values and equal Timestamps. The semantics of Value equality is defined by SampleValue.Equal.
func ( *SamplePair) ( *SamplePair) bool {
	return  ==  || (.Value.Equal(.Value) && .Timestamp.Equal(.Timestamp))
}

func ( SamplePair) () string {
	return fmt.Sprintf("%s @[%s]", .Value, .Timestamp)
}
Sample is a sample pair associated with a metric.
type Sample struct {
	Metric    Metric      `json:"metric"`
	Value     SampleValue `json:"value"`
	Timestamp Time        `json:"timestamp"`
}
Equal compares first the metrics, then the timestamp, then the value. The semantics of value equality is defined by SampleValue.Equal.
func ( *Sample) ( *Sample) bool {
	if  ==  {
		return true
	}

	if !.Metric.Equal(.Metric) {
		return false
	}
	if !.Timestamp.Equal(.Timestamp) {
		return false
	}

	return .Value.Equal(.Value)
}

func ( Sample) () string {
	return fmt.Sprintf("%s => %s", .Metric, SamplePair{
		Timestamp: .Timestamp,
		Value:     .Value,
	})
}
MarshalJSON implements json.Marshaler.
func ( Sample) () ([]byte, error) {
	 := struct {
		 Metric     `json:"metric"`
		  SamplePair `json:"value"`
	}{
		: .Metric,
		: SamplePair{
			Timestamp: .Timestamp,
			Value:     .Value,
		},
	}

	return json.Marshal(&)
}
UnmarshalJSON implements json.Unmarshaler.
func ( *Sample) ( []byte) error {
	 := struct {
		 Metric     `json:"metric"`
		  SamplePair `json:"value"`
	}{
		: .Metric,
		: SamplePair{
			Timestamp: .Timestamp,
			Value:     .Value,
		},
	}

	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	.Metric = .
	.Timestamp = ..Timestamp
	.Value = ..Value

	return nil
}
Samples is a sortable Sample slice. It implements sort.Interface.
type Samples []*Sample

func ( Samples) () int {
	return len()
}
Less compares first the metrics, then the timestamp.
func ( Samples) (,  int) bool {
	switch {
	case [].Metric.Before([].Metric):
		return true
	case [].Metric.Before([].Metric):
		return false
	case [].Timestamp.Before([].Timestamp):
		return true
	default:
		return false
	}
}

func ( Samples) (,  int) {
	[], [] = [], []
}
Equal compares two sets of samples and returns true if they are equal.
func ( Samples) ( Samples) bool {
	if len() != len() {
		return false
	}

	for ,  := range  {
		if !.Equal([]) {
			return false
		}
	}
	return true
}
SampleStream is a stream of Values belonging to an attached COWMetric.
type SampleStream struct {
	Metric Metric       `json:"metric"`
	Values []SamplePair `json:"values"`
}

func ( SampleStream) () string {
	 := make([]string, len(.Values))
	for ,  := range .Values {
		[] = .String()
	}
	return fmt.Sprintf("%s =>\n%s", .Metric, strings.Join(, "\n"))
}
Value is a generic interface for values resulting from a query evaluation.
type Value interface {
	Type() ValueType
	String() string
}

func (Matrix) () ValueType  { return ValMatrix }
func (Vector) () ValueType  { return ValVector }
func (*Scalar) () ValueType { return ValScalar }
func (*String) () ValueType { return ValString }

type ValueType int

const (
	ValNone ValueType = iota
	ValScalar
	ValVector
	ValMatrix
	ValString
)
MarshalJSON implements json.Marshaler.
func ( ValueType) () ([]byte, error) {
	return json.Marshal(.String())
}

func ( *ValueType) ( []byte) error {
	var  string
	if  := json.Unmarshal(, &);  != nil {
		return 
	}
	switch  {
	case "<ValNone>":
		* = ValNone
	case "scalar":
		* = ValScalar
	case "vector":
		* = ValVector
	case "matrix":
		* = ValMatrix
	case "string":
		* = ValString
	default:
		return fmt.Errorf("unknown value type %q", )
	}
	return nil
}

func ( ValueType) () string {
	switch  {
	case ValNone:
		return "<ValNone>"
	case ValScalar:
		return "scalar"
	case ValVector:
		return "vector"
	case ValMatrix:
		return "matrix"
	case ValString:
		return "string"
	}
	panic("ValueType.String: unhandled value type")
}
Scalar is a scalar value evaluated at the set timestamp.
type Scalar struct {
	Value     SampleValue `json:"value"`
	Timestamp Time        `json:"timestamp"`
}

func ( Scalar) () string {
	return fmt.Sprintf("scalar: %v @[%v]", .Value, .Timestamp)
}
MarshalJSON implements json.Marshaler.
func ( Scalar) () ([]byte, error) {
	 := strconv.FormatFloat(float64(.Value), 'f', -1, 64)
	return json.Marshal([...]interface{}{.Timestamp, string()})
}
UnmarshalJSON implements json.Unmarshaler.
func ( *Scalar) ( []byte) error {
	var  string
	 := [...]interface{}{&.Timestamp, &}

	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	,  := strconv.ParseFloat(, 64)
	if  != nil {
		return fmt.Errorf("error parsing sample value: %s", )
	}
	.Value = SampleValue()
	return nil
}
String is a string value evaluated at the set timestamp.
type String struct {
	Value     string `json:"value"`
	Timestamp Time   `json:"timestamp"`
}

func ( *String) () string {
	return .Value
}
MarshalJSON implements json.Marshaler.
func ( String) () ([]byte, error) {
	return json.Marshal([]interface{}{.Timestamp, .Value})
}
UnmarshalJSON implements json.Unmarshaler.
func ( *String) ( []byte) error {
	 := [...]interface{}{&.Timestamp, &.Value}
	return json.Unmarshal(, &)
}
Vector is basically only an alias for Samples, but the contract is that in a Vector, all Samples have the same timestamp.
type Vector []*Sample

func ( Vector) () string {
	 := make([]string, len())
	for ,  := range  {
		[] = .String()
	}
	return strings.Join(, "\n")
}

func ( Vector) () int      { return len() }
func ( Vector) (,  int) { [], [] = [], [] }
Less compares first the metrics, then the timestamp.
func ( Vector) (,  int) bool {
	switch {
	case [].Metric.Before([].Metric):
		return true
	case [].Metric.Before([].Metric):
		return false
	case [].Timestamp.Before([].Timestamp):
		return true
	default:
		return false
	}
}
Equal compares two sets of samples and returns true if they are equal.
func ( Vector) ( Vector) bool {
	if len() != len() {
		return false
	}

	for ,  := range  {
		if !.Equal([]) {
			return false
		}
	}
	return true
}
Matrix is a list of time series.
type Matrix []*SampleStream

func ( Matrix) () int           { return len() }
func ( Matrix) (,  int) bool { return [].Metric.Before([].Metric) }
func ( Matrix) (,  int)      { [], [] = [], [] }

func ( Matrix) () string {
	 := make(Matrix, len())
	copy(, )
	sort.Sort()

	 := make([]string, len())

	for ,  := range  {
		[] = .String()
	}

	return strings.Join(, "\n")