Skip to content

Commit

Permalink
Create and update BAI emailers (#1251)
Browse files Browse the repository at this point in the history
* Create and update BAI emailers

* Run Prague emailer on Nov 30th
  • Loading branch information
Redande committed Oct 26, 2023
1 parent 54a2a99 commit 2c1d518
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
69 changes: 69 additions & 0 deletions backend/bin/opavaStatsEmailer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { PRAGUE_COMPLETION_RECIPIENTS } from "../config"
import sentryLogger from "../lib/logger"
import prisma from "../prisma"
import { sendMail } from "../util/sendMail"

const logger = sentryLogger({ service: "opava-stats-emailer" })

const opavaStatsEmailer = async () => {
// Same recipients as for prague stats as opava stats are sent to the same partner in CZ, who re-distributes the stats to the universities
const recipients = PRAGUE_COMPLETION_RECIPIENTS?.split(";")

if (!recipients) {
throw new Error("No recipients set for completion emails")
}

// TODO: one completion per user?
const result = await prisma.$queryRaw<
Array<{ email: string; completion_date: string; tier: number }>
>`
SELECT co.tier, u.email, co.completion_date
FROM "user" u
JOIN completion co on u.id = co.user_id
WHERE co.course_id = '49cbadd8-be32-454f-9b7d-e84d52100b74'::uuid
AND u.email ILIKE '%@slu.cz'
GROUP BY co.tier, u.email, co.completion_date
ORDER BY co.completion_date DESC, u.email, co.tier;
`

const tiers: Record<number, Array<string>> = {}

for (const { email, completion_date, tier } of result) {
if (!tiers[tier]) {
tiers[tier] = []
}

tiers[tier].push(`${email},${completion_date},${tier}`)
}

const tierNames: Record<number, string> = {
1: "Beginner",
2: "Intermediate",
3: "Advanced",
}

let text = ""

for (let tier = 1; tier <= 3; tier++) {
text += `${tierNames[tier]}:\n\n`
if (!tiers[tier]?.length) {
text += "No completions for this tier\n"
} else {
text += tiers[tier].join("\n")
}
text += "\n"
}

await sendMail({
to: recipients,
text,
subject: "Building AI completions",
})
}

opavaStatsEmailer()
.then(() => prisma.$disconnect().then(() => process.exit(0)))
.catch((error) => {
logger.error(error)
return prisma.$disconnect().then(() => process.exit(1))
})
2 changes: 0 additions & 2 deletions backend/bin/pragueStatsEmailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const pragueStatsEmailer = async () => {
SELECT co.tier, u.email, co.completion_date
FROM "user" u
JOIN completion co on u.id = co.user_id
JOIN user_course_setting ucs on u.id = ucs.user_id and co.course_id = ucs.course_id
WHERE co.course_id = '49cbadd8-be32-454f-9b7d-e84d52100b74'::uuid
AND ucs.other->>'bai_completion' = 'true'
AND u.email ILIKE '%@vse.cz'
GROUP BY co.tier, u.email, co.completion_date
ORDER BY co.completion_date DESC, u.email, co.tier;
Expand Down
35 changes: 35 additions & 0 deletions helm/templates/opava-stats-mailer-cronjob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: opava-stats-emailer
labels:
{{- include "helm.labels" . | nindent 4 }}
spec:
# Run at 10:00 AM on 31st of January and 31st of July
schedule: "0 10 31 1,7 *"
startingDeadlineSeconds: 3600
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 3
jobTemplate:
metadata:
labels:
{{- include "helm.selectorLabels" . | nindent 8 }}
spec:
activeDeadlineSeconds: 7200
template:
spec:
restartPolicy: OnFailure
containers:
- name: opava-stats-emailer
image: "{{ .Values.image.repository }}/moocfi-backend:{{ .Values.image.tag | default .Chart.AppVersion }}"
command: ["sh", "-c", "npm run opava-stats-emailer"]
envFrom:
- secretRef:
name: backend-secret
env:
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: new-redis
key: redis-password
4 changes: 2 additions & 2 deletions helm/templates/prague-stats-mailer-cronjob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ metadata:
labels:
{{- include "helm.labels" . | nindent 4 }}
spec:
# Run at 10:00 AM on 31st of August
schedule: "0 10 31 8 *"
# Run at 10:00 AM on 30th of November
schedule: "0 10 30 11 *"
startingDeadlineSeconds: 3600
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
Expand Down

0 comments on commit 2c1d518

Please sign in to comment.