Skip to content

Commit

Permalink
Clean up and reorganize assessment resolvers
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Lucidi <[email protected]>
  • Loading branch information
mansam committed Oct 18, 2023
1 parent b07a83b commit 4f4cd47
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 266 deletions.
41 changes: 16 additions & 25 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,12 @@ func (h ApplicationHandler) AssessmentList(ctx *gin.Context) {
_ = ctx.Error(err)
return
}

assessments := m.Assessments
for _, a := range archetypes {
assessments = append(assessments, a.Assessments...)
for _, arch := range archetypes {
for _, a := range arch.Assessments {
assessments = append(assessments, *a.Assessment)
}
}

resources := []Assessment{}
for i := range assessments {
r := Assessment{}
Expand Down Expand Up @@ -1125,17 +1125,6 @@ func (r *Application) With(m *model.Application, tags []model.ApplicationTag) {
ref.With(a.ID, "")
r.Assessments = append(r.Assessments, ref)
}
r.Risk = RiskUnknown
}

//
// WithArchetypes updates the resource with archetypes.
func (r *Application) WithArchetypes(archetypes []model.Archetype) {
for _, a := range archetypes {
ref := Ref{}
ref.With(a.ID, a.Name)
r.Archetypes = append(r.Archetypes, ref)
}
}

//
Expand All @@ -1156,26 +1145,28 @@ func (r *Application) WithResolver(resolver *assessment.ApplicationResolver) (er
if err != nil {
return
}
for _, a := range archetypes {
ref := Ref{}
ref.With(a.ID, a.Name)
r.Archetypes = append(r.Archetypes, ref)
}
archetypeTags, err := resolver.ArchetypeTags()
if err != nil {
return
}
r.WithArchetypes(archetypes)
r.WithVirtualTags(archetypeTags, SourceArchetype)
r.WithVirtualTags(resolver.AssessmentTags(), SourceAssessment)
r.Assessed, err = resolver.Assessed()
if err != nil {
return
}
if r.Assessed {
r.Confidence, err = resolver.Confidence()
if err != nil {
return
}
r.Risk, err = resolver.Risk()
if err != nil {
return
}
r.Confidence, err = resolver.Confidence()
if err != nil {
return
}
r.Risk, err = resolver.Risk()
if err != nil {
return
}
return
}
Expand Down
60 changes: 21 additions & 39 deletions api/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,24 @@ func (h ArchetypeHandler) Get(ctx *gin.Context) {
_ = ctx.Error(result.Error)
return
}

membership := assessment.NewMembershipResolver(h.DB(ctx))
applications, err := membership.Applications(m)
if err != nil {
_ = ctx.Error(err)
return
}

questionnaires, err := assessment.NewQuestionnaireResolver(h.DB(ctx))
if err != nil {
_ = ctx.Error(err)
return
}

tags, err := assessment.NewTagResolver(h.DB(ctx))
if err != nil {
_ = ctx.Error(err)
}

resolver := assessment.NewArchetypeResolver(m, tags)

resolver := assessment.NewArchetypeResolver(m, tags, membership, questionnaires)
r := Archetype{}
r.With(m)
r.WithApplications(applications)
r.WithAssessmentTags(resolver.AssessmentTags())
r.Assessed = questionnaires.Assessed(m.Assessments)
r.Risk = questionnaires.Risk(m.Assessments)
r.Confidence = questionnaires.Confidence(m.Assessments)
err = r.WithResolver(resolver)
if err != nil {
_ = ctx.Error(err)
return
}
h.Respond(ctx, http.StatusOK, r)
}

Expand All @@ -103,7 +93,6 @@ func (h ArchetypeHandler) List(ctx *gin.Context) {
_ = ctx.Error(result.Error)
return
}

questionnaires, err := assessment.NewQuestionnaireResolver(h.DB(ctx))
if err != nil {
_ = ctx.Error(err)
Expand All @@ -113,24 +102,18 @@ func (h ArchetypeHandler) List(ctx *gin.Context) {
if err != nil {
_ = ctx.Error(err)
}

membership := assessment.NewMembershipResolver(h.DB(ctx))
resources := []Archetype{}
for i := range list {
m := &list[i]
resolver := assessment.NewArchetypeResolver(m, tags, membership, questionnaires)
r := Archetype{}
applications, err := membership.Applications(m)
r.With(m)
err = r.WithResolver(resolver)
if err != nil {
_ = ctx.Error(err)
return
}
resolver := assessment.NewArchetypeResolver(m, tags)
r.With(m)
r.WithApplications(applications)
r.WithAssessmentTags(resolver.AssessmentTags())
r.Assessed = questionnaires.Assessed(m.Assessments)
r.Risk = questionnaires.Risk(m.Assessments)
r.Confidence = questionnaires.Confidence(m.Assessments)
resources = append(resources, r)
}

Expand Down Expand Up @@ -169,14 +152,13 @@ func (h ArchetypeHandler) Create(ctx *gin.Context) {
}

membership := assessment.NewMembershipResolver(h.DB(ctx))
applications, err := membership.Applications(m)
resolver := assessment.NewArchetypeResolver(m, nil, membership, nil)
r.With(m)
err = r.WithResolver(resolver)
if err != nil {
_ = ctx.Error(err)
return
}

r.With(m)
r.WithApplications(applications)
h.Respond(ctx, http.StatusCreated, r)
}

Expand Down Expand Up @@ -401,27 +383,27 @@ func (r *Archetype) With(m *model.Archetype) {
ref.With(m.Review.ID, "")
r.Review = ref
}
r.Risk = RiskUnknown
}

//
// WithApplications updates the Archetype resource with the applications.
func (r *Archetype) WithApplications(apps []model.Application) {
// WithResolver uses an ArchetypeResolver to update the resource with
// values derived from the archetype's assessments.
func (r *Archetype) WithResolver(resolver *assessment.ArchetypeResolver) (err error) {
r.Risk = resolver.Risk()
r.Confidence = resolver.Confidence()
r.Assessed = resolver.Assessed()
apps, err := resolver.Applications()
for i := range apps {
ref := Ref{}
ref.With(apps[i].ID, apps[i].Name)
r.Applications = append(r.Applications, ref)
}
}

//
// WithAssessmentTags updates the Archetype resource with tags inherited from assessments.
func (r *Archetype) WithAssessmentTags(tags []model.Tag) {
for _, t := range tags {
for _, t := range resolver.AssessmentTags() {
ref := TagRef{}
ref.With(t.ID, t.Name, SourceAssessment, true)
r.Tags = append(r.Tags, ref)
}
return
}

//
Expand Down
58 changes: 7 additions & 51 deletions api/assessment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ const (
AssessmentRoot = AssessmentsRoot + "/:" + ID
)

//
// Assessment status
const (
AssessmentEmpty = "empty"
AssessmentStarted = "started"
AssessmentComplete = "complete"
)

//
// Assessment risks
const (
RiskRed = "red"
RiskYellow = "yellow"
RiskGreen = "green"
RiskUnknown = "unknown"
)

//
// AssessmentHandler handles Assessment resource routes.
type AssessmentHandler struct {
Expand Down Expand Up @@ -189,9 +172,6 @@ func (r *Assessment) With(m *model.Assessment) {
r.Questionnaire = r.ref(m.QuestionnaireID, &m.Questionnaire)
r.Archetype = r.refPtr(m.ArchetypeID, m.Archetype)
r.Application = r.refPtr(m.ApplicationID, m.Application)
_ = json.Unmarshal(m.Sections, &r.Sections)
_ = json.Unmarshal(m.Thresholds, &r.Thresholds)
_ = json.Unmarshal(m.RiskMessages, &r.RiskMessages)
r.Stakeholders = []Ref{}
for _, s := range m.Stakeholders {
ref := Ref{}
Expand All @@ -204,15 +184,13 @@ func (r *Assessment) With(m *model.Assessment) {
ref.With(sg.ID, sg.Name)
r.StakeholderGroups = append(r.StakeholderGroups, ref)
}
if r.Complete() {
r.Status = AssessmentComplete
} else if r.Started() {
r.Status = AssessmentStarted
} else {
r.Status = AssessmentEmpty
}
r.Risk = assessment.RiskLevel(m)
r.Confidence = assessment.Confidence(r.Sections)
a := assessment.Assessment{}
a.With(m)
r.Risk = a.Risk()
r.Confidence = a.Confidence()
r.RiskMessages = a.RiskMessages
r.Thresholds = a.Thresholds
r.Sections = a.Sections
}

//
Expand Down Expand Up @@ -246,25 +224,3 @@ func (r *Assessment) Model() (m *model.Assessment) {
}
return
}

//
// Complete returns whether all sections have been completed.
func (r *Assessment) Complete() bool {
for _, s := range r.Sections {
if !s.Complete() {
return false
}
}
return true
}

//
// Started returns whether any sections have been started.
func (r *Assessment) Started() bool {
for _, s := range r.Sections {
if s.Started() {
return true
}
}
return false
}
Loading

0 comments on commit 4f4cd47

Please sign in to comment.