{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:37:59.861795Z",
     "end_time": "2023-04-12T14:37:59.888130Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from datetime import datetime\n",
    "\n",
    "sys.path.append('../../src')\n",
    "from pjplan import WBS, DefaultScheduler, Task, MermaidGantt, Resource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "dev1 = Resource('dev1')\n",
    "dev2 = Resource('dev2')\n",
    "\n",
    "# Let's define WBS\n",
    "with WBS() as wbs:\n",
    "    wbs // Task(1, 'Task 1', estimate=8, resource=dev1.name)\n",
    "    wbs // Task(2, 'Task 2', estimate=8, predecessors=[wbs[1]], resource=dev1.name)\n",
    "    wbs // Task(3, 'Task 3', estimate=8, predecessors=[wbs[2]], resource=dev1.name)\n",
    "    wbs // Task(4, 'Task 4', estimate=8, resource=dev2.name)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:37:59.891133Z",
     "end_time": "2023-04-12T14:37:59.904134Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "\u001B[91m ID \u001B[0m\u001B[91m NAME   \u001B[0m\u001B[91m RESOURCE \u001B[0m\u001B[91m ESTIMATE \u001B[0m\u001B[91m SPENT \u001B[0m\u001B[91m START \u001B[0m\u001B[91m END \u001B[0m\u001B[91m PREDECESSORS \u001B[0m\n\u001B[94m 1  \u001B[0m\u001B[94m Task 1 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m -     \u001B[0m\u001B[94m -   \u001B[0m\u001B[94m []           \u001B[0m\n\u001B[94m 2  \u001B[0m\u001B[94m Task 2 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m -     \u001B[0m\u001B[94m -   \u001B[0m\u001B[94m [1]          \u001B[0m\n\u001B[94m 3  \u001B[0m\u001B[94m Task 3 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m -     \u001B[0m\u001B[94m -   \u001B[0m\u001B[94m [2]          \u001B[0m"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find critical path in wbs and visualize is\n",
    "wbs.critical_path()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:37:59.907143Z",
     "end_time": "2023-04-12T14:37:59.942662Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "<pjplan.viz.mermaid.gantt.MermaidGantt at 0x262585ecf10>",
      "text/html": "<iframe srcdoc=\"&lt;html&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;body&gt;\n&lt;style&gt;\n#id_1, #id_2, #id_3{fill:red!important;}\n\n&lt;/style&gt;\n\n&lt;div class=&quot;mermaid&quot;&gt;\ngantt\n  dateFormat DD.MM.YYYY HH:mm\n    Task 1: active, id_1, 12.04.2023 00:00, 13.04.2023 00:00\n    Task 2:  id_2, 13.04.2023 00:00, 14.04.2023 00:00\n    Task 3:  id_3, 14.04.2023 00:00, 15.04.2023 00:00\n    Task 4: active, id_4, 12.04.2023 00:00, 13.04.2023 00:00\n\n&lt;/div&gt;\n\n&lt;script src=&quot;https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js&quot;&gt;&lt;/script&gt;\n&lt;script&gt;\n    mermaid.ganttConfig = {\n        mirrorActor: true\n    }\n    mermaid.initialize({\n        startOnLoad: true\n    });\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\" width=\"100%\" height=\"300\" style=\"border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Visualize critical path\n",
    "plan = DefaultScheduler().calc(wbs).wbs\n",
    "plan.critical_path2().gantt_bar_style = {\n",
    "    'fill': 'red'\n",
    "}\n",
    "\n",
    "for t in plan.tasks:\n",
    "    print(t.id, t.start, t.end, t.max_start, t.max_end)\n",
    "\n",
    "MermaidGantt(plan)\n",
    "# plan.critical_path()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:37:59.920654Z",
     "end_time": "2023-04-12T14:37:59.948665Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "<pjplan.viz.mermaid.gantt.MermaidGantt at 0x262585ed150>",
      "text/html": "<iframe srcdoc=\"&lt;html&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;body&gt;\n&lt;style&gt;\n#id_2, #id_3{fill:red!important;}\n\n&lt;/style&gt;\n\n&lt;div class=&quot;mermaid&quot;&gt;\ngantt\n  dateFormat DD.MM.YYYY HH:mm\n  tickInterval 1day\n    Task 1: done, id_1, 11.04.2023 00:00, 12.04.2023 14:43\n    Task 2: active, id_2, 12.04.2023 00:00, 13.04.2023 00:00\n    Task 3: active, id_3, 11.04.2023 00:00, 18.04.2023 00:00\n\n&lt;/div&gt;\n\n&lt;script src=&quot;https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js&quot;&gt;&lt;/script&gt;\n&lt;script&gt;\n    mermaid.ganttConfig = {\n        mirrorActor: true\n    }\n    mermaid.initialize({\n        startOnLoad: true\n    });\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\" width=\"100%\" height=\"300\" style=\"border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with WBS() as wbs:\n",
    "    wbs // Task(1, 'Task 1', estimate=8, start=datetime(2023, 4, 11), resource=dev1.name)\n",
    "    wbs // Task(2, 'Task 2', estimate=8, resource=dev1.name, predecessors=[wbs[1]])\n",
    "    wbs // Task(3, 'Task 3', estimate=24, start=datetime(2023, 4, 11), resource=dev1.name)\n",
    "\n",
    "plan = DefaultScheduler().calc(wbs).wbs\n",
    "plan.critical_path2().gantt_bar_style = {\n",
    "    'fill': 'red'\n",
    "}\n",
    "\n",
    "for t in plan.tasks:\n",
    "    print(t.id, t.start, t.end, t.max_start, t.max_end)\n",
    "\n",
    "MermaidGantt(plan)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:37:59.936661Z",
     "end_time": "2023-04-12T14:37:59.953668Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[91m ID \u001B[0m\u001B[91m NAME   \u001B[0m\u001B[91m RESOURCE \u001B[0m\u001B[91m ESTIMATE \u001B[0m\u001B[91m SPENT \u001B[0m\u001B[91m START            \u001B[0m\u001B[91m END              \u001B[0m\u001B[91m PREDECESSORS \u001B[0m\n",
      "\u001B[94m 1  \u001B[0m\u001B[94m Task 1 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 12.04.2023 00:00 \u001B[0m\u001B[94m 15.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n",
      "\u001B[94m 2  \u001B[0m\u001B[94m Task 2 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 13.04.2023 00:00 \u001B[0m\u001B[94m 18.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n",
      "\u001B[94m 3  \u001B[0m\u001B[94m Task 3 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 18.04.2023 00:00 \u001B[0m\u001B[94m 25.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": "<pjplan.viz.mermaid.gantt.MermaidGantt at 0x2625853af80>",
      "text/html": "<iframe srcdoc=\"&lt;html&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;body&gt;\n&lt;style&gt;\n#id_1, #id_2, #id_3{fill:red!important;}\n\n&lt;/style&gt;\n\n&lt;div class=&quot;mermaid&quot;&gt;\ngantt\n  dateFormat DD.MM.YYYY HH:mm\n  tickInterval 1day\n    Task 1: active, id_1, 12.04.2023 00:00, 15.04.2023 00:00\n    Task 2:  id_2, 13.04.2023 00:00, 18.04.2023 00:00\n    Task 3:  id_3, 18.04.2023 00:00, 25.04.2023 00:00\n\n&lt;/div&gt;\n\n&lt;script src=&quot;https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js&quot;&gt;&lt;/script&gt;\n&lt;script&gt;\n    mermaid.ganttConfig = {\n        mirrorActor: true\n    }\n    mermaid.initialize({\n        startOnLoad: true\n    });\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\" width=\"100%\" height=\"300\" style=\"border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with WBS() as wbs:\n",
    "    wbs // Task(1, 'Task 1', estimate=24, start=datetime(2023, 4, 12), resource=dev1.name)\n",
    "    wbs // Task(2, 'Task 2', estimate=8, resource=dev1.name, start=datetime(2023, 4, 13), predecessors=[wbs[1]])\n",
    "    wbs // Task(3, 'Task 3', estimate=40, resource=dev1.name)\n",
    "\n",
    "\n",
    "schedule = DefaultScheduler().calc(wbs)\n",
    "\n",
    "plan = schedule.wbs\n",
    "plan.critical_path2().gantt_bar_style = {\n",
    "    'fill': 'red'\n",
    "}\n",
    "\n",
    "for t in plan.tasks:\n",
    "    print(t.id, t.start, t.end, t.max_start, t.max_end)\n",
    "\n",
    "MermaidGantt(plan)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:41:53.892551Z",
     "end_time": "2023-04-12T14:41:53.899611Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "|\u001B[91m DATE     \u001B[0m|\u001B[91m DEV1 \u001B[0m|\n| 23-04-12 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-13 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-14 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-15 |\u001B[97m 0.0  \u001B[0m|\n| 23-04-16 |\u001B[97m 0.0  \u001B[0m|\n| 23-04-17 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-18 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-19 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-20 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-21 |\u001B[92m 8.0  \u001B[0m|\n| 23-04-22 |\u001B[97m 0.0  \u001B[0m|\n| 23-04-23 |\u001B[97m 0.0  \u001B[0m|\n| 23-04-24 |\u001B[92m 8.0  \u001B[0m|"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MermaidGantt(schedule.backward_wbs)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:40:46.958889Z",
     "end_time": "2023-04-12T14:40:46.968339Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[91m ID \u001B[0m\u001B[91m NAME   \u001B[0m\u001B[91m RESOURCE \u001B[0m\u001B[91m ESTIMATE \u001B[0m\u001B[91m SPENT \u001B[0m\u001B[91m START            \u001B[0m\u001B[91m END              \u001B[0m\u001B[91m PREDECESSORS \u001B[0m\n",
      "\u001B[94m 1  \u001B[0m\u001B[94m Task 1 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 12.04.2023 00:00 \u001B[0m\u001B[94m 15.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n",
      "\u001B[94m 2  \u001B[0m\u001B[94m Task 2 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 13.04.2023 00:00 \u001B[0m\u001B[94m 18.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n",
      "\u001B[94m 3  \u001B[0m\u001B[94m Task 3 \u001B[0m\u001B[94m dev1     \u001B[0m\u001B[94m          \u001B[0m\u001B[94m       \u001B[0m\u001B[94m 18.04.2023 00:00 \u001B[0m\u001B[94m 25.04.2023 00:00 \u001B[0m\u001B[94m []           \u001B[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": "<pjplan.viz.mermaid.gantt.MermaidGantt at 0x262586b8b80>",
      "text/html": "<iframe srcdoc=\"&lt;html&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;body&gt;\n&lt;style&gt;\n\n&lt;/style&gt;\n\n&lt;div class=&quot;mermaid&quot;&gt;\ngantt\n  dateFormat DD.MM.YYYY HH:mm\n  tickInterval 1day\n    Task 1: active, id_1, 12.04.2023 00:00, 15.04.2023 00:00\n    Task 2:  id_2, 13.04.2023 00:00, 18.04.2023 00:00\n    Task 3:  id_3, 18.04.2023 00:00, 25.04.2023 00:00\n\n&lt;/div&gt;\n\n&lt;script src=&quot;https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js&quot;&gt;&lt;/script&gt;\n&lt;script&gt;\n    mermaid.ganttConfig = {\n        mirrorActor: true\n    }\n    mermaid.initialize({\n        startOnLoad: true\n    });\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\" width=\"100%\" height=\"300\" style=\"border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plan"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-12T14:42:06.220379Z",
     "end_time": "2023-04-12T14:42:06.225898Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plan' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[9], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mplan\u001B[49m\n",
      "\u001B[1;31mNameError\u001B[0m: name 'plan' is not defined"
     ]
    }
   ],
   "source": [
    "schedule.resource_usage"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-11T19:43:17.131966Z",
     "end_time": "2023-04-11T19:43:17.160964Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
