<commit_before>package controller

import (
	"strings"

	"github.com/giantswarm/inago/common"
	"github.com/giantswarm/inago/fleet"
)

// UnitStatusList represents a list of UnitStatus.
type UnitStatusList []fleet.UnitStatus

// Group returns a shortened version of usl where equal status
// are replaced by one UnitStatus where the Name is replaced with "*".
func (usl UnitStatusList) Group() ([]fleet.UnitStatus, error) {
	matchers := map[string]struct{}{}
	newList := []fleet.UnitStatus{}

	hashesEqual := allHashesEqual(usl)

	for _, us := range usl {
		// Group unit status
		grouped, suffix, err := groupUnitStatus(usl, us)
		if err != nil {
			return nil, maskAny(err)
		}

		// Prevent doubled aggregation.
		if _, ok := matchers[suffix]; ok {
			continue
		}
		matchers[suffix] = struct{}{}

		statesEqual := allStatesEqual(grouped)

		// Aggregate.
		if hashesEqual && statesEqual {
			newStatus := grouped[0]
			newStatus.Name = "*"
			newList = append(newList, newStatus)
		} else {
			newList = append(newList, grouped...)
		}
	}

	return newList, nil
}

// groupUnitStatus returns a subset of usl where the sliceID matches the sliceID
// of groupMember, ignoring the unit names and extension.
func groupUnitStatus(usl []fleet.UnitStatus, groupMember fleet.UnitStatus) ([]fleet.UnitStatus, string, error) {
	ID, err := common.SliceID(groupMember.Name)
	if err != nil {
		return nil, "", maskAny(invalidUnitStatusError)
	}

	newList := []fleet.UnitStatus{}
	for _, us := range usl {
		exp := common.ExtExp.ReplaceAllString(us.Name, "")
		if !strings.HasSuffix(exp, ID) {
			continue
		}

		newList = append(newList, us)
	}

	return newList, ID, nil
}

// allHashesEqual is supposed to receive a list of unit statuses that is not
// grouped. This is necessary to compare unit hashes across groups.
func allHashesEqual(usl []fleet.UnitStatus) bool {
	for _, us1 := range usl {
		for _, us2 := range usl {
			for _, m1 := range us1.Machine {
				for _, m2 := range us2.Machine {
					if common.UnitBase(us1.Name) == common.UnitBase(us2.Name) && m1.UnitHash != m2.UnitHash {
						return false
					}
				}
			}
		}
	}

	return true
}

// allStatesEqual returns true if all elements in usl match for the following
// fields: Current, Desired, Machine.SystemdActive. Note this does not compare
// hashes sinces this method is supposed to receive only grouped unit statuses.
func allStatesEqual(usl []fleet.UnitStatus) bool {
	for _, us1 := range usl {
		for _, us2 := range usl {
			if us1.Current != us2.Current {
				return false
			}
			if us1.Desired != us2.Desired {
				return false
			}
			for _, m1 := range us1.Machine {
				for _, m2 := range us2.Machine {<commit_msg>less loops<commit_after>package controller

import (
	"strings"

	"github.com/giantswarm/inago/common"
	"github.com/giantswarm/inago/fleet"
)

// UnitStatusList represents a list of UnitStatus.
type UnitStatusList []fleet.UnitStatus

// Group returns a shortened version of usl where equal status
// are replaced by one UnitStatus where the Name is replaced with "*".
func (usl UnitStatusList) Group() ([]fleet.UnitStatus, error) {
	matchers := map[string]struct{}{}
	newList := []fleet.UnitStatus{}

	hashesEqual := allHashesEqual(usl)

	for _, us := range usl {
		// Group unit status
		grouped, suffix, err := groupUnitStatus(usl, us)
		if err != nil {
			return nil, maskAny(err)
		}

		// Prevent doubled aggregation.
		if _, ok := matchers[suffix]; ok {
			continue
		}
		matchers[suffix] = struct{}{}

		statesEqual := allStatesEqual(grouped)

		// Aggregate.
		if hashesEqual && statesEqual {
			newStatus := grouped[0]
			newStatus.Name = "*"
			newList = append(newList, newStatus)
		} else {
			newList = append(newList, grouped...)
		}
	}

	return newList, nil
}

// groupUnitStatus returns a subset of usl where the sliceID matches the sliceID
// of groupMember, ignoring the unit names and extension.
func groupUnitStatus(usl []fleet.UnitStatus, groupMember fleet.UnitStatus) ([]fleet.UnitStatus, string, error) {
	ID, err := common.SliceID(groupMember.Name)
	if err != nil {
		return nil, "", maskAny(invalidUnitStatusError)
	}

	newList := []fleet.UnitStatus{}
	for _, us := range usl {
		exp := common.ExtExp.ReplaceAllString(us.Name, "")
		if !strings.HasSuffix(exp, ID) {
			continue
		}

		newList = append(newList, us)
	}

	return newList, ID, nil
}

// allHashesEqual is supposed to receive a list of unit statuses that is not
// grouped. This is necessary to compare unit hashes across groups.
func allHashesEqual(usl []fleet.UnitStatus) bool {
	for _, us1 := range usl {
		for _, us2 := range usl {
			if common.UnitBase(us1.Name) != common.UnitBase(us2.Name) {
				continue
			}

			for _, m1 := range us1.Machine {
				for _, m2 := range us2.Machine {
					if m1.UnitHash != m2.UnitHash {
						return false
					}
				}
			}
		}
	}

	return true
}

// allStatesEqual returns true if all elements in usl match for the following
// fields: Current, Desired, Machine.SystemdActive. Note this does not compare
// hashes sinces this method is supposed to receive only grouped unit statuses.
func allStatesEqual(usl []fleet.UnitStatus) bool {
	for _, us1 := range usl {
		for _, us2 := range usl {
			if us1.Current != us2.Current {
				return false
			}
			if us1.Desired != us2.Desired {
				return false
			}
			for _, m1 := range us1.Machine {
				for _, m2 := range us2.Machine {
{'fraction_non_alphanumeric': 0.11579142698088699, 'fraction_numerical': 0.007793653739098163, 'mean_word_length': 3.4108019639934533, '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': '2183641', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1754, 'n_words': 535}