Skip to content

Commit

Permalink
Merge pull request #27 from leaonline/patch-get-responses
Browse files Browse the repository at this point in the history
Patch get responses
  • Loading branch information
jankapunkt committed Oct 12, 2023
2 parents 794311f + 0fbd8e0 commit febf08a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .settingsschema.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ const settingsSchema = schema({
active: Boolean,
dryRun: Boolean
}),
getResponses: schema({
active: Boolean,
dryRun: Boolean
}),
generateAccounts: schema({
dryRun: Boolean,
active: Boolean,
Expand Down
7 changes: 7 additions & 0 deletions fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
mongorestore --host="127.0.0.1" --port="3001" \
--archive=$1 --gzip --verbose \
--nsInclude="leaonline-otulea.*" \
--nsExclude='leaonline-otulea.meteor_accounts_loginServiceConfiguration' \
--nsExclude='leaonline-otulea.meteor_oauth_pendingCredentials' \
--nsFrom="leaonline-otulea.*" --nsTo="meteor.*" --convertLegacyIndexes
103 changes: 103 additions & 0 deletions imports/patches/getResponses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Meteor } from 'meteor/meteor'
import { Response } from '../contexts/response/Response'
import { Unit } from '../contexts/Unit'
import fs from 'fs'

const fields = {
responseId: 1,
userId: 1,
code: 1,
tag: 1,
sessionId: 1,
unitCode: 1,
page: 1,
contentId: 1,
itemId: 1,
score: 1,
responses: 1
}

export const getResponses = ({ dryRun }) => {
const rows = [
Object.keys(fields).join(';')
]

Response.collection().find().forEach(responseDoc => {
const userDoc = Meteor.users.findOne(responseDoc.userId)
const unitDoc = Unit.collection().findOne(responseDoc.unitId)

if (!userDoc || userDoc.isDemoUser || userDoc.isDemo || userDoc.debug) {
return console.debug('skip invalid user ', responseDoc.userId)
}

if (!unitDoc) {
return console.debug('skip missing unit ', responseDoc.unitId)
}

const row = toRow({ responseDoc, userDoc, unitDoc })
rows.push(row)
})

const csvContent = rows.join('\n')

if (!dryRun) {
const timestamp = new Date().toUTCString().replace(/\s+/, '')
const filePath = `${process.cwd()}/competencies-${timestamp}.csv`
console.debug(filePath, 'will be saved')

fs.writeFile(filePath, csvContent, (err) => {
if (err) {
console.log(filePath, err)
}
else {
console.log(filePath, 'saved')
}
})
}
}

const toRow = ({ responseDoc, userDoc, unitDoc }) => {
const userId = userDoc._id
const code = userDoc.username
const tag = userDoc.comment || ''
const { sessionId, responses, page, contentId, scores } = responseDoc
const unitCode = unitDoc.shortCode
const itemId = getItemId({ contentId, page, unitDoc })

let score = 0
scores.forEach((entry) => {
const value = entry.score === 'true' ? 1 : 0
score = (score + value) / scores.length
})

return [
responseDoc._id,
userId,
code,
tag,
sessionId,
unitCode,
page,
contentId,
itemId,
score,
`"${responses.join(',')}"`
].join(';')
}

const getItemId = ({ contentId, page, unitDoc }) => {
const contentPage = unitDoc.pages[page]

if (!contentPage) return '?'

let itemIndex = -1
contentPage.content.some(entry => {
if (entry.type === 'item') {
itemIndex++
}

return entry.contentId === contentId
})

return `${unitDoc.shortCode}_${page}_${itemIndex}`
}
8 changes: 8 additions & 0 deletions imports/startup/server/patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { addDimensionToFeedback } from '../../patches/addDimensionToFeedback'
import { generateAccounts } from '../../patches/generateAccounts'
import { removeDeadAccounts } from '../../patches/removeDeadAccounts'
import { alphaUsers } from '../../patches/alphaUsers'
import { getResponses } from '../../patches/getResponses'

const appName = Meteor.settings.public.app.label
const { notify: defaultNotify, replyTo, from } = Meteor.settings.email
Expand Down Expand Up @@ -71,3 +72,10 @@ if (patches.alphaUsers?.active) {
alphaUsers(patches.alphaUsers)
})
}

if (patches?.getResponses?.active) {
console.debug('[patches]: run getResponses')
Meteor.defer(function () {
getResponses(patches.getResponses)
})
}
4 changes: 4 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
"amount": 60,
"comment": "generated by admin@example.com",
"notify": ["admin@example.com"]
},
"getResponses": {
"active": true,
"dryRun": false
}
}
}

0 comments on commit febf08a

Please sign in to comment.