-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19,027 changed files
with
28,390,321 additions
and
1 deletion.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains 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 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,16 @@ | ||
## Track 2 | ||
|
||
Within each round folder, you will find a detector, monitor and replay folder containing all necessary code to create a streaming knowledge graph environment. | ||
|
||
Within the replayer folder, the replay.py functionality can be adapted to your needs. | ||
You can: | ||
* Load different data segments based on regions where anomalies do occur | ||
* Change the time between knowledge graph events, to speed up local evaluations. | ||
|
||
Do take into account that the challenge organizers will us the default replayer functionality to score the different systems.</br> | ||
|
||
Two docker compose yaml files are also made available: | ||
* a full-application file, that can build and run the detector, monitor and replayer + kafka in one | ||
* a simpler docker compose file, that only exploits the kafka functionality for local development. Running this docker compose file enables participants to start the replayer, monitor and detector as individual python runs, for debugging purposes outisde the docker environment. | ||
|
||
Participants can extend or create a new detector based on the code within the detector folder. Participants are allowed to use other languages than Python to create their detectors. Please contact the challenge organizers through slack if you need additional assitance. |
This file contains 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,16 @@ | ||
FROM python:3.11-slim-bookworm | ||
|
||
# Set the working directory inside the container | ||
WORKDIR /app | ||
|
||
# Copy the requirements file into the container | ||
COPY requirements.txt . | ||
|
||
# Install the Python dependencies | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copy the rest of the application code into the container | ||
COPY . . | ||
|
||
# Define the command to run your application | ||
CMD ["python","-u", "consumer.py"] |
Binary file added
BIN
+358 Bytes
Track 2/Round 1/detector/__pycache__/anomaly_detector.cpython-311.pyc
Binary file not shown.
This file contains 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,7 @@ | ||
def determine_anomaly(graph_timestamp, graph_content): | ||
### Load graph | ||
|
||
## Determine anomlous behaviour | ||
|
||
### return boolean + graph timestamp | ||
return True, graph_timestamp |
This file contains 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,27 @@ | ||
from kafka import KafkaConsumer, KafkaProducer | ||
from anomaly_detector import determine_anomaly | ||
import os | ||
import json | ||
|
||
# Kafka broker configuration | ||
bootstrap_servers = os.environ.get("KAFKA_BOOTSTRAP_SERVERS", "localhost:9092") | ||
input_topic = 'kg' | ||
output_topic = 'result' | ||
|
||
# Function to consume files from input topic, process them, and produce result to output topic | ||
def process_files(consumer, producer): | ||
print("Ready, start listening:") | ||
for message in consumer: | ||
event_timestamp = message.timestamp | ||
data = json.loads(message.value.decode('utf-8')) | ||
anomaly, graph_timestamp = determine_anomaly(data['timestamp'], data['content']) | ||
if anomaly: | ||
anomaly_message = {'event_ts':event_timestamp, 'graph_ts':graph_timestamp} | ||
producer.send(output_topic, value=json.dumps(anomaly_message).encode('utf-8')) | ||
producer.flush() | ||
|
||
|
||
if __name__ == '__main__': | ||
consumer = KafkaConsumer(input_topic, bootstrap_servers=bootstrap_servers) | ||
producer = KafkaProducer(bootstrap_servers=bootstrap_servers) | ||
process_files(consumer, producer) |
This file contains 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 @@ | ||
kafka-python==2.0.2 |
This file contains 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,73 @@ | ||
version: '2.1' | ||
|
||
services: | ||
consumer: | ||
build: | ||
context: ./detector | ||
dockerfile: ./Dockerfile | ||
depends_on: | ||
kafka1: | ||
condition: service_healthy | ||
environment: | ||
KAFKA_BOOTSTRAP_SERVERS: kafka1:19092 | ||
|
||
replayer: | ||
build: | ||
context: ./replayer | ||
dockerfile: ./Dockerfile | ||
depends_on: | ||
kafka1: | ||
condition: service_healthy | ||
environment: | ||
KAFKA_BOOTSTRAP_SERVERS: kafka1:19092 | ||
|
||
monitor: | ||
build: | ||
context: ./monitor | ||
dockerfile: ./Dockerfile | ||
depends_on: | ||
kafka1: | ||
condition: service_healthy | ||
environment: | ||
KAFKA_BOOTSTRAP_SERVERS: kafka1:19092 | ||
|
||
zoo1: | ||
image: confluentinc/cp-zookeeper:7.3.2 | ||
hostname: zoo1 | ||
container_name: zoo1 | ||
ports: | ||
- "2181:2181" | ||
environment: | ||
ZOOKEEPER_CLIENT_PORT: 2181 | ||
ZOOKEEPER_SERVER_ID: 1 | ||
ZOOKEEPER_SERVERS: zoo1:2888:3888 | ||
|
||
kafka1: | ||
image: confluentinc/cp-kafka:7.3.2 | ||
hostname: kafka1 | ||
container_name: kafka1 | ||
ports: | ||
- "9092:9092" | ||
- "29092:29092" | ||
- "9999:9999" | ||
environment: | ||
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:19092,EXTERNAL://${DOCKER_HOST_IP:-127.0.0.1}:9092,DOCKER://host.docker.internal:29092 | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,DOCKER:PLAINTEXT | ||
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL | ||
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181" | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO" | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 | ||
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 | ||
KAFKA_JMX_PORT: 9999 | ||
KAFKA_JMX_HOSTNAME: ${DOCKER_HOST_IP:-127.0.0.1} | ||
KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer | ||
KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true" | ||
healthcheck: | ||
test: kafka-topics --bootstrap-server kafka1:29092 --list | ||
interval: 5s | ||
timeout: 10s | ||
retries: 3 | ||
depends_on: | ||
- zoo1 |
This file contains 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,38 @@ | ||
version: '2.1' | ||
|
||
services: | ||
zoo1: | ||
image: confluentinc/cp-zookeeper:7.3.2 | ||
hostname: zoo1 | ||
container_name: zoo1 | ||
ports: | ||
- "2181:2181" | ||
environment: | ||
ZOOKEEPER_CLIENT_PORT: 2181 | ||
ZOOKEEPER_SERVER_ID: 1 | ||
ZOOKEEPER_SERVERS: zoo1:2888:3888 | ||
|
||
kafka1: | ||
image: confluentinc/cp-kafka:7.3.2 | ||
hostname: kafka1 | ||
container_name: kafka1 | ||
ports: | ||
- "9092:9092" | ||
- "29092:29092" | ||
- "9999:9999" | ||
environment: | ||
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:19092,EXTERNAL://${DOCKER_HOST_IP:-127.0.0.1}:9092,DOCKER://host.docker.internal:29092 | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,DOCKER:PLAINTEXT | ||
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL | ||
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181" | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO" | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 | ||
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 | ||
KAFKA_JMX_PORT: 9999 | ||
KAFKA_JMX_HOSTNAME: ${DOCKER_HOST_IP:-127.0.0.1} | ||
KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer | ||
KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true" | ||
depends_on: | ||
- zoo1 |
This file contains 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,16 @@ | ||
FROM python:3.11-slim-bookworm | ||
|
||
# Set the working directory inside the container | ||
WORKDIR /app | ||
|
||
# Copy the requirements file into the container | ||
COPY requirements.txt . | ||
|
||
# Install the Python dependencies | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copy the rest of the application code into the container | ||
COPY . . | ||
|
||
# Define the command to run your application | ||
CMD ["python","-u", "monitor.py"] |
This file contains 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,71 @@ | ||
from kafka import KafkaConsumer, KafkaProducer | ||
import json | ||
import os | ||
import pandas as pd | ||
|
||
# Kafka broker configuration | ||
bootstrap_servers = os.environ.get("KAFKA_BOOTSTRAP_SERVERS", "localhost:9092") | ||
input_topic = 'result' | ||
|
||
found_true_positive = set() | ||
found_false_positive = set() | ||
true_regions_frame = pd.read_csv('train_labels.csv')[['started_at','finished_at', 'fault_name']] | ||
|
||
true_positives = 0 | ||
false_positives = 0 | ||
average_detection_time = 0 | ||
|
||
def calculate_score(graph_timestamp, false_pos_region_delta=300): | ||
global average_detection_time, true_positives, false_positives | ||
true_regions = true_regions_frame.values | ||
timestamp_found = False | ||
# Iterate over each true window | ||
for true_start, true_end, _ in true_regions: | ||
# Check if the predicted timestamp falls within the current true window | ||
if int(pd.Timestamp(true_start).timestamp()) <= graph_timestamp <= int(pd.Timestamp(true_end).timestamp()): | ||
# If it falls within the window and a true positive within the window was already found , | ||
# just continue, this are predictions related and close to eachother | ||
if (true_start, true_end) in found_true_positive: | ||
timestamp_found = True | ||
break | ||
# If it falls within the window and a true positive within the window is not found yet, | ||
# count it as a true positive and set the flag to True | ||
true_positives += 1 | ||
found_true_positive.add((true_start, true_end)) | ||
timestamp_found=True | ||
break | ||
# If the predicted timestamp falls outside the window, count it as a false positive | ||
if not timestamp_found: | ||
# but check if a previous false positive was within the range of an already detected false positive | ||
in_region = False | ||
for region in found_false_positive: | ||
if region[0] <= graph_timestamp <= region[1]: | ||
#if so, we just neglect it | ||
in_region = True | ||
break | ||
if not in_region: | ||
# otherwise, we add it, together with its range | ||
false_positives += 1 | ||
if false_pos_region_delta is not None: | ||
found_false_positive.add((graph_timestamp, graph_timestamp+false_pos_region_delta)) | ||
|
||
# Function to consume files from input topic, process them, and produce result to output topic | ||
def process_files(consumer): | ||
global average_detection_time, true_positives, false_positives | ||
print("Ready, start listening:") | ||
for message in consumer: | ||
event_timestamp = message.timestamp | ||
data = json.loads(message.value.decode('utf-8')) | ||
if average_detection_time == 0: | ||
average_detection_time = event_timestamp-data['event_ts'] | ||
else: | ||
average_detection_time = (average_detection_time+event_timestamp-data['event_ts'])/2 | ||
|
||
calculate_score(data['graph_ts']) | ||
|
||
print(true_positives, false_positives, average_detection_time) | ||
|
||
|
||
if __name__ == '__main__': | ||
consumer = KafkaConsumer(input_topic, bootstrap_servers=bootstrap_servers) | ||
process_files(consumer) |
This file contains 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,2 @@ | ||
kafka-python==2.0.2 | ||
pandas==2.0.3 |
Oops, something went wrong.