Use generated column for risk score

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-05-22 16:13:19 -07:00
parent 9c5828b525
commit 62a0fb96b7
3 changed files with 14 additions and 10 deletions

View File

@@ -0,0 +1,2 @@
ALTER TABLE risks ADD COLUMN inherent_risk_score INTEGER GENERATED ALWAYS AS (inherent_impact * inherent_likelihood) STORED;
ALTER TABLE risks ADD COLUMN residual_risk_score INTEGER GENERATED ALWAYS AS (residual_impact * residual_likelihood) STORED;

View File

@@ -38,8 +38,10 @@ type (
OwnerID *gid.GID `db:"owner_id"` OwnerID *gid.GID `db:"owner_id"`
InherentLikelihood int `db:"inherent_likelihood"` InherentLikelihood int `db:"inherent_likelihood"`
InherentImpact int `db:"inherent_impact"` InherentImpact int `db:"inherent_impact"`
InherentRiskScore int `db:"inherent_risk_score"`
ResidualLikelihood int `db:"residual_likelihood"` ResidualLikelihood int `db:"residual_likelihood"`
ResidualImpact int `db:"residual_impact"` ResidualImpact int `db:"residual_impact"`
ResidualRiskScore int `db:"residual_risk_score"`
CreatedAt time.Time `db:"created_at"` CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"` UpdatedAt time.Time `db:"updated_at"`
} }
@@ -56,14 +58,6 @@ func (r *Risk) CursorKey(orderBy RiskOrderField) page.CursorKey {
panic(fmt.Sprintf("unsupported order by: %s", orderBy)) panic(fmt.Sprintf("unsupported order by: %s", orderBy))
} }
func (r *Risk) InherentRiskScore() int {
return r.InherentLikelihood * r.InherentImpact
}
func (r *Risk) ResidualRiskScore() int {
return r.ResidualLikelihood * r.ResidualImpact
}
func (r *Risks) LoadByMeasureID( func (r *Risks) LoadByMeasureID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
@@ -85,8 +79,10 @@ WITH rsks AS (
r.note, r.note,
r.inherent_likelihood, r.inherent_likelihood,
r.inherent_impact, r.inherent_impact,
r.inherent_risk_score,
r.residual_likelihood, r.residual_likelihood,
r.residual_impact, r.residual_impact,
r.residual_risk_score,
r.created_at, r.created_at,
r.updated_at r.updated_at
FROM FROM
@@ -107,8 +103,10 @@ SELECT
note, note,
inherent_likelihood, inherent_likelihood,
inherent_impact, inherent_impact,
inherent_risk_score,
residual_likelihood, residual_likelihood,
residual_impact, residual_impact,
residual_risk_score,
created_at, created_at,
updated_at updated_at
FROM FROM
@@ -154,8 +152,10 @@ SELECT
note, note,
inherent_likelihood, inherent_likelihood,
inherent_impact, inherent_impact,
inherent_risk_score,
residual_likelihood, residual_likelihood,
residual_impact, residual_impact,
residual_risk_score,
category, category,
created_at, created_at,
updated_at updated_at
@@ -203,8 +203,10 @@ SELECT
note, note,
inherent_likelihood, inherent_likelihood,
inherent_impact, inherent_impact,
inherent_risk_score,
residual_likelihood, residual_likelihood,
residual_impact, residual_impact,
residual_risk_score,
created_at, created_at,
updated_at updated_at
FROM risks FROM risks

View File

@@ -51,10 +51,10 @@ func NewRisk(r *coredata.Risk) *Risk {
Treatment: r.Treatment, Treatment: r.Treatment,
InherentLikelihood: r.InherentLikelihood, InherentLikelihood: r.InherentLikelihood,
InherentImpact: r.InherentImpact, InherentImpact: r.InherentImpact,
InherentRiskScore: r.InherentRiskScore(), InherentRiskScore: r.InherentRiskScore,
ResidualLikelihood: r.ResidualLikelihood, ResidualLikelihood: r.ResidualLikelihood,
ResidualImpact: r.ResidualImpact, ResidualImpact: r.ResidualImpact,
ResidualRiskScore: r.ResidualRiskScore(), ResidualRiskScore: r.ResidualRiskScore,
Category: r.Category, Category: r.Category,
CreatedAt: r.CreatedAt, CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt, UpdatedAt: r.UpdatedAt,