|
| 1 | +from pathlib import Path |
| 2 | +from feast.repo_config import load_repo_config |
| 3 | +from datetime import datetime, timedelta |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from definitions import ( |
| 9 | + benchmark_feature_service, |
| 10 | + benchmark_feature_views, |
| 11 | + driver, |
| 12 | + driver_hourly_stats_view, |
| 13 | + entity, |
| 14 | + transformed_conv_rate, |
| 15 | +) |
| 16 | + |
| 17 | +from feast import FeatureStore |
| 18 | + |
| 19 | + |
| 20 | +def setup_data(): |
| 21 | + start = datetime.now() - timedelta(days=10) |
| 22 | + |
| 23 | + df = pd.DataFrame() |
| 24 | + df["driver_id"] = np.arange(1000, 1010) |
| 25 | + df["created"] = datetime.now() |
| 26 | + df["conv_rate"] = np.arange(0, 1, 0.1) |
| 27 | + df["acc_rate"] = np.arange(0.5, 1, 0.05) |
| 28 | + df["avg_daily_trips"] = np.arange(0, 1000, 100) |
| 29 | + |
| 30 | + # some of rows are beyond 7 days to test OUTSIDE_MAX_AGE status |
| 31 | + df["event_timestamp"] = start + pd.Series(np.arange(0, 10)).map( |
| 32 | + lambda days: timedelta(days=days) |
| 33 | + ) |
| 34 | + |
| 35 | + # Store data in parquet files. Parquet is convenient for local development mode. For |
| 36 | + # production, you can use your favorite DWH, such as BigQuery. See Feast documentation |
| 37 | + # for more info. |
| 38 | + df.to_parquet("driver_stats.parquet") |
| 39 | + |
| 40 | + # For Benchmarks |
| 41 | + # Please read more in Feast RFC-031 |
| 42 | + # (link https://docs.google.com/document/d/12UuvTQnTTCJhdRgy6h10zSbInNGSyEJkIxpOcgOen1I/edit) |
| 43 | + # about this benchmark setup |
| 44 | + def generate_data( |
| 45 | + num_rows: int, num_features: int, destination: str |
| 46 | + ) -> pd.DataFrame: |
| 47 | + features = [f"feature_{i}" for i in range(num_features)] |
| 48 | + columns = ["entity", "event_timestamp"] + features |
| 49 | + df = pd.DataFrame(0, index=np.arange(num_rows), columns=columns) |
| 50 | + df["event_timestamp"] = datetime.utcnow() |
| 51 | + for column in features: |
| 52 | + df[column] = np.random.randint(1, num_rows, num_rows) |
| 53 | + |
| 54 | + df["entity"] = "key-" + pd.Series(np.arange(1, num_rows + 1)).astype( |
| 55 | + pd.StringDtype() |
| 56 | + ) |
| 57 | + |
| 58 | + df.to_parquet(destination) |
| 59 | + |
| 60 | + generate_data(10**3, 250, "benchmark_data.parquet") |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + print("Running setup_it.py") |
| 65 | + |
| 66 | + setup_data() |
| 67 | + existing_repo_config = load_repo_config(Path(".")) |
| 68 | + |
| 69 | + # Update to default online store since otherwise, relies on Dockerized Redis service |
| 70 | + fs = FeatureStore(config=existing_repo_config.copy(update={"online_store": {}})) |
| 71 | + fs.apply( |
| 72 | + [ |
| 73 | + driver_hourly_stats_view, |
| 74 | + transformed_conv_rate, |
| 75 | + driver, |
| 76 | + entity, |
| 77 | + benchmark_feature_service, |
| 78 | + *benchmark_feature_views, |
| 79 | + ] |
| 80 | + ) |
| 81 | + |
| 82 | + print("setup_it finished") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments