Skip to content

Commit

Permalink
-> Adjustments to filter class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
s-paquette committed Jan 16, 2025
1 parent 50bb4af commit 7410557
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
30 changes: 19 additions & 11 deletions cohorts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ def get_filters_by_data_source(self, source_type=None):
return result

# Returns a dict of the filters defining this cohort organized by filter group
def get_filters_as_dict_simple(self):
def get_filters_as_dict_simple(self, by_prog=False):
result = []

filter_groups = self.filter_group_set.all()

for fg in filter_groups:
filter_group = fg.filter_set.all().get_filter_set()
filter_group = fg.filter_set.all().get_filter_set(by_prog=by_prog)
result.append(filter_group)
return result

Expand Down Expand Up @@ -335,10 +335,18 @@ def get_op(cls, op_string):


class FilterQuerySet(models.QuerySet):
def get_filter_set(self):

# Returns this queryset of filters, optionally broken out by program
def get_filter_set(self, by_prog=False):
filters = {}
for fltr in self.all():
filters.update(fltr.get_filter())
for fltr in self.select_related('program', 'attribute').all():
if by_prog:
prog = fltr.program.name if fltr.program else '*'
if prog not in filters:
filters[prog] = {}
filters[prog][fltr.get_attr_name()] = fltr.value.split(fltr.value_delimiter)
else:
filters.update(fltr.get_filter())
return filters

def get_filter_set_array(self, active_only=False):
Expand Down Expand Up @@ -442,13 +450,13 @@ def get_operator(self):
return self.OP_TO_STR[self.operator]

def get_filter(self):
if self.operator not in [self.OR, self.BTW]:
return {
self.get_attr_name(): { 'op': self.get_operator(), 'values': self.value.split(self.value_delimiter) }
}
return {
self.get_attr_name(): self.value.split(self.value_delimiter)
prog = self.program.name if self.program else None
filter = {
self.get_attr_name(): {'values': self.value.split(self.value_delimiter), 'program': prog }
}
if self.operator not in [self.OR, self.BTW]:
filter['op'] = self.get_operator()
return filter

def get_filter_flat(self):
return {
Expand Down
9 changes: 5 additions & 4 deletions cohorts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def get_cohort_stats(cohort_id=0, filters=None, sources=None):
raise Exception("If you don't provide a cohort ID, you must provide filters!")

if not filters:
filters = cohort.get_filters_as_dict_simple()[0]
filters = cohort.get_filters_for_counts()

totals = ["case_barcode"]

for prog in filters:
if prog not in stats['programs']:
stats['programs'][prog] = {}

result = count_public_metadata_solr(None, inc_filters=filters[prog], program_id=prog, with_counts=False)
result = count_public_metadata_solr(None, inc_filters=filters, program_id=prog, with_counts=False)
prog_totals = result.get('programs', {}).get(prog,{}).get('totals',None)
if prog_totals:
for total_count in prog_totals:
Expand Down Expand Up @@ -98,15 +98,16 @@ def delete_cohort(user, cohort_id):
cohort.save()
cohort_info = {
'notes': 'Cohort {} (\'{}\') has been deleted.'.format(cohort_id, cohort.name),
'data': {'filters': cohort.get_filters_as_dict_simple()},
'data': {'filters': cohort.get_filters_as_dict_simple(by_prog=True)},
}
except ObjectDoesNotExist:
cohort_info = {
'message': 'Cohort ID {} has already been deleted.'.format(cohort_id)
}
return cohort_info


# Expects a filter format of:
# { program.id: { attribute.id: { 'values': [...] }, [...] }, [...] }
def create_cohort(user, filters=None, name=None, desc=None, source_id=None, version=None, stats=None, case_insens=True):

if not filters and not name and not desc:
Expand Down

0 comments on commit 7410557

Please sign in to comment.