Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/dataconnect/provisionCloudSql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@
`Found existing Cloud SQL instance ${clc.bold(instanceId)}.`,
);
stats.databaseVersion = existingInstance.databaseVersion;
stats.dataconnectLabel = existingInstance.settings?.userLabels?.["firebase-data-connect"] as
| cloudSqlAdminClient.DataConnectLabel
| undefined;
stats.dataconnectLabel =
(existingInstance.settings?.userLabels?.[
"firebase-data-connect"
] as cloudSqlAdminClient.DataConnectLabel) || "absent";

const why = getUpdateReason(existingInstance, requireGoogleMlIntegration);
if (why) {
Expand Down Expand Up @@ -107,8 +108,8 @@
}
}
await upsertDatabase({ ...args });
} catch (err: any) {

Check warning on line 111 in src/dataconnect/provisionCloudSql.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err.status !== 404) {

Check warning on line 112 in src/dataconnect/provisionCloudSql.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
throw err;
}
// Cloud SQL instance is not found, start its creation.
Expand Down Expand Up @@ -181,11 +182,11 @@
try {
await cloudSqlAdminClient.getDatabase(projectId, instanceId, databaseId);
utils.logLabeledBullet("dataconnect", `Found existing Postgres Database ${databaseId}.`);
} catch (err: any) {

Check warning on line 185 in src/dataconnect/provisionCloudSql.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err.status !== 404) {

Check warning on line 186 in src/dataconnect/provisionCloudSql.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .status on an `any` value
// Skip it if the database is not accessible.
// Possible that the CSQL instance is in the middle of something.
logger.debug(`Unexpected error from Cloud SQL: ${err}`);

Check warning on line 189 in src/dataconnect/provisionCloudSql.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
utils.logLabeledWarning("dataconnect", `Postgres Database ${databaseId} is not accessible.`);
return;
}
Expand Down
27 changes: 17 additions & 10 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

// askQuestions prompts the user about the Data Connect service they want to init. Any prompting
// logic should live here, and _no_ actuation logic should live here.
export async function askQuestions(setup: Setup): Promise<void> {

Check warning on line 100 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const info: RequiredInfo = {
analyticsFlow: "cli",
appDescription: "",
Expand Down Expand Up @@ -150,10 +150,10 @@

// actuate writes product specific files and makes product specifc API calls.
// It does not handle writing firebase.json and .firebaserc
export async function actuate(setup: Setup, config: Config, options: any): Promise<void> {

Check warning on line 153 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 153 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
// Most users will want to persist data between emulator runs, so set this to a reasonable default.
const dir: string = config.get("dataconnect.source", "dataconnect");

Check warning on line 155 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`);

Check warning on line 156 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
config.set("emulators.dataconnect.dataDir", dataDir);

const info = setup.featureInfo?.dataconnect;
Expand All @@ -166,20 +166,27 @@
info.locationId = info.locationId || FDC_DEFAULT_REGION;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;

const startTime = Date.now();
try {
await actuateWithInfo(setup, config, info, options);
await sdk.actuate(setup, config);
} finally {
void trackGA4("dataconnect_init", {
flow: info.analyticsFlow,
project_status: setup.projectId
? setup.isBillingEnabled
? info.shouldProvisionCSQL
? "blaze_provisioned_csql"
: "blaze"
: "spark"
: "missing",
});
const sdkInfo = setup.featureInfo?.dataconnectSdk;
void trackGA4(
"dataconnect_init",
{
flow: info.analyticsFlow,
project_status: setup.projectId
? setup.isBillingEnabled
? info.shouldProvisionCSQL
? "blaze_provisioned_csql"
: "blaze"
: "spark"
: "missing",
...(sdkInfo ? sdk.initAppCounters(sdkInfo) : {}),
},
Date.now() - startTime,
);
}

if (info.appDescription) {
Expand Down
56 changes: 44 additions & 12 deletions src/init/features/dataconnect/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,60 @@ export async function chooseApp(): Promise<App[]> {
}

export async function actuate(setup: Setup, config: Config) {
const fdcInfo = setup.featureInfo?.dataconnect;
const sdkInfo = setup.featureInfo?.dataconnectSdk;
if (!sdkInfo) {
throw new Error("Data Connect SDK feature RequiredInfo is not provided");
}
const startTime = Date.now();
try {
await actuateWithInfo(setup, config, sdkInfo);
} finally {
let flow = "no_app";
if (sdkInfo.apps.length) {
const platforms = sdkInfo.apps.map((a) => a.platform.toLowerCase()).sort();
flow = `${platforms.join("_")}_app`;
// If `firebase init dataconnect:sdk` is run alone, emit GA stats.
// Otherwise, `firebase init dataconnect` will emit those stats.
const fdcInfo = setup.featureInfo?.dataconnect;
if (!fdcInfo) {
void trackGA4(
"dataconnect_init",
{
flow: "cli_sdk",
project_status: setup.projectId
? setup.isBillingEnabled
? "blaze"
: "spark"
: "missing",
...initAppCounters(sdkInfo),
},
Date.now() - startTime,
);
}
if (fdcInfo) {
fdcInfo.analyticsFlow += `_${flow}`;
} else {
void trackGA4("dataconnect_init", {
project_status: setup.projectId ? (setup.isBillingEnabled ? "blaze" : "spark") : "missing",
flow: `cli_sdk_${flow}`,
});
}
}

export function initAppCounters(info: SdkRequiredInfo): { [key: string]: number } {
const counts = {
num_web_apps: 0,
num_android_apps: 0,
num_ios_apps: 0,
num_flutter_apps: 0,
};

for (const app of info.apps ?? []) {
switch (app.platform) {
case Platform.WEB:
counts.num_web_apps++;
break;
case Platform.ANDROID:
counts.num_android_apps++;
break;
case Platform.IOS:
counts.num_ios_apps++;
break;
case Platform.FLUTTER:
counts.num_flutter_apps++;
break;
}
}
return counts;
}

async function actuateWithInfo(setup: Setup, config: Config, info: SdkRequiredInfo) {
Expand Down
Loading