From 1758567767b19c27146cdb82c13561d5439cffab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Mon, 6 Jan 2020 09:48:50 +0100 Subject: [PATCH] Version19.112.10 --- swad_changelog.h | 4 +-- swad_degree.c | 1 + swad_global.c | 2 ++ swad_global.h | 5 ++++ swad_user.c | 65 ++++++++++++++++++++++++++++++++++++++++-------- swad_user.h | 1 + 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index ad3017803..801d686e9 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -492,7 +492,7 @@ enscript -2 --landscape --color --file-align=2 --highlight --line-numbers -o - * En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 19.112.9 (2020-01-05)" +#define Log_PLATFORM_VERSION "SWAD 19.112.10 (2020-01-06)" #define CSS_FILE "swad19.112.css" #define JS_FILE "swad19.91.1.js" /* @@ -501,8 +501,8 @@ ps2pdf source.ps destination.pdf // TODO: No se puede entrar con DNI '1' suponiendo que no tenga password ¿por qué? // TODO: Mapas más estrechos en móvil // TODO: Quitar todos los EXTRA_DATA. -// TODO: ¿ Cachear Usr_GetNumUsrsInCrssOfDeg ? + Version 19.112.10:Jan 06, 2020 Optimization in number of users in courses of a degree. (278468 lines) Version 19.112.9: Jan 05, 2020 Optimization in number of users in a course. (278419 lines) Version 19.112.8: Jan 05, 2020 Optimization in number of courses in a degree. (278396 lines) Version 19.112.7: Jan 05, 2020 Optimization in number of degrees in a centre. (278384 lines) diff --git a/swad_degree.c b/swad_degree.c index deada882a..e17840c5c 100644 --- a/swad_degree.c +++ b/swad_degree.c @@ -1514,6 +1514,7 @@ void Deg_RemoveDegreeCompletely (long DegCod) /***** Flush caches *****/ Crs_FlushCacheNumCrssInDeg (); + Usr_FlushCacheNumUsrsInCrssOfDeg (); /***** Delete all the degrees in sta_degrees table not present in degrees table *****/ Pho_RemoveObsoleteStatDegrees (); diff --git a/swad_global.c b/swad_global.c index a79cdc9ec..b21008722 100644 --- a/swad_global.c +++ b/swad_global.c @@ -417,6 +417,8 @@ void Gbl_InitializeGlobals (void) Crs_FlushCacheNumCrssInDeg (); Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (); Usr_FlushCacheNumUsrsInCrssOfCtr (); + Usr_FlushCacheNumUsrsInCrssOfDeg (); + Usr_FlushCacheNumUsrsInCrs (); Usr_FlushCacheUsrIsSuperuser (); Usr_FlushCacheUsrBelongsToIns (); Usr_FlushCacheUsrBelongsToCtr (); diff --git a/swad_global.h b/swad_global.h index 44da33dc0..e56a044dc 100644 --- a/swad_global.h +++ b/swad_global.h @@ -797,6 +797,11 @@ struct Globals long CtrCod; unsigned NumUsrs; } NumUsrsInCrssOfCtr[Rol_NUM_ROLES]; + struct + { + long DegCod; + unsigned NumUsrs; + } NumUsrsInCrssOfDeg[Rol_NUM_ROLES]; struct { long CrsCod; diff --git a/swad_user.c b/swad_user.c index ea8cd4011..f4e379f83 100644 --- a/swad_user.c +++ b/swad_user.c @@ -4223,18 +4223,63 @@ unsigned Usr_GetNumUsrsInCrs (Rol_Role_t Role,long CrsCod) /*********** Count how many users with a role belong to a degree *************/ /*****************************************************************************/ +void Usr_FlushCacheNumUsrsInCrssOfDeg (void) + { + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_UNK].DegCod = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_STD].DegCod = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_NET].DegCod = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_TCH].DegCod = -1L; + + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_UNK].NumUsrs = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_STD].NumUsrs = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_NET].NumUsrs = + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_TCH].NumUsrs = 0; + } + unsigned Usr_GetNumUsrsInCrssOfDeg (Rol_Role_t Role,long DegCod) { - /***** Get the number of users in courses of a degree from database ******/ - return - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in courses of a degree", - "SELECT COUNT(DISTINCT crs_usr.UsrCod)" - " FROM courses,crs_usr" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", - DegCod,(unsigned) Role); + /***** 1. Fast check: Trivial case *****/ + if (DegCod <= 0) + return 0; + + /***** 2. Fast check: Trivial case *****/ + switch (Role) + { + case Rol_UNK: // Here Rol_UNK means all + case Rol_STD: + case Rol_NET: + case Rol_TCH: + break; + default: + return 0; + } + + /***** 3. Fast check: If cached... *****/ + if (DegCod == Gbl.Cache.NumUsrsInCrssOfDeg[Role].DegCod) + return Gbl.Cache.NumUsrsInCrssOfDeg[Role].NumUsrs; + + /***** 4. Slow: number of users in courses of a degree from database *****/ + Gbl.Cache.NumUsrsInCrssOfDeg[Role].DegCod = DegCod; + if (Role == Rol_UNK) + Gbl.Cache.NumUsrsInCrssOfDeg[Rol_UNK].NumUsrs = + (unsigned) DB_QueryCOUNT ("can not get the number of users" + " in courses of a degree", + "SELECT COUNT(DISTINCT crs_usr.UsrCod)" + " FROM courses,crs_usr" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=crs_usr.CrsCod", + DegCod); + else + Gbl.Cache.NumUsrsInCrssOfDeg[Role].NumUsrs = + (unsigned) DB_QueryCOUNT ("can not get the number of users" + " in courses of a degree", + "SELECT COUNT(DISTINCT crs_usr.UsrCod)" + " FROM courses,crs_usr" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", + DegCod,(unsigned) Role); + return Gbl.Cache.NumUsrsInCrssOfDeg[Role].NumUsrs; } /*****************************************************************************/ diff --git a/swad_user.h b/swad_user.h index 78739f12c..f188dab9e 100644 --- a/swad_user.h +++ b/swad_user.h @@ -409,6 +409,7 @@ void Usr_WriteRowUsrMainData (unsigned NumUsr,struct UsrData *UsrDat, void Usr_FlushCacheNumUsrsInCrs (void); unsigned Usr_GetNumUsrsInCrs (Rol_Role_t Role,long CrsCod); +void Usr_FlushCacheNumUsrsInCrssOfDeg (void); unsigned Usr_GetNumUsrsInCrssOfDeg (Rol_Role_t Role,long DegCod); void Usr_FlushCacheNumUsrsInCrssOfCtr (void); unsigned Usr_GetNumUsrsInCrssOfCtr (Rol_Role_t Role,long CtrCod);