{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "import datetime\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.plotting import cursession, figure, show, output_notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*To run these examples you must execute the command `python bokeh-server` in the top-level Bokeh source directory first.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook(url=\"default\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "TS_MULT_us = 1e6\n",
    "UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime\n",
    "\n",
    "def int2dt(ts, ts_mult=TS_MULT_us):\n",
    "    \"\"\"Convert timestamp (integer) to datetime\"\"\"\n",
    "    return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))\n",
    "        \n",
    "def td2int(td, ts_mult=TS_MULT_us):\n",
    "    \"\"\"Convert timedelta to integer\"\"\"\n",
    "    return(int(td.total_seconds()*ts_mult))\n",
    "        \n",
    "def dt2int(dt, ts_mult=TS_MULT_us):\n",
    "    \"\"\"Convert datetime to integer\"\"\"\n",
    "    delta = dt - UNIX_EPOCH\n",
    "    return(int(delta.total_seconds()*ts_mult))\n",
    "        \n",
    "def int_from_last_sample(dt, td):\n",
    "    return(dt2int(dt) - dt2int(dt) % td2int(td))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "TS_MULT = 1e3\n",
    "td_delay = datetime.timedelta(seconds=0.5)\n",
    "delay_s = td_delay.total_seconds()\n",
    "delay_int = td2int(td_delay, TS_MULT)\n",
    "\n",
    "value = 1000 # initial value\n",
    "N = 100 # number of elements into circular buffer\n",
    "\n",
    "buff = collections.deque([value]*N, maxlen=N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t_now = datetime.datetime.utcnow()\n",
    "ts_now = dt2int(t_now, TS_MULT)\n",
    "t = collections.deque(np.arange(ts_now-N*delay_int, ts_now, delay_int), maxlen=N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p = figure(x_axis_type=\"datetime\")\n",
    "p.line(list(t), list(buff), color=\"#0000FF\", name=\"line_example\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "renderer = p.select(dict(name=\"line_example\"))[0]\n",
    "ds = renderer.data_source\n",
    "show(p)\n",
    "while True:\n",
    "    ts_now = dt2int(datetime.datetime.utcnow(), 1e3)\n",
    "    t.append(ts_now)\n",
    "    ds.data['x'] = list(t)\n",
    "\n",
    "    value += np.random.uniform(-1, 1)\n",
    "    buff.append(value)\n",
    "    ds.data['y'] = list(buff)\n",
    "    \n",
    "    cursession().store_objects(ds)\n",
    "    time.sleep(delay_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
