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(gzip): add gzip compression on update bot #137

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions components/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { JsonObject, Component } from "merapi";
import { IHelper } from "interfaces/main";
const pkg = require("../../package.json");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ak kurang yakin apakah kita benar2 perlu ini 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini buat ambil sourcing version nya kan?


export default class Api extends Component {
private draftApi: any;
Expand All @@ -11,6 +12,9 @@ export default class Api extends Component {
private userApi: any;
private teamApi: any;
public deploymentApi: any;
public timeout: any;
public gzip: string;
public version: string;
private sessionApi: any;
private cachesApi: any;
private utilApi: any;
Expand All @@ -31,6 +35,9 @@ export default class Api extends Component {
const currentLogin = this.helper.getProp("current_login") as string || "user";
const tokenObj = this.helper.getProp("token") as JsonObject || {};
this.bearer.apiKey = `Bearer ${tokenObj[currentLogin]}`;
this.timeout = this.helper.getProp("timeout") as number || 300000;
this.gzip = this.helper.getProp("gzip") as string || "false";
Comment on lines +38 to +39
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini seperti hidden features di sini 😂
mungkin kita perlu mendokumentasikannya lebih baik untuk options yg bisa dipakai oleh user

Copy link
Contributor

@oktavianidewi oktavianidewi Nov 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iya. aku kepikiran ide, kalo saat push, file size nya dibaca dulu, baru user bs milih mau di push pake di-compress atau nggak, gimana mas? wkkk

this.version = pkg.version

this.botApi = new this.zaun.BotApi();
this.authApi = new this.zaun.AuthApi();
Expand Down
95 changes: 75 additions & 20 deletions components/bots/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { Component, Config, IHash, JsonObject } from "merapi";
import { v4 as uuid } from "uuid";
import { CatchError } from "../scripts/helper";
import { isDate } from "util";
import * as zlib from "zlib";
import inquirer = require("inquirer");
const Table = require("cli-table");
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import * as ora from "ora";

const Table = require("cli-table");
const colors = require("colors");
const repl = require("repl");
const util = require("util");
Expand All @@ -15,8 +18,14 @@ const fs = require("fs");
const deasync = require("deasync");

export default class Bot extends Component {
private spinner: ora.Ora;
constructor(private compile: ICompile, private helper: IHelper, private tester: ITester, private api: any) {
super();

this.spinner = ora({
discardStdin: false,
spinner: "line",
})
Comment on lines +25 to +28
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini untuk progress barnya. ak memilih spinner yg line karena hanya itu saja yg disupport di Windows

Copy link
Contributor

@oktavianidewi oktavianidewi Nov 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iya gpp lah mas. padahal yg dots lebih ai-ketching ya lol

}

public init(name: string, options: JsonObject) {
Expand Down Expand Up @@ -172,9 +181,11 @@ export default class Bot extends Component {

@CatchError
public async push(options: JsonObject) {
this.spinner.prefixText = "[push]"
const desc = this.helper.loadYaml("./bot.yml");
desc.tag = options.tag || null;

this.spinner.start("Compiling your bot...");
let bot = Config.create(desc, { left: "${", right: "}" });
bot = this.compile.execDirectives(bot, process.cwd());
bot.resolve();
Expand All @@ -187,37 +198,81 @@ export default class Bot extends Component {
return;
}

this.spinner.succeed();

const projectId = this.getProject();

botDesc.id = projectId;

let latestBotRevision;
try {
this.spinner.start("Getting your project details...");
const { response: { body: data } } = await this.helper.toPromise(
this.api.projectApi,
this.api.projectApi.projectsProjectIdBotGet, botDesc.id
);

if (data.revision) {
this.spinner.succeed();

latestBotRevision = data.revision;
const url = `${this.api.apiClient.basePath}/projects/${projectId}/bot/revisions/${latestBotRevision}`;
const requestConfig: AxiosRequestConfig = {
headers: {
"Authorization": this.api.bearer.apiKey,
"user-agent": `kata-cli@${this.api.version}`,
},
timeout: this.api.timeout,
}
Comment on lines +219 to +226
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kita pke axios untuk custom requestnya. agak tedious pada saat ngebuild url nya

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpp mas, sementara gini aja dulu. nanti di kata-cli yg baru dibikin schema api call buat axios request nya biar g tedious


if (this.api.gzip === "true") {
requestConfig.transformRequest = [(data, headers) => {
const botString = JSON.stringify(data);
headers["content-encoding"] = "deflate";
headers["content-type"] = "application/json";
Comment on lines +230 to +232
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api gateway kita perlu dua headers ini supaya bisa decompress data yg terkirim

const gzip = zlib.deflateSync(botString, { });
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kita menggunakan stdlib compression punya nodejs karena saat ini api gateway kita masih belum bisa handle compression dari library lain

return gzip;
}];
}

const { data: newBot } = await this.helper.toPromise(
this.api.botApi, this.api.botApi.projectsProjectIdBotRevisionsRevisionPut,
projectId, latestBotRevision, botDesc
);
const { data: project } = await this.helper.toPromise(
this.api.projectApi,
this.api.projectApi.projectsProjectIdGet, projectId
);
let newBot;

console.log(`Updated bot ${colors.green(project.name)} with revision: ${newBot.revision.substring(0, 7)}`);
try {
this.spinner.start("Pushing your bot...");
const data = await axios.put(url, botDesc, requestConfig)
.then((response: AxiosResponse) => {
return response.data;
});
newBot = data;
this.spinner.succeed();
} catch (e) {
console.error("Error while updating bot");
// console.log(this.helper.wrapError(e));
this.spinner.fail(this.helper.wrapError(e));
}

try {
// console.log("getting project details...")
this.spinner.start("Getting project details...");
const { data: project } = await this.helper.toPromise(
this.api.projectApi,
this.api.projectApi.projectsProjectIdGet, projectId
);

this.spinner.succeed(`Updated bot ${colors.green(project.name)} with revision: ${newBot.revision.substring(0, 7)}`);
} catch (e) {
console.error("Error while updating bot");
// console.log(this.helper.wrapError(e));
this.spinner.fail(this.helper.wrapError(e));
}
} else {
this.spinner.fail("Could not find latest bot revision from this project.");
throw Error("Could not find latest bot revision from this project.");
}
} catch (e) {
console.error("Error");
console.log(this.helper.wrapError(e));
// console.log(this.helper.wrapError(e));
this.spinner.fail(this.helper.wrapError(e));
}

this.helper.dumpYaml("./bot.yml", desc);
Expand Down Expand Up @@ -560,7 +615,7 @@ export default class Bot extends Component {
name: environment.name,
value: environment.id
}));

let { environmentId } = await inquirer.prompt<any>([
{
type: "list",
Expand All @@ -569,14 +624,14 @@ export default class Bot extends Component {
choices: choicesEnvironment
}
]);

const dataChannels = await this.helper.toPromise(this.api.deploymentApi, this.api.deploymentApi.projectsProjectIdEnvironmentsEnvironmentIdChannelsGet , projectId, environmentId, null);
const channels: object[] = dataChannels.response.body;
const choicesChannel = channels.map((channel: any) => ({
name: channel.name,
value: channel.id
}));

let { channelId } = await inquirer.prompt<any>([
{
type: "list",
Expand All @@ -585,7 +640,7 @@ export default class Bot extends Component {
choices: choicesChannel
}
]);

let { start, end, error } = await inquirer.prompt<any>([
{
type: "text",
Expand Down Expand Up @@ -613,7 +668,7 @@ export default class Bot extends Component {
]
},
]);

if (isDate(start) == false) {
start = new Date().setHours(0,0,0)
} else {
Expand All @@ -624,12 +679,12 @@ export default class Bot extends Component {
} else {
end = new Date(end).setHours(23,59,59)
}

const errorGroup = error.group
const errorCode = error.code

const { response } = await this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsProjectIdErrorsGet , projectId, environmentId, channelId, errorGroup, errorCode, new Date(start).toISOString(), new Date(end).toISOString());

if (response && response.body && response.body.data) {
const table = new Table({
head: ["Time", "Error Code", "Error Message"],
Expand All @@ -645,7 +700,7 @@ export default class Bot extends Component {
}
} else {
console.log("Please select Project first");
}
}
} catch (e) {
console.error(this.helper.wrapError(e));
}
Expand Down
4 changes: 4 additions & 0 deletions lib/components/api/api.js

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

62 changes: 58 additions & 4 deletions lib/components/bots/bot.js

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

Loading