Repository navigation
add abstract base class for density-clustering-based alert aggregation #377
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa049d2
add abstract base class for density-clustering-based alert aggregation
LukasKarner 61200eb
prepare placeholder to add density cluster docs
ipmach 86edb69
add identifier
ipmach bc63479
Automated documentation update
github-actions[bot] b5aabd5
fix typing issues and improve documentation
LukasKarner 0d15c99
add tests for density-clustering-based alert aggregation
LukasKarner f57a7fc
fixed tests
LukasKarner c4e1e38
Merge pull request #1 from LukasKarner/docs
LukasKarner 992bb4a
add documentation
LukasKarner 5ce746b
fix requested changes
LukasKarner 7bd4493
fix tests
LukasKarner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Density Cluster Alert Aggregation | ||
|
|
||
| The density-clustering-based alert aggregator is a base class to implement alert aggregation by clustering alerts according a given distance function of alerts. | ||
|
|
||
| ## In/out | ||
|
|
||
| | | Schema | Description | | ||
| |------------|----------------------------|--------------------| | ||
| | **Input** | [DetectorSchema](../schemas.md) | Alerts from detectors | | ||
| | **Output** | [AggregateSchema](../schemas.md) | Aggregated alerts | | ||
|
|
||
| ## At a glance | ||
|
|
||
| | Learns from training data | Auto-configuration | Needs `events` | Federation | | ||
| |---|---|---|---| | ||
| | ❌ | ❌ | ❌ | ❌ | | ||
|
|
||
| ## Description | ||
|
|
||
| This class implements alert aggregation by density clustering. | ||
|
|
||
| ### Implementation Details | ||
|
|
||
| Creating a custom density clustering aggregator requires the implementation of 2 methods in subclasses: | ||
|
|
||
| 1. One has to implement either the single_metric or the metric function. | ||
| The single_metric function computes the distance between 2 alerts, | ||
| while the metric function computes the distance matrix between a list of alerts. | ||
| If both functions are implemented, the metric function will be used. | ||
|
|
||
| 2. The discard_alert_fn function must be implemented to determine which alerts | ||
| should no longer be considered for aggregation. | ||
| To this end, after each aggregation step, the discard_alert_fn | ||
| function is called with the list of relevant alerts. | ||
| It should return a list of booleans indicating which alerts should be discarded (True) | ||
| and which should be kept (False). | ||
|
|
||
| ### Labelling of Alerts | ||
|
|
||
| The group labels assigned to alerts are published in the output schema as a | ||
| dictionary mapping alert IDs to group IDs in the "alertDetails" field. | ||
| Every alert is assigned a group ID immediately after it is received, | ||
| but as the aggregation progresses, groups may be merged. | ||
| This is published in the output schema as a list of messages in the "alertDetails" field. | ||
| Likewise, when a group is finished and no longer reachable from any active alert, | ||
| this is also published in the output schema as a list of messages in the "alertDetails" field. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/alert_aggregators/density_cluster.py:example_1" | ||
| ``` | ||
|
|
||
| ## Configuration file | ||
|
|
||
| The configuration used by the example above. It sets only what this use case needs; every other parameter keeps its default (see [Configuration arguments](#configuration-arguments)). | ||
|
|
||
| ```yaml | ||
| --8<-- "docs/examples/alert_aggregators/density_cluster.yaml" | ||
| ``` | ||
|
|
||
| Load it with `yaml.safe_load` and pass the dict as `config=`, as in the example. The key under `alert_aggregators:` must match the aggregator's `name`. | ||
|
|
||
| ## Configuration arguments | ||
|
|
||
| All parameters this alert aggregator accepts, grouped by the YAML block they go in. **Scope** tells whether a parameter is `specific` to this aggregator or `shared` with other alert aggregators. | ||
|
|
||
| <!-- Start arguments --> | ||
|
|
||
| <!-- End arguments --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # --8<-- [start:example_1] | ||
| import yaml | ||
|
|
||
| from detectmatelibrary.alert_aggregation.density_clustering import ( | ||
| DensityClusteringAggregation, | ||
| ) | ||
| from detectmatelibrary.schemas import AggregateSchema, DetectorSchema | ||
|
|
||
| with open("docs/examples/alert_aggregators/density_cluster.yaml") as f: | ||
| config = yaml.safe_load(f) | ||
|
|
||
|
|
||
| def make_alert(alert_id: str, score: float) -> DetectorSchema: | ||
| return DetectorSchema( | ||
| { | ||
| "detectorID": "detector", | ||
| "detectorType": "dummy", | ||
| "alertID": alert_id, | ||
| "detectedTimestamp": 0, | ||
| "logIDs": [f"log-{alert_id}"], | ||
| "score": score, | ||
| "logTimestamps": [0], | ||
| "description": "test alert", | ||
| "receivedTimestamp": 0, | ||
| "alertDetails": {}, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class ConcreteDensityClusteringAggregation(DensityClusteringAggregation): | ||
| def single_metric( | ||
| self, | ||
| alert1: DetectorSchema, | ||
| alert2: DetectorSchema | ||
| ) -> float: | ||
| return abs(alert1["score"] - alert2["score"]) | ||
|
|
||
| def discard_alert_fn(self, alerts: list[DetectorSchema]) -> list[bool]: | ||
| return [False for alert in alerts] | ||
|
|
||
|
|
||
| aggregator = ConcreteDensityClusteringAggregation(config) | ||
| first = make_alert("a", 0.0) | ||
| second = make_alert("b", 6.0) | ||
| bridge = make_alert("c", 3.0) | ||
|
|
||
| first_output = AggregateSchema() | ||
| aggregator.aggregate_alerts(first, first_output) | ||
|
|
||
| second_output = AggregateSchema() | ||
| aggregator.aggregate_alerts(second, second_output) | ||
|
|
||
| bridge_output = AggregateSchema() | ||
| aggregator.aggregate_alerts(bridge, bridge_output) | ||
|
|
||
| print(first_output["alertDetails"]["a"] == "0") # True | ||
| print(second_output["alertDetails"]["b"] == "1") # True | ||
| print(bridge_output["alertDetails"]["c"] == "0") # True | ||
|
|
||
| # group labels 0 and 1 are merged into a single group | ||
| print(aggregator.active_groups[1] == {0, 1}) # True | ||
|
|
||
| # --8<-- [end:example_1] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| alert_aggregators: | ||
| DensityClusteringAggregator: | ||
| method_type: density_clustering_aggregator | ||
| delta: 4.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| from .basic_concat import BasicConcatAggregation, BasicConcatAggregationConfig | ||
| from .density_clustering import ( | ||
| DensityClusteringAggregation, | ||
| DensityClusteringAggregationConfig, | ||
| ) | ||
| from .logid_count import LogIDCountAggegation, LogIDCountAggegationConfig | ||
|
|
||
| __all__ = [ | ||
| "BasicConcatAggregation", | ||
| "BasicConcatAggregationConfig", | ||
| "DensityClusteringAggregation", | ||
| "DensityClusteringAggregationConfig", | ||
| "LogIDCountAggegation", | ||
| "LogIDCountAggegationConfig", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.