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

Feature/cli analytics #105

Merged
merged 13 commits into from
Jan 10, 2019
28 changes: 25 additions & 3 deletions components/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Command, CommandDescriptor, CommandList } from "interfaces/main";
import { Component, IConfigReader, IInjector } from "merapi";
import { Command, CommandDescriptor, CommandList, IHelper } from "interfaces/main";
import { Component, IConfigReader, IInjector, Json, JsonObject } from "merapi";

const commander = require("commander");

export default class Main extends Component {

constructor(
private config : IConfigReader,
private injector : IInjector
private injector : IInjector,
private helper : IHelper
) {
super();
}
Expand All @@ -21,6 +22,10 @@ export default class Main extends Component {
if (argv.length === 2 || validCommands.indexOf(argv[2]) === -1) {
commander.parse([argv[0], argv[1], '-h']);
}

this.sendNotificationTracking()
this.sendDataAnalytics(argv)
this.saveCommandSession(argv)
}

async compile(commands: CommandList, program: Command, currKey : string = "") {
Expand Down Expand Up @@ -91,4 +96,21 @@ export default class Main extends Component {
handlerMethod(...args);
}
}

private sendDataAnalytics(argv:string[]) {
const command = Object.assign([], argv)

this.helper.sendGoogleAnalytics('commands', 'track', command.splice(2).join(' '))
}

private sendNotificationTracking() {
const status:Boolean = this.helper.checkNotificationStatus();
if (!status) console.log(`\nStarting from Kata CLI vv2.1.0, we added analytics to Kata CLI that will collect usage data every time you typed a command. To learn about what we collect and how we use it, visit https://privacy.kata.ai/kata-cli-analytics\n`)
}

private saveCommandSession(argv:string[]) {
const command = Object.assign([], argv)

this.helper.addCommandSession(command.splice(2).join(' '))
}
}
1 change: 1 addition & 0 deletions components/projects/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default class Project {
},
]);
this.helper.setProp("projectId", project.id);
this.helper.setProp("projectName", project.name);
console.log(colors.green(`Project "${project.name}" (${project.id}) is successfully selected`));
return;
}
Expand Down
85 changes: 84 additions & 1 deletion components/scripts/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require("fs");
const os = require("os");
const path = require("path");
const inquirer = require("inquirer");
const analytics = require('universal-analytics');

function wrapError(error: any) {
let errorMessage;
Expand All @@ -30,9 +31,12 @@ export const CatchError = Catch(Error, (error: any) => {


export default class Helper extends Component {
private google:any;

constructor(private config : IConfig) {
super();

this.google = analytics(this.config.default('config.trackingId', 'UA-131926842-1'));
}

public getFiles(dir : string, ending : string) : string[] {
Expand Down Expand Up @@ -173,7 +177,13 @@ export default class Helper extends Component {
}

public wrapError(error : any) {
return wrapError(error);
const errorMessage:string = wrapError(error);

const commands:JsonObject[] = this.getCommandSession()
this.sendGoogleAnalytics('commands', 'debug', '', commands, errorMessage)
this.clearCommandSession()

return errorMessage
}

public difference(object : any, base : any) {
Expand Down Expand Up @@ -201,4 +211,77 @@ export default class Helper extends Component {

console.log(jsonProp);
}

public checkNotificationStatus(): Boolean {
const jsonPath = `${os.homedir()}/.katanotif`;

if (fs.existsSync(jsonPath)) {
return true
} else {
fs.writeFileSync(jsonPath, "true", "utf8");
return false
}
}

public addCommandSession(command:string): void {
const jsonPath = `${os.homedir()}/.katacommand`;
let jsonData:JsonObject[] = [];

if (fs.existsSync(jsonPath)) jsonData = JSON.parse(fs.readFileSync(jsonPath, "utf8"));

if (jsonData.length > 0) {
const lastData:JsonObject = jsonData[jsonData.length - 1]
const diff:number = Math.abs(Number(lastData.timestamp) - new Date().getTime()) / 36e5;

if (diff >= 1) jsonData = [] //Lebih dari 1 jam ?
}

jsonData.push({ timestamp: new Date().getTime(), command: command })

fs.writeFileSync(jsonPath, JSON.stringify(jsonData), "utf8");
}

public getCommandSession(): JsonObject[] {
const jsonPath = `${os.homedir()}/.katacommand`;
let jsonData:JsonObject[] = [];

if (fs.existsSync(jsonPath)) jsonData = JSON.parse(fs.readFileSync(jsonPath, "utf8"));

return jsonData
}

public clearCommandSession(): void {
const jsonPath = `${os.homedir()}/.katacommand`;

fs.writeFileSync(jsonPath, "[]", "utf8");
}

public sendGoogleAnalytics(event:string, action:string, command:string, lastSession?:JsonObject[], errorMessage?:string): void {
let firstLogin = this.getProp("first_login") as JsonObject;
let projectId = this.getProp("projectId") as string;
let projectName = this.getProp("projectName") as string;
const version = this.config.default("version", "1.0.0")

if (!firstLogin) firstLogin = { id: null, username: null, type: null }
if (!projectId) projectId = null
if (!projectName) projectName = null

const data:JsonObject = {
userId: firstLogin.id,
username: firstLogin.username,
currentUserType: firstLogin.type,
activeProjectId: projectId,
activeProjectName: projectName,
command: command,
versionCLI: version,
timestamp: new Date().getTime()
}

if (lastSession) data.lastSession = lastSession
if (errorMessage) data.errorMessage = errorMessage

this.google.event(event, action, JSON.stringify(data), (err:any) => {
if (err) console.log(this.wrapError(err));
})
}
}
3 changes: 2 additions & 1 deletion components/users/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export default class User extends Component {
} else {
console.log("Please log in first");
}


this.helper.clearCommandSession();
} catch (e) {
console.log(this.helper.wrapError(e));
}
Expand Down
5 changes: 5 additions & 0 deletions interfaces/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ export interface IHelper {
delete() : Boolean;
wrapError(error : any) : string;
difference(object: any, base: any): Object;
checkNotificationStatus() : Boolean;
addCommandSession(command:string): void;
getCommandSession(): JsonObject[];
clearCommandSession(): void;
sendGoogleAnalytics(event:string, action:string, data:string): void;
}

export interface ITester {
Expand Down
19 changes: 18 additions & 1 deletion lib/components/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/components/projects/project.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 72 additions & 1 deletion lib/components/scripts/helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/components/users/user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"lodash": "^4.17.11",
"merapi": "^0.17.1",
"merapi-proxy": "^0.1.7",
"universal-analytics": "^0.4.20",
"uuid": "^3.2.1",
"zaun": "^2.0.3"
},
Expand Down
1 change: 1 addition & 0 deletions service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ components:


config:
trackingId: "UA-131926842-1"
channels:
type:
- line
Expand Down