Skip to content

Commit

Permalink
now non-admin users can use the stats-detailed and stats-summary repo… (
Browse files Browse the repository at this point in the history
#29)

* now non-admin users can use the stats-detailed and stats-summary report commands

* update readme file to reflect code changes
  • Loading branch information
eacmen authored Aug 5, 2020
1 parent 9052fba commit d3d8f20
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ Gathering Upload Statistics
For deployments that support multiple organizations or business units we have the ability
to gather useful statistics for the uploaded firmware based on organization. One command
will simply give you the total number of firmware that each organization uploaded, the other
command will give more detailed information about each upload.
command will give more detailed information about each upload.
To summarize multiple organizations you need to be an Administrator. If these commands are
run by a non-admin, instead of summarizing multiple organizations it will summarize the users
within that organization.
To get upload count statistics:
Expand Down
2 changes: 1 addition & 1 deletion centrifuge_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6.1'
__version__ = '0.6.2'
15 changes: 13 additions & 2 deletions centrifuge_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,12 @@ def get_stats_obj(cli, ctx, include_individuals):
cli.limit = -1

reports_json = json.loads(ctx.invoke(list_command))
users_json = json.loads(ctx.invoke(users_list))
account_json = json.loads(ctx.invoke(account_info))
users_json = None
if account_json['isAdministrator']:
users_json = json.loads(ctx.invoke(users_list))

stats_obj = CentrifugeStats(reports_json, users_json, include_individuals)
stats_obj = CentrifugeStats(reports_json, users_json, account_json, include_individuals)

cli.echo_enabled = True
cli.outfmt = outfmt
Expand Down Expand Up @@ -536,6 +539,14 @@ def new(cli, email, password, orgid, admin, expires, no_expire):
return(result)


@cli.command(name='account-info')
@pass_cli
def account_info(cli):
result = cli.do_GET(f'/api/user/account')
cli.echo(result)
return(result)


@cli.group()
@click.option('--userid', metavar='ID', help='User ID of the user being modified', required=True)
@pass_cli
Expand Down
28 changes: 20 additions & 8 deletions centrifuge_cli/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@

class CentrifugeStats(object):

def __init__(self, reports_json, users_json, include_individuals=False):
def __init__(self, reports_json, users_json, account_json, include_individuals=False):
self.reports_json = reports_json
self.users_json = users_json
self.account_json = account_json

self.user_id_to_org_name = {}
for user in self.users_json:
org = user['organization']
if org is not None:
self.user_id_to_org_name[user['id']] = org
elif include_individuals:
self.user_id_to_org_name[user['id']] = user['username']
if users_json is not None:
for user in self.users_json:
org = user['organization']
if org is not None:
self.user_id_to_org_name[user['id']] = org
elif include_individuals:
self.user_id_to_org_name[user['id']] = user['username']
else:
# non-admins dont have access to the user list so derive it
# from the report list
for report in self.reports_json['results']:
user_id = report['User']['id']
if user_id not in self.user_id_to_org_name:
self.user_id_to_org_name[user_id] = report['User']['username']

def get_summary(self, outfmt):
org_name_to_upload_md5 = {}
Expand All @@ -39,7 +48,10 @@ def get_summary(self, outfmt):

data.append([org, count])

df = pd.DataFrame(data, columns=['Organization', 'Unique Uploads'])
if self.users_json is not None:
df = pd.DataFrame(data, columns=['Organization', 'Unique Uploads'])
else:
df = pd.DataFrame(data, columns=['Username', 'Unique Uploads'])

if outfmt == 'csv':
return(df.to_csv())
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "centrifuge-cli"
version = "0.6.1"
version = "0.6.2"
description = "A command line utility for interacting with the Centrifuge Firmware Analysis Platform's REST API."
authors = ["Peter Eacmen <[email protected]>"]
readme = 'README.rst'
Expand Down

0 comments on commit d3d8f20

Please sign in to comment.