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

Push to prod #228

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
572 changes: 558 additions & 14 deletions layers/api_dependencies/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion layers/api_dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
"dependencies": {
"@aws-crypto/sha256-browser": "^2.0.1",
"@aws-sdk/client-sfn": "^3.563.0",
"@aws-sdk/client-sns": "^3.606.0",
"@aws-sdk/credential-provider-node": "^3.563.0",
"@honeybadger-io/js": "^4.9.3",
"@smithy/node-http-handler": "^2.5.0",
"@smithy/protocol-http": "^3.3.0",
"@smithy/signature-v4": "^2.3.0",
"@honeybadger-io/js": "^4.9.3",
"axios": ">=0.21.1",
"cookie": "^0.5.0",
"debug": "^4.3.4",
"http-status-codes": "^2.2.0",
"iiif-builder": "^1.0.7",
"jsonschema": "^1.4.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"lz-string": "^1.4.4",
Expand Down
17,640 changes: 5,530 additions & 12,110 deletions node/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "^2.5.2",
"aws-sdk-client-mock": "^4.0.1",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"choma": "^1.2.1",
Expand Down
125 changes: 125 additions & 0 deletions node/src/handlers/post-chat-feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const { wrap } = require("./middleware");
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns");

const Validator = require("jsonschema").Validator;

const feedbackSchema = {
type: "object",
properties: {
sentiment: { enum: ["positive", "negative"] },
context: {
type: "object",
properties: {
ref: { type: "string" },
question: { type: "string" },
answer: { type: "string" },
source_documents: {
type: "array",
items: { type: "string" },
},
},
required: ["ref", "question", "answer", "source_documents"],
additionalProperties: false,
},
feedback: {
type: "object",
properties: {
options: { type: "array", items: { type: "string" } },
text: { type: "string" },
email: { type: "string" },
},
required: ["options", "text", "email"],
additionalProperties: false,
},
},
required: ["sentiment", "context", "feedback"],
additionalProperties: false,
};

const handler = wrap(async (event, context) => {
if (!event.userToken.isLoggedIn() && !event.userToken.isSuperUser()) {
return {
statusCode: 401,
headers: { "Content-Type": "text/plain" },
body: "Authorization Required",
};
}

// Pass in the S3 and SNS clients if they are injected
// to workound an issue with the mocking library
// https://github.com/m-radzikowski/aws-sdk-client-mock
const s3Client = context?.injections?.s3Client
? context.injections.s3Client
: new S3Client({});
const snsClient = context?.injections?.snsClient
? context.injections.snsClient
: new SNSClient({});

try {
const content = JSON.parse(event.body);
const v = new Validator();
var result = v.validate(content, feedbackSchema);

const errors = result.errors.map((e) => e.stack.replace("instance.", ""));
if (errors.length > 0) {
return {
statusCode: 400,
headers: { "Content-Type": "text/plain" },
body: JSON.stringify(errors.join(", ")),
};
}
await uploadToS3(
s3Client,
`${content.sentiment}/${content.context.ref}.json`,
content
);

await sendNotification(
snsClient,
`Chat feedback: ${content.sentiment} response (${
process.env.HONEYBADGER_ENV || process.env.DEV_PREFIX || "dev"
})`,
JSON.stringify(content, null, 2)
);

return {
statusCode: 200,
headers: { "content-type": "text/plain" },
body: JSON.stringify({
message: `Feedback received. Thank you.`,
}),
};
} catch (err) {
console.error(err);
return {
statusCode: 500,
headers: { "content-type": "text/plain" },
body: "Internal Server Error",
};
}
});

const uploadToS3 = async (s3Client, key, body) => {
const command = new PutObjectCommand({
Bucket: process.env.CHAT_FEEDBACK_BUCKET,
Key: key,
Body: JSON.stringify(body, null, 2),
ContentType: "application/json",
});

return await s3Client.send(command);
};

const sendNotification = async (snsClient, subject, message) => {
const command = new PublishCommand({
TopicArn: process.env.CHAT_FEEDBACK_TOPIC_ARN,
Subject: subject,
Message: message,
});
const response = await snsClient.send(command);
console.log(response);
return response;
};

module.exports = { handler };
Loading
Loading