Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 1443 → Rev 1444

/trunk/scripts/modules/maj/201310-propret.sql
New file
0,0 → 1,103
DELIMITER |
 
-- initialisation (structure)
DROP PROCEDURE IF EXISTS update_struct;
CREATE PROCEDURE update_struct()
BEGIN
-- ajout de la colonne "proposition_retenue"
DECLARE CONTINUE HANDLER FOR SQLSTATE '42S21' SELECT "colonne proposition_retenue déjà existante"; /* = Duplicate column name */
ALTER TABLE `BASEDEL`.`del_commentaire` ADD `proposition_retenue` INT(1) NOT NULL DEFAULT 0 COMMENT "La proposition \"validée\" une fois que l'auteur à validé et que le nom_sel de cel_obs a été modifié à partir du nom_sel de del_commentaire."; -- '
END;
|
 
-- petite functon bonus
DROP PROCEDURE IF EXISTS getCommentaires;
CREATE PROCEDURE getCommentaires(IN _s1 INT(20))
BEGIN
SELECT id_commentaire, ce_observation, ce_proposition, texte, nom_sel, nom_sel_nn, proposition_retenue FROM `BASEDEL`.`del_commentaire` WHERE ce_observation = _s1;
END;
|
 
-- procédure créant des tables temporaires permettant de faciliter le processus de MAJ
DROP PROCEDURE rebuild_retenues;
CREATE PROCEDURE rebuild_retenues()
BEGIN
DROP TEMPORARY TABLE IF EXISTS _temp_having_retenue, _temp_multi_prop, _temp_mono_prop;
 
-- les observations déjà retenues (pour pouvoir être exclues des processus postérieurs)
CREATE TEMPORARY TABLE _temp_having_retenue ENGINE=MEMORY AS ( \
SELECT ce_observation FROM `BASEDEL`.`del_commentaire` co WHERE proposition_retenue = 1);
 
-- observations ayant plusieurs propositions (= commentaire dont nom_sel IS NOT NULL)
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_multi_prop ENGINE=MEMORY AS ( \
SELECT ce_observation FROM `BASEDEL`.`del_commentaire` co \
WHERE nom_sel IS NOT NULL \
AND proposition_retenue = 0 /* == ce_observation NOT IN (SELECT ce_proposition FROM _temp_having_retenue) */ \
GROUP BY ce_observation HAVING COUNT(id_commentaire) > 1); -- 1053
 
-- observations ayant une seule proposition (= commentaire dont nom_sel IS NOT NULL)
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_mono_prop ENGINE=MEMORY AS ( \
SELECT ce_observation FROM `BASEDEL`.`del_commentaire` \
WHERE nom_sel IS NOT NULL \
AND proposition_retenue = 0 /* == ce_observation NOT IN (SELECT ce_proposition FROM _temp_having_retenue) */ \
GROUP BY ce_observation HAVING COUNT(id_commentaire) = 1);
 
END;
|
 
DELIMITER ;
 
 
CALL update_struct();
-- table temporaire de tous les comptages (somme des votes pour chaque proposition de del_commentaire):
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_stats ENGINE=MEMORY AS ( SELECT ce_proposition, SUM(valeur) AS val FROM `BASEDEL`.`del_commentaire_vote` GROUP BY ce_proposition); -- 5912
-- propositions ayant eu au moins 1 vote positif non-anonyme
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_authvotepos ENGINE=MEMORY AS (SELECT DISTINCT ce_proposition FROM `BASEDEL`.`del_commentaire_vote` WHERE valeur = 1 AND LENGTH(ce_utilisateur) != 32 ORDER BY ce_proposition); -- 3794
CALL rebuild_retenues(); -- initialisation
 
-- core
-- SELECT ce_observation, id_commentaire FROM `BASEDEL`.`del_commentaire` co INNER JOIN _temp_multi_prop mp USING (ce_observation) INNER JOIN `BASECEL`.`cel_obs` ce ON mp.ce_observation = id_observation AND (co.nom_sel = ce.nom_sel AND co.nom_sel_nn = ce.nom_sel_nn) WHERE co.nom_sel_nn != 0 AND co.proposition_retenue = 0; -- 470, dont 464 ce_observation unique
-- SELECT ce_observation, id_commentaire FROM `BASEDEL`.`del_commentaire` co INNER JOIN _temp_multi_prop mp USING (ce_observation) INNER JOIN `BASECEL`.`cel_obs` ce ON mp.ce_observation = id_observation AND (co.nom_sel = ce.nom_sel OR co.nom_sel_nn = ce.nom_sel_nn) WHERE co.nom_sel_nn != 0 AND co.proposition_retenue = 0; -- 520, dont 493 ce_observation unique
 
-- Étant donné les doublons nous pouvons commencer ou finir par filtrer sur les stats de vote
-- Comme il est plus simple de finir par ça, commençons correctement par les nom_sel comme ci-dessus avec ceux sans ambiguité:
-- C'est à dire observations dotées de multiples proposition dont une et une seule match exactement le nom-sel du carnet en ligne
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_updatable1 ENGINE=MEMORY AS ( \
SELECT /* ce_observation, */ id_commentaire FROM `BASEDEL`.`del_commentaire` co \
INNER JOIN _temp_multi_prop mp USING (ce_observation) /* faisant partie des observations ayant plusieurs propositions */ \
INNER JOIN `BASECEL`.`cel_obs` ce ON mp.ce_observation = id_observation AND (co.nom_sel = ce.nom_sel AND co.nom_sel_nn = ce.nom_sel_nn) \
WHERE co.nom_sel_nn != 0 \
GROUP BY co.ce_observation HAVING COUNT(co.id_commentaire) = 1 /* observations n'ayant pas d'ambiguité sur le commentaire correspondant au nom du carnet en ligne */ ); -- 335
 
UPDATE `BASEDEL`.`del_commentaire` co SET co.proposition_retenue = 1 WHERE co.id_commentaire IN (SELECT id_commentaire FROM _temp_updatable1);
SELECT ROW_COUNT() AS "nb MAJ par match sur nom_sel sans ambiguité";
CALL rebuild_retenues();
 
 
-- Pour les observations ayant plusieurs nom-sel communs au carnet en ligne, il s'agit de ne sélectionner que celle
-- ayant le plus de vote (idéalement les propositions doublonnes ne devraient pas exister)
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_updatable2 ENGINE=MEMORY AS ( \
SELECT co.ce_observation, id_commentaire, i1.val FROM `BASEDEL`.`del_commentaire` co \
INNER JOIN _temp_multi_prop mp USING (ce_observation) /* uniquement les multi-propositions, inclue le check sur _temp_having_retenue */ \
INNER JOIN (SELECT ce_proposition, val FROM _temp_stats) AS i1 ON i1.ce_proposition = co.id_commentaire /* et ayant des votes */ \
INNER JOIN `BASECEL`.`cel_obs` ce ON co.ce_observation = ce.id_observation AND (co.nom_sel = ce.nom_sel AND co.nom_sel_nn = ce.nom_sel_nn) /* et un nom_sel acceptable */ \
WHERE co.nom_sel_nn != 0 \
GROUP BY ce_observation HAVING i1.val = MAX(i1.val) /* parmis les doublonnes, prendre la proposition ayant le plus de SUM(votes) */ \
ORDER BY ce_observation ); -- 249 (sans le processus précédent, moins autrement s'il l'on exclue les observation déjà "validée")
 
UPDATE `BASEDEL`.`del_commentaire` co SET co.proposition_retenue = 1 WHERE co.id_commentaire IN (SELECT id_commentaire FROM _temp_updatable2);
SELECT ROW_COUNT() AS "nb MAJ par nombre de vote";
CALL rebuild_retenues();
 
-- Enfin, les observation n'ayant qu'une seule proposition sont passées comme retenues si
-- elles ont été notée positivement au moins une fois par un utilisateur authentifié
CREATE TEMPORARY TABLE IF NOT EXISTS _temp_updatable3 ENGINE=MEMORY AS ( \
SELECT co.id_commentaire FROM `BASEDEL`.`del_commentaire` co \
INNER JOIN _temp_mono_prop AS i1 USING (ce_observation) /* l'inverse de _temp_multi_prop, inclue le check sur _temp_having_retenue */ \
INNER JOIN _temp_authvotepos ta ON co.id_commentaire = ta.ce_proposition /* et uniquement si faisant partie des commentaires doté d'un vote positif non-anonyme */ \
INNER JOIN `BASECEL`.`cel_obs` ce ON co.ce_observation = ce.id_observation AND (co.nom_sel = ce.nom_sel AND co.nom_sel_nn = ce.nom_sel_nn) \
WHERE co.nom_sel_nn != 0); -- 2216 sans test sur cel_obs, 2193 avec
 
UPDATE `BASEDEL`.`del_commentaire` co SET co.proposition_retenue = 1 WHERE co.id_commentaire IN (SELECT id_commentaire FROM _temp_updatable3);
SELECT ROW_COUNT() AS "nb MAJ ayant 1 seule proposition mais un vote non-anonyme positif";
CALL rebuild_retenues();
/trunk/scripts/modules/maj/Makefile
19,7 → 19,18
o_maj1: clean o_201309-index-views
cat $(fichiers_generes) > maj1.comp.sql
 
 
# maj1 contient: 201310-propret.sql
o_maj2: fichiers_generes = $(addsuffix .comp.sql,$(filter-out clean,$?))
o_maj2: clean o_201310-propret
cat $(fichiers_generes) > maj2.comp.sql
 
 
# SHOW INDEX FROM cel_obs WHERE Key_name = 'transmission';
# SHOW INDEX FROM cel_obs WHERE Key_name = 'date_transmission';
o_201309-index-views:
$(call do_subst,201309-index-views.sql) > $@.comp.sql
 
# SHOW COLUMNS FROM del_commentaire LIKE 'proposition_retenu';
o_201310-propret:
$(call do_subst,201310-propret.sql) > $@.comp.sql