1414from django .contrib .auth import get_user_model
1515from django .core .cache import cache
1616from django .db import connection , models
17- from django .db .models import Count , F , Prefetch , Q , Sum
17+ from django .db .models import Count , F , Prefetch , Sum
1818from django .db .models .functions import Lower
1919from django .utils import timezone
2020from django .utils .html import strip_tags
@@ -1103,7 +1103,7 @@ def generate_word_cloud(self):
11031103 def calculate_results (self , segment = None ):
11041104 from stop_words import safe_get_stop_words
11051105
1106- from ureport .stats .models import AgeSegment , GenderSegment , PollStats , PollStatsCounter , PollWordCloud
1106+ from ureport .stats .models import AgeSegment , GenderSegment , PollStatsCounter , PollWordCloud
11071107
11081108 org = self .poll .org
11091109 open_ended = self .is_open_ended ()
@@ -1160,32 +1160,6 @@ def calculate_results(self, segment=None):
11601160 osm_id = boundary .get ("osm_id" ).upper ()
11611161
11621162 categories_results = (
1163- PollStats .get_question_stats (org .id , self )
1164- .filter (
1165- Q (location__id = boundary ["id" ])
1166- | Q (location__parent__id = boundary ["id" ])
1167- | Q (location__parent__parent__id = boundary ["id" ])
1168- )
1169- .exclude (flow_result_category = None )
1170- .values ("flow_result_category__category" )
1171- .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
1172- .values ("label" , "count" )
1173- )
1174- categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in categories_results }
1175-
1176- unset_count_stats = (
1177- PollStats .get_question_stats (org .id , self )
1178- .filter (flow_result_category = None )
1179- .filter (
1180- Q (location__id = boundary ["id" ])
1181- | Q (location__parent__id = boundary ["id" ])
1182- | Q (location__parent__parent__id = boundary ["id" ])
1183- )
1184- .aggregate (Sum ("count" ))
1185- )
1186- unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
1187-
1188- new_categories_results = (
11891163 PollStatsCounter .objects .filter (
11901164 org_id = org .id ,
11911165 flow_result = self .flow_result ,
@@ -1196,11 +1170,11 @@ def calculate_results(self, segment=None):
11961170 .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
11971171 .values ("label" , "count" )
11981172 )
1199- new_categories_results_dict = {
1200- elt ["label" ].lower (): elt ["count" ] for elt in new_categories_results
1173+ categories_results_dict = {
1174+ elt ["label" ].lower (): elt ["count" ] for elt in categories_results
12011175 }
12021176
1203- new_unset_count_stats = (
1177+ unset_count_stats = (
12041178 PollStatsCounter .objects .filter (
12051179 org_id = org .id ,
12061180 flow_result = self .flow_result ,
@@ -1209,17 +1183,7 @@ def calculate_results(self, segment=None):
12091183 .filter (flow_result_category = None )
12101184 .aggregate (Sum ("count" ))
12111185 )
1212- new_unset_count = new_unset_count_stats .get ("count__sum" , 0 ) or 0
1213-
1214- self ._log_stats_comparison (
1215- categories_results_dict ,
1216- new_categories_results_dict ,
1217- unset_count ,
1218- new_unset_count ,
1219- "location" ,
1220- f"{ location_part } :{ osm_id } " ,
1221- org .id ,
1222- )
1186+ unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
12231187
12241188 for category_obj in categories_qs :
12251189 key = category_obj .flow_result_category .category .lower ()
@@ -1260,23 +1224,6 @@ def calculate_results(self, segment=None):
12601224 data_key = "35+"
12611225
12621226 categories_results = (
1263- PollStats .get_question_stats (org .id , self )
1264- .filter (age_segment_id = age ["id" ])
1265- .exclude (flow_result_category = None )
1266- .values ("flow_result_category__category" )
1267- .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
1268- .values ("label" , "count" )
1269- )
1270- categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in categories_results }
1271-
1272- unset_count_stats = (
1273- PollStats .get_question_stats (org .id , self )
1274- .filter (flow_result_category = None , age_segment_id = age ["id" ])
1275- .aggregate (Sum ("count" ))
1276- )
1277- unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
1278-
1279- new_categories_results = (
12801227 PollStatsCounter .objects .filter (
12811228 org_id = org .id ,
12821229 flow_result = self .flow_result ,
@@ -1287,11 +1234,11 @@ def calculate_results(self, segment=None):
12871234 .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
12881235 .values ("label" , "count" )
12891236 )
1290- new_categories_results_dict = {
1291- elt ["label" ].lower (): elt ["count" ] for elt in new_categories_results
1237+ categories_results_dict = {
1238+ elt ["label" ].lower (): elt ["count" ] for elt in categories_results
12921239 }
12931240
1294- new_unset_count_stats = (
1241+ unset_count_stats = (
12951242 PollStatsCounter .objects .filter (
12961243 org_id = org .id ,
12971244 flow_result = self .flow_result ,
@@ -1300,17 +1247,7 @@ def calculate_results(self, segment=None):
13001247 .filter (flow_result_category = None )
13011248 .aggregate (Sum ("count" ))
13021249 )
1303- new_unset_count = new_unset_count_stats .get ("count__sum" , 0 ) or 0
1304-
1305- self ._log_stats_comparison (
1306- categories_results_dict ,
1307- new_categories_results_dict ,
1308- unset_count ,
1309- new_unset_count ,
1310- "age" ,
1311- f"{ age ['min_age' ]} :{ age ['max_age' ]} " ,
1312- org .id ,
1313- )
1250+ unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
13141251
13151252 categories = []
13161253 for category_obj in categories_qs :
@@ -1338,24 +1275,6 @@ def calculate_results(self, segment=None):
13381275 results = []
13391276 for gender in genders :
13401277 categories_results = (
1341- PollStats .get_question_stats (org .id , self )
1342- .filter (gender_segment_id = gender ["id" ])
1343- .exclude (flow_result_category = None )
1344- .values ("flow_result_category__category" )
1345- .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
1346- .values ("label" , "count" )
1347- )
1348- categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in categories_results }
1349- categories = []
1350-
1351- unset_count_stats = (
1352- PollStats .get_question_stats (org .id , self )
1353- .filter (flow_result_category = None , gender_segment_id = gender ["id" ])
1354- .aggregate (Sum ("count" ))
1355- )
1356- unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
1357-
1358- new_categories_results = (
13591278 PollStatsCounter .objects .filter (
13601279 org_id = org .id ,
13611280 flow_result = self .flow_result ,
@@ -1366,11 +1285,12 @@ def calculate_results(self, segment=None):
13661285 .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
13671286 .values ("label" , "count" )
13681287 )
1369- new_categories_results_dict = {
1370- elt ["label" ].lower (): elt ["count" ] for elt in new_categories_results
1288+ categories_results_dict = {
1289+ elt ["label" ].lower (): elt ["count" ] for elt in categories_results
13711290 }
1291+ categories = []
13721292
1373- new_unset_count_stats = (
1293+ unset_count_stats = (
13741294 PollStatsCounter .objects .filter (
13751295 org_id = org .id ,
13761296 flow_result = self .flow_result ,
@@ -1379,17 +1299,7 @@ def calculate_results(self, segment=None):
13791299 .filter (flow_result_category = None )
13801300 .aggregate (Sum ("count" ))
13811301 )
1382- new_unset_count = new_unset_count_stats .get ("count__sum" , 0 ) or 0
1383-
1384- self ._log_stats_comparison (
1385- categories_results_dict ,
1386- new_categories_results_dict ,
1387- unset_count ,
1388- new_unset_count ,
1389- "gender" ,
1390- gender ["gender" ],
1391- org .id ,
1392- )
1302+ unset_count = unset_count_stats .get ("count__sum" , 0 ) or 0
13931303
13941304 for category_obj in categories_qs :
13951305 key = category_obj .flow_result_category .category .lower ()
@@ -1411,27 +1321,14 @@ def calculate_results(self, segment=None):
14111321 )
14121322
14131323 else :
1414- categories_results = (
1415- PollStats .get_question_stats (org .id , self )
1416- .exclude (flow_result_category = None )
1417- .values ("flow_result_category__category" )
1418- .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
1419- .values ("label" , "count" )
1420- )
1421- categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in categories_results }
1422-
1423- new_stats_count = (
1324+ stats_count = (
14241325 PollStatsCounter .objects .filter (org_id = org .id , flow_result = self .flow_result , scope = "all" )
14251326 .exclude (flow_result_category = None )
14261327 .values ("flow_result_category__category" )
14271328 .annotate (label = F ("flow_result_category__category" ), count = Sum ("count" ))
14281329 .values ("label" , "count" )
14291330 )
1430- new_categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in new_stats_count }
1431-
1432- self ._log_stats_comparison (
1433- categories_results_dict , new_categories_results_dict , None , None , "total" , "all" , org .id
1434- )
1331+ categories_results_dict = {elt ["label" ].lower (): elt ["count" ] for elt in stats_count }
14351332
14361333 categories = []
14371334
@@ -1482,23 +1379,16 @@ def get_responded(self):
14821379 return self .calculate_responded ()
14831380
14841381 def calculate_responded (self ):
1485- from ureport .stats .models import PollStats , PollStatsCounter
1382+ from ureport .stats .models import PollStatsCounter
14861383
14871384 key = PollQuestion .POLL_QUESTION_RESPONDED_CACHE_KEY % (self .poll .org .pk , self .poll .pk , self .pk )
1488- responded_stats = (
1489- PollStats .get_question_stats (self .poll .org_id , question = self )
1490- .exclude (flow_result_category = None )
1491- .aggregate (Sum ("count" ))
1492- )
14931385
1494- new_responded_stats = (
1386+ responded_stats = (
14951387 PollStatsCounter .objects .filter (org_id = self .poll .org_id , flow_result = self .flow_result , scope = "all" )
14961388 .exclude (flow_result_category = None )
14971389 .aggregate (Sum ("count" ))
14981390 )
14991391
1500- self ._log_stats_comparison (responded_stats , new_responded_stats , None , None , "total" , "all" , self .poll .org_id )
1501-
15021392 results = responded_stats .get ("count__sum" , 0 ) or 0
15031393 cache .set (key , {"results" : results }, None )
15041394 return results
@@ -1514,18 +1404,14 @@ def get_polled(self):
15141404 return self .calculate_polled ()
15151405
15161406 def calculate_polled (self ):
1517- from ureport .stats .models import PollStats , PollStatsCounter
1407+ from ureport .stats .models import PollStatsCounter
15181408
15191409 key = PollQuestion .POLL_QUESTION_POLLED_CACHE_KEY % (self .poll .org .pk , self .poll .pk , self .pk )
15201410
1521- polled_stats = PollStats .get_question_stats (self .poll .org_id , question = self ).aggregate (Sum ("count" ))
1522- results = polled_stats .get ("count__sum" , 0 ) or 0
1523-
1524- new_polled_stats = PollStatsCounter .objects .filter (
1411+ polled_stats = PollStatsCounter .objects .filter (
15251412 org_id = self .poll .org_id , flow_result = self .flow_result , scope = "all"
15261413 ).aggregate (Sum ("count" ))
1527-
1528- self ._log_stats_comparison (polled_stats , new_polled_stats , None , None , "total" , "all" , self .poll .org_id )
1414+ results = polled_stats .get ("count__sum" , 0 ) or 0
15291415
15301416 cache .set (key , {"results" : results }, None )
15311417 return results
0 commit comments