|
1 | 1 | def ingest(): |
| 2 | + """ |
| 3 | + Generate random metrics data with occasional anomalies (spikes, drops, |
| 4 | + or plateaus). |
| 5 | + """ |
| 6 | + |
2 | 7 | import random |
3 | 8 | import time |
4 | 9 |
|
5 | 10 | import pandas as pd |
6 | 11 |
|
7 | | - # generate random metrics |
8 | | - metric1_value = random.uniform(0, 10) |
9 | | - metric2_value = random.uniform(0, 10) |
| 12 | + # Define the number of metrics |
| 13 | + metrics = ["metric1", "metric2", "metric3", "metric4", "metric5"] |
| 14 | + metric_values = [] |
10 | 15 |
|
11 | | - # get current timestamp |
| 16 | + # Get current timestamp |
12 | 17 | current_timestamp = int(time.time()) |
13 | 18 |
|
14 | | - # make df |
| 19 | + # Different anomaly types |
| 20 | + anomaly_types = ["spike", "drop", "plateau"] |
| 21 | + |
| 22 | + # Generate random metrics and introduce occasional anomalies (1% chance) |
| 23 | + anomaly_chance = random.random() |
| 24 | + anomaly_type = (random.choice(anomaly_types) |
| 25 | + if anomaly_chance <= 0.01 else None) |
| 26 | + |
| 27 | + for metric in metrics: |
| 28 | + if anomaly_type == "spike": |
| 29 | + # Generate a spike (e.g., a value significantly higher than normal) |
| 30 | + metric_value = random.uniform(15, 30) |
| 31 | + elif anomaly_type == "drop": |
| 32 | + # Generate a drop (e.g., a negative or significantly low value) |
| 33 | + metric_value = random.uniform(-10, -1) |
| 34 | + elif anomaly_type == "plateau": |
| 35 | + # Generate the same value for all metrics (e.g., a plateau) |
| 36 | + plateau_value = random.uniform(5, 6) |
| 37 | + metric_values = [plateau_value] * len(metrics) |
| 38 | + break # Exit the loop early since all metrics have the same value |
| 39 | + else: |
| 40 | + # Generate a normal value |
| 41 | + metric_value = random.uniform(0, 10) |
| 42 | + |
| 43 | + metric_values.append(metric_value) |
| 44 | + |
| 45 | + # Create a DataFrame with the metric data |
15 | 46 | data = { |
16 | | - "metric_name": ["metric1", "metric2"], |
17 | | - "metric_value": [metric1_value, metric2_value], |
18 | | - "metric_timestamp": [current_timestamp, current_timestamp], |
| 47 | + "metric_name": metrics, |
| 48 | + "metric_value": metric_values, |
| 49 | + "metric_timestamp": [current_timestamp] * len(metrics), |
19 | 50 | } |
20 | 51 | df = pd.DataFrame(data) |
21 | 52 |
|
|
0 commit comments