Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement mapping priority #908

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion keep-ui/app/mapping/rules-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export default function RulesTable({ mappings }: { mappings: MappingRule[] }) {
header: "#",
cell: (context) => context.row.original.id,
}),
columnHelper.display({
id: "priority",
header: "Priority",
cell: (context) => context.row.original.priority,
}),
columnHelper.display({
id: "name",
header: "Name",
Expand Down Expand Up @@ -84,7 +89,7 @@ export default function RulesTable({ mappings }: { mappings: MappingRule[] }) {

const table = useReactTable({
columns,
data: mappings,
data: mappings.sort((a, b) => b.priority - a.priority),
getCoreRowModel: getCoreRowModel(),
});

Expand Down
8 changes: 8 additions & 0 deletions keep/api/bl/enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
self.db_session.query(MappingRule)
.filter(MappingRule.tenant_id == self.tenant_id)
.filter(MappingRule.disabled == False)
.order_by(MappingRule.priority.desc())
.all()
)

Expand All @@ -59,6 +60,7 @@
for row in rule.rows:
if all(
get_nested_attribute(alert, attribute) == row.get(attribute)
or row.get(attribute) == "*" # Wildcard
for attribute in rule.matchers
):
self.logger.info(
Expand All @@ -73,6 +75,12 @@
for key, value in row.items()
if key not in rule.matchers
}

# Enrich the alert with the matched row
for key, value in enrichments.items():
setattr(alert, key, value)

Check warning on line 81 in keep/api/bl/enrichments.py

View check run for this annotation

Codecov / codecov/patch

keep/api/bl/enrichments.py#L80-L81

Added lines #L80 - L81 were not covered by tests

# Save the enrichments to the database
enrich_alert(
self.tenant_id, alert.fingerprint, enrichments, self.db_session
)
Expand Down
27 changes: 14 additions & 13 deletions keep/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,9 @@ def whoami(info: Info):

@cli.command()
@click.option("--multi-tenant", is_flag=True, help="Enable multi-tenant mode")
@click.option("--port", "-p", type=int, default=8080, help="The port to run the API on")
@click.option(
"--port",
"-p",
type=int,
default=8080,
help="The port to run the API on"
)
@click.option(
"--host",
"-h",
type=str,
default="0.0.0.0",
help="The host to run the API on"
"--host", "-h", type=str, default="0.0.0.0", help="The host to run the API on"
)
def api(multi_tenant: bool, port: int, host: str):
"""Start the API."""
Expand Down Expand Up @@ -745,8 +735,18 @@ def list_mappings(info: Info):
help="The matchers of the mapping, as a comma-separated list of strings.",
required=True,
)
@click.option(
"--priority",
"-p",
type=int,
help="The priority of the mapping, higher priority means this rule will execute first.",
required=False,
default=0,
)
@pass_info
def create(info: Info, name: str, description: str, file: str, matchers: str):
def create(
info: Info, name: str, description: str, file: str, matchers: str, priority: int
):
"""Create a mapping rule."""
if os.path.isfile(file) and file.endswith(".csv"):
with open(file, "rb") as f:
Expand Down Expand Up @@ -775,6 +775,7 @@ def create(info: Info, name: str, description: str, file: str, matchers: str):
"file_name": file_name,
"matchers": matchers.split(","),
"rows": rows,
"priority": priority,
},
)

Expand Down
Loading