{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "95048f00-13ba-4d0c-9b77-e070a8c0089d",
   "metadata": {},
   "source": [
    "# Workbook Commands\n",
    "\n",
    "Operations on Seeq workbooks.\n",
    "\n",
    "Some commands may take a long time to finish so we recommend scheduling a single run.\n",
    "\n",
    "⚠️ Only Seeq Administrators can run this notebook.\n",
    "\n",
    "⚠️ Use it with caution and request Seeq's assistance if needed."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcd2fa43-2b1f-46f0-9b96-deeb1a24c9c0",
   "metadata": {},
   "source": [
    "## Importing the module\n",
    "\n",
    "First import the workbooks module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a261d885-2c59-4b0b-81a3-21f5c2c34511",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from seeq import spy\n",
    "from source import workbook\n",
    "\n",
    "# Set the compatibility option so that you maximize the chance that SPy will remain compatible with your notebook/script\n",
    "spy.options.compatibility = 192"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "240dff1b",
   "metadata": {},
   "source": [
    "## Authentication\n",
    "\n",
    "Log into Seeq Server if you're not using Seeq Data Lab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77baf41d",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy.login(url='http://localhost:34216', credentials_file='../credentials.key', force=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69581fa7",
   "metadata": {},
   "source": [
    "\n",
    "## Examples\n",
    "\n",
    "The available commands are:\n",
    "\n",
    "- `workbook.workbook_list()`\n",
    "- `workbook.items()`\n",
    "- `workbook.clear_cache()`\n",
    "- `workbook.swap()`\n",
    "\n",
    "To get details on the parameters use Shift+Tab to see the function's docstring.\n",
    "\n",
    "#### List all workbooks and write to file named `workbook_list.csv`\n",
    "\n",
    "```\n",
    "workbook.workbook_list(filter='ALL', \n",
    "                       csv='workbook_list.csv')\n",
    "```\n",
    "\n",
    "#### Get items in use for all workbooks in the list `workbook_list.csv` and write the output to `workbook_items.csv`.\n",
    "\n",
    "```\n",
    "workbook.items(workbook_csv='workbook_list.csv', \n",
    "               csv='workbook_items.csv', \n",
    "               ignore_errors=True)\n",
    "```\n",
    "\n",
    "#### Clear cache for all items in a workbook.\n",
    "\n",
    "```\n",
    "workbook.clear_cache(workbook_id='cbd49e47-2725-44e6-984b-6c96ce898501')\n",
    "```\n",
    "\n",
    "#### Swap datasource items withing the workbook for other items. This will use the map created with the `datasources.map()` command and the list of workbook created with `worbook.workbook_list()`.\n",
    "\n",
    "```\n",
    "workbook.swap(map_csv='datasource_map.csv', \n",
    "              workbook_csv='workbook_list.csv', \n",
    "              ignore_errors=True, \n",
    "              csv='swap_output.csv')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd14bcaf-7440-48ae-ae85-dc59ec509157",
   "metadata": {},
   "source": [
    "## Schedule a single-run\n",
    "\n",
    "1. Make sure the entire notebook only has the commands you want to run.\n",
    "\n",
    "2. Set the number of minutes from now you want to start the script and execute only the cell below but make sure to keep the cell `spy.jobs.unschedule()` at the end of this notebook, that will ensure the job will run only once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee85c5d9-5bab-4c29-a1d3-e55d57cf769e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# How many minutes from now do you want to run this notebook?\n",
    "minutes = 10\n",
    "\n",
    "from datetime import datetime, timedelta\n",
    "spy.options.default_timezone = 'UTC'\n",
    "\n",
    "def datetime_to_cron(dt):\n",
    "    return f\"{dt.second} {dt.minute} {dt.hour} {dt.day} {dt.strftime('%b').upper()} ? {dt.year}\"\n",
    "\n",
    "cron_exp = datetime_to_cron(datetime.now() + timedelta(minutes=minutes))\n",
    "spy.jobs.schedule(schedule_spec=cron_exp, quiet=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ef7a517-0ae4-48a0-8172-753a84c649fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# DO NOT REMOVE THIS CELL\n",
    "spy.jobs.unschedule()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11",
   "language": "python",
   "name": "python311"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
