Skip to content

Commit 3772ca2

Browse files
committed
updates on script lighthouse to isnert data
1 parent c05c78b commit 3772ca2

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

scripts/script_lighthouse.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
#########################################################################################
2+
# This script retrieves Lighthouse scores for a given date range from BigQuery and inserts
3+
# the results into Firestore. The script defines a temporary function GET_LIGHTHOUSE
4+
# to extract the median Lighthouse scores for each client. The results are then converted
5+
# to a JSON format and inserted into Firestore.
6+
#
7+
# How to consume the script:
8+
#
9+
# param1 (optional): start_date (YYYY-MM-DD)
10+
# param2 (optional): end_date (YYYY-MM-DD)
11+
#
12+
# Example:
13+
# python script_lighthouse.py 2020-01-01 2020-12-31
14+
#
15+
# If no parameters are provided, the script will retrieve all available data.
16+
#
117
import sys
218
import uuid
319
from google.cloud import bigquery
420
from google.cloud import firestore
521
from decimal import Decimal
22+
from datetime import datetime
623

724
def convert_decimal_to_float(data):
825
if isinstance(data, Decimal):
@@ -70,7 +87,7 @@ def execute_query_and_insert_result(start_date, end_date):
7087
''';
7188
7289
SELECT
73-
date,
90+
STRING(DATE(date)) as date,
7491
app AS technology,
7592
rank,
7693
geo,
@@ -90,7 +107,7 @@ def execute_query_and_insert_result(start_date, end_date):
90107

91108
# Construct the WHERE clause based on the provided parameters
92109
if start_date and end_date:
93-
query += f"WHERE date >= '{start_date}' AND date <= '{end_date}'"
110+
query += f"WHERE date >= '{start_date}' AND date <= '{end_date}' "
94111

95112
query += " GROUP BY date, app, rank, geo"
96113

@@ -107,7 +124,7 @@ def execute_query_and_insert_result(start_date, end_date):
107124
# Convert date
108125
#
109126
item = dict(row.items())
110-
item['date'] = str(row['date'])
127+
#item['date'] = str(row['date'])
111128
item = convert_decimal_to_float(item)
112129

113130
record_ref = collection_ref.document(uuid.uuid4().hex)
@@ -119,10 +136,11 @@ def execute_query_and_insert_result(start_date, end_date):
119136
batch.commit()
120137
# Start a new batch for the next iteration.
121138
batch = firestore_client.batch()
139+
print(datetime.now())
122140
idx = 0
123141

124142
batch.commit()
125-
print("Data inserted into Firestore successfully.")
143+
print("Data insert finsihed successfully.")
126144

127145
# Get command-line arguments
128146
start_date = sys.argv[1] if len(sys.argv) > 1 else None

0 commit comments

Comments
 (0)