Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: report closing wizard manually #823

Merged
merged 8 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions packages/backend/src/panels/AbstractWebviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getClassLogger } from "../logger/logger-wrapper";
import { RpcExtension } from "@sap-devx/webview-rpc/out.ext/rpc-extension";
import { createFlowPromise, FlowPromise } from "../utils/promise";
import * as cheerio from "cheerio";
import { AnalyticsWrapper } from "../usage-report/usage-analytics-wrapper";
import { get } from "lodash";
import { Constants } from "../utils/constants";

export abstract class AbstractWebviewPanel {
public viewType: string;
Expand Down Expand Up @@ -105,6 +108,24 @@ export abstract class AbstractWebviewPanel {

protected dispose() {
this.setFocused(false);
const isGeneratorCompleted = get(this.webViewPanel, Constants.GENERATOR_COMPLETED);
const yeomanui: any = get(this, "yeomanui");
// Verify the dispose happened before the user has finished the wizard and not by clicking "Finish".
// When user clicks "Finish" the generated has ended and the success/failure result will be set in "GENERATOR_COMPLETED".
// If it has not been ended, "GENERATOR_COMPLETED" will be undefined.
if (yeomanui && isGeneratorCompleted === undefined) {
const promptItems: any = get(yeomanui, "gen.prompts.items") ?? [];
const currentPromptCount = yeomanui.promptCount;
const numOfPromopts = promptItems.length;
const wizardStepName = promptItems[currentPromptCount - 1].name;
AnalyticsWrapper.updateGeneratorClosedManually(
yeomanui.generatorName ?? "",
wizardStepName,
currentPromptCount,
numOfPromopts,
this.logger,
);
}

// Clean up our resources
this.webViewPanel.dispose();
Expand Down
22 changes: 22 additions & 0 deletions packages/backend/src/usage-report/usage-analytics-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AnalyticsWrapper {
private static readonly EVENT_TYPES = {
PROJECT_GENERATION_STARTED: "Project generation started",
PROJECT_GENERATOR_SELECTED: "Project generator selected",
PROJECT_GENERATOR_CLOSED: "Project generator was closed manually",
PROJECT_GENERATED_SUCCESSFULLY: "Project generated successfully",
};

Expand Down Expand Up @@ -87,4 +88,25 @@ export class AnalyticsWrapper {
logger?.error(error);
}
}

public static updateGeneratorClosedManually(
generatorName: string,
wizardStepName: string,
currentStep: number,
totalNumOfSteps: number,
logger?: IChildLogger,
): void {
try {
const eventName = AnalyticsWrapper.EVENT_TYPES.PROJECT_GENERATOR_CLOSED;
const properties: any = {
generatorName,
wizardStepName,
currentStep,
totalNumOfSteps,
};
AnalyticsWrapper.report({ eventName, properties, logger });
} catch (error) {
logger?.error(error);
}
}
}
1 change: 1 addition & 0 deletions packages/backend/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { devspace } from "@sap/bas-sdk";
class ConstantsUtil {
public IS_IN_BAS = !isEmpty(get(process, "env.WS_BASE_URL")) || devspace.getBasMode() === "personal-edition";
public HOMEDIR_PROJECTS: string = join(homedir(), "projects");
public GENERATOR_COMPLETED: string = "generatorCompleted";
}

export const Constants = new ConstantsUtil();
4 changes: 3 additions & 1 deletion packages/backend/src/vscode-youi-events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { isEmpty, size, isNil } from "lodash";
import { isEmpty, size, isNil, set } from "lodash";
import { YouiEvents } from "./youi-events";
import { IRpc } from "@sap-devx/webview-rpc/out.ext/rpc-common";
import { GeneratorOutput } from "./vscode-output";
Expand All @@ -8,6 +8,7 @@ import { getClassLogger } from "./logger/logger-wrapper";
import { getImage } from "./images/messageImages";
import { AppWizard, MessageType, Severity } from "@sap-devx/yeoman-ui-types";
import { WorkspaceFile } from "./utils/workspaceFile";
import { Constants } from "./utils/constants";

class YoUiAppWizard extends AppWizard {
constructor(private readonly events: VSCodeYouiEvents) {
Expand Down Expand Up @@ -65,6 +66,7 @@ export class VSCodeYouiEvents implements YouiEvents {
type: string,
targetFolderPath?: string,
): void {
set(this.webviewPanel, Constants.GENERATOR_COMPLETED, success);
this.doClose();
void this.showDoneMessage(success, message, selectedWorkspace, type, targetFolderPath);
}
Expand Down
51 changes: 51 additions & 0 deletions packages/backend/test/panels/YeomanUIPanel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { expect } from "chai";
import { join } from "path";
import { homedir } from "os";
import messages from "../../src/messages";
import { AnalyticsWrapper } from "../../src/usage-report/usage-analytics-wrapper";

describe("YeomanUIPanel unit test", () => {
let sandbox: SinonSandbox;
Expand All @@ -22,6 +23,7 @@ describe("YeomanUIPanel unit test", () => {
let panel: YeomanUIPanel.YeomanUIPanel;
let setWebviewPanelStub: SinonStub;
let createWebviewPanelStub: SinonStub;
let trackerWrapperMock: SinonMock;

before(() => {
sandbox = createSandbox();
Expand All @@ -41,6 +43,7 @@ describe("YeomanUIPanel unit test", () => {
panel = new YeomanUIPanel.YeomanUIPanel(vscode.context);
setWebviewPanelStub = sandbox.stub(panel, "setWebviewPanel");
createWebviewPanelStub = sandbox.stub(panel, "createWebviewPanel");
trackerWrapperMock = sandbox.mock(AnalyticsWrapper);
});

afterEach(() => {
Expand All @@ -49,6 +52,7 @@ describe("YeomanUIPanel unit test", () => {
npmUtilsMock.verify();
windowMock.verify();
commandsMock.verify();
trackerWrapperMock.verify();
setWebviewPanelStub.restore();
createWebviewPanelStub.restore();
});
Expand Down Expand Up @@ -269,4 +273,51 @@ describe("YeomanUIPanel unit test", () => {
expect(await panel["showOpenDialog"](required, canSelectFiles)).to.equal(required);
});
});

describe("dispose", () => {
const objYeomanui: any = {
generatorName: "generator-name",
promptCount: 1,
gen: {
prompts: {
items: [
{
name: "step1",
},
{
name: "step2",
},
],
},
},
};

const webviewPanel = {
dispose: () => {},
};

beforeEach(() => {
set(panel, "yeomanui", objYeomanui);
set(panel, "webViewPanel", webviewPanel);
set(panel, "disposables", []);
set(panel, "cleanFlowPromise", () => {});

commandsMock.expects("executeCommand").withExactArgs("setContext", "yeomanUI.Focused", false).resolves();
});

it("dispose - calling usage analytics when panel is manually closed.", () => {
trackerWrapperMock
.expects("updateGeneratorClosedManually")
.withArgs("generator-name", "step1", 1, 2)
.once()
.resolves();
panel["dispose"]();
});

it("dispose - not calling usage analytics when generation ended and user clicked finish.", () => {
set(webviewPanel, Constants.GENERATOR_COMPLETED, true);
trackerWrapperMock.expects("updateGeneratorClosedManually").never().resolves();
panel["dispose"]();
});
});
});
Loading