# MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from django.db.models import Q
from impact.v1.helpers.criterion_helper import CriterionHelper


class MatchingCriterionHelper(CriterionHelper):
    def __init__(self, subject):
        super().__init__(subject)
        self._app_ids_to_targets = {}
        self._target_counts = {}

    def app_count(self, apps, option_name):
        return self.target_counts(apps).get(option_name, 0)

    def refine_feedbacks(self, feedbacks, target, refinement):
        if not target:
            return None
        query = Q(**{refinement: target})
        return feedbacks.filter(query)

    def find_app_ids(self, feedbacks, apps, target):
        if not feedbacks:
            return []
        result = []
        app_map = self.app_ids_to_targets(apps)
        return [app_id for app_id in
                feedbacks.values_list("application_id", flat=True)
                if app_id in app_map and app_map[app_id] == target.id]

    def app_ids_to_targets(self, apps):
        if not self._app_ids_to_targets:
            self.calc_app_ids_to_targets(apps)
        return self._app_ids_to_targets

    def target_counts(self, apps):
        if not self._app_ids_to_targets:
            self.calc_app_ids_to_targets(apps)
        return self._target_counts

@@ -441,200 +441,8 @@
 0)%0A%0A
-    def refine_feedbacks(self, feedbacks, target, refinement):%0A        if not target:%0A            return None%0A        query = Q(**%7Brefinement: target%7D)%0A        return feedbacks.filter(query)%0A%0A
     
@@ -822,38 +822,70 @@
  apps):%0A        
-if not
+self._check_cache(apps)%0A        return
  self._app_ids_t
@@ -897,50 +897,70 @@
 gets
-:
+%0A
 %0A    
-  
+def target_counts(self, apps):%0A
       
+  
 self.
-calc_app_ids_to_targets
+_check_cache
 (app
@@ -987,49 +987,43 @@
 lf._
-app_ids_to_targets%0A%0A    def target_counts
+target_counts%0A%0A    def _check_cache
 (sel
@@ -1124,39 +1124,4 @@
 ps)%0A
-        return self._target_counts%0A

Remove dead code and minor refactor