Skip to content

Commit

Permalink
refactor: cli telemetry refinement (#9594)
Browse files Browse the repository at this point in the history
* feat: cli telemetry improvement

* feat: improve cli telemetry

* test: ut

* feat: revert

* test: ut

* test: ut
  • Loading branch information
jayzhang authored Aug 11, 2023
1 parent 5466052 commit a99162f
Show file tree
Hide file tree
Showing 26 changed files with 190 additions and 160 deletions.
11 changes: 6 additions & 5 deletions packages/cli/src/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export default async function activate(
}
}
}
const core = createFxCore();
const core = getFxCore();
return ok(core);
}

export function createFxCore(): FxCore {
let fxCore: FxCore;
export function getFxCore(): FxCore {
if (fxCore) return fxCore;
const tools: Tools = {
logProvider: CLILogProvider,
tokenProvider: {
Expand All @@ -42,6 +43,6 @@ export function createFxCore(): FxCore {
telemetryReporter: CliTelemetry.reporter,
ui: CLIUserInteraction,
};
const core = new FxCore(tools);
return core;
fxCore = new FxCore(tools);
return fxCore;
}
75 changes: 58 additions & 17 deletions packages/cli/src/commands/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { cloneDeep, pick } from "lodash";
import path from "path";
import * as uuid from "uuid";
import { createFxCore } from "../activate";
import { getFxCore } from "../activate";
import { TextType, colorize } from "../colorize";
import { logger } from "../commonlib/logger";
import Progress from "../console/progress";
Expand All @@ -36,10 +36,12 @@ import {
UnknownOptionError,
} from "../error";
import CliTelemetry from "../telemetry/cliTelemetry";
import { TelemetryProperty } from "../telemetry/cliTelemetryEvents";
import { TelemetryComponentType, TelemetryProperty } from "../telemetry/cliTelemetryEvents";
import UI from "../userInteraction";
import { getSystemInputs } from "../utils";
import { helper } from "./helper";
import { tryDetectCICDPlatform } from "../commonlib/common/cicdPlatformDetector";
import { CliConfigOptions } from "../userSetttings";

class CLIEngine {
isBundledElectronApp(): boolean {
Expand All @@ -60,20 +62,20 @@ class CLIEngine {
const findRes = this.findCommand(rootCmd, args);
const foundCommand = findRes.cmd;
const remainingArgs = findRes.remainingArgs;
debugLogs.push(`find matched command: ${colorize(foundCommand.fullName, TextType.Commands)}`);
debugLogs.push(`matched command: ${colorize(foundCommand.fullName, TextType.Commands)}`);

const context: CLIContext = {
command: foundCommand,
optionValues: {},
globalOptionValues: {},
argumentValues: [],
telemetryProperties: {},
telemetryProperties: {
[TelemetryProperty.CommandName]: foundCommand.fullName,
[TelemetryProperty.Component]: TelemetryComponentType,
[CliConfigOptions.RunFrom]: tryDetectCICDPlatform(),
},
};

if (context.command.telemetry) {
CliTelemetry.sendTelemetryEvent(context.command.telemetry.event);
}

// 2. parse args
const parseRes = this.parseArgs(context, root, remainingArgs, debugLogs);

Expand All @@ -82,6 +84,36 @@ class CLIEngine {
logger.debug(log);
}
}

// load project meta in telemetry properties
if (context.optionValues.projectPath) {
const core = getFxCore();
const res = await core.getProjectMetadata(context.optionValues.projectPath as string);
if (res.isOk()) {
const value = res.value;
context.telemetryProperties[TelemetryProperty.ProjectId] = value.projectId || "";
context.telemetryProperties[TelemetryProperty.SettingsVersion] = value.version || "";
}
}

logger.debug(
`parsed context: ${JSON.stringify(
pick(context, [
"optionValues",
"globalOptionValues",
"argumentValues",
"telemetryProperties",
]),
null,
2
)}`
);

// send start event
if (context.command.telemetry) {
CliTelemetry.sendTelemetryEvent(context.command.telemetry.event, context.telemetryProperties);
}

if (parseRes.isErr()) {
this.processResult(context, parseRes.error);
return;
Expand Down Expand Up @@ -132,7 +164,7 @@ class CLIEngine {
inputs.ignoreEnvInfo = true;
const skipCommands = ["teamsfx new", "teamsfx new sample", "teamsfx upgrade"];
if (!skipCommands.includes(context.command.fullName) && context.optionValues.projectPath) {
const core = createFxCore();
const core = getFxCore();
const res = await core.projectVersionCheck(inputs);
if (res.isErr()) {
throw res.error;
Expand Down Expand Up @@ -273,11 +305,12 @@ class CLIEngine {
// next token is an option, current option value is undefined
} else {
option.value = nextToken;
remainingArgs.shift();
}
}
} else {
// array
const nextToken = remainingArgs.shift();
const nextToken = remainingArgs[0];
if (nextToken) {
const findNextOptionRes = findOption(nextToken);
if (findNextOptionRes?.option) {
Expand All @@ -290,6 +323,7 @@ class CLIEngine {
for (const v of values) {
option.value.push(v);
}
remainingArgs.shift();
}
}
}
Expand Down Expand Up @@ -380,13 +414,20 @@ class CLIEngine {

UI.interactive = context.globalOptionValues.interactive as boolean;

debugLogs.push(
`parsed context: ${JSON.stringify(
pick(context, ["optionValues", "globalOptionValues", "argumentValues"]),
null,
2
)}`
);
// set global option telemetry property
context.telemetryProperties[TelemetryProperty.CommandDebug] =
context.globalOptionValues.debug + "";
context.telemetryProperties[TelemetryProperty.CommandVerbose] =
context.globalOptionValues.verbose + "";
context.telemetryProperties[TelemetryProperty.CommandHelp] =
context.globalOptionValues.help + "";
context.telemetryProperties[TelemetryProperty.CommandInteractive] =
context.globalOptionValues.interactive + "";
context.telemetryProperties[TelemetryProperty.CommandVersion] =
context.globalOptionValues.version + "";
context.telemetryProperties[TelemetryProperty.CorrelationId] =
context.optionValues.correlationId;

return ok(undefined);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/addSPFxWebpart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand } from "@microsoft/teamsfx-api";
import { SPFxAddWebpartInputs, SPFxAddWebpartOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { ProjectFolderOption } from "../common";

Expand All @@ -15,7 +15,7 @@ export const addSPFxWebpartCommand: CLICommand = {
},
handler: async (ctx) => {
const inputs = ctx.optionValues as SPFxAddWebpartInputs;
const core = createFxCore();
const core = getFxCore();
const res = await core.addWebpart(inputs);
return res;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CreateProjectInputs, CreateProjectOptions } from "@microsoft/teamsfx-co
import chalk from "chalk";
import { assign } from "lodash";
import * as uuid from "uuid";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { logger } from "../../commonlib/logger";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/cliTelemetryEvents";
import { createSampleCommand } from "./createSample";
Expand All @@ -31,7 +31,7 @@ export const createCommand: CLICommand = {
handler: async (ctx: CLIContext) => {
const inputs = ctx.optionValues as CreateProjectInputs;
inputs.projectId = inputs.projectId ?? uuid.v4();
const core = createFxCore();
const core = getFxCore();
const res = await core.createProject(inputs);
assign(ctx.telemetryProperties, {
[TelemetryProperty.NewProjectId]: inputs.projectId,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/createSample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import chalk from "chalk";
import { assign } from "lodash";
import * as uuid from "uuid";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { logger } from "../../commonlib/logger";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/cliTelemetryEvents";

Expand All @@ -25,7 +25,7 @@ export const createSampleCommand: CLICommand = {
handler: async (ctx: CLIContext) => {
const inputs = ctx.optionValues as CreateSampleProjectInputs;
inputs.projectId = inputs.projectId ?? uuid.v4();
const core = createFxCore();
const core = getFxCore();
const res = await core.createSampleProject(inputs);
assign(ctx.telemetryProperties, {
[TelemetryProperty.NewProjectId]: inputs.projectId,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { CLICommand, CLIContext, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { strings } from "../../resource";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { EnvOption, ProjectFolderOption } from "../common";
Expand All @@ -14,7 +14,7 @@ export const deployCommand: CLICommand = {
event: TelemetryEvent.Deploy,
},
handler: async (ctx: CLIContext) => {
const core = createFxCore();
const core = getFxCore();
const inputs = ctx.optionValues as InputsWithProjectPath;
const res = await core.deployArtifacts(inputs);
return res;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/envAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, InputsWithProjectPath, err } from "@microsoft/teamsfx-api";
import { CreateEnvArguments, CreateEnvInputs, CreateEnvOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { WorkspaceNotSupported } from "../../cmds/preview/errors";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { isWorkspaceSupported } from "../../utils";
Expand All @@ -21,7 +21,7 @@ export const envAddCommand: CLICommand = {
if (!isWorkspaceSupported(inputs.projectPath)) {
return err(WorkspaceNotSupported(inputs.projectPath));
}
const core = createFxCore();
const core = getFxCore();
const result = await core.createEnv(inputs);
return result;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, CLIContext, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { SelectTeamsManifestInputs, SelectTeamsManifestOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { EnvOption, ProjectFolderOption } from "../common";

Expand Down Expand Up @@ -32,7 +32,7 @@ export const packageCommand: CLICommand = {
event: TelemetryEvent.Build,
},
handler: async (ctx: CLIContext) => {
const core = createFxCore();
const core = getFxCore();
const inputs = ctx.optionValues as SelectTeamsManifestInputs & InputsWithProjectPath;
const res = await core.createAppPackage(inputs);
return res;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/permissionGrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, InputsWithProjectPath, err, ok } from "@microsoft/teamsfx-api";
import { PermissionGrantInputs, PermissionGrantOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { azureMessage, spfxMessage } from "../../cmds/permission";
import { logger } from "../../commonlib/logger";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
Expand All @@ -29,7 +29,7 @@ export const permissionGrantCommand: CLICommand = {
logger.info(azureMessage);
logger.info(spfxMessage);
// setAppTypeInputs(inputs);// app type input is unused in FxCore
const core = createFxCore();
const core = getFxCore();
const result = await core.grantPermission(inputs);
if (result.isErr()) {
return err(result.error);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/permissionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, InputsWithProjectPath, err, ok } from "@microsoft/teamsfx-api";
import { PermissionListInputs, PermissionListOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { azureMessage, spfxMessage } from "../../cmds/permission";
import { logger } from "../../commonlib/logger";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
Expand All @@ -28,7 +28,7 @@ export const permissionStatusCommand: CLICommand = {
handler: async (ctx) => {
const inputs = ctx.optionValues as PermissionListInputs & InputsWithProjectPath;
const listAll = inputs.all || false;
const core = createFxCore();
const core = getFxCore();
// print necessary messages
logger.info(azureMessage);
logger.info(spfxMessage);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/provision.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { CLICommand, CLIContext, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { strings } from "../../resource";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { EnvOption, ProjectFolderOption } from "../common";
Expand All @@ -14,7 +14,7 @@ export const provisionCommand: CLICommand = {
event: TelemetryEvent.Provision,
},
handler: async (ctx: CLIContext) => {
const core = createFxCore();
const core = getFxCore();
const inputs = ctx.optionValues as InputsWithProjectPath;
const res = await core.provisionResources(inputs);
return res;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, CLIContext, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { assign } from "lodash";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { strings } from "../../resource";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { EnvOption, ProjectFolderOption } from "../common";
Expand All @@ -16,7 +16,7 @@ export const publishCommand: CLICommand = {
},
handler: async (ctx: CLIContext) => {
const inputs = ctx.optionValues as InputsWithProjectPath;
const core = createFxCore();
const core = getFxCore();
if (!ctx.globalOptionValues.interactive) {
assign(inputs, ctx.optionValues);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/updateAadApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { DeployAadManifestInputs, DeployAadManifestOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { ProjectFolderOption } from "../common";

Expand All @@ -16,7 +16,7 @@ export const updateAadAppCommand: CLICommand = {
handler: async (ctx) => {
const inputs = ctx.optionValues as DeployAadManifestInputs & InputsWithProjectPath;
inputs.ignoreEnvInfo = false;
const core = createFxCore();
const core = getFxCore();
const res = await core.deployAadManifest(inputs);
return res;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/models/updateTeamsApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import { CLICommand, InputsWithProjectPath } from "@microsoft/teamsfx-api";
import { SelectTeamsManifestOptions } from "@microsoft/teamsfx-core";
import { createFxCore } from "../../activate";
import { getFxCore } from "../../activate";
import { TelemetryEvent } from "../../telemetry/cliTelemetryEvents";
import { EnvOption, ProjectFolderOption } from "../common";

Expand All @@ -15,7 +15,7 @@ export const updateTeamsAppCommand: CLICommand = {
},
handler: async (ctx) => {
const inputs = ctx.optionValues as InputsWithProjectPath;
const core = createFxCore();
const core = getFxCore();
const res = await core.deployTeamsManifest(inputs);
return res;
},
Expand Down
Loading

0 comments on commit a99162f

Please sign in to comment.