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 1 commit
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
4 changes: 4 additions & 0 deletions components/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class Api extends Component {
private userApi: any;
private teamApi: any;
public deploymentApi: any;
public timeout: any;
public gzip: boolean;
private sessionApi: any;
private cachesApi: any;
private utilApi: any;
Expand All @@ -31,6 +33,8 @@ 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 || 30000;
oktavianidewi marked this conversation as resolved.
Show resolved Hide resolved
this.gzip = this.helper.getProp("gzip") as boolean || false;

this.botApi = new this.zaun.BotApi();
this.authApi = new this.zaun.AuthApi();
Expand Down
64 changes: 44 additions & 20 deletions components/bots/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { v4 as uuid } from "uuid";
import { CatchError } from "../scripts/helper";
import { isDate } from "util";
import inquirer = require("inquirer");
const Table = require("cli-table");
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import * as pako from "pako";

const Table = require("cli-table");
const colors = require("colors");
const repl = require("repl");
const util = require("util");
Expand Down Expand Up @@ -200,18 +202,40 @@ export default class Bot extends Component {

if (data.revision) {
latestBotRevision = data.revision;
const url = `${this.api.apiClient.basePath}/projects/${projectId}/bot/revisions/${latestBotRevision}`;
const requestConfig: AxiosRequestConfig = {
headers: {
"Authorization": this.api.bearer.apiKey,
},
timeout: this.api.timeout,
}

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
);

console.log(`Updated bot ${colors.green(project.name)} with revision: ${newBot.revision.substring(0, 7)}`);
if (this.api.gzip) {
oktavianidewi marked this conversation as resolved.
Show resolved Hide resolved
requestConfig.transformRequest = (data, headers) => {
headers["content-encoding"] = "gzip";
headers["content-type"] = "text/plain";
return pako.deflate(JSON.stringify(data), { to: "string" });
};
}

try {
const newBot = await axios.put(url, botDesc, requestConfig)
.then((response: AxiosResponse) => response.data);

// 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
);

console.log(`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));
}
} else {
throw Error("Could not find latest bot revision from this project.");
}
Expand Down Expand Up @@ -560,7 +584,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 +593,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 +609,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 +637,7 @@ export default class Bot extends Component {
]
},
]);

if (isDate(start) == false) {
start = new Date().setHours(0,0,0)
} else {
Expand All @@ -624,12 +648,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 +669,7 @@ export default class Bot extends Component {
}
} else {
console.log("Please select Project first");
}
}
} catch (e) {
console.error(this.helper.wrapError(e));
}
Expand Down
Loading