Skip to content

Commit

Permalink
Merge pull request #5185 from lwyszomi/global-stats-fix
Browse files Browse the repository at this point in the history
Improvements in global stats
  • Loading branch information
czue committed Jan 4, 2015
2 parents 599172c + a7a913a commit 842ead9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions custom/ewsghana/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class EWSGlobalStats(GlobalStats):
template_name = "ewsghana/global_stats.html"
show_supply_point_types = True
root_name = 'Country'


class EWSConfigView(BaseConfigView):
Expand Down
2 changes: 1 addition & 1 deletion custom/ilsgateway/templates/ilsgateway/global_stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h2>Global stats</h2>
</thead>
<tbody>
<tr role="row">
<td>Country</td>
<td>{{ root_name }}</td>
<td>{{ country }}</td>
</tr>
<tr role="row">
Expand Down
24 changes: 17 additions & 7 deletions custom/ilsgateway/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
import itertools
from corehq.apps.commtrack.models import CommtrackConfig, StockState
from corehq.apps.products.models import SQLProduct
from corehq.apps.domain.views import BaseDomainView
Expand Down Expand Up @@ -28,6 +29,7 @@ class GlobalStats(BaseDomainView):
section_url = ""
template_name = "ilsgateway/global_stats.html"
show_supply_point_types = False
root_name = 'MOHSW'

@property
def main_context(self):
Expand All @@ -45,14 +47,22 @@ def main_context(self):

main_context = super(GlobalStats, self).main_context
location_types = Domain.get_by_name(self.domain).location_types
entities_reported_stock = sum([SQLLocation.objects.filter(
domain=self.domain, location_type=location_type.name).count() for location_type in location_types
if not location_type.administrative])
administrative_types = [
location_type.name
for location_type in location_types
if not location_type.administrative
]
entities_reported_stock = SQLLocation.objects.filter(
domain=self.domain,
location_type__in=administrative_types
).count()

context = {
'country': SQLLocation.objects.filter(domain=self.domain, location_type='country').count(),
'region': SQLLocation.objects.filter(domain=self.domain, location_type='region').count(),
'district': SQLLocation.objects.filter(domain=self.domain, location_type='district').count(),
'root_name': self.root_name,
'country': SQLLocation.objects.filter(domain=self.domain,
location_type__iexact=self.root_name).count(),
'region': SQLLocation.objects.filter(domain=self.domain, location_type__iexact='region').count(),
'district': SQLLocation.objects.filter(domain=self.domain, location_type__iexact='district').count(),
'entities_reported_stock': entities_reported_stock,
'facilities': len(facilities),
'contacts': contacts[0]['value'] if contacts else 0,
Expand All @@ -71,7 +81,7 @@ def main_context(self):
'psychiatric hospital', 'regional medical store', 'regional hospital', 'polyclinic',
'teaching hospital', 'central medical store', '']
supply_point_types_map = {supply_point_type: 0 for supply_point_type in supply_point_types}
facility_ids = [facility.location_id for facility in facilities]
facility_ids = [location.location_id for location in SQLLocation.objects.all()]
for facility in iter_docs(Location.get_db(), facility_ids):
supply_point_type = facility.get('metadata', {}).get('supply_point_type', "").lower()
supply_point_types_map[supply_point_type] += 1
Expand Down

0 comments on commit 842ead9

Please sign in to comment.