{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "EstimateDuration.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyOXuogvNERRkTG3v1zsmZeO",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/gmihaila/machine_learning_things/blob/master/notebooks%20/EstimateDuration.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "V8gFFNxISKIb",
        "colab_type": "text"
      },
      "source": [
        "# EstimateDuration"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lEPujXhASbaV",
        "colab_type": "text"
      },
      "source": [
        "# Imports"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9XsGO6ZXScgc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import time\n",
        "from datetime import datetime, timedelta"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "t__KXKfTSeTA",
        "colab_type": "text"
      },
      "source": [
        "# Code"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JgyBO3heSEGr",
        "colab_type": "code",
        "outputId": "e115cd00-11ca-4887-9f93-3b06149afc9f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "source": [
        "class EstimateDuration(object):\n",
        "  def __init__(self, steps):\n",
        "    assert steps > 0\n",
        "    self.steps = steps\n",
        "    self.start_time = time.time()\n",
        "    self.time_steps = []\n",
        "    self.current_time_step = 0\n",
        "    self.start_date = datetime.now()\n",
        "\n",
        "  def start_step(self,):\n",
        "    self.start_time = time.time()\n",
        "    return\n",
        "\n",
        "  def end_step(self,):\n",
        "    end_time = time.time()\n",
        "    self.time_steps.append(end_time - self.start_time)\n",
        "    self.current_time_step += 1\n",
        "    return\n",
        "\n",
        "  def elapsed(self,):\n",
        "    return self.convert_seconds(sum(self.time_steps))\n",
        "\n",
        "  def convert_seconds(self, seconds):\n",
        "    d= seconds//86400\n",
        "    h= (seconds-(d*86400))//3600\n",
        "    m= (seconds - ((d*86400) + (h*3600)))//60\n",
        "    s= seconds - ((d*86400) + (h*3600) + (m*60))\n",
        "    return \"%d:%d:%d:%d\"%(d,h,m,s)\n",
        "\n",
        "  def eta(self, return_strings=True):\n",
        "    length_step = sum(self.time_steps)/self.current_time_step\n",
        "    estimated_seconds = length_step * (self.steps - self.current_time_step)\n",
        "    estimated_date = datetime.now() + timedelta(seconds=estimated_seconds)\n",
        "    if return_strings:\n",
        "      estimated_time = self.convert_seconds(estimated_seconds)\n",
        "      return estimated_time, estimated_date.strftime(\"%d-%b-%Y (%H:%M:%S)\")\n",
        "    else:\n",
        "      return estimated_seconds, estimated_date\n",
        "\n",
        "  \n",
        "# test\n",
        "tracker = EstimateDuration(10)\n",
        "\n",
        "print(datetime.now().strftime(\"%d-%b-%Y (%H:%M:%S)\"))\n",
        "\n",
        "for i in range(10):\n",
        "  tracker.start_step()\n",
        "  if i %2 == 0:\n",
        "    time.sleep(3)\n",
        "  else:\n",
        "    time.sleep(1)\n",
        "  tracker.end_step()\n",
        "  print('eta',tracker.eta(return_strings=False))\n",
        "\n",
        "tracker.elapsed(), tracker.eta(), datetime.now().strftime(\"%d-%b-%Y (%H:%M:%S)\")"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "20-Apr-2020 (14:55:17)\n",
            "eta (27.02792716026306, datetime.datetime(2020, 4, 20, 14, 55, 47, 946784))\n",
            "eta (16.016749382019043, datetime.datetime(2020, 4, 20, 14, 55, 37, 936992))\n",
            "eta (16.35031692186991, datetime.datetime(2020, 4, 20, 14, 55, 41, 273910))\n",
            "eta (12.012553095817566, datetime.datetime(2020, 4, 20, 14, 55, 37, 937513))\n",
            "eta (11.009690046310425, datetime.datetime(2020, 4, 20, 14, 55, 39, 936269))\n",
            "eta (8.007166067759195, datetime.datetime(2020, 4, 20, 14, 55, 37, 935067))\n",
            "eta (6.434496811458042, datetime.datetime(2020, 4, 20, 14, 55, 39, 366197))\n",
            "eta (4.003724098205566, datetime.datetime(2020, 4, 20, 14, 55, 37, 936769))\n",
            "eta (2.113107999165853, datetime.datetime(2020, 4, 20, 14, 55, 39, 49516))\n",
            "eta (0.0, datetime.datetime(2020, 4, 20, 14, 55, 37, 937797))\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('0:0:0:20', ('0:0:0:0', '20-Apr-2020 (14:55:37)'), '20-Apr-2020 (14:55:37)')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I0smj3wsTfBn",
        "colab_type": "code",
        "outputId": "e327d4f8-e565-452a-bb77-c5006cafeebf",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "from datetime import datetime, timedelta\n",
        "x = datetime.now() + timedelta(seconds=3)\n",
        "x += timedelta(seconds=300)\n",
        "\n",
        "x"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "datetime.datetime(2020, 4, 20, 14, 56, 40, 678165)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tZrwoZOxEk68",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2e57dd48-01ad-4934-e261-600d4bb2cecc"
      },
      "source": [
        "x.strftime(\"%d-%b-%Y (%H:%M:%S)\")"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'20-Apr-2020 (14:56:40)'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    }
  ]
}