-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: develop
Are you sure you want to change the base?
Changes from all commits
1d4d6cd
6614d9b
fdbe579
989077b
ca60a90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
|
||
export default class Api extends Component { | ||
private draftApi: any; | ||
|
@@ -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; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini seperti hidden features di sini 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iya. aku kepikiran ide, kalo saat |
||
this.version = pkg.version | ||
|
||
this.botApi = new this.zaun.BotApi(); | ||
this.authApi = new this.zaun.AuthApi(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini untuk progress barnya. ak memilih spinner yg There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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(); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, { }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -560,7 +615,7 @@ export default class Bot extends Component { | |
name: environment.name, | ||
value: environment.id | ||
})); | ||
|
||
let { environmentId } = await inquirer.prompt<any>([ | ||
{ | ||
type: "list", | ||
|
@@ -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", | ||
|
@@ -585,7 +640,7 @@ export default class Bot extends Component { | |
choices: choicesChannel | ||
} | ||
]); | ||
|
||
let { start, end, error } = await inquirer.prompt<any>([ | ||
{ | ||
type: "text", | ||
|
@@ -613,7 +668,7 @@ export default class Bot extends Component { | |
] | ||
}, | ||
]); | ||
|
||
if (isDate(start) == false) { | ||
start = new Date().setHours(0,0,0) | ||
} else { | ||
|
@@ -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"], | ||
|
@@ -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)); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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?