Skip to content

Commit

Permalink
Log evaluation history in new tables (#3659)
Browse files Browse the repository at this point in the history
This adds an interface and implementation for logging rule evaluation
statuses. The logic flow is described in the design doc. Note that this
does not wire in the logic into the engine yet, nor does it track
remediations/alerts. These will be added in future PRs.

Relates to: #3556
  • Loading branch information
dmjb authored Jun 19, 2024
1 parent ea51048 commit 6ff17de
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 5 deletions.
88 changes: 88 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions database/query/eval_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- name: GetLatestEvalStateForRuleEntity :one
SELECT eh.* FROM evaluation_rule_entities AS re
JOIN latest_evaluation_statuses AS les ON les.rule_entity_id = re.id
JOIN evaluation_statuses AS eh ON les.evaluation_history_id = eh.id
WHERE re.rule_id = $1
AND (
re.repository_id = $2
OR re.pull_request_id = $3
OR re.artifact_id = $4
)
FOR UPDATE;

-- name: InsertEvaluationRuleEntity :one
INSERT INTO evaluation_rule_entities(
rule_id,
repository_id,
pull_request_id,
artifact_id
) VALUES (
$1,
$2,
$3,
$4
)
RETURNING id;

-- name: InsertEvaluationStatus :one
INSERT INTO evaluation_statuses(
rule_entity_id,
status,
details
) VALUES (
$1,
$2,
$3
)
RETURNING id;

-- name: UpdateEvaluationTimes :exec
UPDATE evaluation_statuses
SET
evaluation_times = $1,
most_recent_evaluation = NOW()
WHERE id = $2;

-- name: UpsertLatestEvaluationStatus :exec
INSERT INTO latest_evaluation_statuses(
rule_entity_id,
evaluation_history_id
) VALUES (
$1,
$2
)
ON CONFLICT (rule_entity_id, evaluation_history_id) DO UPDATE
SET evaluation_history_id = $2;
8 changes: 7 additions & 1 deletion database/query/rule_instances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ SELECT * FROM rule_instances WHERE profile_id = $1 AND entity_type = $2;
DELETE FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND NOT id = ANY(sqlc.arg(updated_ids)::UUID[]);
AND NOT id = ANY(sqlc.arg(updated_ids)::UUID[]);

-- name: GetIDByProfileEntityName :one
SELECT id FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND name = $3;
167 changes: 167 additions & 0 deletions internal/db/eval_history.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ff17de

Please sign in to comment.