Created
June 6, 2023 15:49
-
-
Save MrAsynchronous/93e1035034c9e57874dffdb0b3ced619 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Request, Response } from "@google-cloud/functions-framework" | |
| import {InfluxDB, Point} from '@influxdata/influxdb-client' | |
| import { PubSub } from "@google-cloud/pubsub"; | |
| require('dotenv').config({ | |
| path: "../../.env" | |
| }) | |
| const influxDBClient = new InfluxDB({ | |
| url: process.env.INFLUX_URL, | |
| token: process.env.INFLUX_TOKEN | |
| }); | |
| const writeApi = influxDBClient.getWriteApi( | |
| process.env.INFLUX_ORG, | |
| process.env.INFLUX_BUCKET | |
| ); | |
| const pubSubClient = new PubSub(); | |
| type Event = { | |
| event_name: string, | |
| time_sent: number, | |
| event_context: string, | |
| origin_gameid: number, | |
| origin_placeid: number, | |
| place_version: string, | |
| serverid: string, | |
| player_userid: number, | |
| sdk_info: { | |
| version: number | |
| }, | |
| payload: Object | |
| } | |
| type PubSubEvent = { | |
| attributes: { | |
| eventname: string, | |
| eventcontext: string, | |
| playeruserid: number, | |
| gameid: number, | |
| placeid: number, | |
| placeversion: string, | |
| serverid: string, | |
| eventtime: number | |
| }, | |
| data: string | |
| } | |
| /* | |
| Ingests an event from a Roblox game server, and delegates it to both | |
| Influx DB and Google Analytics (BigQuery via Pub / Sub) | |
| */ | |
| export async function ingestEvent(req: Request, res: Response) { | |
| const event: Event = req.body; | |
| // Create point from event | |
| const point = new Point(event.event_name) | |
| .tag("time_sent", event.time_sent.toString()) | |
| .tag("event_context", event.event_context) | |
| .tag("origin_gameid", event.origin_gameid.toString()) | |
| .tag("origin_placeid", event.origin_placeid.toString()) | |
| .tag("place_version", event.place_version) | |
| .tag("player_userid", event.player_userid.toString()) | |
| .stringField("payload", JSON.stringify(event.payload)); | |
| writeApi.writePoint(point); | |
| // Create Pub/Sub message | |
| const pubSubEvent: PubSubEvent = { | |
| attributes: { | |
| eventname: event.event_name, | |
| eventcontext: event.event_context, | |
| playeruserid: event.player_userid, | |
| gameid: event.origin_gameid, | |
| placeid: event.origin_placeid, | |
| placeversion: event.place_version, | |
| serverid: event.serverid, | |
| eventtime: event.time_sent | |
| }, | |
| data: Buffer.from(JSON.stringify(event.payload)).toString('base64') | |
| } | |
| const messageId = await pubSubClient | |
| .topic(process.env.PUBSUB_TOPIC) | |
| .publishMessage({ | |
| data: Buffer.from(JSON.stringify(pubSubEvent)), | |
| }) | |
| console.log(messageId); | |
| res.status(200).send("OK, Sent Event!"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment