Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbacon committed Feb 26, 2010
0 parents commit 39a6a17
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
Empty file added README
Empty file.
59 changes: 59 additions & 0 deletions barcharter.py
@@ -0,0 +1,59 @@
#!/usr/bin/python

import sys
skip_ips = ["93.97.158.26"]
html_template_head = """
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["imagebarchart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
"""

html_template_foot = """
var chart = new google.visualization.ImageBarChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, min: 0});
}
</script>
</head>
<body>
<div id='chart_div' style='width: 700px; height: 240px;'></div>
</body>
</html>
"""
cols_html = """
data.addColumn('string', 'Source');
data.addColumn('number', 'Count');"""
row_html = """
data.setValue(%d, 0, '%s');
data.setValue(%d, 1, %d);
"""
data_html = "%(count)s, undefined, undefined"

def get_data(file_handle):
name = file_handle.name.split("/")[-1].split(".")[0]
count = 0
for row in file_handle.readlines():
ip = row.split()[0]
if ip in skip_ips:
continue
count += 1
skip_ips.append(ip)
return name, count

def plot_data(sources):
html = html_template_head
html += cols_html
html += " data.addRows(%d);" % len(sources)
idx = 0
for name, count in sources:
html += row_html % (idx, name, idx, count);
idx += 1
html += html_template_foot
print html

sources = [get_data(open(x, "r")) for x in sys.argv[1:]]
plot_data(sources)
97 changes: 97 additions & 0 deletions customlogcharter.py
@@ -0,0 +1,97 @@
#!/usr/bin/python

import sys
import re
from datetime import datetime, timedelta
skip_ips = ["93.97.158.26"]
html_template_head = """
<head>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages':['annotatedtimeline']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Date');
"""

html_template_foot = """
var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(data, {displayAnnotations: true});
}
</script>
</head>
<body>
<div id='chart_div' style='width: 700px; height: 240px;'></div>
</body>
</html>
"""
cols_html = """
data.addColumn('number', '%s');
data.addColumn('string', 'title1');
data.addColumn('string', 'text1');"""
row_html = """
data.addRows([
[new Date(%(year)s,%(month)s ,%(day)s, %(hour)s, 0, 0), %(value_seq)s],
]);
"""
data_html = "%(count)s, undefined, undefined"

def get_data(file_handle):
rows = []
times = []
buckets = {}
start_date = None
local_skip_ips = skip_ips[:]
for line in file_handle:
if not line.strip():
continue
ip = line.split()[0]
if ip in local_skip_ips:
continue
local_skip_ips.append(ip)
match = re.match(r".*\[([^\]]+).*", line)
if match:
when = datetime.strptime(match.group(1),
"%d/%b/%Y:%H:%M:%S +0000")
rounded = when.strftime("%d/%b/%Y:%H")
when = datetime.strptime(rounded, "%d/%b/%Y:%H")
if not start_date:
start_date = when
end_date = when
val = buckets.setdefault(when, 0)
buckets[when] = val + 1
name = file_handle.name.split("/")[-1].split(".")[0]
return name, start_date, end_date, buckets

def plot_data(sources):
html = html_template_head
for name, _, _, _ in sources:
html += cols_html % name
times = []
start_date = min([x for _, x, _, _ in sources])
end_date = max([y for _, _, y, _ in sources])
when = start_date
while when < end_date:
times.append(when)
when += timedelta(0,0,0,0,0,1)
rows = []
for when in times:
value_seq = []
for _, _, _, buckets in sources:
values = {'year':when.year,
'month':when.month,
'day':when.day,
'hour':when.hour,
'count':buckets.get(when, 0)}
value_seq.append(data_html % values)
values['value_seq'] = ",".join(value_seq)
rows.append(row_html % values)
html += "\n".join(rows)
html += html_template_foot
print html

sources = [get_data(open(x, "r")) for x in sys.argv[1:]]
plot_data(sources)

0 comments on commit 39a6a17

Please sign in to comment.