Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
haiqi96 committed Jan 23, 2025
1 parent ba63a76 commit 4f40126
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
34 changes: 26 additions & 8 deletions components/log-viewer-webui/server/src/S3Manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fastifyPlugin from "fastify-plugin";

import {
GetObjectCommand,
S3Client,
Expand All @@ -16,15 +18,27 @@ const PRE_SIGNED_URL_EXPIRY_TIME_SECONDS = 3600;
class S3Manager {
#s3Client;

#enabled;

/**
* @param {string} region
* @param {string|null} region
*/
constructor (
region,
) {
this.#s3Client = new S3Client({
region: region,
});
constructor (region) {
console.log(`Region's length is ${region.length}`)
console.log(`Region is ${region}`)
console.log(region)
this.#enabled = null !== region;
console.log(`Enabled is ${this.#enabled}`)
if (true === this.#enabled) {
console.log(`Initializing S3 client`)
this.#s3Client = new S3Client({
region: region,
});
}
}

isEnabled () {
return this.#enabled;
}

/**
Expand Down Expand Up @@ -55,4 +69,8 @@ class S3Manager {
}
}

export default S3Manager;
export default fastifyPlugin(async (app, region) => {
console.log(`Region in Plugin init is ${region}`)
console.log(region)
await app.decorate("s3Manager", new S3Manager(region));
});
3 changes: 3 additions & 0 deletions components/log-viewer-webui/server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DbManager from "./DbManager.js";
import exampleRoutes from "./routes/example.js";
import queryRoutes from "./routes/query.js";
import staticRoutes from "./routes/static.js";
import S3Manager from "./S3Manager.js";


/**
Expand Down Expand Up @@ -41,6 +42,8 @@ const app = async ({
port: settings.MongoDbPort,
},
});
console.log(settings.StreamFilesS3Region)
await server.register(S3Manager, settings.StreamFilesS3Region);
}

await server.register(staticRoutes);
Expand Down
25 changes: 10 additions & 15 deletions components/log-viewer-webui/server/src/routes/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ import {StatusCodes} from "http-status-codes";

import settings from "../../settings.json" with {type: "json"};
import {EXTRACT_JOB_TYPES} from "../DbManager.js";
import S3Manager from "../S3Manager.js";


const S3_MANAGER = (
null === settings.StreamFilesS3PathPrefix ||
null === settings.StreamFilesS3Region
) ?
null :
new S3Manager(settings.StreamFilesS3Region);


/**
* Submits a stream extraction job and returns the metadata of the extracted stream.
*
* @param {object} props
* @param {import("fastify").FastifyInstance | {dbManager: DbManager}} props.fastify
* @param {import("fastify").FastifyInstance |
* {dbManager: DbManager} |
* {s3Manager: S3Manager}} props.fastify
* @param {EXTRACT_JOB_TYPES} props.jobType
* @param {number} props.logEventIdx
* @param {string} props.streamId
Expand Down Expand Up @@ -63,7 +56,9 @@ const extractStreamAndGetMetadata = async ({
/**
* Creates query routes.
*
* @param {import("fastify").FastifyInstance | {dbManager: DbManager}} fastify
* @param {import("fastify").FastifyInstance |
* {dbManager: DbManager} |
* {s3Manager: S3Manager}} fastify
* @param {import("fastify").FastifyPluginOptions} options
* @return {Promise<void>}
*/
Expand Down Expand Up @@ -96,12 +91,12 @@ const routes = async (fastify, options) => {
});
}

if (null === S3_MANAGER) {
streamMetadata.path = `/streams/${streamMetadata.path}`;
} else {
streamMetadata.path = await S3_MANAGER.getPreSignedUrl(
if (fastify.s3Manager.isEnabled()) {
streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
`s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
);
} else {
streamMetadata.path = `/streams/${streamMetadata.path}`;
}

return streamMetadata;
Expand Down

0 comments on commit 4f40126

Please sign in to comment.