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
+ #
1
17
import sys
2
18
import uuid
3
19
from google .cloud import bigquery
4
20
from google .cloud import firestore
5
21
from decimal import Decimal
22
+ from datetime import datetime
6
23
7
24
def convert_decimal_to_float (data ):
8
25
if isinstance (data , Decimal ):
@@ -70,7 +87,7 @@ def execute_query_and_insert_result(start_date, end_date):
70
87
''';
71
88
72
89
SELECT
73
- date,
90
+ STRING(DATE(date)) as date,
74
91
app AS technology,
75
92
rank,
76
93
geo,
@@ -90,7 +107,7 @@ def execute_query_and_insert_result(start_date, end_date):
90
107
91
108
# Construct the WHERE clause based on the provided parameters
92
109
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 } ' "
94
111
95
112
query += " GROUP BY date, app, rank, geo"
96
113
@@ -107,7 +124,7 @@ def execute_query_and_insert_result(start_date, end_date):
107
124
# Convert date
108
125
#
109
126
item = dict (row .items ())
110
- item ['date' ] = str (row ['date' ])
127
+ # item['date'] = str(row['date'])
111
128
item = convert_decimal_to_float (item )
112
129
113
130
record_ref = collection_ref .document (uuid .uuid4 ().hex )
@@ -119,10 +136,11 @@ def execute_query_and_insert_result(start_date, end_date):
119
136
batch .commit ()
120
137
# Start a new batch for the next iteration.
121
138
batch = firestore_client .batch ()
139
+ print (datetime .now ())
122
140
idx = 0
123
141
124
142
batch .commit ()
125
- print ("Data inserted into Firestore successfully." )
143
+ print ("Data insert finsihed successfully." )
126
144
127
145
# Get command-line arguments
128
146
start_date = sys .argv [1 ] if len (sys .argv ) > 1 else None
0 commit comments