import subprocess

import pandas as pd

import io

import json

## The command usage-report:generate is provided by the app
## user_usage_report, that must be installed and enabled in
## the system.
## JSON output would be easier to parse but it actually isn't
## as the JSON file created is not correctly formatted as a JSON
## file.
result_set = subprocess.check_output(
                                     ["occ", "usage-report:generate"]
                                    )

result_set = result_set.decode()

column_names = [
                "userid","extract_date","assigned_quota", "used_quota",
                "files", "shares","uploads", "downloads"
               ]

df = pd.read_csv(
                 io.StringIO(result_set),
                 names=column_names
                )

## Transformation to int is necessary as json library cannot serialize numpy
## data types
results = {
            "total_number_of_users": int(df.userid.count()),
            "total_assigned_quota_gb": df.assigned_quota.agg("sum")/1024/1024/1024,
            "total_used_quota_gb": df.used_quota.agg("sum")/1024/1024/1024
          }

json.dump(results,
          open("/var/lib/zabbix/output/nc_quota_metrics.json","w")
         )
