Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import subprocess
  2. import datetime
  3.  
  4. import parse
  5. from timeout_decorator import timeout, TimeoutError
  6. import pandas
  7. from bokeh.plotting import figure, ColumnDataSource, curdoc
  8.  
  9. MAX_PING = 500
  10. MAX_TABLE_LENGTH = 20
  11.  
  12.  
  13. @timeout(seconds=MAX_PING/1e3)
  14. def _ping(host='google.com'):
  15. r = subprocess.run(['ping', '-c', '1', host], stdout=subprocess.PIPE)
  16. time_str = r.stdout.decode('utf-8').split('\n')[1].split(' ')[-2]
  17. return float(parse.parse('time={}', time_str)[0])
  18.  
  19.  
  20. def ping(**kwargs):
  21. try:
  22. p = _ping(**kwargs)
  23. except TimeoutError:
  24. p = 'NaN'
  25. return p
  26.  
  27.  
  28. def make_doc(doc):
  29. source = ColumnDataSource(
  30. pandas.DataFrame({
  31. 'time': [],
  32. 'latency': []}))
  33.  
  34. def update():
  35. new = {'time': [datetime.datetime.now()],
  36. 'latency': [ping()]}
  37. source.stream(pandas.DataFrame(new))
  38.  
  39. # Setup line figure
  40. fig_1 = figure(sizing_mode='scale_width',
  41. x_axis_type='datetime')
  42. fig_1.line(
  43. source=source,
  44. x='time',
  45. y='latency',
  46. color='black',
  47. line_width=2,)
  48. fig_1.xaxis.axis_label = 'Time'
  49. fig_1.yaxis.axis_label = 'Round-Trip Time to google.com [ms]'
  50. fig_1.x_range.follow = "end"
  51. fig_1.x_range.follow_interval = 6e4
  52. fig_1.x_range.range_padding = 0
  53.  
  54. # Setup Document
  55. doc.title = "High Street Ping"
  56. doc.add_periodic_callback(update, 200)
  57. doc.add_root(fig_1)
  58.  
  59.  
  60. # Create document
  61. make_doc(curdoc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement