Skip to content

Commit

Permalink
Merge pull request #830 from rage/relay-data-to-plagiarism-backend
Browse files Browse the repository at this point in the history
Relay data to plagiarism backend
  • Loading branch information
kalleprkl authored Oct 3, 2021
2 parents d0c3dd1 + 42bf633 commit 9837e19
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 5 deletions.
10 changes: 10 additions & 0 deletions kubernetes/backendv2-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ spec:
secretKeyRef:
name: backend-database-secret
key: SENTRY_DSN
- name: CSD_URL
valueFrom:
secretKeyRef:
name: backend-database-secret
key: CSD_URL
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: backend-database-secret
key: JWT_SECRET
initContainers:
- name: quizzes-backendv2-run-migrations
image: ${BACKENDV2_IMAGE}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Knex from "knex"

export async function up(knex: Knex): Promise<void> {
await knex.raw(
`alter table quiz add column check_plagiarism boolean default false;`,
)
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`alter table quiz drop column check_plagiarism;`)
}
87 changes: 83 additions & 4 deletions packages/backendv2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/backendv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "concurrently npm:ts-watch npm:watch",
"test": "cross-env NODE_ENV=test && jest --runInBand",
"test-open-conn": "cross-env NODE_ENV=test && jest --runInBand --detectOpenHandles ",
"reset-test-db": "cross-env NODE_ENV=test && (dropdb quizzes_test || true) && createdb quizzes_test && knex migrate:latest",
"reset-test-db": "cross-env NODE_ENV=test && (dropdb quizzes_test || true) && createdb quizzes_test && cross-env NODE_ENV=test && knex migrate:latest",
"migrate": "knex migrate:latest",
"seed": "knex seed:run --specific a.ts",
"update-expired-courses": "node ./dist/bin/update-expired-courses.js",
Expand Down Expand Up @@ -46,6 +46,7 @@
"@types/csv-parse": "^1.2.2",
"@types/csv-stringify": "^3.1.0",
"@types/jsonstream": "^0.8.30",
"@types/jsonwebtoken": "^8.5.4",
"@types/lodash": "^4.14.161",
"JSONStream": "^1.3.5",
"axios": "^0.21.1",
Expand All @@ -54,6 +55,7 @@
"dotenv": "^8.2.0",
"ioredis": "^4.19.4",
"jsonstream": "^1.0.3",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.1",
"koa": "^2.12.0",
"koa-bodyparser": "^4.3.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/backendv2/src/models/language.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BaseModel from "./base_model"

class Language extends BaseModel {
name!: string

static get tableName() {
return "language"
}
Expand Down
1 change: 1 addition & 0 deletions packages/backendv2/src/models/quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Quiz extends BaseModel {
title!: string
body!: string
submitMessage!: string
checkPlagiarism!: boolean

static get tableName() {
return "quiz"
Expand Down
25 changes: 25 additions & 0 deletions packages/backendv2/src/models/quiz_answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import softDelete from "objection-soft-delete"
import { mixin } from "objection"
import QuizAnswerStatusModification from "./quiz_answer_status_modification"
import { TStatusModificationOperation } from "./../types/index"
import { relayNewAnswer } from "../services/plagiarism"
import Language from "./language"

type QuizAnswerStatus =
| "draft"
Expand Down Expand Up @@ -581,6 +583,9 @@ class QuizAnswer extends mixin(BaseModel, [
}
await trx.commit()
quiz.course = course
if (quiz.checkPlagiarism) {
await this.relayPlagiarismData(savedQuizAnswer, quiz)
}
return {
quiz,
quizAnswer: savedQuizAnswer,
Expand All @@ -592,6 +597,26 @@ class QuizAnswer extends mixin(BaseModel, [
}
}

private static async relayPlagiarismData(quizAnswer: QuizAnswer, quiz: Quiz) {
const language = await Language.query().findById(quiz.course.languageId)
for (const quizItem of quiz.items) {
if (quizItem.type === "essay") {
const itemAnswer = quizAnswer.itemAnswers.find(
itemAnswer => itemAnswer.quizItemId === quizItem.id,
)
if (itemAnswer) {
relayNewAnswer({
quizId: quiz.id,
quizAnswerId: quizAnswer.id,
quizItemAnswerId: itemAnswer.id,
language: language.name.split(" ")[0].toLowerCase(),
data: itemAnswer.textData,
})
}
}
}
}

public static async update(
quizAnswer: QuizAnswer,
userQuizState: UserQuizState,
Expand Down
27 changes: 27 additions & 0 deletions packages/backendv2/src/services/plagiarism.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from "axios"
import jwt from "jsonwebtoken"
import { GlobalLogger } from "../middleware/logger"

const CancelToken = axios.CancelToken
const source = CancelToken.source()

export const relayNewAnswer = async (data: any) => {
try {
const csd_url = process.env.CSD_URL || "http://localhost/5150"
await axios.post(csd_url + "/new", data, {
headers: {
authorization: jwt.sign(
{ source: "quizzes" },
process.env.JWT_SECRET || "",
),
},
cancelToken: source.token,
})
setTimeout(() => {
GlobalLogger.warn("plagiarism detection: request timed out")
source.cancel()
}, 10000)
} catch (error) {
GlobalLogger.error("plagiarism detection: backend responded with error")
}
}
13 changes: 13 additions & 0 deletions packages/backendv2/tests/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const input = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -86,6 +87,7 @@ export const input = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -238,6 +240,8 @@ interface QuizValidator {
awardPointsEvenIfWrong: false
grantPointsPolicy: "grant_whenever_possible"
autoReject: true

checkPlagiarism: false
title: "quiz"
body: "body"
submitMessage: "nice one!"
Expand All @@ -261,6 +265,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -375,6 +380,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -475,6 +481,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz 1",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -589,6 +596,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz 2",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -663,6 +671,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -737,6 +746,7 @@ export const validation = {
awardPointsEvenIfWrong: false,
grantPointsPolicy: "grant_whenever_possible",
autoReject: true,
checkPlagiarism: false,
title: "quiz",
body: "body",
submitMessage: "nice one!",
Expand Down Expand Up @@ -990,6 +1000,7 @@ export const validation = {
quiz: {
autoConfirm: true,
autoReject: true,
checkPlagiarism: false,
awardPointsEvenIfWrong: false,
courseId: "46d7ceca-e1ed-508b-91b5-3cc8385fa44b",
createdAt: expect.stringMatching(dateTime),
Expand Down Expand Up @@ -1271,6 +1282,7 @@ export const validation = {
excludedFromScore: false,
autoConfirm: true,
autoReject: true,
checkPlagiarism: false,
triesLimited: true,
tries: 1,
grantPointsPolicy: "grant_whenever_possible",
Expand Down Expand Up @@ -1460,6 +1472,7 @@ export const validation = {
excludedFromScore: false,
autoConfirm: true,
autoReject: true,
checkPlagiarism: false,
triesLimited: true,
tries: 1,
grantPointsPolicy: "grant_whenever_possible",
Expand Down

0 comments on commit 9837e19

Please sign in to comment.