Add script to generate sample data for divisibility endpoint
🤖 Generated with BackendIM... (backend.im)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
930e84fc96
commit
1254ceb807
125
generate_sample_data.py
Normal file
125
generate_sample_data.py
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
import random
|
||||||
|
import requests
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
def generate_sample_data(base_url="http://localhost:8000", num_samples=100):
|
||||||
|
"""
|
||||||
|
Generate sample data for the divisibility endpoint by making requests to the API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
base_url (str): Base URL of the API
|
||||||
|
num_samples (int): Number of sample data points to generate
|
||||||
|
"""
|
||||||
|
print(f"Generating {num_samples} sample data points for the Number Divisibility API...")
|
||||||
|
|
||||||
|
# Use both GET and POST endpoints to create diverse sample data
|
||||||
|
for i in range(num_samples):
|
||||||
|
method = "GET" if i % 2 == 0 else "POST"
|
||||||
|
number = random.randint(1, 1000) # Generate a random number between 1 and 1000
|
||||||
|
|
||||||
|
try:
|
||||||
|
if method == "GET":
|
||||||
|
response = requests.get(f"{base_url}/divisibility/{number}")
|
||||||
|
else:
|
||||||
|
response = requests.post(
|
||||||
|
f"{base_url}/divisibility",
|
||||||
|
json={"number": number}
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
result = response.json()
|
||||||
|
print(f"Added: Number {number} is{'' if result['is_divisible_by_2'] else ' not'} divisible by 2")
|
||||||
|
else:
|
||||||
|
print(f"Error: Failed to add number {number}. Status code: {response.status_code}")
|
||||||
|
|
||||||
|
# Add a small delay to avoid hammering the API
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
except requests.RequestException as e:
|
||||||
|
print(f"Error: Failed to connect to API at {base_url}. Make sure the API is running.")
|
||||||
|
print(f"Exception: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
print("\nSample data generation completed.")
|
||||||
|
print(f"Generated {num_samples} data points for the Number Divisibility API.")
|
||||||
|
print(f"You can view the history at {base_url}/history")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def generate_offline_data(db_path="/app/storage/db/db.sqlite", num_samples=100):
|
||||||
|
"""
|
||||||
|
Generate sample data directly to the database without making API requests.
|
||||||
|
Use this when the API is not running.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db_path (str): Path to the SQLite database
|
||||||
|
num_samples (int): Number of sample data points to generate
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
import sqlite3
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
db_file = Path(db_path)
|
||||||
|
|
||||||
|
# Check if database directory exists
|
||||||
|
if not db_file.parent.exists():
|
||||||
|
db_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Connect to the database
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# Create table if it doesn't exist
|
||||||
|
cursor.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS number_checks (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
number INTEGER NOT NULL,
|
||||||
|
is_divisible_by_2 BOOLEAN NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
print(f"Generating {num_samples} sample data points directly to the database...")
|
||||||
|
|
||||||
|
# Generate sample data
|
||||||
|
for i in range(num_samples):
|
||||||
|
number = random.randint(1, 1000)
|
||||||
|
is_divisible = number % 2 == 0
|
||||||
|
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO number_checks (number, is_divisible_by_2) VALUES (?, ?)",
|
||||||
|
(number, is_divisible)
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"Added: Number {number} is{'' if is_divisible else ' not'} divisible by 2")
|
||||||
|
|
||||||
|
# Commit the changes
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
print("\nSample data generation completed.")
|
||||||
|
print(f"Generated {num_samples} data points directly to the database.")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: Failed to generate offline data.")
|
||||||
|
print(f"Exception: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="Generate sample data for the Number Divisibility API")
|
||||||
|
parser.add_argument("--samples", type=int, default=100, help="Number of sample data points to generate")
|
||||||
|
parser.add_argument("--url", type=str, default="http://localhost:8000", help="Base URL of the API")
|
||||||
|
parser.add_argument("--offline", action="store_true", help="Generate data directly to the database without API requests")
|
||||||
|
parser.add_argument("--db-path", type=str, default="/app/storage/db/db.sqlite", help="Path to the SQLite database file (for offline mode)")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.offline:
|
||||||
|
generate_offline_data(db_path=args.db_path, num_samples=args.samples)
|
||||||
|
else:
|
||||||
|
generate_sample_data(base_url=args.url, num_samples=args.samples)
|
Loading…
x
Reference in New Issue
Block a user