Files
audita/internal/framework/runner/work_totals.go

109 lines
3.3 KiB
Go
Raw Normal View History

package runner
import (
"slices"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
)
// reportOperations selects report detail after counting and application
// attribution. Use a new slice so discarded operations can be released.
func reportOperations(operations []outcomes.Operation) []outcomes.Operation {
var report []outcomes.Operation
for _, operation := range operations {
if operation.FailureClass != outcomes.FailureRecoverable {
if !operation.Succeeded || len(operation.RecoveryReasons) == 0 {
continue
}
operation.ProposalIndices = nil
}
operation.Cause = nil
report = append(report, operation)
}
return report
}
func newWorkUnit(spec contracts.ModuleRunSpec, section contracts.SectionMetadata, moduleIndex int) WorkUnitOutcome {
return WorkUnitOutcome{
ModuleIndex: moduleIndex, StageIndex: spec.Index, StageID: spec.ID, ModuleKey: spec.ModuleKey,
ModuleInstance: spec.InstanceName, Section: section, State: outcomes.WorkNotStarted,
}
}
type proposalRecovery struct {
recovered bool
filtered bool
}
// Application can discover additional defects. Keep only the proposal operation
// flags needed to update totals and the identities of approved revisions.
func (p *modulePipelineResult) rememberProposalRecovery(unit *WorkUnitOutcome) {
for _, operation := range unit.Operations {
if operation.Reference.Kind == outcomes.OperationProposal && operation.Succeeded {
if p.proposalFacts == nil {
p.proposalFacts = make(map[int]proposalRecovery)
}
p.proposalFacts[unit.Section.Index] = proposalRecovery{
recovered: len(operation.RecoveryReasons) > 0, filtered: len(operation.Filtered) > 0,
}
break
}
}
for _, repair := range unit.Repairs {
if repair.Outcome == "approved" {
if p.revised == nil {
p.revised = make(map[int]struct{})
}
p.revised[repair.ProposalIndex] = struct{}{}
}
}
}
func (p *modulePipelineResult) recordApplyFiltering(filters map[int][]outcomes.FilteredProposal, reports bool) {
for pos, items := range filters {
original := items[:0]
var repairOperations map[int]*outcomes.Operation
for _, item := range items {
if _, repaired := p.revised[item.ProposalIndex]; !repaired {
original = append(original, item)
continue
}
if reports {
if repairOperations == nil {
repairOperations = approvedRepairOperations(p.WorkUnits[pos].Operations)
}
if op := repairOperations[item.ProposalIndex]; op != nil {
mergeOperationFiltering(op, []outcomes.FilteredProposal{item})
}
}
}
if facts, ok := p.proposalFacts[pos]; ok && len(original) != 0 {
if !facts.recovered {
p.Totals.Recoveries++
}
if !facts.filtered {
p.Totals.FilteringResponses++
}
if reports {
mergeFilteredProposals(&p.WorkUnits[pos], original)
}
}
}
}
func approvedRepairOperations(operations []outcomes.Operation) map[int]*outcomes.Operation {
byProposal := make(map[int]*outcomes.Operation)
for i := range operations {
op := &operations[i]
if op.Reference.Kind == outcomes.OperationCorrectionRepair && op.Reference.ProposalIndex != nil &&
slices.Contains(op.RecoveryReasons, outcomes.RecoveryCorrectionRevised) {
index := *op.Reference.ProposalIndex
if byProposal[index] == nil {
byProposal[index] = op
}
}
}
return byProposal
}