|
| 1 | +import click |
| 2 | +from datetime import datetime |
| 3 | +from google.cloud import bigquery |
| 4 | + |
| 5 | + |
| 6 | +@click.command() |
| 7 | +@click.option("--bq_project_id", help="GCP BigQuery project id", show_default=True, default="moz-fx-fxa-prod") |
| 8 | +def main(bq_project_id): |
| 9 | + |
| 10 | + query = """ |
| 11 | + SELECT 'account_customers' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_account_customers_v1` |
| 12 | + UNION ALL |
| 13 | + SELECT 'account_groups' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_account_groups_v1` |
| 14 | + UNION ALL |
| 15 | + SELECT 'account_reset_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_account_reset_tokens_v1` |
| 16 | + UNION ALL |
| 17 | + SELECT 'accounts' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_accounts_v1` |
| 18 | + UNION ALL |
| 19 | + SELECT 'carts' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_carts_v1` |
| 20 | + UNION ALL |
| 21 | + SELECT 'device_commands' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_device_commands_v1` |
| 22 | + UNION ALL |
| 23 | + SELECT 'devices' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_devices_v1` |
| 24 | + UNION ALL |
| 25 | + SELECT 'email_bounces' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_email_bounces_v1` |
| 26 | + UNION ALL |
| 27 | + SELECT 'emails' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_emails_v1` |
| 28 | + UNION ALL |
| 29 | + SELECT 'linked_accounts' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_linked_accounts_v1` |
| 30 | + UNION ALL |
| 31 | + SELECT 'oauth_codes' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_oauth_codes_v1` |
| 32 | + UNION ALL |
| 33 | + SELECT 'oauth_refresh_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_oauth_refresh_tokens_v1` |
| 34 | + UNION ALL |
| 35 | + SELECT 'oauth_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_oauth_tokens_v1` |
| 36 | + UNION ALL |
| 37 | + SELECT 'password_change_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_password_change_tokens_v1` |
| 38 | + UNION ALL |
| 39 | + SELECT 'password_forgot_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_password_forgot_tokens_v1` |
| 40 | + UNION ALL |
| 41 | + SELECT 'paypal_customers' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_paypal_customers_v1` |
| 42 | + UNION ALL |
| 43 | + SELECT 'recovery_codes' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_recovery_codes_v1` |
| 44 | + UNION ALL |
| 45 | + SELECT 'security_events' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_security_events_v1` |
| 46 | + UNION ALL |
| 47 | + SELECT 'sent_emails' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_sent_emails_v1` |
| 48 | + UNION ALL |
| 49 | + SELECT 'session_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_session_tokens_v1` |
| 50 | + UNION ALL |
| 51 | + SELECT 'signin_codes' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_signin_codes_v1` |
| 52 | + UNION ALL |
| 53 | + SELECT 'totp' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_totp_v1` |
| 54 | + UNION ALL |
| 55 | + SELECT 'unblock_codes' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_unblock_codes_v1` |
| 56 | + UNION ALL |
| 57 | + SELECT 'unverified_tokens' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_unverified_tokens_v1` |
| 58 | + UNION ALL |
| 59 | + SELECT 'verification_reminders' AS table_name, COUNT(*) AS total_rows FROM `moz-fx-data-shared-prod.accounts_db_external.fxa_verification_reminders_v1` |
| 60 | + UNION ALL |
| 61 | + (SELECT |
| 62 | + "accounts_with_secondary_emails" AS table_name, |
| 63 | + COUNT(DISTINCT accounts.uid) AS total_rows |
| 64 | + FROM |
| 65 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_accounts_v1` accounts |
| 66 | + JOIN |
| 67 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_emails_v1` emails |
| 68 | + ON |
| 69 | + accounts.uid = emails.uid |
| 70 | + WHERE |
| 71 | + emails.isPrimary = FALSE |
| 72 | + ) |
| 73 | + UNION ALL |
| 74 | + (SELECT |
| 75 | + "accounts_with_unverified_emails" AS table_name, |
| 76 | + COUNT(DISTINCT accounts.uid) AS total_rows |
| 77 | + FROM |
| 78 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_accounts_v1` accounts |
| 79 | + JOIN |
| 80 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_emails_v1` emails |
| 81 | + ON |
| 82 | + accounts.uid = emails.uid |
| 83 | + WHERE |
| 84 | + emails.isVerified = FALSE |
| 85 | + ) |
| 86 | + UNION ALL |
| 87 | + ( |
| 88 | + SELECT |
| 89 | + "accounts_linked_to_google" AS table_name, |
| 90 | + COUNT(uid) AS total_rows |
| 91 | + FROM |
| 92 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_linked_accounts_v1` |
| 93 | + WHERE |
| 94 | + providerId=1 -- see LinkedAccountProviderIds at https://github.com/mozilla/fxa/blob/main/packages/fxa-settings/src/lib/types.ts |
| 95 | + ) |
| 96 | + UNION ALL |
| 97 | + ( |
| 98 | + SELECT |
| 99 | + "accounts_linked_to_apple" AS table_name, |
| 100 | + COUNT(uid) AS total_rows |
| 101 | + FROM |
| 102 | + `moz-fx-data-shared-prod.accounts_db_external.fxa_linked_accounts_v1` |
| 103 | + WHERE |
| 104 | + providerId=2 -- see LinkedAccountProviderIds at https://github.com/mozilla/fxa/blob/main/packages/fxa-settings/src/lib/types.ts |
| 105 | + ) |
| 106 | + """ |
| 107 | + |
| 108 | + client = bigquery.Client(project=bq_project_id) |
| 109 | + query_job = client.query(query) |
| 110 | + |
| 111 | + print("Running query:", query_job.job_id) |
| 112 | + |
| 113 | + results = query_job.result() |
| 114 | + |
| 115 | + rows_to_insert = [ |
| 116 | + { |
| 117 | + "date": datetime.now().strftime("%Y-%m-%d"), |
| 118 | + "name": row["table_name"], |
| 119 | + "count": row["total_rows"], |
| 120 | + } |
| 121 | + for row in results |
| 122 | + ] |
| 123 | + |
| 124 | + # Insert rows into the new table |
| 125 | + errors = client.insert_rows_json("mozdata.analysis.wclouser_fxa_health_db_counts", rows_to_insert) |
| 126 | + |
| 127 | + # Check for errors during insertion |
| 128 | + if errors: |
| 129 | + print("Errors occurred while inserting rows: ", errors) |
| 130 | + else: |
| 131 | + print("Data inserted successfully.") |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments