-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "ask api get-metrics" and "ask api export-package" commands
- Loading branch information
Showing
13 changed files
with
1,042 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { AbstractCommand } = require('@src/commands/abstract-command'); | ||
const Messenger = require('@src/view/messenger'); | ||
const jsonView = require('@src/view/json-view'); | ||
const SmapiClient = require('@src/clients/smapi-client'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
|
||
class GetMetricsCommand extends AbstractCommand { | ||
name() { | ||
return 'get-metrics'; | ||
} | ||
|
||
description() { | ||
return 'get calculated metrics, insights, and advanced analytics reporting for skills usage.'; | ||
} | ||
|
||
requiredOptions() { | ||
return ['skill-id', 'start-time', 'end-time', 'period', 'metric', 'stage', 'skill-type']; | ||
} | ||
|
||
optionalOptions() { | ||
return ['intent', 'locale', 'max-results', 'next-token', 'profile', 'debug']; | ||
} | ||
|
||
handle(cmd, cb) { | ||
let profile; | ||
try { | ||
profile = profileHelper.runtimeProfile(cmd.profile); | ||
} catch (err) { | ||
Messenger.getInstance().error(err); | ||
return cb(err); | ||
} | ||
|
||
const smapiClient = new SmapiClient({ | ||
profile, | ||
doDebug: cmd.debug | ||
}); | ||
smapiClient.skill.getMetrics(cmd.skillId, cmd.startTime, cmd.endTime, cmd.period, cmd.metric, | ||
cmd.stage, cmd.skillType, cmd.intent, cmd.locale, cmd.nextToken, cmd.maxResults, (err, response) => { | ||
if (err) { | ||
Messenger.getInstance().error(err); | ||
return cb(err); | ||
} | ||
if (response.statusCode >= 300) { | ||
const error = jsonView.toString(response.body); | ||
Messenger.getInstance().error(error); | ||
cb(error); | ||
} else { | ||
Messenger.getInstance().info(jsonView.toString(response.body)); | ||
cb(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
createCommand: new GetMetricsCommand(optionModel).createCommand() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
const CONSTANTS = require('@src/utils/constants'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Retry = require('@src/utils/retry-utility'); | ||
|
||
module.exports = { | ||
pollExportStatus | ||
}; | ||
|
||
/** | ||
* Wrapper for polling smapi skill package export status. | ||
* @param {String} exportId | ||
* @param {Function} callback (err, lastExportStatus) | ||
*/ | ||
function pollExportStatus(smapiClient, exportId, callback) { | ||
const retryConfig = { | ||
base: CONSTANTS.CONFIGURATION.RETRY.GET_PACKAGE_EXPORT_STATUS.MIN_TIME_OUT, | ||
factor: CONSTANTS.CONFIGURATION.RETRY.GET_PACKAGE_EXPORT_STATUS.FACTOR, | ||
maxRetry: CONSTANTS.CONFIGURATION.RETRY.GET_PACKAGE_EXPORT_STATUS.MAX_RETRY | ||
}; | ||
const retryCall = (loopCallback) => { | ||
smapiClient.skillPackage.getExportStatus(exportId, (pollErr, pollResponse) => { | ||
if (pollErr) { | ||
return loopCallback(pollErr); | ||
} | ||
if (pollResponse.statusCode >= 300) { | ||
return loopCallback(jsonView.toString(pollResponse.body)); | ||
} | ||
loopCallback(null, pollResponse); | ||
}); | ||
}; | ||
const shouldRetryCondition = retryResponse => retryResponse.body.status === CONSTANTS.SKILL.PACKAGE_STATUS.IN_PROGRESS; | ||
Retry.retry(retryConfig, retryCall, shouldRetryCondition, (err, res) => callback(err, err ? null : res)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const R = require('ramda'); | ||
|
||
const { AbstractCommand } = require('@src/commands/abstract-command'); | ||
const CONSTANTS = require('@src/utils/constants'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Messenger = require('@src/view/messenger'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
const SmapiClient = require('@src/clients/smapi-client/index.js'); | ||
const zipUtils = require('@src/utils/zip-utils'); | ||
|
||
const helper = require('./helper.js'); | ||
|
||
class ExportPackageCommand extends AbstractCommand { | ||
name() { | ||
return 'export-package'; | ||
} | ||
|
||
description() { | ||
return 'download the skill package to "skill-package" folder in current directory'; | ||
} | ||
|
||
requiredOptions() { | ||
return ['skill-id', 'stage']; | ||
} | ||
|
||
optionalOptions() { | ||
return ['profile', 'debug']; | ||
} | ||
|
||
handle(cmd, cb) { | ||
let profile; | ||
try { | ||
profile = profileHelper.runtimeProfile(cmd.profile); | ||
// 0.check if a skill-package file exists | ||
if (fs.existsSync(CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE)) { | ||
throw new Error(`A ${CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE} fold already exists in the current working directory.`); | ||
} | ||
} catch (err) { | ||
Messenger.getInstance().error(err); | ||
return cb(err); | ||
} | ||
const smapiClient = new SmapiClient({ | ||
profile, | ||
doDebug: cmd.debug | ||
}); | ||
|
||
// 1.request to export skill package | ||
smapiClient.skillPackage.exportPackage(cmd.skillId, cmd.stage, (exportErr, exportResponse) => { | ||
if (exportErr) { | ||
Messenger.getInstance().error(exportErr); | ||
return cb(exportErr); | ||
} | ||
if (exportResponse.statusCode >= 300) { | ||
const error = jsonView.toString(exportResponse.body); | ||
Messenger.getInstance().error(error); | ||
return cb(error); | ||
} | ||
const exportId = path.basename(R.view(R.lensPath(['headers', 'location']), exportResponse)); | ||
|
||
// 2.poll for the skill package export status | ||
helper.pollExportStatus(smapiClient, exportId, (pollErr, pollResponse) => { | ||
if (pollErr) { | ||
Messenger.getInstance().error(pollErr); | ||
return cb(pollErr); | ||
} | ||
// 3.download skill package into local file system | ||
const skillPackageLocation = R.view(R.lensPath(['body', 'skill', 'location']), pollResponse); | ||
const rootPath = process.cwd(); | ||
const targetPath = path.join(rootPath, CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE); | ||
zipUtils.unzipRemoteZipFile(skillPackageLocation, targetPath, false, (unzipErr) => { | ||
if (unzipErr) { | ||
Messenger.getInstance().error(unzipErr); | ||
return cb(unzipErr); | ||
} | ||
Messenger.getInstance().info(`The skill package had been downloaded into ${targetPath}.`); | ||
cb(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ExportPackageCommand; | ||
module.exports.createCommand = new ExportPackageCommand(optionModel).createCommand(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.