Browse Source

[api] Add new /content_summary public api (#3620)

* [api] Add new /content_summary public api
Nidhi Bhat G 1 year ago
parent
commit
55ab28d636

+ 23 - 1
apps/filebrowser/src/filebrowser/api.py

@@ -31,6 +31,7 @@ from desktop.lib.fs.gc.gs import get_gs_home_directory
 from azure.abfs.__init__ import get_home_dir_for_abfs
 from azure.abfs.__init__ import get_home_dir_for_abfs
 from aws.s3.s3fs import get_s3_home_directory
 from aws.s3.s3fs import get_s3_home_directory
 
 
+from filebrowser.views import _normalize_path
 
 
 LOG = logging.getLogger()
 LOG = logging.getLogger()
 
 
@@ -129,4 +130,25 @@ def rename(request):
     raise Exception(_('The destination path "%s" already exists.') % dest_path)
     raise Exception(_('The destination path "%s" already exists.') % dest_path)
 
 
   request.fs.rename(src_path, dest_path)
   request.fs.rename(src_path, dest_path)
-  return HttpResponse(status=200)
+  return HttpResponse(status=200)
+
+@error_handler
+def content_summary(request, path):
+  path = _normalize_path(path)
+  response = {}
+
+  if not path:
+    raise Exception(_('Path parameter is required to fetch content summary.'))
+
+  if not request.fs.exists(path):
+    return JsonResponse(response, status=404)
+
+  try:
+    stats = request.fs.get_content_summary(path)
+    replication_factor = request.fs.stats(path)['replication']
+    stats.summary.update({'replication': replication_factor})
+    response['summary'] = stats.summary
+  except Exception as e:
+    raise Exception(_('Failed to fetch content summary for "%s". ') % path)
+
+  return JsonResponse(response)

+ 5 - 0
desktop/core/src/desktop/api_public.py

@@ -229,6 +229,11 @@ def storage_rename(request):
   django_request = get_django_request(request)
   django_request = get_django_request(request)
   return filebrowser_api.rename(django_request)
   return filebrowser_api.rename(django_request)
 
 
+@api_view(["GET"])
+def storage_content_summary(request, path):
+  django_request = get_django_request(request)
+  return filebrowser_api.content_summary(django_request, path)
+
 # Importer
 # Importer
 
 
 @api_view(["POST"])
 @api_view(["POST"])

+ 2 - 1
desktop/core/src/desktop/api_public_urls_v1.py

@@ -98,7 +98,8 @@ urlpatterns += [
   re_path(r'^storage/upload/file/?$', api_public.storage_upload_file, name='storage_upload_file'),
   re_path(r'^storage/upload/file/?$', api_public.storage_upload_file, name='storage_upload_file'),
   re_path(r'^storage/mkdir$', api_public.storage_mkdir, name='storage_mkdir'),
   re_path(r'^storage/mkdir$', api_public.storage_mkdir, name='storage_mkdir'),
   re_path(r'^storage/touch$', api_public.storage_touch, name='storage_touch'),
   re_path(r'^storage/touch$', api_public.storage_touch, name='storage_touch'),
-  re_path(r'^storage/rename$', api_public.storage_rename, name='storage_rename')
+  re_path(r'^storage/rename$', api_public.storage_rename, name='storage_rename'),
+  re_path(r'^storage/content_summary=(?P<path>.*)$', api_public.storage_content_summary, name='storage_content_summary'),
 ]
 ]
 
 
 urlpatterns += [
 urlpatterns += [