airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 02/08: AIRAVATA-3034 File, directory size, with sorting
Date Fri, 24 May 2019 18:28:38 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch airavata-3016
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit 33f12580728571d2d8c8ba108502062412e3d700
Author: Marcus Christie <machristie@apache.org>
AuthorDate: Fri May 24 10:02:41 2019 -0400

    AIRAVATA-3034 File, directory size, with sorting
---
 django_airavata/apps/api/data_products_helper.py   |  8 ++++++--
 django_airavata/apps/api/datastore.py              | 19 ++++++++++++++++++
 django_airavata/apps/api/serializers.py            |  2 ++
 .../js/models/UserStorageDirectory.js              |  2 +-
 .../js/models/UserStorageFile.js                   |  3 ++-
 .../components/storage/UserStoragePathViewer.vue   | 23 ++++++++++++++++++++--
 6 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/django_airavata/apps/api/data_products_helper.py b/django_airavata/apps/api/data_products_helper.py
index cdd67b1..88d9f4e 100644
--- a/django_airavata/apps/api/data_products_helper.py
+++ b/django_airavata/apps/api/data_products_helper.py
@@ -67,20 +67,24 @@ def listdir(request, path):
             dpath = os.path.join(path, d)
             created_time = datastore.get_created_time(
                 request.user.username, dpath)
+            size = datastore.size(request.user.username, dpath)
             directories_data.append({'name': d,
                                      'path': dpath,
-                                     'created_time': created_time})
+                                     'created_time': created_time,
+                                     'size': size})
         files_data = []
         for f in files:
             user_rel_path = os.path.join(path, f)
             created_time = datastore.get_created_time(
                 request.user.username, user_rel_path)
+            size = datastore.size(request.user.username, user_rel_path)
             full_path = datastore.path(request.user.username, user_rel_path)
             data_product_uri = _get_data_product_uri(request, full_path)
             files_data.append({'name': f,
                                'path': user_rel_path,
                                'data-product-uri': data_product_uri,
-                               'created_time': created_time})
+                               'created_time': created_time,
+                               'size': size})
         return directories_data, files_data
     else:
         raise ObjectDoesNotExist("User storage path does not exist")
diff --git a/django_airavata/apps/api/datastore.py b/django_airavata/apps/api/datastore.py
index cfe7f36..34d858e 100644
--- a/django_airavata/apps/api/datastore.py
+++ b/django_airavata/apps/api/datastore.py
@@ -124,6 +124,15 @@ def get_created_time(username, file_path):
     return user_data_storage.get_created_time(file_path)
 
 
+def size(username, file_path):
+    user_data_storage = _user_data_storage(username)
+    full_path = path_(username, file_path)
+    if os.path.isdir(full_path):
+        return _get_dir_size(full_path)
+    else:
+        return user_data_storage.size(file_path)
+
+
 def path(username, file_path):
     return path_(username, file_path)
 
@@ -141,3 +150,13 @@ def _user_data_storage(username):
     return FileSystemStorage(
         location=os.path.join(settings.GATEWAY_DATA_STORE_DIR,
                               _user_dir_name(username)))
+
+
+# from https://stackoverflow.com/a/1392549
+def _get_dir_size(start_path='.'):
+    total_size = 0
+    for dirpath, dirnames, filenames in os.walk(start_path):
+        for f in filenames:
+            fp = os.path.join(dirpath, f)
+            total_size += os.path.getsize(fp)
+    return total_size
diff --git a/django_airavata/apps/api/serializers.py b/django_airavata/apps/api/serializers.py
index 8d7cb7b..4e6a0fe 100644
--- a/django_airavata/apps/api/serializers.py
+++ b/django_airavata/apps/api/serializers.py
@@ -753,6 +753,7 @@ class UserStorageFileSerializer(serializers.Serializer):
     downloadURL = serializers.SerializerMethodField()
     dataProductURI = serializers.CharField(source='data-product-uri')
     createdTime = serializers.DateTimeField(source='created_time')
+    size = serializers.IntegerField()
 
     def get_downloadURL(self, file):
         """Getter for downloadURL field."""
@@ -767,6 +768,7 @@ class UserStorageDirectorySerializer(serializers.Serializer):
     name = serializers.CharField()
     path = serializers.CharField()
     createdTime = serializers.DateTimeField(source='created_time')
+    size = serializers.IntegerField()
     url = FullyEncodedHyperlinkedIdentityField(
         view_name='django_airavata_api:user-storage-items',
         lookup_field='path',
diff --git a/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageDirectory.js
b/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageDirectory.js
index 8108d6e..83faa77 100644
--- a/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageDirectory.js
+++ b/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageDirectory.js
@@ -1,6 +1,6 @@
 import BaseModel from "./BaseModel";
 
-const FIELDS = ["name", "path", { name: "createdTime", type: "date" }];
+const FIELDS = ["name", "path", { name: "createdTime", type: "date" }, "size"];
 
 export default class UserStorageDirectory extends BaseModel {
   constructor(data = {}) {
diff --git a/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageFile.js
b/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageFile.js
index 42d81b0..f10f2f4 100644
--- a/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageFile.js
+++ b/django_airavata/apps/api/static/django_airavata_api/js/models/UserStorageFile.js
@@ -4,7 +4,8 @@ const FIELDS = [
   "name",
   "downloadURL",
   "dataProductURI",
-  { name: "createdTime", type: "date" }
+  { name: "createdTime", type: "date" },
+  "size"
 ];
 
 export default class UserStorageFile extends BaseModel {
diff --git a/django_airavata/apps/workspace/static/django_airavata_workspace/js/components/storage/UserStoragePathViewer.vue
b/django_airavata/apps/workspace/static/django_airavata_workspace/js/components/storage/UserStoragePathViewer.vue
index 1c97824..b65f90a 100644
--- a/django_airavata/apps/workspace/static/django_airavata_workspace/js/components/storage/UserStoragePathViewer.vue
+++ b/django_airavata/apps/workspace/static/django_airavata_workspace/js/components/storage/UserStoragePathViewer.vue
@@ -57,6 +57,12 @@ export default {
           label: "Created Time",
           key: "createdTimestamp",
           sortable: true
+        },
+        {
+          label: "Size",
+          key: "size",
+          sortable: true,
+          formatter: value => this.getFormattedSize(value)
         }
       ];
     },
@@ -68,7 +74,8 @@ export default {
             path: d.path,
             type: "dir",
             createdTime: d.createdTime,
-            createdTimestamp: d.createdTime.getTime() // for sorting
+            createdTimestamp: d.createdTime.getTime(), // for sorting
+            size: d.size
           };
         });
         const files = this.userStoragePath.files.map(f => {
@@ -77,7 +84,8 @@ export default {
             type: "file",
             downloadURL: f.downloadURL,
             createdTime: f.createdTime,
-            createdTimestamp: f.createdTime.getTime() // for sorting
+            createdTimestamp: f.createdTime.getTime(), // for sorting
+            size: f.size
           };
         });
         return dirs.concat(files);
@@ -89,6 +97,17 @@ export default {
   methods: {
     fromNow(date) {
       return moment(date).fromNow();
+    },
+    getFormattedSize(size) {
+      if (size > Math.pow(2, 30)) {
+        return Math.round(size / Math.pow(2,30)) + " GB";
+      } else if (size > Math.pow(2, 20)) {
+        return Math.round(size / Math.pow(2,20)) + " MB";
+      } else if (size > Math.pow(2, 10)) {
+        return Math.round(size / Math.pow(2,10)) + " KB";
+      } else {
+        return size + " bytes";
+      }
     }
   }
 };


Mime
View raw message