Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1735 | version exports | added time taken
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Jan 16, 2024
1 parent 2f17337 commit d81a9a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 13 additions & 2 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import time
from datetime import datetime
from json import JSONDecodeError

Expand Down Expand Up @@ -99,6 +100,7 @@ def delete_collection(collection_id):

@app.task(base=QueueOnce, bind=True)
def export_source(self, version_id):
start_time = time.time()
from core.sources.models import Source
logger.info('Finding source version...')

Expand All @@ -113,14 +115,20 @@ def export_source(self, version_id):
version.add_processing(self.request.id)
try:
logger.info('Found source version %s. Beginning export...', version.version)
write_export_file(version, 'source', 'core.sources.serializers.SourceVersionExportSerializer', logger)
write_export_file(
version,
'source', 'core.sources.serializers.SourceVersionExportSerializer',
logger,
start_time
)
logger.info('Export complete!')
finally:
version.remove_processing(self.request.id)


@app.task(base=QueueOnce, bind=True)
def export_collection(self, version_id):
start_time = time.time()
from core.collections.models import Collection
logger.info('Finding collection version...')

Expand All @@ -141,7 +149,10 @@ def export_collection(self, version_id):
try:
logger.info('Found collection version %s. Beginning export...', version.version)
write_export_file(
version, 'collection', 'core.collections.serializers.CollectionVersionExportSerializer', logger
version,
'collection', 'core.collections.serializers.CollectionVersionExportSerializer',
logger,
start_time
)
logger.info('Export complete!')
finally:
Expand Down
6 changes: 4 additions & 2 deletions core/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import random
import shutil
import tempfile
import time
import uuid
import zipfile
from collections import OrderedDict
Expand Down Expand Up @@ -194,7 +195,7 @@ def get_class(kls):


def write_export_file(
version, resource_type, resource_serializer_type, logger
version, resource_type, resource_serializer_type, logger, start_time
): # pylint: disable=too-many-statements,too-many-locals,too-many-branches
from core.concepts.models import Concept
from core.mappings.models import Mapping
Expand Down Expand Up @@ -326,7 +327,8 @@ def write_export_file(
logger.info(f'{resource_name} has no mappings to serialize.')

with open('export.json', 'a') as out:
out.write(']}')
end_time = str(round((time.time() - start_time) + 2, 2)) + 'secs'
out.write('], "export_time": ' + json.dumps(end_time, cls=encoders.JSONEncoder) + '}')

with zipfile.ZipFile('export.zip', 'w', zipfile.ZIP_DEFLATED) as _zip:
_zip.write('export.json')
Expand Down
13 changes: 12 additions & 1 deletion core/integration_tests/tests_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,20 @@ def test_export_source(self, export_service_mock): # pylint: disable=too-many-l
exported_data = json.loads(zipped_file.read('export.json').decode('utf-8'))

self.assertEqual(
exported_data, {**SourceVersionExportSerializer(source_v1).data, 'concepts': ANY, 'mappings': ANY}
exported_data,
{
**SourceVersionExportSerializer(source_v1).data,
'concepts': ANY,
'mappings': ANY,
'export_time': ANY
}
)

time_taken = exported_data['export_time']
self.assertTrue('secs' in time_taken)
time_taken = float(time_taken.replace('secs', ''))
self.assertTrue(time_taken > 2)

exported_concepts = exported_data['concepts']
expected_concepts = ConceptVersionExportSerializer([concept2, concept1], many=True).data

Expand Down

0 comments on commit d81a9a4

Please sign in to comment.