Repository: rucio/rucio

Issue:
list-rse-usage with --show-accounts does not work
Motivation
----------
`rucio list-rse-usage --show-accounts BNL-OSG2_LOCALGROUPDISK`
fails with
```
2019-01-25 09:07:28,376	ERROR	An unknown exception occurred.
Details: no error information passed (http status code: 500 ('internal_server_error', 'server_error', '/o
', '\xe2\x9c\x97'))
```
On the server the following exception is shown:
```
[Fri Jan 25 13:25:43.381208 2019] [:error] [pid 20836:tid 139736349660928] float division by zero
[Fri Jan 25 13:25:43.383132 2019] [:error] [pid 20836:tid 139736349660928] Traceback (most recent call last):
[Fri Jan 25 13:25:43.383155 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/rucio/web/rest/rse.py", line 614, in GET
[Fri Jan 25 13:25:43.383159 2019] [:error] [pid 20836:tid 139736349660928]     usage = get_rse_usage(rse, issuer=ctx.env.get('issuer'), source=source, per_account=per_account)
[Fri Jan 25 13:25:43.383161 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/rucio/api/rse.py", line 265, in get_rse_usage
[Fri Jan 25 13:25:43.383164 2019] [:error] [pid 20836:tid 139736349660928]     return rse_module.get_rse_usage(rse=rse, source=source, per_account=per_account)
[Fri Jan 25 13:25:43.383166 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/retrying.py", line 49, in wrapped_f
[Fri Jan 25 13:25:43.383169 2019] [:error] [pid 20836:tid 139736349660928]     return Retrying(*dargs, **dkw).call(f, *args, **kw)
[Fri Jan 25 13:25:43.383171 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/retrying.py", line 206, in call
[Fri Jan 25 13:25:43.383173 2019] [:error] [pid 20836:tid 139736349660928]     return attempt.get(self._wrap_exception)
[Fri Jan 25 13:25:43.383175 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/retrying.py", line 247, in get
[Fri Jan 25 13:25:43.383178 2019] [:error] [pid 20836:tid 139736349660928]     six.reraise(self.value[0], self.value[1], self.value[2])
[Fri Jan 25 13:25:43.383180 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/retrying.py", line 200, in call
[Fri Jan 25 13:25:43.383182 2019] [:error] [pid 20836:tid 139736349660928]     attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
[Fri Jan 25 13:25:43.383184 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/rucio/db/sqla/session.py", line 270, in new_funct
[Fri Jan 25 13:25:43.383187 2019] [:error] [pid 20836:tid 139736349660928]     return function(*args, **kwargs)
[Fri Jan 25 13:25:43.383189 2019] [:error] [pid 20836:tid 139736349660928]   File "/usr/lib/python2.7/site-packages/rucio/core/rse.py", line 568, in get_rse_usage
[Fri Jan 25 13:25:43.383191 2019] [:error] [pid 20836:tid 139736349660928]     account_usages.append({'used': row.bytes, 'account': row.account, 'percentage': round(float(row.bytes) / float(total) * 100, 2)})
[Fri Jan 25 13:25:43.383194 2019] [:error] [pid 20836:tid 139736349660928] ZeroDivisionError: float division by zero
```





Discussion:

Resolving patch:
diff --git a/lib/rucio/core/rse.py b/lib/rucio/core/rse.py
--- a/lib/rucio/core/rse.py
+++ b/lib/rucio/core/rse.py
@@ -565,7 +565,8 @@ def get_rse_usage(rse, source=None, rse_id=None, session=None, per_account=False
             account_usages = []
             for row in query_account_usage:
                 if row.bytes != 0:
-                    account_usages.append({'used': row.bytes, 'account': row.account, 'percentage': round(float(row.bytes) / float(total) * 100, 2)})
+                    percentage = round(float(row.bytes) / float(total) * 100, 2) if total else 0
+                    account_usages.append({'used': row.bytes, 'account': row.account, 'percentage': percentage})
             account_usages.sort(key=lambda x: x['used'], reverse=True)
             rse_usage['account_usages'] = account_usages
         usage.append(rse_usage)


Test patch: