Skip to content

Commit

Permalink
feat: delete orphaned alert
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Jan 14, 2024
1 parent d4d6355 commit d86e00d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
13 changes: 10 additions & 3 deletions frontend/src/components/alert/Alert.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="box">
<div class="box" v-if="!isDeleted">
<ErrorMessage
class="block"
:error="error"
Expand All @@ -25,7 +25,7 @@
<tr>
<th>Artifacts</th>
<td>
<Artifacts :artifacts="alert.artifacts"></Artifacts>
<Artifacts :artifacts="alert.artifacts" @delete="onArtifactsDeleted"></Artifacts>
</td>
</tr>
<tr v-if="alert.tags.length > 0">
Expand Down Expand Up @@ -66,6 +66,7 @@ export default defineComponent({
},
emits: ["delete"],
setup(_, context) {
const isDeleted = ref(false)
const error = ref<AxiosError>()
const onSetError = (newError: AxiosError) => {
Expand All @@ -80,13 +81,19 @@ export default defineComponent({
context.emit("delete")
}
const onArtifactsDeleted = () => {
isDeleted.value = true
}
return {
onDelete,
getLocalDatetime,
getHumanizedRelativeTime,
onSetError,
onDisposeError,
error
error,
isDeleted,
onArtifactsDeleted
}
}
})
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/components/alert/AlertDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<tr>
<th>Artifacts</th>
<td>
<Artifacts :artifacts="alert.artifacts"></Artifacts>
<Artifacts :artifacts="alert.artifacts" @delete="onArtifactsDeleted"></Artifacts>
</td>
</tr>
<tr v-if="alert.tags.length > 0">
Expand Down Expand Up @@ -85,13 +85,16 @@ export default defineComponent({
context.emit("delete")
}
const onArtifactsDeleted = onDelete
return {
onDelete,
getLocalDatetime,
getHumanizedRelativeTime,
onSetError,
onDisposeError,
error
error,
onArtifactsDeleted
}
}
})
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/artifact/ArtifactTag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default defineComponent({
required: true
}
},
setup(props) {
emits: ["delete"],
setup(props, context) {
const isDeleted = ref(false)
const isDeleteButtonEnabled = ref(false)
Expand All @@ -42,6 +43,7 @@ export default defineComponent({
if (confirmed) {
await deleteArtifactTask.perform(props.artifact.id)
isDeleted.value = true
context.emit("delete")
}
}
Expand Down
20 changes: 19 additions & 1 deletion frontend/src/components/artifact/ArtifactTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
v-for="artifact in artifacts"
:key="artifact.id"
:artifact="artifact"
@delete="onDelete"
></ArtifactTag>
</div>
</template>

<script lang="ts">
import { defineComponent, type PropType } from "vue"
import { defineComponent, type PropType, ref, watch } from "vue"
import ArtifactTag from "@/components/artifact/ArtifactTag.vue"
import type { Artifact } from "@/types"
Expand All @@ -24,6 +25,23 @@ export default defineComponent({
type: Array as PropType<Artifact[]>,
required: true
}
},
emits: ["delete"],
setup(props, context) {
const count = ref(0)
const onDelete = () => {
count.value += 1
}
watch(count, () => {
// Emit "delete" when all the artifacts are deleted
if (count.value >= props.artifacts.length) {
context.emit("delete")
}
})
return { onDelete }
}
})
</script>
8 changes: 8 additions & 0 deletions lib/mihari/models/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Artifact < ActiveRecord::Base
# @return [String, nil]
attr_accessor :rule_id

before_destroy do
@alert = alert
end

after_destroy do
@alert.destroy unless @alert.artifacts.any?
end

#
# Check uniqueness
#
Expand Down
14 changes: 14 additions & 0 deletions spec/models/artifact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,18 @@
expect(count).to eq(0)
end
end

describe ".after_destroy" do
let(:artifact) { FactoryBot.create(:artifact) }
let(:alert) { artifact.alert }

it do
expect(Mihari::Models::Alert.exists?(alert.id)).to eq(true)
end

it do
artifact.destroy
expect(Mihari::Models::Alert.exists?(alert.id)).to eq(false)
end
end
end

0 comments on commit d86e00d

Please sign in to comment.