Skip to content

Instantly share code, notes, and snippets.

@moonblade
Last active July 19, 2016 18:03
Show Gist options
  • Save moonblade/ac6fdc504180b3682d7fe19bea3ca113 to your computer and use it in GitHub Desktop.
Save moonblade/ac6fdc504180b3682d7fe19bea3ca113 to your computer and use it in GitHub Desktop.
speedLogger
Copy and paste to relevant files in the same path as speed.json
run ./view.sh
#!/usr/bin/env python
import json
import re
import dateutil.parser
import datetime
import pytz
# tz = pytz.timezone('Asia/Kolkata')
epoch = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=None) + datetime.timedelta(hours=5,minutes=30)
def clean_json(string):
string = re.sub(",[ \t\r\n]+}", "}", string)
string = re.sub(",[ \t\r\n]+\]", "]", string)
return string
def unix_time_millis(dt):
return (dt - epoch).total_seconds() * 1000.0
def getTime(content):
return unix_time_millis(dateutil.parser.parse(content['time']).replace(tzinfo=None));
with open('speed.json', 'r') as content_file:
contents = content_file.read()
contents = "[" + contents + "]"
print("data=[", end="")
print('["Time","Download","Upload"],', end="")
contents = json.loads(clean_json(contents))
for content in contents:
print('[' + "new Date(" + str(getTime(content)) +
'),' + content['download'] + ',' + content['upload'] + '],', end="")
print("]", end="")
#!/bin/bash
dir="~/workspace/speedLogger/"
speedtest-cli --simple > $dir"temp"
# /usr/local/bin/speedtest-cli --simple > $dir"temp"
# dir="/home/administrator/.speed/"
temp=$dir"temp"
out=$dir"speed.json"
latency=$(/bin/cat $temp | head -1 | cut -f 2 -d" ")
latency_unit=$(/bin/cat $temp | head -1 | cut -f 3 -d" ")
download=$(/bin/cat $temp | head -2 | tail -1 | cut -f2 -d" ")
download_unit=$(/bin/cat $temp | head -2 | tail -1 | cut -f3 -d" ")
upload=$(/bin/cat $temp | head -3 | tail -1 | cut -f2 -d" ")
upload_unit=$(/bin/cat $temp | head -3 | tail -1 | cut -f3 -d" ")
date=$(/bin/date)
/bin/echo "
{
\"time\" : \""$date"\",
\"download\" : \""$download"\",
\"download_unit\" : \""$download_unit"\",
\"upload\" : \""$upload"\",
\"upload_unit\" : \""$upload_unit"\",
\"latency\" : \""$latency"\",
\"latency_unit\" : \""$latency_unit"\",
}," >> $out
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
console.log(data)
function drawChart() {
var drawable = google.visualization.arrayToDataTable(data);
var options = {
title: 'Speed (Mbps)',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(drawable, options);
}
</script>
</head>
<body>
<div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>
#!/bin/bash
./getData.py > data.js
xdg-open view.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment