Files
discordeno/template/bigbot/src/analytics.ts
Jonathan Ho a8a89d4b16 feat(Bigbot): added docker-compose (#2538)
* chore: fix script extension

* chore: remove extention

* fix: at least it run can now

* chore: fix dev script

* refactor: change to use env file for config

* chore: update readme

* refactor: put dotenv to top

* feat: add docker and influxdb

* chore: disable deno fmt on bigbot node_modules

* style: deno fmt

* style: deno fmt after upgrade deno

* chore: remove dev console.log

* chore: reverse the influxdb env

* chore: limit the influxdb to localhost

* refactor: seperate build process

* chore: add user pass docs

* refactor: route all to handler

* fix: skip if webhook not exist

* fix: change to use runMethod for send response

* chore: gateway don't depend on bot

* fix: skip if webhook not exist

* fix: first command and error reply not sending

* fix: add try catch to error reply

* fix: reverse interactive.reply

* feat: add fetch analytics to bot process
2022-10-22 07:28:33 -05:00

56 lines
1.9 KiB
TypeScript

import { InfluxDB, Point } from "@influxdata/influxdb-client";
import { RestManager } from "discordeno/rest";
const INFLUX_ORG = process.env.INFLUX_ORG as string;
const INFLUX_BUCKET = process.env.INFLUX_BUCKET as string;
const INFLUX_TOKEN = process.env.INFLUX_TOKEN as string;
const INFLUX_URL = process.env.INFLUX_URL as string;
export const influxDB = INFLUX_URL && INFLUX_TOKEN ? new InfluxDB({ url: INFLUX_URL, token: INFLUX_TOKEN }) : undefined;
export const Influx = influxDB?.getWriteApi(INFLUX_ORG, INFLUX_BUCKET);
export const setupAnalyticsHooks = (rest: RestManager) => {
// If influxdb data is provided, enable analytics in this proxy.
if (Influx) {
rest.fetching = function (options) {
Influx?.writePoint(
new Point("restEvents")
// MARK THE TIME WHEN EVENT ARRIVED
.timestamp(new Date())
// SET THE GUILD ID
.stringField("type", "REQUEST_FETCHING")
.tag("method", options.method)
.tag("url", options.url)
.tag("bucket", options.bucketId ?? "NA"),
);
};
rest.fetched = function (options, response) {
Influx?.writePoint(
new Point("restEvents")
// MARK THE TIME WHEN EVENT ARRIVED
.timestamp(new Date())
// SET THE GUILD ID
.stringField("type", "REQUEST_FETCHED")
.tag("method", options.method)
.tag("url", options.url)
.tag("bucket", options.bucketId ?? "NA")
.intField("status", response.status)
.tag("statusText", response.statusText),
);
};
setInterval(() => {
console.log(`[Influx - REST] Saving events...`);
Influx?.flush()
.then(() => {
console.log(`[Influx - REST] Saved events!`);
})
.catch((error) => {
console.log(`[Influx - REST] Error saving events!`, error);
});
// Every 30seconds
}, 30000);
}
};