This is an automated email from the ASF dual-hosted git repository. skrawcz pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/hamilton.git
commit 311b369f6690191d561e37d716813ce5543d50a7 Author: Stefan Krawczyk <[email protected]> AuthorDate: Mon Feb 23 23:27:55 2026 -0800 Consolidates ui test_tracking scripts --- ui/test_tracking.py | 128 --------------------------------------------- ui/test_tracking_simple.py | 67 +++++++++++++++++++++--- 2 files changed, 60 insertions(+), 135 deletions(-) diff --git a/ui/test_tracking.py b/ui/test_tracking.py deleted file mode 100644 index afb69f6b..00000000 --- a/ui/test_tracking.py +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/env python3 -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -""" -Test script to validate Hamilton UI tracking functionality. - -This script creates a simple Hamilton DAG, executes it with tracking enabled, -and sends the execution data to the Hamilton UI server. - -Usage: - # Start the UI server first (in another terminal): - hamilton ui - - # Then run this script: - python ui/test_tracking.py -""" - -import sys - -import pandas as pd - -from hamilton import driver -from hamilton_sdk import adapters - -print("=" * 60) -print("Hamilton UI Tracking Test") -print("=" * 60) -print() - - -# Define simple Hamilton functions -def input_data() -> pd.DataFrame: - """Create sample input data.""" - return pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 4, 6, 8, 10]}) - - -def x_squared(input_data: pd.DataFrame) -> pd.Series: - """Square the x column.""" - return input_data["x"] ** 2 - - -def y_doubled(input_data: pd.DataFrame) -> pd.Series: - """Double the y column.""" - return input_data["y"] * 2 - - -def sum_all(x_squared: pd.Series, y_doubled: pd.Series) -> float: - """Sum all values.""" - return x_squared.sum() + y_doubled.sum() - - -def average(sum_all: float, input_data: pd.DataFrame) -> float: - """Calculate average.""" - total_elements = len(input_data) * 2 # x and y columns - return sum_all / total_elements - - -print("📊 Creating Hamilton DAG...") -print() - -# Create tracker adapter -tracker = adapters.HamiltonTracker( - project_id=1, # Using default project ID - username="test-user", - dag_name="test_tracking_dag", - tags={"test": "mini-mode", "environment": "local"}, - hamilton_api_url="http://localhost:8241", - hamilton_ui_url="http://localhost:8241", - api_key="test-key", # Using permissive mode key -) - -# Build driver with tracker -dr = driver.Builder().with_modules(sys.modules[__name__]).with_adapters(tracker).build() - -print("✅ DAG created with tracker adapter") -print(" Project ID: 1") -print(" DAG Name: test_tracking_dag") -print(" API URL: http://localhost:8241") -print() - -# Visualize the DAG -print("📈 DAG Structure:") -dr.display_all_functions() -print() - -# Execute the DAG -print("▶️ Executing DAG...") -result = dr.execute(["average", "sum_all", "x_squared", "y_doubled"]) - -print() -print("=" * 60) -print("Results:") -print("=" * 60) -for key, value in result.items(): - if isinstance(value, pd.Series): - print(f"\n{key}:") - print(value) - else: - print(f"{key}: {value}") - -print() -print("=" * 60) -print("✅ Execution Complete!") -print("=" * 60) -print() -print("🌐 View results in the UI:") -print(" http://localhost:8241") -print() -print("💡 Tips:") -print(" - Check the 'Projects' page to see your test project") -print(" - Click on the project to see execution runs") -print(" - Explore the DAG visualization and execution details") -print() diff --git a/ui/test_tracking_simple.py b/ui/test_tracking_simple.py index bc5ea197..5735fa0a 100644 --- a/ui/test_tracking_simple.py +++ b/ui/test_tracking_simple.py @@ -32,6 +32,7 @@ Usage: # Then run this script (optionally with a user name for API calls): python ui/test_tracking_simple.py python ui/test_tracking_simple.py --user my-username + python ui/test_tracking_simple.py --dag dataframe # DataFrame-based DAG """ import argparse @@ -50,6 +51,12 @@ def _parse_args(): default="test-user", help="User name to use for API calls (x-api-user and tracker username)", ) + parser.add_argument( + "--dag", + choices=["simple", "dataframe"], + default="simple", + help="DAG to run: 'simple' (default) or 'dataframe' (DataFrame-based)", + ) return parser.parse_args() @@ -133,8 +140,15 @@ except Exception as e: print() -# Step 3: Create and run a simple Hamilton DAG -print("🚀 Creating Hamilton DAG...") +# Step 3: Create and run a Hamilton DAG +if args.dag == "dataframe": + DAG_OUTPUTS = ["average", "sum_all", "x_squared", "y_doubled"] + DAG_NAME = "test_tracking_dag" +else: + DAG_OUTPUTS = ["average_squared", "sum_squared"] + DAG_NAME = "simple_test_dag" + +print("🚀 Creating Hamilton DAG (" + args.dag + ")...") # Define simple Hamilton functions inline @@ -158,21 +172,60 @@ def average_squared(sum_squared: float, input_numbers: pd.Series) -> float: return sum_squared / len(input_numbers) +# DataFrame-based DAG (--dag dataframe) +def input_data() -> pd.DataFrame: + """Create sample input data.""" + return pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 4, 6, 8, 10]}) + + +def x_squared(input_data: pd.DataFrame) -> pd.Series: + """Square the x column.""" + return input_data["x"] ** 2 + + +def y_doubled(input_data: pd.DataFrame) -> pd.Series: + """Double the y column.""" + return input_data["y"] * 2 + + +def sum_all(x_squared: pd.Series, y_doubled: pd.Series) -> float: + """Sum all values.""" + return x_squared.sum() + y_doubled.sum() + + +def average(sum_all: float, input_data: pd.DataFrame) -> float: + """Calculate average.""" + total_elements = len(input_data) * 2 # x and y columns + return sum_all / total_elements + + # Build driver WITHOUT tracking first (to test basic functionality) print(" Building DAG...") dr = driver.Builder().with_modules(sys.modules[__name__]).build() print(" DAG created successfully!") +if args.dag == "dataframe": + print() + print("📈 DAG structure:") + dr.display_all_functions() print() # Step 4: Execute without tracking print("▶️ Executing DAG (without tracking)...") -result = dr.execute(["average_squared", "sum_squared"]) +result = dr.execute(DAG_OUTPUTS) print() print("Results:") -print(f" - sum_squared: {result['sum_squared']}") -print(f" - average_squared: {result['average_squared']}") +if args.dag == "dataframe": + for key, value in result.items(): + if isinstance(value, pd.Series): + print(f" {key}:") + print(value.to_string()) + else: + print(f" {key}: {value}") +else: + print(f" - sum_squared: {result['sum_squared']}") + print(f" - average_squared: {result['average_squared']}") print() # Step 5: Now execute WITH tracking @@ -183,7 +236,7 @@ try: tracker = adapters.HamiltonTracker( project_id=project_id, username=USER_NAME, - dag_name="simple_test_dag", + dag_name=DAG_NAME, tags={"test": "true", "environment": "local"}, hamilton_api_url="http://localhost:8241", hamilton_ui_url="http://localhost:8241", @@ -194,7 +247,7 @@ try: dr_tracked = driver.Builder().with_modules(sys.modules[__name__]).with_adapters(tracker).build() # Execute - result_tracked = dr_tracked.execute(["average_squared", "sum_squared"]) + result_tracked = dr_tracked.execute(DAG_OUTPUTS) print("✅ Execution tracked successfully!") print()
