{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9413b090-8b0e-4113-90a3-c47f06b23eb1",
   "metadata": {},
   "source": [
    "# Datasource commands\n",
    "\n",
    "Operations on Seeq datasources and associated connectors\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": "aaa7d98d-cc0c-41be-b20c-2164869f7fed",
   "metadata": {},
   "source": [
    "## Importing the module\n",
    "\n",
    "First import the datasource module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71af3ebc-7711-4db6-9fa9-60bd66c078a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from seeq import spy\n",
    "from source import datasource\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": "e47f3094",
   "metadata": {},
   "source": [
    "## Authentication\n",
    "\n",
    "Log into Seeq Server if you're not using Seeq Data Lab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe725a66",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy.login(url='http://localhost:34216', credentials_file='../credentials.key', force=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35a820d9-8b94-4fcb-9ac0-559fc9aab989",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "The available commands are:\n",
    "\n",
    "- `datasource.datasource_list()`\n",
    "- `datasource.items()`\n",
    "- `datasource.recount()`\n",
    "- `datasource.archive()`\n",
    "- `datasource.cache()`\n",
    "- `datasource.cache_fill()`\n",
    "- `datasource.map_same_server()`\n",
    "- `datasource.map_old_server()`\n",
    "- `datasource.map_new_server()`\n",
    "\n",
    "To get details on the parameters use `Shift+Tab` to see the function's docstring.\n",
    "\n",
    "#### List all datasources and output to a file named `datasource_list.csv`.\n",
    "\n",
    "```\n",
    "datasource.datasource_list(csv='datasource_list.csv')\n",
    "```\n",
    "\n",
    "#### List all the items for a given datasource.\n",
    "\n",
    "```\n",
    "datasource.items(id='a027964e-d7cb-435b-8e97-0af531dce06a', \n",
    "                 include_archived=True, \n",
    "                 csv='datasource_items.csv')\n",
    "```\n",
    "\n",
    "#### Turn on caching for a datasource.\n",
    "\n",
    "```\n",
    "datasource.cache(command='on', \n",
    "                 id='aff8b821-46ae-49d4-8dd5-1029c468c652')\n",
    "```\n",
    "\n",
    "#### Archive a datasource.\n",
    "\n",
    "```\n",
    "datasource.archive(datasource_name='Demo', \n",
    "                   datasource_class='ADX', \n",
    "                   datasource_id='10eddc92-40f9-4a30-aa93-5f4d6b9004b5')\n",
    "```\n",
    "\n",
    "#### Create a datasource map to be used on a workbook swap and output to file `datasource_map.csv`\n",
    "\n",
    "```\n",
    "datasource.map_same_server(id='fddb531a-0876-44b3-b12d-bc6f570885e8', \n",
    "                           data_id_regex='(?<tagPath>.*)Example(?<tagPath2>.*)', \n",
    "                           new_datasource_id='fb41aba0-d0ce-4764-8e8b-72220445bf9e', \n",
    "                           new_data_id_regex='${tagPath}New Example ${tagPath2}', \n",
    "                           csv='datasource_map.csv')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dfd3d09-77dc-45ee-8990-b1cef768924c",
   "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": "61304cd2-cb14-4a2a-829e-c15dff48984d",
   "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": "6c0156f6-011b-4889-9047-8e2170070cc2",
   "metadata": {
    "tags": []
   },
   "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
}
