Skip to content

Commit

Permalink
fixes on migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
maceto committed Oct 25, 2023
1 parent eea6013 commit edd3dba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ terraform.rc
__pycache__
.pytest_cache

utils.txt
utils.txt
logs
4 changes: 2 additions & 2 deletions scripts/script_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def execute_query_and_insert_result(start_date, end_date):
))) AS adoption
FROM
`httparchive.core_web_vitals.technologies`
WHERE
"""

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

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

Expand Down
5 changes: 3 additions & 2 deletions scripts/script_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ def execute_query_and_insert_result(start_date, end_date):

# Create a new Firestore document for each result and insert it into the "technologies" collection
collection_ref = firestore_client.collection('categories')
print(results)

print("Data inserted started.")
for row in results:

item = dict(row.items())

print(item)
#print(item)

doc_ref = collection_ref.document()
doc_ref.set(item)
Expand Down
10 changes: 6 additions & 4 deletions scripts/script_core_web_vitals.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ def execute_query_and_insert_result(start_date, end_date):
))) AS vitals
FROM
`httparchive.core_web_vitals.technologies`
WHERE
"""

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

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

Expand All @@ -110,13 +110,15 @@ def execute_query_and_insert_result(start_date, end_date):

# Create a new Firestore document for each result and insert it into the "technologies" collection
collection_ref = firestore_client.collection('core_web_vitals')
print(results)
#print(results)

print("Data inserted started.")
for row in results:

item = dict(row.items())
item['date'] = str(row['date'])

print(item)
#print(item)

doc_ref = collection_ref.document()
doc_ref.set(item)
Expand Down
26 changes: 19 additions & 7 deletions scripts/script_lighthouse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import uuid
from google.cloud import bigquery
from google.cloud import firestore
from decimal import Decimal
Expand Down Expand Up @@ -97,21 +98,32 @@ def execute_query_and_insert_result(start_date, end_date):
query_job = bq_client.query(query)
results = query_job.result()

# Create a new Firestore document for each result and insert it into the "technologies" collection
collection_ref = firestore_client.collection('lighthouse')
print(results)
for row in results:

idx = 0

print("Data insert process started.")

batch = collection_ref.batch()
for row in results:
# Convert date
#
item = dict(row.items())
item['date'] = str(row['date'])

item = convert_decimal_to_float(item)

print(item)
record_ref = collection_ref.document(uuid.uuid4().hex)
batch.set(record_ref, row)
idx += 1

doc_ref = collection_ref.document()
doc_ref.set(item)
# Commit the batch at every 500th record.
if idx == 499:
batch.commit()
# Start a new batch for the next iteration.
batch = collection_ref.batch()
idx = 0

batch.commit()
print("Data inserted into Firestore successfully.")

# Get command-line arguments
Expand Down

0 comments on commit edd3dba

Please sign in to comment.