Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1814 | changelog | updated result keys and …
Browse files Browse the repository at this point in the history
…added diff with verbosity
  • Loading branch information
snyaggarwal committed May 6, 2024
1 parent e6d6587 commit 10b2638
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
56 changes: 36 additions & 20 deletions core/common/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ def __init__(self, resources1, resources2, identity='mnemonic', verbosity=0): #
self.resources2 = resources2
self.identity = identity
self.verbosity = verbosity
self.same = {}
self.same_standard = {}
self.same_smart = {}
self.changed_smart = {}
self.changed_standard = {}
self.result = {}
self.result_concise = {}
self._resources1_map = None
self._resources1_map_retired = None
self._resources2_map = None
Expand Down Expand Up @@ -305,14 +306,14 @@ def populate_diff_from_common(self):
for key, info in common.items():
checksums1 = resources1_map[key]['checksums']
checksums2 = resources2_map[key]['checksums']
if checksums1['standard'] != checksums2['standard']:
self.changed_standard[key] = info
elif self.include_same:
self.same[key] = info
if checksums1['smart'] != checksums2['smart']:
self.changed_smart[key] = info
elif self.include_same:
elif checksums1['standard'] != checksums2['standard']:
self.changed_standard[key] = info
elif self.include_same and checksums1['smart'] == checksums2['smart']:
self.same_smart[key] = info
elif self.include_same and checksums1['standard'] == checksums2['standard']:
self.same_standard[key] = info

def get_struct(self, values):
total = len(values or [])
Expand All @@ -327,13 +328,25 @@ def prepare(self):
self.result = {
'new': self.get_struct(self.new),
'removed': self.get_struct(self.deleted),
'retired': self.get_struct(self.retired),
'changed': self.get_struct(self.changed_standard),
'smart_changed': self.get_struct(self.changed_smart),
'changed_total': len(self.retired or []) + len(self.changed_standard or []) + len(self.changed_smart or []),
'changed_retired': self.get_struct(self.retired),
'changed_major': self.get_struct(self.changed_smart),
'changed_minor': self.get_struct(self.changed_standard),
}
if self.include_same:
self.result['same'] = self.get_struct(self.same)
self.result['smart_same'] = self.get_struct(self.same_smart)
self.result['same_total'] = len(self.same_standard or []) + len(self.same_smart or [])
self.result['same_minor'] = self.get_struct(self.same_standard)
self.result['same_major'] = self.get_struct(self.same_smart)

def set_concise_result(self):
self.result_concise = {
'new': len(self.new or []),
'removed': len(self.deleted or []),
'changed_total': self.result['changed_total'],
'changed_retired': len(self.retired or []),
'changed_major': len(self.changed_smart or []),
'changed_minor': len(self.changed_standard or []),
}

def process(self, refresh=False):
if refresh:
Expand Down Expand Up @@ -395,7 +408,7 @@ def process(self): # pylint: disable=too-many-locals,too-many-branches,too-many
mappings_result = {}
traversed_mappings = set()
traversed_concepts = set()
diff_keys = ['new', 'removed', 'retired', 'smart_changed', 'changed']
diff_keys = ['new', 'removed', 'changed_retired', 'changed_major', 'changed_minor']
for key in diff_keys: # pylint: disable=too-many-nested-blocks
diff = self.concepts_diff.result.get(key, False)
if isinstance(diff, dict):
Expand Down Expand Up @@ -425,7 +438,10 @@ def process(self): # pylint: disable=too-many-locals,too-many-branches,too-many
section_summary[concept_id] = summary
if section_summary:
concepts_result[key] = section_summary
same_concept_ids = self.concepts_diff.result['same'][self.identity]
same_concept_ids = {
*get(self.concepts_diff.result, f'same_minor.{self.identity}', []),
*get(self.concepts_diff.result, f'same_major.{self.identity}', []),
}
for key in diff_keys: # pylint: disable=too-many-nested-blocks
diff = self.mappings_diff.result.get(key, False)
if isinstance(diff, dict):
Expand All @@ -441,17 +457,17 @@ def process(self): # pylint: disable=too-many-locals,too-many-branches,too-many
from_concept = mapping.from_concept
concept_id = from_concept_code
if concept_id in same_concept_ids:
if 'same_with_mapping_changes' not in concepts_result:
concepts_result['same_with_mapping_changes'] = {}
if concept_id not in concepts_result['same_with_mapping_changes']:
concepts_result['same_with_mapping_changes'][concept_id] = {
if 'changed_mappings_only' not in concepts_result:
concepts_result['changed_mappings_only'] = {}
if concept_id not in concepts_result['changed_mappings_only']:
concepts_result['changed_mappings_only'][concept_id] = {
'id': concept_id,
'display_name': get(from_concept, 'display_name'),
'mappings': {}
}
if key not in concepts_result['same_with_mapping_changes'][concept_id]['mappings']:
concepts_result['same_with_mapping_changes'][concept_id]['mappings'][key] = []
concepts_result['same_with_mapping_changes'][concept_id]['mappings'][key].append(
if key not in concepts_result['changed_mappings_only'][concept_id]['mappings']:
concepts_result['changed_mappings_only'][concept_id]['mappings'][key] = []
concepts_result['changed_mappings_only'][concept_id]['mappings'][key].append(
self.get_mapping_summary(mapping, mapping_id))
else:
section_summary[mapping_id] = self.get_mapping_summary(mapping, mapping_id)
Expand Down
3 changes: 2 additions & 1 deletion core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,5 @@ def source_version_compare(version1_uri, version2_uri, is_changelog, verbosity):
from core.sources.models import Source
version1 = Source.objects.get(uri=version1_uri)
version2 = Source.objects.get(uri=version2_uri)
return Source.changelog(version1, version2) if is_changelog else Source.compare(version1, version2, verbosity)
fn = Source.changelog if is_changelog else Source.compare
return fn(version1, version2, verbosity)
12 changes: 10 additions & 2 deletions core/sources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ def compare(version1, version2, verbosity=0): # pragma: no cover
}

@staticmethod
def changelog(version1, version2): # pragma: no cover
def changelog(version1, version2, verbosity=0): # pragma: no cover
from core.common.checksums import ChecksumDiff
concepts_diff = ChecksumDiff(
resources1=version1.get_concepts_queryset().only('mnemonic', 'checksums', 'retired'),
Expand All @@ -833,7 +833,7 @@ def changelog(version1, version2): # pragma: no cover
mappings_diff.process()
log = ChecksumChangelog(version1, version2, concepts_diff, mappings_diff)
log.process()
return {
result = {
'meta': {
'version1': {
'uri': version1.uri,
Expand All @@ -848,3 +848,11 @@ def changelog(version1, version2): # pragma: no cover
},
**log.result
}
if verbosity > 0:
concepts_diff.set_concise_result()
mappings_diff.set_concise_result()
result['meta']['diff'] = {
'concepts': concepts_diff.result_concise,
'mappings': mappings_diff.result_concise,
}
return result
9 changes: 4 additions & 5 deletions core/sources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,8 @@ class SourceVersionComparisonView(BaseAPIView, TaskMixin): # pragma: no cover
swagger_schema = None

def get_objects(self):
data = self.request.data
version1_uri = data.get('version1')
version2_uri = data.get('version2')
version1_uri = self.request.data.get('version1')
version2_uri = self.request.data.get('version2')
version1 = get_object_or_404(Source.objects.filter(uri=version1_uri))
version2 = get_object_or_404(Source.objects.filter(uri=version2_uri))
self.check_object_permissions(self.request, version1)
Expand All @@ -624,8 +623,8 @@ def post(self, _):
except: # pylint: disable=bare-except
verbosity = 0
is_changelog = self.request.query_params.get('changelog', False) in get_truthy_values()
result = self.perform_task(
source_version_compare, (version1.uri, version2.uri, is_changelog, verbosity))
fn = Source.changelog if is_changelog else Source.compare
result = fn(version1, version2, verbosity)
if isinstance(result, Response):
return result
return Response(result)

0 comments on commit 10b2638

Please sign in to comment.