helpers.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Helper functions for the benchmarking integration."""
  2. import json
  3. from datetime import datetime
  4. import psycopg2
  5. def insert_benchmarking_data(
  6. db_connection_url: str,
  7. lighthouse_data: dict,
  8. performance_data: list[dict],
  9. commit_sha: str,
  10. pr_title: str,
  11. ):
  12. """Insert the benchmarking data into the database.
  13. Args:
  14. db_connection_url: The URL to connect to the database.
  15. lighthouse_data: The Lighthouse data to insert.
  16. performance_data: The performance data to insert.
  17. commit_sha: The commit SHA to insert.
  18. pr_title: The PR title to insert.
  19. """
  20. # Serialize the JSON data
  21. lighthouse_json = json.dumps(lighthouse_data)
  22. performance_json = json.dumps(performance_data)
  23. # Get the current timestamp
  24. current_timestamp = datetime.now()
  25. # Connect to the database and insert the data
  26. with psycopg2.connect(db_connection_url) as conn, conn.cursor() as cursor:
  27. insert_query = """
  28. INSERT INTO benchmarks (lighthouse, performance, commit_sha, pr_title, time)
  29. VALUES (%s, %s, %s, %s, %s);
  30. """
  31. cursor.execute(
  32. insert_query,
  33. (
  34. lighthouse_json,
  35. performance_json,
  36. commit_sha,
  37. pr_title,
  38. current_timestamp,
  39. ),
  40. )
  41. # Commit the transaction
  42. conn.commit()