# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import time
import numpy as np

from bokeh.models.sources import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, push_notebook
from bokeh.models import DatetimeTickFormatter
from math import pi

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
from pandasticsearch import Select, DataFrame

import datetime
from pytz import timezone
f"{datetime.datetime.now(timezone('US/Central')):%Y-%m-%d %H:%M:%S}"

import datetime,time
from pytz import timezone
timestamp=time.mktime(time.strptime("2019-07-29 00:00:00","%Y-%m-%d %H:%M:%S"))
dt=datetime.datetime.fromtimestamp(timestamp)
dt=dt+datetime.timedelta(seconds=30)
f"{dt:%Y-%m-%d %H:%M:%S}"

# Using the new query format

# +
hostname="http://noname-sms.us.cray.com:30200"
client =  Elasticsearch(hostname, http_compress=True)

start="2019-07-29 00:00:00"
timestamp=time.mktime(time.strptime(start,"%Y-%m-%d %H:%M:%S"))
dt=datetime.datetime.fromtimestamp(timestamp)
dt=dt+datetime.timedelta(seconds=30)
end=f"{dt:%Y-%m-%d %H:%M:%S}"

q="""
    {
        "size":0,
        "query": {
            "bool": {
              "must": [{
                  "match_all": {}
                },
                {
                  "range": {
                    "timereported": {
                      "gte": "%s",
                      "lte": "%s",
                      "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
              }
            }
          ],
          "must_not": []
        }
      },
        "_source":{
            "excludes":[]
        },
        "aggs": {
        "2": {
          "date_histogram": {
            "field": "timereported",
            "interval": "30s",
            "time_zone": "America/Chicago",
            "min_doc_count": 1
          }
        }
      }
    }
""" % (gte,lte)

#print("Query: \"%s\"" % q)
resp = client.search(index="shasta-logs-*", body=q)
print("Number of responses: " + "{:,}".format(resp['hits']['total']))
# -

output_notebook()

p = figure(plot_width=800, plot_height=400)
test_data = ColumnDataSource(data=dict(x=[0], y=[0]))
#line = my_figure.line("x", "y", source=test_data)
line = p.circle("x", "y", source=test_data, size = 8, color = 'navy', alpha=0.3)
p.title.text = 'Message Counts per 30 minutes'
p.background_fill_color="#f5f5f5"
p.grid.grid_line_color="white"
p.yaxis.axis_label = 'Count'
p.xaxis.axis_label =' timereported per 30 minutes'
p.xaxis.formatter=DatetimeTickFormatter(
        hours=["%d %B %Y"],
        days=["%d %B %Y"],
        months=["%d %B %Y"],
        years=["%d %B %Y"],
    )
p.xaxis.major_label_orientation = pi/4
handle = show(p, notebook_handle=True)

# +
from threading import Thread

stop_threads = False

def blocking_callback(id, stop):
    new_data=dict(x=[0], y=[0])

    step = 0
    step_size = 0.1  # increment for increasing step
    max_step = 10  # arbitrary stop point for example
    period = 1  # in seconds (simulate waiting for new data)
    n_show = 1000  # number of points to keep and show

    while True:

        new_data['x'] = [step]
        new_data['y'] = [np.random.randint(0,1000)]

        test_data.stream(new_data, n_show)

        push_notebook(handle=handle)
        step += step_size
        time.sleep(period)

        if stop():
            print("exit")
            break

thread = Thread(target=blocking_callback, args=(id, lambda: stop_threads))
thread.start()
# -

# preceding streaming is not blocking
for cnt in range(10):
    print("Do this, while plot is still streaming", cnt)

# you might also want to stop the thread
stop_threads=True
del thread



StarcoderdataJupyterScriptsDedupFiltered