-
Notifications
You must be signed in to change notification settings - Fork 11
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
Mejora carga pendiente #17
Conversation
…rgas aquellas que fueron parcialmente cargadas (solo la primer eleccion)
elecciones/models.py
Outdated
cargadas=Count( | ||
Subquery( | ||
# cantidad de elecciones para las que hay votos reportados en la mesa | ||
VotoMesaReportado.objects.filter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esta es la query problematica, parece haber un problema con el OuterRef
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lo estuve viendo y no sé porque funciona mal el Count antes del Subquery. El Subquery solo anda bien salvo cuando no hay ninguna instancia. Eso se puede parchar con Coalesce creo:
.annotate(
cargadas=Coalesce(
Subquery(
# cantidad de elecciones para las que hay votos reportados en la mesa
VotoMesaReportado.objects.filter(
mesa__id=OuterRef('id')
).values('eleccion').distinct()
# Eleccion.objects.filter(
# mesa__id=OuterRef('id'),
# votomesareportado__isnull=False
# ).distinct().values('id')
), 0
)
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gonzigaran, al final no era correcta esta solucion. la subquery lo que devuelve son ids, y el test pasaba de pedo porque el coalesce lo castea y usa el id resultante (no hay count de los ids).
igual ya lo resolví con una denormalizacion y un contador que se incrementa en un post_save.
gracias por el intento igual
elecciones/models.py
Outdated
) | ||
) | ||
) | ||
# qs.values('id', 'a_cargar', 'cargadas') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Para el escenario planteado test_con_carga_pendiente_incluye_mesa_con_eleccion_sin_cargar
(Pdb) qs.values('id', 'a_cargar', 'cargadas')
<QuerySet [{'id': 1, 'a_cargar': 1, 'cargadas': 0}, {'id': 2, 'a_cargar': 1, 'cargadas': 1}, {'id': 3, 'a_cargar': 3, 'cargadas': 3}]>
(Pdb)
Como se ve, la mesa 3 figura con 3 elecciones cargadas, cuando en realidad se cargaron datos de una sola.
Lo llamativo es que al hacer la query anidada explicitamente, el resultado es correcto
(Pdb) VotoMesaReportado.objects.filter(mesa__id=3).values('eleccion').distinct()
<QuerySet [{'eleccion': 1}]>
).distinct() | ||
|
||
return qs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
por si sirve, esta es la query resultante
SELECT DISTINCT "elecciones_mesa"."id",
"elecciones_mesa"."estado",
"elecciones_mesa"."hora_escrutada",
"elecciones_mesa"."numero",
"elecciones_mesa"."es_testigo",
"elecciones_mesa"."circuito_id",
"elecciones_mesa"."lugar_votacion_id",
"elecciones_mesa"."url",
"elecciones_mesa"."electores",
"elecciones_mesa"."taken",
"elecciones_mesa"."orden_de_carga",
"elecciones_mesa"."carga_confirmada",
COUNT("elecciones_mesa_eleccion"."eleccion_id") AS "a_cargar",
COUNT(
(SELECT DISTINCT U0."eleccion_id"
FROM "elecciones_votomesareportado" U0
WHERE U0."mesa_id" = ("elecciones_mesa"."id"))) AS "cargadas",
EXISTS
(SELECT U0."id", U0."created", U0."modified", U0."problema", U0."mesa_id", U0."reportado_por_id", U0."descripcion", U0."estado", U0."resuelto_por_id"
FROM "problemas_problema" U0
WHERE (U0."mesa_id" = ("elecciones_mesa"."id")
AND NOT (U0."estado" = resuelto
AND U0."estado" IS NOT NULL))) AS "tiene_problemas"
FROM "elecciones_mesa"
INNER JOIN "adjuntos_attachment" ON ("elecciones_mesa"."id" = "adjuntos_attachment"."mesa_id")
LEFT OUTER JOIN "elecciones_mesa_eleccion" ON ("elecciones_mesa"."id" = "elecciones_mesa_eleccion"."mesa_id")
WHERE ("adjuntos_attachment"."id" IS NOT NULL
AND "elecciones_mesa"."orden_de_carga" >= 1
AND ("elecciones_mesa"."taken" IS NULL
OR "elecciones_mesa"."taken" < 2019-05-06 04:22:56.600570+00:00)
AND EXISTS
(SELECT U0."id", U0."created", U0."modified", U0."problema", U0."mesa_id", U0."reportado_por_id", U0."descripcion", U0."estado", U0."resuelto_por_id"
FROM "problemas_problema" U0
WHERE (U0."mesa_id" = ("elecciones_mesa"."id")
AND NOT (U0."estado" = resuelto
AND U0."estado" IS NOT NULL))) = FALSE)
GROUP BY "elecciones_mesa"."id",
EXISTS
(SELECT U0."id", U0."created", U0."modified", U0."problema", U0."mesa_id", U0."reportado_por_id", U0."descripcion", U0."estado", U0."resuelto_por_id"
FROM "problemas_problema" U0
WHERE (U0."mesa_id" = ("elecciones_mesa"."id")
AND NOT (U0."estado" = resuelto
AND U0."estado" IS NOT NULL)))
HAVING COUNT(
(SELECT DISTINCT U0."eleccion_id"
FROM "elecciones_votomesareportado" U0
WHERE U0."mesa_id" = ("elecciones_mesa"."id"))) < (COUNT("elecciones_mesa_eleccion"."eleccion_id"))
assert set(Mesa.con_carga_pendiente()) == {m2} | ||
|
||
|
||
def test_con_carga_pendiente_incluye_mesa_con_eleccion_sin_cargar(db): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
este es el test que falla
Basado en la idea de Gonzalo Zigarán @gonzigaran
redirige a eleccion sin cargar de la mesa
Arreglos para correr tests en el entorno de desarrollo local
el método "con_carga_pendiente()" es el que devuelve las mesas que pueden ser asignables para cargar.
Existia un bug con mesas que tenian problemas pero resueltos que ahora se arregla via una annotation.
pero ademas intento contemplar el caso de mesas con multiples categorias ("elecciones") donde se carga una pero luego quedan "colgadas" las siguientes ya que en el dispatcher actual, si tiene cargas para la eleccion con id=1, esa mesa ya no será considerada en el pool aunque tenga más elecciones a cargar.
No obstante esto ultimo no estaría funcionando. ver
test_con_carga_pendiente_incluye_mesa_con_eleccion_sin_cargar()