Skip to content

Commit

Permalink
fix csv stats
Browse files Browse the repository at this point in the history
  • Loading branch information
xtian7489 committed Oct 7, 2024
1 parent 113d435 commit a1c56cd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 57 deletions.
89 changes: 44 additions & 45 deletions lib/api-v2/stats/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,12 @@ app.get('/stats/listadoDeVotosPorVotante/csv',
usersArray.forEach((user) => {
if (user.dni) dniUserMap[user.dni] = user
})
req.dniList = dniList
req.dniUserMap = dniUserMap
// add user to vote
req.votesArray.forEach((vote) => {
if (vote.dni && dniUserMap[vote.dni]) {
vote.user = dniUserMap[`${vote.dni}`]
} else {
vote.user = null
}
})

// group votes by dni
let votesByDNI = {}
req.votesArray.forEach(vote => {
if (votesByDNI[vote.dni]) {
votesByDNI[vote.dni].push(vote)
} else {
votesByDNI[vote.dni] = [vote]
}
votesByDNI[vote.dni] = vote
})
req.votesByDNI = votesByDNI
next()
Expand All @@ -180,40 +167,38 @@ app.get('/stats/listadoDeVotosPorVotante/csv',
let DNIsArray = Object.keys(req.votesByDNI)
var infoVotes = DNIsArray.map((dni) => {
let votes = req.votesByDNI[dni]
let firstVote = votes[0]
let userVote = req.dniUserMap[dni]
let facultad = null
if (firstVote.user && firstVote.user.facultad) {
facultad = firstVote.user.facultad.nombre
} else if (firstVote.facultad) {
facultad = firstVote.facultad.nombre
} else {

if (userVote && userVote.facultad) {
facultad = userVote.facultad.nombre
} else if (votes.facultad) {
facultad = votes.facultad.nombre
}
else {
facultad = '-Sin dato-'
}
let claustro = null
if (firstVote.user && firstVote.user.claustro) {
claustro = firstVote.user.claustro.nombre
} else if (firstVote.claustro) {
claustro = firstVote.claustro.nombre
if (userVote && userVote.claustro) {
claustro = userVote.claustro.nombre
} else {
claustro = '-Sin dato-'
}

return [
escapeTxt(firstVote.dni),
escapeTxt(firstVote.user ? firstVote.user.firstName : '- No Registrado -'),
escapeTxt(firstVote.user ? firstVote.user.lastName : '- No Registrado -'),
firstVote.user ? 'Si' : 'No',
escapeTxt(votes.dni),
escapeTxt(userVote ? userVote.firstName : '- No Registrado -'),
escapeTxt(userVote ? userVote.lastName : '- No Registrado -'),
userVote ? 'Si' : 'No',
escapeTxt(facultad),
escapeTxt(claustro),
firstVote.author.dni !== firstVote.dni ? 'Presencial' : 'Online',
votes.user.dni !== votes.dni ? 'Presencial' : 'Online',
votes.length,
votes[0] ? escapeTxt(votes[0].topic.mediaTitle) : '-',
votes[1] ? escapeTxt(votes[1].topic.mediaTitle) : '-',
votes[2] ? escapeTxt(votes[2].topic.mediaTitle) : '-'
votes.voto1 ? escapeTxt(votes.voto1.mediaTitle) : '-',
votes.voto2 ? escapeTxt(votes.voto2.mediaTitle) : '-',
]
})

var data = [['DNI', 'Nombre', 'Apellido', 'Registrado?', 'Facultad', 'Claustro', 'Formato', 'Cantidad Votos', 'Voto 1', 'Voto 2']]
var data = [['DNI', 'Nombre', 'Apellido', 'Registrado?', 'Facultad', 'Formato', 'Cantidad Votos', 'Voto 1', 'Voto 2']]
data = data.concat(infoVotes)
json2csv(data, function (err, csv) {
if (err) {
Expand Down Expand Up @@ -259,13 +244,22 @@ app.get('/stats/listadoDeVotos/csv',
req.dniList = dniList
req.dniUserMap = dniUserMap
// add user to vote
const votes = []
req.votesArray.forEach((vote) => {
if (vote.dni && dniUserMap[vote.dni]) {
vote.user = dniUserMap[`${vote.dni}`]
} else {
vote.user = null
}
if (vote.voto1) votes.push({
...vote.voto1._doc,
author: vote.user,
dni: vote.dni,
user: dniUserMap[vote.dni]
})
if (vote.voto2) votes.push({
...vote.voto2._doc,
author: vote.user,
dni: vote.dni,
user: dniUserMap[vote.dni]
})
})
req.votesArray = votes
next()
},
function sendCsv(req, res, next) {
Expand Down Expand Up @@ -297,11 +291,10 @@ app.get('/stats/listadoDeVotos/csv',
escapeTxt(vote.user ? vote.user.firstName : '- No Registrado -'),
escapeTxt(vote.user ? vote.user.lastName : '- No Registrado -'),
escapeTxt(facultad),
escapeTxt(claustro),
escapeTxt(vote.topic.mediaTitle)
escapeTxt(vote.mediaTitle)
]
})
var data = [['Fecha Votacion', 'Autor DNI', 'Autor Nombre', 'Autor Apellido', 'Formato', 'DNI Votante', 'Registrado?', 'Nombre Votante', 'Apellido Votante', 'Facultad Votante', 'Claustro Votante', 'Proyecto']]
var data = [['Fecha Votacion', 'Autor DNI', 'Autor Nombre', 'Autor Apellido', 'Formato', 'DNI Votante', 'Registrado?', 'Nombre Votante', 'Apellido Votante', 'Facultad Votante', 'Proyecto']]

data = data.concat(infoVotos)
json2csv(data, function (err, csv) {
Expand Down Expand Up @@ -337,8 +330,14 @@ app.get('/stats/votosPorProyectos/csv',
// group by topic
let votesByTopic = {}
votesArray.forEach((vote) => {
if (!votesByTopic[vote.topic.id]) votesByTopic[vote.topic.id] = []
votesByTopic[vote.topic.id].push(vote)
if (vote.voto1) {
if (!votesByTopic[vote.voto1.id]) votesByTopic[vote.voto1.id] = []
votesByTopic[vote.voto1.id].push(vote.voto1)
}
if (vote.voto2) {
if (!votesByTopic[vote.voto2.id]) votesByTopic[vote.voto2.id] = []
votesByTopic[vote.voto2.id].push(vote.voto2)
}
})
req.votesByTopic = votesByTopic
next()
Expand All @@ -347,7 +346,7 @@ app.get('/stats/votosPorProyectos/csv',
var infoVotos = Object.keys(req.votesByTopic)
var infoProyectos = infoVotos.map((topicId) => {
let votes = req.votesByTopic[topicId]
let topic = votes[0].topic
let topic = votes[0]
return [
escapeTxt(topic.mediaTitle),
escapeTxt(votes.length)
Expand Down
22 changes: 15 additions & 7 deletions lib/api/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,29 @@ app.get('/stats/votacion',
// add user to vote
// clone votesArray
// let votesArray = req.votesArray
const votes = []
req.votesArray.forEach((vote) => {
if (vote.dni && dniUserMap[vote.dni]) {
vote.user = dniUserMap[`${vote.dni}`]
} else {
vote.user = null
}
if (vote.voto1) votes.push({
...vote.voto1._doc,
author: vote.user,
dni: vote.dni,
user: dniUserMap[vote.dni]
})
if (vote.voto2) votes.push({
...vote.voto2._doc,
author: vote.user,
dni: vote.dni,
user: dniUserMap[vote.dni]
})
})
// req.votesArray = votesArray
req.votesArray = votes
next()
},
async function getStats(req, res, next) {
// console.log(req.votesArray)
log('Getting stats')
// send 200
let votesCount = await dbApi.vote.getCountVotes()
let votesCount = req.votesArray.length
let dniList = req.dniList
let usersWhoDidntVoted = await dbApi.user.getUsersWhoDidntVoted(dniList)
let usersWhoDidntVotedCount = usersWhoDidntVoted.length
Expand Down
22 changes: 17 additions & 5 deletions lib/db-api/vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports.getVotesByTopic = function get (topicId) {

exports.getVotesVotacion = function () {
log('Looking for Vote of votación')

// this does not work, the voting schema does not have a value field
return Vote
.find({value: 'voto'})
.populate('topic author')
Expand All @@ -75,7 +75,7 @@ exports.getCountVotes = function getCountVotes() {
log('Getting total of votes in votes')

return Vote
.count({ value: 'voto' })
.count()
.catch(err => log('Found error %j', err))
.then(obj => {
log('Delivering %s Votes of votación', obj && obj.length)
Expand All @@ -95,12 +95,24 @@ exports.getDistinctDNI = function getDistinctDNI() {
})
}

exports.getVotesVotacionWithEverything = function () {
exports.getVotesVotacionWithEverything = async function () {
log('Looking for Vote of votación')

return Vote
.find({ value: 'voto' })
.populate('topic author claustro facultad')
.find()
.populate({
path: 'voto1',
populate: {
path: 'owner'
}
})
.populate({
path: 'voto2',
populate: {
path: 'owner'
}
})
.populate('user claustro facultad')
// primeros creados primero
.sort('createdAt')
.catch(err => log('Found error %j', err))
Expand Down

0 comments on commit a1c56cd

Please sign in to comment.