{ "cells": [ { "cell_type": "markdown", "id": "3bf211fe", "metadata": {}, "source": [ "# Access an activity report" ] }, { "cell_type": "markdown", "id": "a74b5172", "metadata": {}, "source": [ "This example shows how to connect to Granta MI and access the activity report. It shows how to view all activity\n", "entries, or how to select entries that meet one or more criteria." ] }, { "cell_type": "markdown", "id": "71c3a6f5", "metadata": {}, "source": [ "## Connect to Granta MI" ] }, { "cell_type": "markdown", "id": "d94834eb", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. For more information, see the\n", "[Basic Usage](../1_Basic_usage.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "c1667651", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.system import Connection\n", "\n", "connection = Connection(\"http://my_grantami_server/mi_servicelayer\").with_autologon()\n", "client = connection.connect()" ] }, { "cell_type": "markdown", "id": "63eabfcf", "metadata": {}, "source": [ "## Fetch activity report" ] }, { "cell_type": "markdown", "id": "9b2d0a0b", "metadata": {}, "source": [ "To fetch the activity report, use the `get_activity_report()` method. This method returns an iterator." ] }, { "cell_type": "markdown", "id": "6d882c3b", "metadata": {}, "source": [ "
\n", "\n", "**Info:**\n", "\n", "Accessing activity log information requires MI_SYSTEM_ADMIN permissions on the Granta MI application server.\n", "Calling the `get_activity_report()` without sufficient permissions will raise a `ApiException` with a 403\n", "status code.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "ba4a15d6", "metadata": {}, "outputs": [], "source": [ "all_items = client.get_activity_report()" ] }, { "cell_type": "markdown", "id": "84e03240", "metadata": {}, "source": [ "Iterate over the first few items in the response, using `itertools.islice` to slice the iterator:" ] }, { "cell_type": "code", "execution_count": null, "id": "a792b00c", "metadata": {}, "outputs": [], "source": [ "from itertools import islice\n", "\n", "print(\"Printing first 5...\")\n", "for item in islice(all_items, 5):\n", " print(\n", " f\"dbkey: {item.database_key}, user: {item.username}, date:{item.activity_date}, \"\n", " f\"mode: {item.usage_mode.value}, app names: {item.application_names}\"\n", " )" ] }, { "cell_type": "markdown", "id": "6ab13fad", "metadata": {}, "source": [ "### Iterators and paging" ] }, { "cell_type": "markdown", "id": "12e2586d", "metadata": {}, "source": [ "Iterators and paged API responses are efficient, because the data is only requested at the point at which it is\n", "required.\n", "\n", "However, by design, iterators do not have a defined length. This means calling `len(all_items)` directly will raise an\n", "exception. To find the number of items returned, first convert the response to a `list`.\n", "\n", "Iterating over an iterator consumes the items iterated over, so first re-run the `get_activity_report()` to get a\n", "fresh iterator." ] }, { "cell_type": "code", "execution_count": null, "id": "f698e1b2", "metadata": {}, "outputs": [], "source": [ "all_items = client.get_activity_report() # Initial request fetches the first page of results.\n", "\n", "all_items_list = list(all_items) # Exhausts the iterator and stores all items in a list.\n", "# Subsequent pages are fetched via additional requests as the iterator is exhausted.\n", "\n", "print(f\"{len(all_items_list)} activity report items in Granta MI.\")" ] }, { "cell_type": "markdown", "id": "c12a78e8", "metadata": {}, "source": [ "In general though, converting an iterator to a list should be avoided unless necessary." ] }, { "cell_type": "markdown", "id": "3b5062cd", "metadata": {}, "source": [ "The default page size is set to 1000, which should be suitable for most situations.\n", "To control the page size, use the `page_size` argument:" ] }, { "cell_type": "code", "execution_count": null, "id": "0a142830", "metadata": {}, "outputs": [], "source": [ "# Initial request fetches the first page of results.\n", "all_items_page_size_100 = client.get_activity_report(page_size=100)\n", "\n", "len(list(all_items_page_size_100)) # 11 additional requests are made. Each request returns a maximum of 100 results." ] }, { "cell_type": "markdown", "id": "8d07263d", "metadata": {}, "source": [ "## Fetch activities for a specific database" ] }, { "cell_type": "markdown", "id": "70c00879", "metadata": {}, "source": [ "To fetch activities based on one or more criteria, first create an `ActivityReportFilter` object. The\n", "constructor takes no arguments, and instead is built using a fluent interface. Use the\n", "`with_database_key()` method to add a filter on the database key." ] }, { "cell_type": "code", "execution_count": null, "id": "433caccd", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.system import ActivityReportFilter\n", "\n", "database_filter = ActivityReportFilter().with_database_key(\"MI_Corporate_Design_Data\")" ] }, { "cell_type": "markdown", "id": "f3f9127b", "metadata": {}, "source": [ "Use the `get_activity_report_where()` method to only get report items that match the filter." ] }, { "cell_type": "code", "execution_count": null, "id": "69e013a0", "metadata": {}, "outputs": [], "source": [ "design_data_items = client.get_activity_report_where(database_filter)" ] }, { "cell_type": "markdown", "id": "f13d75e3", "metadata": {}, "source": [ "Again, iterate over the first 5 items in the response:" ] }, { "cell_type": "code", "execution_count": null, "id": "9886755c", "metadata": {}, "outputs": [], "source": [ "print(\"Printing first 5...\")\n", "for item in islice(design_data_items, 5):\n", " print(\n", " f\"dbkey: {item.database_key}, user: {item.username}, date:{item.activity_date}, \"\n", " f\"mode: {item.usage_mode.value}, app names: {item.application_names}\"\n", " )" ] }, { "cell_type": "markdown", "id": "f23986d3", "metadata": {}, "source": [ "Observe that all activity report items contain the requested database key." ] }, { "cell_type": "markdown", "id": "e40e870b", "metadata": {}, "source": [ "## Apply a more complex filter" ] }, { "cell_type": "markdown", "id": "d4f10ddb", "metadata": {}, "source": [ "Create a more complex filter, applying the following criteria to the activity:\n", "\n", "* The activity was performed by `'USER_5'`.\n", "* The activity occurred within the first quarter of 2025.\n", "* The activity was an `EDIT` operation on the `'MI_Corporate_Design_Data'` database.\n", "* The application `'MI Scripting Toolkit'` was used for the activity." ] }, { "cell_type": "code", "execution_count": null, "id": "87dee3a6", "metadata": { "lines_to_next_cell": 0 }, "outputs": [], "source": [ "from datetime import date\n", "\n", "from ansys.grantami.system import ActivityUsageMode\n", "\n", "complex_filter = (\n", " ActivityReportFilter()\n", " .with_username(\"USER_5\")\n", " .with_date_from(date.fromisoformat(\"2025-01-01\"), inclusive=True)\n", " .with_date_to(date.fromisoformat(\"2025-03-31\"), inclusive=True)\n", " .with_usage_mode(ActivityUsageMode.EDIT)\n", " .with_database_key(\"MI_Corporate_Design_Data\", exact_match=True)\n", " .with_application_name(\"MI Scripting Toolkit\")\n", ")\n", "\n", "specific_logs = client.get_activity_report_where(complex_filter)" ] }, { "cell_type": "markdown", "id": "133c76db", "metadata": {}, "source": [ "Again, iterate over the first 5 items in the response. In this case, fewer than 5 items were returned by Granta MI." ] }, { "cell_type": "code", "execution_count": null, "id": "4f295de1", "metadata": {}, "outputs": [], "source": [ "for item in islice(specific_logs, 5):\n", " print(\n", " f\"dbkey: {item.database_key}, user: {item.username}, date:{item.activity_date}, \"\n", " f\"mode: {item.usage_mode.value}, app names: {item.application_names}\"\n", " )" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }