Files
lincodex/backend/internal/usecase/matching/explain.go
T

155 lines
5.7 KiB
Go

package matching
import (
"slices"
"lincodex/backend/internal/domain"
)
// explanationInput is what the explanation reads: the computed scores of the
// entry and the facts of the card. It builds nothing that the scores did not
// compute, so a change of the requirements moves the numbers and the
// explanation together.
type explanationInput struct {
confirmed bool
declaredGrade *domain.GradeCode
policy domain.MatchingPolicy
scores scores
context PublicContext
// requestedFormats are the work formats that the search asks for; it is
// empty when neither the need nor a filter names any.
requestedFormats []domain.QuestionnaireWorkFormat
// needDomainConfirmed tells why the domain is unknown: the card has no
// domain, and the need may not have confirmed one either.
needDomainConfirmed bool
// percentileReason is the reason that the percentile is null; the zero
// value means the cohort is below the minimum.
percentileReason UnknownReason
// semanticsKnown is true when Sem came from stored vectors.
semanticsKnown bool
}
// explain builds the bounded, templated explanation and the unknown flags.
// An unknown component appears only in the flags and is never shown as a
// verdict about the person, and the absence of FSP data is neither a gap nor
// an unknown here: the competition component is zero by definition.
func explain(input explanationInput) ([]EvidenceItem, []UnknownFlag) {
return evidenceSummary(input), unknownFlags(input)
}
func evidenceSummary(input explanationInput) []EvidenceItem {
items := make([]EvidenceItem, 0, 6)
if input.confirmed {
items = append(items, EvidenceItem{Code: EvidenceConfirmedCategory, State: EvidenceStateSupports})
}
if input.declaredGrade != nil {
items = append(items, EvidenceItem{Code: EvidenceDeclaredGrade, State: EvidenceStateDeclaredOnly})
}
if state, ok := skillGroupState(input, true); ok {
items = append(items, EvidenceItem{Code: EvidenceRequiredSkill, State: state})
}
if state, ok := skillGroupState(input, false); ok {
items = append(items, EvidenceItem{Code: EvidenceDesiredSkill, State: state})
}
if len(input.requestedFormats) > 0 && len(input.context.WorkFormats) > 0 {
state := EvidenceStateGap
for _, format := range input.context.WorkFormats {
if slices.Contains(input.requestedFormats, format) {
state = EvidenceStateSupports
break
}
}
items = append(items, EvidenceItem{Code: EvidenceWorkConditionsMatch, State: state})
}
if compatibility := input.context.SalaryCompatibility; compatibility != nil {
state := EvidenceStateGap
if *compatibility == domain.CandidateViewSalaryCompatible {
state = EvidenceStateSupports
}
items = append(items, EvidenceItem{Code: EvidenceSalaryCompatibility, State: state})
}
return items
}
// skillGroupState summarizes the skills of one weight class from their
// computed contributions. A skill is supported by a measurement that reaches
// the policy threshold, declared when only a stated claim carries it, partial
// when a measurement exists but stays below the threshold, and a gap when
// nothing counts. The group is supported or a gap only when every skill is.
func skillGroupState(input explanationInput, required bool) (EvidenceState, bool) {
threshold := int64(input.policy.NearSkillThreshold) * picoPerBasisPoint
var total, supported, declared, gaps int
for _, skill := range input.scores.skills {
if skill.required != required {
continue
}
total++
switch {
case skill.pico == 0:
gaps++
case skill.measured && skill.pico >= threshold:
supported++
case !skill.measured:
declared++
}
}
switch {
case total == 0:
return "", false
case supported == total:
return EvidenceStateSupports, true
case gaps == total:
return EvidenceStateGap, true
case declared == total:
return EvidenceStateDeclaredOnly, true
default:
return EvidenceStatePartial, true
}
}
// unknownFlags lists, in the order of the contract's component list, every
// value that is neutral or absent. A confirmed entry shows the neutral T and
// the neutral freshness and responsiveness; an unconfirmed entry shows that
// the specialization has no confirmation instead.
func unknownFlags(input explanationInput) []UnknownFlag {
flags := make([]UnknownFlag, 0, 9)
if !input.semanticsKnown {
flags = append(flags, UnknownFlag{Component: UnknownSemantics, Reason: ReasonMLUnavailable})
}
if input.confirmed {
reason := input.percentileReason
if reason == "" {
reason = ReasonCohortBelow30
}
flags = append(flags, UnknownFlag{Component: UnknownPercentile, Reason: reason})
} else {
flags = append(flags, UnknownFlag{Component: UnknownAssessment, Reason: ReasonNoConfirmationForSpecialization})
}
if input.confirmed {
flags = append(flags,
UnknownFlag{Component: UnknownFreshness, Reason: ReasonNoFreshnessHistory},
UnknownFlag{Component: UnknownResponsiveness, Reason: ReasonNoResponseHistory},
)
}
if input.context.ExperienceBand == nil {
flags = append(flags, UnknownFlag{Component: UnknownExperienceBand, Reason: ReasonNotInPublicCard})
}
if len(input.context.WorkFormats) == 0 {
flags = append(flags, UnknownFlag{Component: UnknownWorkFormats, Reason: ReasonNotInPublicCard})
}
if input.context.SalaryCompatibility == nil {
flags = append(flags, UnknownFlag{Component: UnknownSalaryCompatibility, Reason: ReasonNotInPublicCard})
}
flags = append(flags, domainFlag(input))
return flags
}
// domainFlag says why Dom is the neutral value. The anonymous card never
// carries a domain, so a confirmed need domain cannot be compared with it.
func domainFlag(input explanationInput) UnknownFlag {
if input.needDomainConfirmed {
return UnknownFlag{Component: UnknownDomain, Reason: ReasonNotInPublicCard}
}
return UnknownFlag{Component: UnknownDomain, Reason: ReasonNeedFieldUnconfirmed}
}