/*
Copyright 2019 The Kubernetes Authors All rights reserved.

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 metrics

import (
	"context"
	"fmt"

	"go.opencensus.io/stats"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/tag"
)

// Float64MetricRepresentation represents a snapshot of a float64 metrics.
// This is used for inspecting metric internals.
type Float64MetricRepresentation struct {
	// Name is the metric name.
	Name string
	// Labels contains all metric labels in key-value pair format.
	Labels map[string]string
	// Value is the value of the metric.
	Value float64
}

// Float64Metric represents an float64 metric.
type Float64Metric struct {
	name    string
	measure *stats.Float64Measure
}

// NewFloat64Metric create a Float64Metric metrics, returns nil when viewName is empty.
func NewFloat64Metric(metricID MetricID, viewName string, description string, unit string, aggregation Aggregation, tagNames []string) (*Float64Metric, error) {
	if viewName == "" {
		return nil, nil
	}

	MetricMap.AddMapping(metricID, viewName)

	tagKeys, err := getTagKeysFromNames(tagNames)
	if err != nil {
		return nil, fmt.Errorf("failed to create metric %q because of tag creation failure: %v", viewName, err)
	}

	var aggregationMethod *view.Aggregation
	switch aggregation {
	case LastValue:
		aggregationMethod = view.LastValue()
	case Sum:
		aggregationMethod = view.Sum()
	default:
		return nil, fmt.Errorf("unknown aggregation option %q", aggregation)
	}

	measure := stats.Float64(viewName, description, unit)
	newView := &view.View{
		Name:        viewName,
		Measure:     measure,
		Description: description,
		Aggregation: aggregationMethod,
		TagKeys:     tagKeys,
	}
	if err := view.Register(newView); err != nil {
		return nil, fmt.Errorf("failed to register view for metric %q: %v", viewName, err)
	}

	metric := Float64Metric{viewName, measure}
	return &metric, nil
}

// Record records a measurement for the metric, with provided tags as metric labels.
func (metric *Float64Metric) Record(tags map[string]string, measurement float64) error {
	var mutators []tag.Mutator

	tagMapMutex.RLock()
	defer tagMapMutex.RUnlock()

	for tagName, tagValue := range tags {
		tagKey, ok := tagMap[tagName]
		if !ok {
			return fmt.Errorf("referencing none existing tag %q in metric %q", tagName, metric.name)
		}
		mutators = append(mutators, tag.Upsert(tagKey, tagValue))
	}

	return stats.RecordWithTags(
		context.Background(),
		mutators,
		metric.measure.M(measurement))
}
