<commit_before>// Copyright © 2017 Circonus, Inc. <<EMAIL>>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package check

import (
	"encoding/json"
	"strings"

	"github.com/circonus-labs/circonus-gometrics/api"
	"github.com/pkg/errors"
)

func (c *Check) getFullCheckMetrics() ([]api.CheckBundleMetric, error) {
	cbmPath := strings.Replace(c.bundle.CID, "check_bundle", "check_bundle_metrics", -1)
	cbmPath += "?query_broker=1" // force for full set of metrics (active and available)

	data, err := c.client.Get(cbmPath)
	if err != nil {
		return nil, errors.Wrap(err, "fetching check bundle metrics")
	}

	var metrics api.CheckBundleMetrics
	if err := json.Unmarshal(data, &metrics); err != nil {
		return nil, errors.Wrap(err, "parsing check bundle metrics")
	}

	return metrics.Metrics, nil
}

func (c *Check) updateCheckBundleMetrics(m *map[string]api.CheckBundleMetric) error {
	metrics := make([]api.CheckBundleMetric, 0, len(*m))

	for mn, mv := range *m {
		c.logger.Debug().Str("name", mn).Msg("configuring new check bundle metric")
		metrics = append(metrics, mv)
	}

	cfg := &api.CheckBundleMetrics{
		CID:     strings.Replace(c.bundle.CID, "check_bundle", "check_bundle_metrics", 1),
		Metrics: metrics,
	}

	c.logger.Debug().Interface("payload", cfg).Msg("sending new metrics to API")

	results, err := c.client.UpdateCheckBundleMetrics(cfg)
	if err != nil {
		return errors.Wrap(err, "enabling new metrics")
	}

	for _, ms := range results.Metrics {
		switch ms.Status {
		case "active":
			c.logger.Info().Str("metric", ms.Name).Msg("enabled")
		case "noop":
			c.logger.Info().Str("metric", ms.Name).Msg("already enabled")
		case "fail":
			c.logger.Info().Str("metric", ms.Name).Msg("could not enable")
		default:
			c.logger.Info().Str("metric", ms.Name).Str("status", ms.Status).Msg("unknown status")
		}
	}

	return nil
}<commit_msg>use reflect to determine if an 'n' metric is actually a histogram revert back to updating check bundle directly rather than using check_bundle_metrics (due to histogram issue)<commit_after>// Copyright © 2017 Circonus, Inc. <<EMAIL>>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package check

import (
	"encoding/json"
	"reflect"
	"strings"

	cgm "github.com/circonus-labs/circonus-gometrics"
	"github.com/circonus-labs/circonus-gometrics/api"
	"github.com/pkg/errors"
)

func (c *Check) getFullCheckMetrics() ([]api.CheckBundleMetric, error) {
	cbmPath := strings.Replace(c.bundle.CID, "check_bundle", "check_bundle_metrics", -1)
	cbmPath += "?query_broker=1" // force for full set of metrics (active and available)

	data, err := c.client.Get(cbmPath)
	if err != nil {
		return nil, errors.Wrap(err, "fetching check bundle metrics")
	}

	var metrics api.CheckBundleMetrics
	if err := json.Unmarshal(data, &metrics); err != nil {
		return nil, errors.Wrap(err, "parsing check bundle metrics")
	}

	return metrics.Metrics, nil
}

// func (c *Check) updateCheckBundleMetrics(m *map[string]api.CheckBundleMetric) error {
// 	metrics := make([]api.CheckBundleMetric, 0, len(*m))
//
// 	for mn, mv := range *m {
// 		c.logger.Debug().Str("name", mn).Msg("configuring new check bundle metric")
// 		metrics = append(metrics, mv)
// 	}
//
// 	cfg := &api.CheckBundleMetrics{
// 		CID:     strings.Replace(c.bundle.CID, "check_bundle", "check_bundle_metrics", 1),
// 		Metrics: metrics,
// 	}
//
// 	c.logger.Debug().Interface("payload", cfg).Msg("sending new metrics to API")
//
// 	results, err := c.client.UpdateCheckBundleMetrics(cfg)
// 	if err != nil {
// 		return errors.Wrap(err, "enabling new metrics")
// 	}
//
// 	for _, ms := range results.Metrics {
// 		if ms.Result == nil {
// 			c.logger.Info().Interface("metric", ms).Msg("nil 'Result' field, unknown operation status")
// 			continue
// 		}
// 		switch *ms.Result {
// 		case "success":
// 			c.logger.Info().Str("metric", ms.Name).Msg("enabled")
// 		case "noop":
// 			c.logger.Info().Str("metric", ms.Name).Msg("already enabled")
// 		case "failure":
// 			c.logger.Info().Str("metric", ms.Name).Msg("could not enable")
// 		default:
// 			c.logger.Info().Str("metric", ms.Name).Str("result", *ms.Result).Msg("unknown result")
// 		}
// 	}
//
// 	return nil
// }

func (c *Check) updateCheckBundleMetrics(m *map[string]api.CheckBundleMetric) error {

	cid := c.bundle.CID
	bundle, err := c.client.FetchCheckBundle(api.CIDType(&cid))
	if err != nil {
		return errors.Wrap(err, "unable to fetch up-to-date copy of check")
	}

	metrics := make([]api.CheckBundleMetric, 0, len(*m))

	for mn, mv := range *m {
		c.logger.Debug().Str("name", mn).Msg("configuring new check bundle metric")
		metrics = append(metrics, mv)
	}

	bundle.Metrics = append(bundle.Metrics, metrics...)

	newBundle, err := c.client.UpdateCheckBundle(bundle)
	if err != nil {
		return errors.Wrap(err, "unable to update check bundle with new metrics")
	}

	c.bundle = newBundle

	return nil
}

func (c *Check) configMetric(mn string, mv cgm.Metric) api.CheckBundleMetric {

	cm := api.CheckBundleMetric{
		Name:   mn,
		Status: activeMetricStatus,
	}

	mtype := "numeric"
	switch mv.Type {
	case "n":
		c.logger.Debug().Str("mn", mn).Interface("mv", mv).Str("reflect_type", reflect.TypeOf(mv.Value).Kind().String()).Msg("circ type n")
		if reflect.TypeOf(mv.Value).Kind().String() == "slice" {
			mtype = "histogram"
		}
	case "s":
		mtype = "text"
	}

	cm.Type = mtype

	return cm
}
{'fraction_non_alphanumeric': 0.1696704897141817, 'fraction_numerical': 0.0030948479883488077, 'mean_word_length': 3.8662533215234722, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2234040', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1908, 'n_words': 473}