|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b8a41dda", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# [admin] Usage Reports command\n", |
| 9 | + "\n", |
| 10 | + "\n", |
| 11 | + "The `reports` command in `admin` scope could help you manage groups.\n" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "id": "740039bc", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "## Setup PrimeHub Python SDK\n" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": 1, |
| 25 | + "id": "effe3482", |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "PrimeHub Python SDK setup successfully\n" |
| 33 | + ] |
| 34 | + } |
| 35 | + ], |
| 36 | + "source": [ |
| 37 | + "from primehub import PrimeHub, PrimeHubConfig\n", |
| 38 | + "ph = PrimeHub(PrimeHubConfig())\n", |
| 39 | + "\n", |
| 40 | + "if ph.is_ready():\n", |
| 41 | + " print(\"PrimeHub Python SDK setup successfully\")\n", |
| 42 | + "else:\n", |
| 43 | + " print(\"PrimeHub Python SDK couldn't get the group information, follow the 00-getting-started.ipynb to complete it\")" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "id": "bfb99744", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Help documentation" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 2, |
| 57 | + "id": "9dd7964e", |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [ |
| 60 | + { |
| 61 | + "name": "stdout", |
| 62 | + "output_type": "stream", |
| 63 | + "text": [ |
| 64 | + "Help on AdminReport in module primehub.admin_reports object:\n", |
| 65 | + "\n", |
| 66 | + "class AdminReport(primehub.Helpful, primehub.Module)\n", |
| 67 | + " | AdminReport(primehub: primehub.PrimeHub, **kwargs)\n", |
| 68 | + " | \n", |
| 69 | + " | Method resolution order:\n", |
| 70 | + " | AdminReport\n", |
| 71 | + " | primehub.Helpful\n", |
| 72 | + " | primehub.Module\n", |
| 73 | + " | primehub.HTTPSupport\n", |
| 74 | + " | builtins.object\n", |
| 75 | + " | \n", |
| 76 | + " | Methods defined here:\n", |
| 77 | + " | \n", |
| 78 | + " | download(self, url, **kwargs)\n", |
| 79 | + " | Download a report csv file from the given url.\n", |
| 80 | + " | \n", |
| 81 | + " | It will convert the URI to filename by default. For example, there are summary url and details url\n", |
| 82 | + " | * https://primehub-python-sdk.primehub.io/api/report/monthly/2022/12\n", |
| 83 | + " | * https://primehub-python-sdk.primehub.io/api/report/monthly/details/2022/12\n", |
| 84 | + " | \n", |
| 85 | + " | Will save to\n", |
| 86 | + " | * 202212.csv\n", |
| 87 | + " | * 202212_details.csv\n", |
| 88 | + " | \n", |
| 89 | + " | If you give a dest, it will use the given dest as the filename.\n", |
| 90 | + " | \n", |
| 91 | + " | :type url: str\n", |
| 92 | + " | :param url: The report url.\n", |
| 93 | + " | \n", |
| 94 | + " | :type dest: str\n", |
| 95 | + " | :param dest: The local path to save the report csv file\n", |
| 96 | + " | \n", |
| 97 | + " | :type recusive: bool\n", |
| 98 | + " | :param recusive: Copy recursively, it works when a path is a directory.\n", |
| 99 | + " | \n", |
| 100 | + " | help_description(self)\n", |
| 101 | + " | one line description for all commands\n", |
| 102 | + " | \n", |
| 103 | + " | list(self, **kwargs) -> Iterator\n", |
| 104 | + " | List reports\n", |
| 105 | + " | \n", |
| 106 | + " | :type page: int\n", |
| 107 | + " | :param page: the page of all data\n", |
| 108 | + " | \n", |
| 109 | + " | :rtype Iterator\n", |
| 110 | + " | :return user iterator\n", |
| 111 | + " | \n", |
| 112 | + " | ----------------------------------------------------------------------\n", |
| 113 | + " | Data and other attributes defined here:\n", |
| 114 | + " | \n", |
| 115 | + " | __abstractmethods__ = frozenset()\n", |
| 116 | + " | \n", |
| 117 | + " | ----------------------------------------------------------------------\n", |
| 118 | + " | Data descriptors inherited from primehub.Helpful:\n", |
| 119 | + " | \n", |
| 120 | + " | __dict__\n", |
| 121 | + " | dictionary for instance variables (if defined)\n", |
| 122 | + " | \n", |
| 123 | + " | __weakref__\n", |
| 124 | + " | list of weak references to the object (if defined)\n", |
| 125 | + " | \n", |
| 126 | + " | ----------------------------------------------------------------------\n", |
| 127 | + " | Methods inherited from primehub.Module:\n", |
| 128 | + " | \n", |
| 129 | + " | __init__(self, primehub: primehub.PrimeHub, **kwargs)\n", |
| 130 | + " | Initialize self. See help(type(self)) for accurate signature.\n", |
| 131 | + " | \n", |
| 132 | + " | display(self, action: dict, value: Any)\n", |
| 133 | + " | \n", |
| 134 | + " | get_display(self) -> primehub.utils.display.Displayable\n", |
| 135 | + " | \n", |
| 136 | + " | ----------------------------------------------------------------------\n", |
| 137 | + " | Static methods inherited from primehub.Module:\n", |
| 138 | + " | \n", |
| 139 | + " | output(result: dict, object_path: str)\n", |
| 140 | + " | Give a dict {'data': {'a': {'b': 'c'}}}\n", |
| 141 | + " | we could get the c by the path a.b\n", |
| 142 | + " | \n", |
| 143 | + " | ----------------------------------------------------------------------\n", |
| 144 | + " | Data descriptors inherited from primehub.Module:\n", |
| 145 | + " | \n", |
| 146 | + " | current_group\n", |
| 147 | + " | \n", |
| 148 | + " | endpoint\n", |
| 149 | + " | \n", |
| 150 | + " | group_id\n", |
| 151 | + " | \n", |
| 152 | + " | group_name\n", |
| 153 | + " | \n", |
| 154 | + " | primehub_config\n", |
| 155 | + "\n" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "help(ph.admin.reports)" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "id": "19b1d67f", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "## Usage reports management" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 3, |
| 174 | + "id": "9633fcc5", |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [ |
| 177 | + { |
| 178 | + "name": "stdout", |
| 179 | + "output_type": "stream", |
| 180 | + "text": [ |
| 181 | + "Usage: \n", |
| 182 | + " primehub admin reports <command>\n", |
| 183 | + "\n", |
| 184 | + "Get reports\n", |
| 185 | + "\n", |
| 186 | + "Available Commands:\n", |
| 187 | + " download Download a report by url\n", |
| 188 | + " list List reports\n", |
| 189 | + "\n", |
| 190 | + "Options:\n", |
| 191 | + " -h, --help Show the help\n", |
| 192 | + "\n", |
| 193 | + "Global Options:\n", |
| 194 | + " --config CONFIG Change the path of the config file (Default: ~/.primehub/config.json)\n", |
| 195 | + " --endpoint ENDPOINT Override the GraphQL API endpoint\n", |
| 196 | + " --token TOKEN Override the API Token\n", |
| 197 | + " --group GROUP Override the current group\n", |
| 198 | + " --json Output the json format (output human-friendly format by default)\n" |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "source": [ |
| 203 | + "!primehub admin reports help" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "id": "a903cc39", |
| 209 | + "metadata": {}, |
| 210 | + "source": [ |
| 211 | + "## Examples" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "id": "eb6dc413", |
| 217 | + "metadata": {}, |
| 218 | + "source": [ |
| 219 | + "You could find [more examples on our github](https://github.com/InfuseAI/primehub-python-sdk/blob/main/docs/CLI/admin/groups.md)." |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": 4, |
| 225 | + "id": "fab56243", |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [], |
| 228 | + "source": [ |
| 229 | + "import pandas as pd" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "markdown", |
| 234 | + "id": "461d85cf", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "#### List all usage reports" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "id": "fa172bf3", |
| 244 | + "metadata": {}, |
| 245 | + "outputs": [], |
| 246 | + "source": [ |
| 247 | + "reports_df = pd.DataFrame(ph.admin.reports.list())\n", |
| 248 | + "reports_df" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "id": "c93324ed", |
| 254 | + "metadata": {}, |
| 255 | + "source": [ |
| 256 | + "#### Download usage reports" |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": 6, |
| 262 | + "id": "6ef6faa8", |
| 263 | + "metadata": {}, |
| 264 | + "outputs": [ |
| 265 | + { |
| 266 | + "data": { |
| 267 | + "text/plain": [ |
| 268 | + "{'filename': '/home/jovyan/primehub-python-sdk/docs/notebook/admin-portal/latest-summary-reports.csv'}" |
| 269 | + ] |
| 270 | + }, |
| 271 | + "execution_count": 6, |
| 272 | + "metadata": {}, |
| 273 | + "output_type": "execute_result" |
| 274 | + } |
| 275 | + ], |
| 276 | + "source": [ |
| 277 | + "# Summary Reports\n", |
| 278 | + "latest_summary_url = reports_df[\"summaryUrl\"][0]\n", |
| 279 | + "\n", |
| 280 | + "ph.admin.reports.download(latest_summary_url, dest=\"./latest-summary-reports.csv\")" |
| 281 | + ] |
| 282 | + }, |
| 283 | + { |
| 284 | + "cell_type": "code", |
| 285 | + "execution_count": 7, |
| 286 | + "id": "3593537a", |
| 287 | + "metadata": {}, |
| 288 | + "outputs": [ |
| 289 | + { |
| 290 | + "data": { |
| 291 | + "text/plain": [ |
| 292 | + "{'filename': '/home/jovyan/primehub-python-sdk/docs/notebook/admin-portal/latest-detailed-reports.csv'}" |
| 293 | + ] |
| 294 | + }, |
| 295 | + "execution_count": 7, |
| 296 | + "metadata": {}, |
| 297 | + "output_type": "execute_result" |
| 298 | + } |
| 299 | + ], |
| 300 | + "source": [ |
| 301 | + "# Detailed Reports\n", |
| 302 | + "latest_detailed_url = reports_df[\"detailedUrl\"][0]\n", |
| 303 | + "\n", |
| 304 | + "ph.admin.reports.download(latest_detailed_url, dest=\"./latest-detailed-reports.csv\")" |
| 305 | + ] |
| 306 | + } |
| 307 | + ], |
| 308 | + "metadata": { |
| 309 | + "kernelspec": { |
| 310 | + "display_name": "Python 3", |
| 311 | + "language": "python", |
| 312 | + "name": "python3" |
| 313 | + }, |
| 314 | + "language_info": { |
| 315 | + "codemirror_mode": { |
| 316 | + "name": "ipython", |
| 317 | + "version": 3 |
| 318 | + }, |
| 319 | + "file_extension": ".py", |
| 320 | + "mimetype": "text/x-python", |
| 321 | + "name": "python", |
| 322 | + "nbconvert_exporter": "python", |
| 323 | + "pygments_lexer": "ipython3", |
| 324 | + "version": "3.7.10" |
| 325 | + } |
| 326 | + }, |
| 327 | + "nbformat": 4, |
| 328 | + "nbformat_minor": 5 |
| 329 | +} |
0 commit comments