Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge 7231dde into f988d20
Browse files Browse the repository at this point in the history
  • Loading branch information
ecruzado committed Aug 15, 2018
2 parents f988d20 + 7231dde commit a0c555b
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 24 deletions.
7 changes: 6 additions & 1 deletion bin/bst-speak.ts
Expand Up @@ -2,6 +2,7 @@
import * as program from "commander";
import {Global} from "../lib/core/global";
import {VirtualDeviceClient} from "../lib/external/virtual-device";
import {BstStatistics, BstCommand} from "../lib/statistics/bst-statistics";

program.version(Global.version());

Expand Down Expand Up @@ -62,7 +63,11 @@ Global.initializeCLI().then(
if (token) {
console.log("Your token is saved, you can now use this command without providing a token");
}

let nodeId = undefined;
if (Global.config() && Global.config().secretKey && Global.config().secretKey()) {
nodeId = Global.config().secretKey();
}
BstStatistics.instance().record(BstCommand.speak, undefined, nodeId);
console.log(VirtualDeviceClient.renderResult(virtualDeviceResponse));
});

Expand Down
25 changes: 17 additions & 8 deletions bin/bst-test.ts
@@ -1,17 +1,26 @@
#!/usr/bin/env node
import * as program from "commander";
import {Global} from "../lib/core/global";
import {BstStatistics, BstCommand} from "../lib/statistics/bst-statistics";

const skillTesting = require("skill-testing-ml");

program.version(Global.version());

program
.usage("[test-pattern-regex]")
.description("Runs unit-tests for a skill - automatically searches for YML test files and runs them")
.parse(process.argv);
Global.initializeCLI(false).then(() => {

const testCLI = new skillTesting.CLI();
testCLI.run(process.argv).then((success) => {
process.exitCode = success ? 0 : 1;
});
program
.usage("[test-pattern-regex]")
.description("Runs unit-tests for a skill - automatically searches for YML test files and runs them")
.parse(process.argv);

const testCLI = new skillTesting.CLI();
testCLI.run(process.argv).then((success) => {
let nodeId = undefined;
if (Global.config() && Global.config().secretKey && Global.config().secretKey()) {
nodeId = Global.config().secretKey();
}
BstStatistics.instance().record(BstCommand.test, undefined, nodeId);
process.exitCode = success ? 0 : 1;
});
});
4 changes: 4 additions & 0 deletions circle.yml
Expand Up @@ -3,6 +3,8 @@ machine:
version: 6.9.2
services:
- docker
environment:
SKIP_STATISTICS: "true"

dependencies:
pre:
Expand Down Expand Up @@ -38,6 +40,7 @@ deployment:
-e AWS_SECRET_ACCESS_KEY=$PROD_AWS_SECRET_ACCESS_KEY
-e SSL_CERT="$SSL_CERT"
-e SSL_KEY="$SSL_KEY"
-e SOURCE_API_URL="$SOURCE_API_URL_PROD"
-d
--name bst
--size s4
Expand All @@ -61,6 +64,7 @@ deployment:
-e AWS_SECRET_ACCESS_KEY=$PROD_AWS_SECRET_ACCESS_KEY
-e SSL_CERT="$SSL_CERT"
-e SSL_KEY="$SSL_KEY"
-e SOURCE_API_URL="$SOURCE_API_URL_DEV"
-d
--name bst-dev
--size s4
Expand Down
14 changes: 10 additions & 4 deletions lib/client/bst-config.ts
Expand Up @@ -19,11 +19,17 @@ export class BSTConfig {
* Loads the configuration
* Done all synchronously as this is done first thing at startup and everything waits on it
*/
public static async load(): Promise<BSTConfig> {
await BSTConfig.bootstrapIfNeeded();
public static async load(createConfigFileIfNeeded?: boolean): Promise<BSTConfig> {
createConfigFileIfNeeded = createConfigFileIfNeeded || true;
if (createConfigFileIfNeeded) {
await BSTConfig.bootstrapIfNeeded();
}

let data = fs.readFileSync(BSTConfig.configPath());
let config = JSON.parse(data.toString());
let config = undefined;
if (fs.existsSync(BSTConfig.configPath())) {
let data = fs.readFileSync(BSTConfig.configPath());
config = JSON.parse(data.toString());
}

let bstConfig = new BSTConfig();
bstConfig.loadFromJSON(config);
Expand Down
12 changes: 10 additions & 2 deletions lib/client/bst-virtual-alexa.ts
@@ -1,6 +1,7 @@
import {Global} from "../core/global";
import {SkillContext, VirtualAlexa} from "virtual-alexa";
import * as fs from "fs";
import {BstStatistics, BstCommand} from "../statistics/bst-statistics";

interface SavedSession {
id: string;
Expand All @@ -21,6 +22,7 @@ export class BSTVirtualAlexa {
private interactionModelProvided: boolean = false;
private sampleUtterancesProvided: boolean = false;
private intentSchemaProvided: boolean = false;
private nodeId: string;

private static FileTypes = {
InterationModel: "Interaction Model",
Expand Down Expand Up @@ -186,6 +188,10 @@ export class BSTVirtualAlexa {
*/
public start(): void {
this.virtualAlexa = this.validateFilesAndBuild();
const globalConfig = Global.config();
if (globalConfig && globalConfig.configuration && globalConfig.configuration.secretKey) {
this.nodeId = globalConfig.configuration.secretKey;
}
}

/**
Expand All @@ -207,7 +213,7 @@ export class BSTVirtualAlexa {
}).catch(error => {
callback(error, null, request);
});

BstStatistics.instance().record(BstCommand.utter, undefined, this.nodeId);
return this;
}

Expand All @@ -230,7 +236,7 @@ export class BSTVirtualAlexa {
}).catch(error => {
callback(error, null, request);
});

BstStatistics.instance().record(BstCommand.intend, undefined, this.nodeId);
return this;
}

Expand All @@ -249,6 +255,8 @@ export class BSTVirtualAlexa {
}).catch(error => {
callback(error, null, request);
});

BstStatistics.instance().record(BstCommand.launch, undefined, this.nodeId);
return this;
}
}
10 changes: 6 additions & 4 deletions lib/core/global.ts
Expand Up @@ -13,13 +13,15 @@ export class Global {
private static _configuration: BSTConfig = null;
private static _cli: boolean = false;

public static async initializeCLI(): Promise<void> {
public static async initializeCLI(createConfigFileIfNeeded?: boolean): Promise<void> {
createConfigFileIfNeeded = createConfigFileIfNeeded || true;
Global.initialize(true);
await Global.loadConfig();
await Global.loadConfig(createConfigFileIfNeeded);
}

public static async loadConfig(): Promise<void> {
const config = await BSTConfig.load();
public static async loadConfig(createConfigFileIfNeeded?: boolean): Promise<void> {
createConfigFileIfNeeded = createConfigFileIfNeeded || true;
const config = await BSTConfig.load(createConfigFileIfNeeded);
Global._configuration = config;
}

Expand Down
21 changes: 18 additions & 3 deletions lib/statistics/bst-statistics.ts
@@ -1,8 +1,15 @@
import * as http from "http";
import * as https from "https";

const SECURE_SOURCE_API_END_POINT = process.env.SECURE_SOURCE_API_END_POINT || true;
export const SOURCE_API_URL = process.env.SOURCE_API_URL || "source-api-dev";
const getSecureSourceApiEndPoint = () => {
const envValue = process.env.SECURE_SOURCE_API_END_POINT;
if (envValue && envValue === "false") {
return false;
}
return true;
};
const SECURE_SOURCE_API_END_POINT = getSecureSourceApiEndPoint();
export const SOURCE_API_URL = process.env.SOURCE_API_URL || "source-api.bespoken.tools";

export class BstStatistics {
static FLUSH_TIME = 10000;
Expand Down Expand Up @@ -67,6 +74,9 @@ export class StatisticsContext {
}

public transmit(logBatch: any, flushed?: (error?: Error) => void) {
if (process.env.SKIP_STATISTICS === "true") {
return;
}
const dataAsString = JSON.stringify({bstStats: logBatch});
const dataLength = Buffer.byteLength(dataAsString);
const options = {
Expand Down Expand Up @@ -97,10 +107,14 @@ export class StatisticsContext {
http.request(options, functionCallback);

httpRequest.on("error", function (error: any) {
if (process.env.DISPLAY_STATISTICS_ERROR) {
console.log("error", error);
}
if (flushed !== undefined && flushed !== null) {
flushed(error);
}
});
httpRequest.setNoDelay(true);
httpRequest.write(dataAsString);
httpRequest.end();
}
Expand All @@ -123,7 +137,8 @@ export const BstCommand = {
intend: "intend",
speak: "speak",
proxy: "proxy",
test: "test"
test: "test",
launch: "launch"
};

export const BstEvent = {
Expand Down
33 changes: 31 additions & 2 deletions test/bin/bst-speak-test.ts
Expand Up @@ -10,9 +10,9 @@ describe("bst-speak", function() {

let globalModule = {
Global: {
initializeCLI: async function () {
initializeCLI: sinon.spy(async function () {

},
}),
config: function () {
return { sourceID: () => "mySource" };
},
Expand All @@ -35,6 +35,7 @@ describe("bst-speak", function() {
mockery.warnOnReplace(false);
mockery.registerMock("../lib/core/global", globalModule);
sandbox = sinon.sandbox.create();
globalModule.Global.initializeCLI.reset();
});

afterEach(function () {
Expand Down Expand Up @@ -159,6 +160,34 @@ describe("bst-speak", function() {
NodeUtil.load("../../bin/bst-speak.js");
});
});

it("call initializeCLI with default", function() {
return new Promise((resolve, reject) => {

process.argv = command("node bst-speak.js --token Token Hello");
mockery.registerMock("../lib/external/virtual-device", {
VirtualDeviceClient: {
speak: function() {
return {
transcript: "Response"
};
},
renderResult: function (result: any) {
try {
assert.equal(result.transcript, "Response");
} catch (error) {
reject(error);
}
return "Response Rendered";
},
}
});

NodeUtil.load("../../bin/bst-speak.js");
assert.equal(globalModule.Global.initializeCLI.getCall(0).args[0], undefined);
resolve();
});
});
});
});

Expand Down
69 changes: 69 additions & 0 deletions test/bin/bst-test-test.ts
@@ -0,0 +1,69 @@
import * as assert from "assert";
import * as mockery from "mockery";
import * as sinon from "sinon";
import {NodeUtil} from "../../lib/core/node-util";
import {BSTProcess} from "../../lib/client/bst-config";
import {SinonSandbox} from "sinon";

describe("bst-speak", function() {

let globalModule = {
Global: {
initializeCLI: sinon.spy(async function () {

}),
config: function () {
return { sourceID: () => "mySource" };
},
running : function() {
let p = new BSTProcess();
p.port = 9999;
return p;
},

version: function () {
return "0.0.0";
}
}
};

let sandbox: SinonSandbox = null;
beforeEach(function () {
mockery.enable({useCleanCache: true});
mockery.warnOnUnregistered(false);
mockery.warnOnReplace(false);
mockery.registerMock("../lib/core/global", globalModule);
sandbox = sinon.sandbox.create();
globalModule.Global.initializeCLI.reset();
});

afterEach(function () {
sandbox.restore();
mockery.deregisterAll();
mockery.disable();
});

describe("test command", function() {
it("call initializeCLI with false", function() {
return new Promise((resolve, reject) => {

process.argv = command("node bst-test.js");
mockery.registerMock("skill-testing-ml", {
CLI: function() {
return {
run: async function () {
}
};
}
});
NodeUtil.load("../../bin/bst-test.js");
assert.equal(globalModule.Global.initializeCLI.getCall(0).args[0], false);
resolve();
});
});
});
});

let command = function (command: string): Array<string> {
return command.split(" ");
};
5 changes: 5 additions & 0 deletions test/statistics/bst-statistics-test.ts
Expand Up @@ -3,8 +3,12 @@ import {BstStatistics, BstCommand, BstEvent, SOURCE_API_URL} from "../../lib/sta
import * as nock from "nock";

describe("BstStatistics", function() {
beforeEach(function() {
process.env.SKIP_STATISTICS = "false";
});

afterEach(function (done) {
process.env.SKIP_STATISTICS = "true";
nock.cleanAll();
if (!nock.isActive()) {
nock.activate();
Expand All @@ -14,6 +18,7 @@ describe("BstStatistics", function() {

describe("#record()", function() {
it("send bst stats to source api", function(done) {

nock(`https://${SOURCE_API_URL}`)
.persist()
.post("/v1/postBstStats", (body: any) => {
Expand Down

0 comments on commit a0c555b

Please sign in to comment.