<commit_before>package caicloudprocessors

import (
	"github.com/golang/glog"
	"k8s.io/heapster/metrics/core"
	"k8s.io/heapster/metrics/core/caicloud"
)

type PodAggregator struct {
	MetricsToAggregate []string
}

func (p *PodAggregator) Name() string {
	return "pod_aggregator"
}

func (p *PodAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) {
	pods := make(map[string]*core.MetricSet)

	for key, metricSet := range batch.MetricSets {

		if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePodContainer {
			podName, found := metricSet.Labels[core.LabelPodName.Key]
			appName, found2 := metricSet.Labels[caicloudcore.LabelAppName.Key]
			namespaceName, found3 := metricSet.Labels[core.LabelNamespaceName.Key]
			if found && found2 && found3 {
				podKey := caicloudcore.PodKey(namespaceName, appName, podName)
				pod, found := pods[podKey]
				if !found {
					if podFromBatch, found := batch.MetricSets[podKey]; found {
						pod = podFromBatch
					} else {
						pod = podMetricSet(metricSet.Labels)
						pods[podKey] = pod
					}
					if err := Aggregate(metricSet, pod, p.MetricsToAggregate); err != nil {
						return nil, err
					}
				}
			} else {
				glog.Errorf("No namespace and/or app and/or pod info in pod %s: %v", key, metricSet.Labels)
			}
		}
	}
	for key, val := range pods {
		batch.MetricSets[key] = val
	}
	return batch, nil
}

var labelsToPopulate = []core.LabelDescriptor{
	core.LabelPodName,
	caicloudcore.LabelAppName,
	core.LabelNamespaceName,
	core.LabelHostname,
}

func podMetricSet(labels map[string]string) *core.MetricSet {
	newLabels := map[string]string{
		core.LabelMetricSetType.Key: core.MetricSetTypePod,
	}
	for _, l := range labelsToPopulate {
		if val, ok := labels[l.Key]; ok {
			newLabels[l.Key] = val
		}
	}
	return &core.MetricSet{
		MetricValues: make(map[string]core.MetricValue),
		Labels:       newLabels,
	}
}<commit_msg>fix a bug that pod don't have nodename label<commit_after>package caicloudprocessors

import (
	"github.com/golang/glog"
	"k8s.io/heapster/metrics/core"
	"k8s.io/heapster/metrics/core/caicloud"
)

type PodAggregator struct {
	MetricsToAggregate []string
}

func (p *PodAggregator) Name() string {
	return "pod_aggregator"
}

func (p *PodAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) {
	pods := make(map[string]*core.MetricSet)

	for key, metricSet := range batch.MetricSets {

		if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePodContainer {
			podName, found := metricSet.Labels[core.LabelPodName.Key]
			appName, found2 := metricSet.Labels[caicloudcore.LabelAppName.Key]
			namespaceName, found3 := metricSet.Labels[core.LabelNamespaceName.Key]
			if found && found2 && found3 {
				podKey := caicloudcore.PodKey(namespaceName, appName, podName)
				pod, found := pods[podKey]
				if !found {
					if podFromBatch, found := batch.MetricSets[podKey]; found {
						pod = podFromBatch
					} else {
						pod = podMetricSet(metricSet.Labels)
						pods[podKey] = pod
					}
					if err := Aggregate(metricSet, pod, p.MetricsToAggregate); err != nil {
						return nil, err
					}
				}
			} else {
				glog.Errorf("No namespace and/or app and/or pod info in pod %s: %v", key, metricSet.Labels)
			}
		}
	}
	for key, val := range pods {
		batch.MetricSets[key] = val
	}
	return batch, nil
}

var labelsToPopulate = []core.LabelDescriptor{
	core.LabelNamespaceName,
	core.LabelPodNamespace,
	caicloudcore.LabelAppName,
	core.LabelPodName,
	core.LabelNodename,
	core.LabelHostname,
	core.LabelHostID,
}

func podMetricSet(labels map[string]string) *core.MetricSet {
	newLabels := map[string]string{
		core.LabelMetricSetType.Key: core.MetricSetTypePod,
	}
	for _, l := range labelsToPopulate {
		if val, ok := labels[l.Key]; ok {
			newLabels[l.Key] = val
		}
	}
	return &core.MetricSet{
		MetricValues: make(map[string]core.MetricValue),
		Labels:       newLabels,
	}
}
{'fraction_non_alphanumeric': 0.12406762804574839, 'fraction_numerical': 0.0029835902536051715, 'mean_word_length': 4.02875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6977852', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1412, 'n_words': 289}