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

fix: stream app update W-10942900 #106

Merged
merged 3 commits into from
Apr 25, 2022
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
12 changes: 11 additions & 1 deletion command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@
{
"command": "analytics:app:update",
"plugin": "@salesforce/analytics",
"flags": ["apiversion", "folderid", "json", "loglevel", "targetusername", "templateid"]
"flags": [
"allevents",
"apiversion",
"async",
"folderid",
"json",
"loglevel",
"targetusername",
"templateid",
"wait"
]
},
{
"command": "analytics:asset:publisher:create",
Expand Down
3 changes: 3 additions & 0 deletions messages/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
"applogFlagLongDescription": "Specify to include app creation log details.",
"appCreateAsyncDescription": "create app asynchronously",
"appCreateAsyncLongDescription": "Create app asynchronously.",
"appUpdateAsyncDescription": "update app asynchronously",
"appUpdateAsyncLongDescription": "Update app asynchronously.",
"appCreateAllEventsDescription": "verbose display of all app create events",
"appCreateAllEventsLongDescription": "Verbose display of all app create events.",
"appDefinitionFileFlagDescription": "Tableau CRM template definition file; required unless --templateid is specified",
"appDefinitionFileFlagLongDescription": "Tableau CRM template definition file; required unless a --templateid value is specified.",
"appsFound": "Found [%s] Tableau CRM apps.",
"startAppCreation": "Successfully started the creation process for Tableau CRM app [%s].",
"startAppUpdate": "Successfully started the update process for Tableau CRM app [%s].",
"appCreateEvent": "%s %s %s, 0 failed",
"verboseAppCreateEventSuccess": "%s %s %s of %s [%s]",
"appCreateEventFail": "%s %s %s of %s [%s] with message [%s]",
Expand Down
232 changes: 15 additions & 217 deletions src/commands/analytics/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@
import { promises as fs } from 'fs';
import { EOL } from 'os';
import { flags, SfdxCommand } from '@salesforce/command';
import { Messages, Org, SfdxError, StatusResult, StreamingClient } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { JsonMap } from '@salesforce/ts-types';
import chalk from 'chalk';
import { Messages, Org, SfdxError } from '@salesforce/core';
import Folder, { CreateAppBody } from '../../../lib/analytics/app/folder';
import { DEF_APP_CREATE_TIMEOUT } from '../../../lib/analytics/constants';
import WaveAssetEvent from '../../../lib/analytics/event/waveAssetEvent';
import AppStreaming from '../../../lib/analytics/event/appStreaming';
import { DEF_APP_CREATE_UPDATE_TIMEOUT } from '../../../lib/analytics/constants';
import WaveTemplate from '../../../lib/analytics/template/wavetemplate';
import { throwWithData } from '../../../lib/analytics/utils';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/analytics', 'app');

type StreamingResult = {
EventType: string;
Status: string;
Index: number;
Total: number;
ItemLabel: string;
Message: string;
};

export default class Create extends SfdxCommand {
public static description = messages.getMessage('createCommandDescription');
public static longDescription = messages.getMessage('createCommandLongDescription');
Expand Down Expand Up @@ -77,30 +64,35 @@ export default class Create extends SfdxCommand {
wait: flags.number({
char: 'w',
description: messages.getMessage('streamingWaitDescription'),
longDescription: messages.getMessage('streamingWaitLongDescription', [DEF_APP_CREATE_TIMEOUT]),
longDescription: messages.getMessage('streamingWaitLongDescription', [DEF_APP_CREATE_UPDATE_TIMEOUT]),
min: 0,
default: DEF_APP_CREATE_TIMEOUT
default: DEF_APP_CREATE_UPDATE_TIMEOUT
})
};

protected static requiresUsername = true;
protected static requiresProject = false;

public streamingResults = [] as StreamingResult[];

public async run() {
const folder = new Folder(this.org as Org);
const body = await this.generateCreateAppBody();

const appStreaming = new AppStreaming(
this.org as Org,
this.flags.allEvents as boolean,
this.flags.wait as number,
this.ux
);

// if they're not creating from a template (i.e. an empty app), then don't listen for events since there won't be
// any and this will just hang until the timeout
if (this.flags.async || this.flags.wait <= 0 || !body.templateSourceId) {
const waveAppId = await this.createApp(folder, body);
const waveAppId = await appStreaming.createApp(folder, body);
this.ux.log(messages.getMessage('createAppSuccessAsync', [waveAppId]));
return { id: waveAppId };
} else {
const waveAppId = await this.streamEvent(folder, body);
return { id: waveAppId, events: this.streamingResults };
const waveAppId = await appStreaming.streamCreateEvent(folder, body);
return { id: waveAppId, events: appStreaming.getStreamingResults() };
}
}

Expand Down Expand Up @@ -158,198 +150,4 @@ export default class Create extends SfdxCommand {
);
}
}

private async streamEvent(folder: Folder, body: CreateAppBody) {
let folderId: string | undefined;
const options = new StreamingClient.DefaultOptions(this.org as Org, '/event/WaveAssetEvent', message =>
this.streamProcessor(folderId, message)
);
const timeout: Duration = Duration.minutes(this.flags.wait as number);
options.setHandshakeTimeout(timeout);
options.setSubscribeTimeout(timeout);
const asyncStatusClient: StreamingClient = await StreamingClient.create(options);
await asyncStatusClient.handshake();
// Stream all WaveAssetEvents events for this org
asyncStatusClient.replay(-1);
try {
await asyncStatusClient.subscribe(async () => {
folderId = await this.createApp(folder, body);
});
} catch (error) {
if ((error as Record<string, unknown>).code === 'genericTimeoutMessage') {
// include the app id in the error if we timeout waiting, since the app got created
throwWithData(messages.getMessage('timeoutMessage', [(error as Record<string, unknown>).message as string]), {
id: folderId
});
}
throw error;
}
return folderId;
}

private async createApp(folder: Folder, body: CreateAppBody) {
const waveAppId = await folder.create(body);
this.ux.styledHeader(messages.getMessage('startAppCreation', [waveAppId]));
return waveAppId;
}

private streamProcessor(folderId: string | undefined, event: JsonMap): StatusResult {
const waveAssetEvent = new WaveAssetEvent(event);

// Only handle events for the newly created folder id and ignore for tests
if (folderId !== waveAssetEvent.folderId && waveAssetEvent.folderId !== 'test') {
return { completed: false };
}

switch (waveAssetEvent.eventType) {
case 'ExternalData': {
this.logEvent('External Data', 'created', waveAssetEvent);
break;
}
case 'Dataset': {
this.logEvent('Datasets', 'created', waveAssetEvent);
break;
}
case 'Lens': {
this.logEvent('Lenses', 'created', waveAssetEvent);
break;
}
case 'Dashboard': {
this.logEvent('Dashboards', 'created', waveAssetEvent);
break;
}
case 'Dataflow': {
this.logEvent('Dataflows', 'created', waveAssetEvent);
break;
}
case 'Recipe': {
this.logEvent('Recipes', 'created', waveAssetEvent);
break;
}
case 'ExecutionPlan': {
this.logEvent('Execution Plan', 'created', waveAssetEvent);
break;
}
case 'UserDataflowInstance': {
this.logEvent('User Dataflow Instructions', 'executed', waveAssetEvent);
break;
}
case 'ExtendedType': {
this.logEvent('Template Extensions', 'created', waveAssetEvent);
break;
}
case 'SystemDataflowInstance': {
this.logEvent('System Dataflow Instructions', 'executed', waveAssetEvent);
break;
}
case 'ReplicationDataflowInstance': {
this.logEvent('Replication Dataflow Instructions', 'executed', waveAssetEvent);
break;
}
case 'UserXmd': {
this.logEvent('User XMDs', 'created', waveAssetEvent);
break;
}
case 'AssetPruning': {
this.logEvent('Asset', 'deleted', waveAssetEvent);
break;
}
case 'Image': {
this.logEvent('Images', 'created', waveAssetEvent);
break;
}
case 'Application': {
this.createEventJson(waveAssetEvent);
if (waveAssetEvent.status === 'Success') {
this.ux.log(this.getStreamResultMark(true), messages.getMessage('finishAppCreation', [waveAssetEvent.label]));
} else {
// failed or cancelled
this.ux.log(
this.getStreamResultMark(false),
messages.getMessage('finishAppCreationFailure', [waveAssetEvent.message])
);
throwWithData(messages.getMessage('finishAppCreationFailure', [waveAssetEvent.message]), {
id: folderId,
events: this.streamingResults
});
}
return { completed: true };
}
}
return { completed: false };
}

private getStreamResultMark(success: boolean) {
if (process.platform === 'win32') {
return '';
}
if (success) {
return chalk.green('✔');
}
return chalk.red('✖');
}

private logEvent(eventDisplayName: string, action: string, event: WaveAssetEvent) {
if (this.flags.allevents && event.total > 0) {
this.createEventJson(event);
if (event.status === 'Success') {
this.ux.log(
this.getStreamResultMark(true),
messages.getMessage('verboseAppCreateEventSuccess', [
messages.getMessage('appCreateSuccessfulLabel'),
eventDisplayName,
event.index,
event.total,
event.label
])
);
} else {
this.ux.log(
this.getStreamResultMark(false),
messages.getMessage('appCreateEventFail', [
messages.getMessage('appCreateEventFailureLabel'),
eventDisplayName,
event.index,
event.total,
event.label,
event.message
])
);
}
} else {
if (event.status === 'Success' && event.total > 0 && event.total === event.index) {
this.createEventJson(event);
this.ux.log(
this.getStreamResultMark(true),
messages.getMessage('appCreateEvent', [event.total, eventDisplayName, action])
);
}
if (event.status !== 'Success') {
this.createEventJson(event);
this.ux.log(
this.getStreamResultMark(true),
messages.getMessage('appCreateEventFail', [
messages.getMessage('appCreateEventFailureLabel'),
eventDisplayName,
event.index,
event.total,
event.label,
event.message
])
);
}
}
}

private createEventJson(event: WaveAssetEvent) {
const entry = {
EventType: event.eventType,
Status: event.status,
Index: event.index,
Total: event.total,
ItemLabel: event.label,
Message: event.message
};
this.streamingResults.push(entry);
}
}
44 changes: 39 additions & 5 deletions src/commands/analytics/app/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { flags, SfdxCommand } from '@salesforce/command';
import { Messages, Org } from '@salesforce/core';
import { DEF_APP_CREATE_UPDATE_TIMEOUT } from '../../../lib/analytics/constants';
import AppStreaming from '../../../lib/analytics/event/appStreaming';

import Folder from '../../../lib/analytics/app/folder';

Expand All @@ -20,7 +22,7 @@ export default class Update extends SfdxCommand {
public static examples = ['$ sfdx analytics:app:update -f folderId -t templateId'];

protected static flagsConfig = {
templateid: flags.id({
templateid: flags.string({
char: 't',
required: true,
description: messages.getMessage('templateidForUpdateFlagDescription'),
Expand All @@ -31,6 +33,23 @@ export default class Update extends SfdxCommand {
required: true,
description: messages.getMessage('folderidFlagDescription'),
longDescription: messages.getMessage('folderidFlagLongDescription')
}),
async: flags.boolean({
char: 'a',
description: messages.getMessage('appUpdateAsyncDescription'),
longDescription: messages.getMessage('appUpdateAsyncLongDescription')
}),
allevents: flags.boolean({
char: 'v',
description: messages.getMessage('appCreateAllEventsDescription'),
longDescription: messages.getMessage('appCreateAllEventsLongDescription')
}),
wait: flags.number({
char: 'w',
description: messages.getMessage('streamingWaitDescription'),
longDescription: messages.getMessage('streamingWaitLongDescription', [DEF_APP_CREATE_UPDATE_TIMEOUT]),
min: 0,
default: DEF_APP_CREATE_UPDATE_TIMEOUT
})
};

Expand All @@ -39,9 +58,24 @@ export default class Update extends SfdxCommand {

public async run() {
const folder = new Folder(this.org as Org);
const waveAppId = await folder.update(this.flags.folderid as string, this.flags.templateid as string);
// If error occurs here fails out in the update call and reports back, otherwise success
this.ux.log(messages.getMessage('updateSuccess', [waveAppId]));
return waveAppId;
const appStreaming = new AppStreaming(
this.org as Org,
this.flags.allEvents as boolean,
this.flags.wait as number,
this.ux
);
if (this.flags.async || this.flags.wait <= 0) {
const waveAppId = await folder.update(this.flags.folderid as string, this.flags.templateid as string);
// If error occurs here fails out in the update call and reports back, otherwise success
this.ux.log(messages.getMessage('updateSuccess', [waveAppId]));
return { id: waveAppId };
} else {
const waveAppId = await appStreaming.streamUpdateEvent(
folder,
this.flags.folderid as string,
this.flags.templateid as string
);
return { id: waveAppId, events: appStreaming.getStreamingResults() };
}
}
}
Loading