Skip to content

Commit

Permalink
Merge pull request RIPE-NCC#129 from danielquinn/feature/new-renderers
Browse files Browse the repository at this point in the history
Using the extended log file format for HTTP
  • Loading branch information
astrikos committed Dec 15, 2015
2 parents ac3f481 + 0710ee4 commit cb96213
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
44 changes: 43 additions & 1 deletion ripe/atlas/tools/renderers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,54 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ..helpers.colours import colourise

from .base import Renderer as BaseRenderer
from .base import Result


class Renderer(BaseRenderer):
"""
We're abusing the Extended Log File Format here to render the result,
amending it to include a few things not originally specified in the W3C
Working Draft: http://www.w3.org/TR/WD-logfile.html Namely:
http-version, header-bytes, and body-bytes
"""

RENDERS = [BaseRenderer.TYPE_HTTP]
COLOURS = {
"2": "green",
"3": "blue",
"4": "yellow",
"5": "red"
}

def on_result(self, result, probes=None):
print("Not ready yet\n")
r = "#Version: 1.0\n#Date: {}\n#Fields: {}\n".format(
result.created.strftime("%Y-%m-%d %H:%M:%S"),
"cs-method cs-uri c-ip s-ip sc-status time-taken http-version "
"header-bytes body-bytes"
)
for response in result.responses:
r += self._colourise_by_status(
"{} {} {} {} {} {} {} {} {}\n".format(
result.method,
result.uri,
response.source_address,
response.destination_address,
response.code,
response.response_time,
response.version,
response.head_size,
response.body_size
),
response.code
)

return Result(r + "\n", result.probe_id)

def _colourise_by_status(self, output, status):
try:
return colourise(output, self.COLOURS[str(status)[0]])
except (IndexError, KeyError):
return colourise(output, "red")
3 changes: 2 additions & 1 deletion ripe/atlas/tools/renderers/ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .base import Renderer as BaseRenderer
from .base import Result


class Renderer(BaseRenderer):

RENDERS = [BaseRenderer.TYPE_NTP]

def on_result(self, result, probes=None):
print("Not ready yet\n")
return Result("Not ready yet\n", result.probe_id)
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .helpers import TestArgumentTypeHelper
from .renderers import (
TestPingRenderer,
TestHttpRenderer,
TestSSLConsistency,
TestAggregatePing,
TestRawRenderer,
Expand All @@ -37,6 +38,7 @@
TestReportCommand,
TestArgumentTypeHelper,
TestPingRenderer,
TestHttpRenderer,
TestSSLConsistency,
TestAggregatePing,
TestRawRenderer,
Expand Down
2 changes: 2 additions & 0 deletions tests/renderers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .ping import TestPingRenderer
from .http import TestHttpRenderer
from .aggregate_ping import TestAggregatePing
from .ssl_consistency import TestSSLConsistency
from .raw import TestRawRenderer

__all__ = [
TestPingRenderer,
TestHttpRenderer,
TestAggregatePing,
TestSSLConsistency
]
48 changes: 48 additions & 0 deletions tests/renderers/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (c) 2015 RIPE NCC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest

from ripe.atlas.sagan import Result
from ripe.atlas.tools.renderers.http import Renderer


class TestHttpRenderer(unittest.TestCase):

def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self.basic = Result.get('{"lts":64,"from":"217.13.64.36","msm_id":2841267,"fw":4720,"timestamp":1450185727,"uri":"http://at-vie-as1120.anchors.atlas.ripe.net:80/4096","prb_id":1,"result":[{"rt":45.953289,"src_addr":"217.13.64.36","hsize":131,"af":4,"bsize":1668618,"res":200,"method":"GET","ver":"1.1","dst_addr":"193.171.255.2"}],"group_id":2841267,"type":"http","msm_name":"HTTPGet"}')
self.multiple = Result.get('{"lts":64,"from":"217.13.64.36","msm_id":2841267,"fw":4720,"timestamp":1450185727,"uri":"http://at-vie-as1120.anchors.atlas.ripe.net:80/4096","prb_id":1,"result":[{"rt":45.953289,"src_addr":"217.13.64.36","hsize":131,"af":4,"bsize":1668618,"res":200,"method":"GET","ver":"1.1","dst_addr":"193.171.255.2"},{"rt":45.953289,"src_addr":"217.13.64.36","hsize":131,"af":4,"bsize":1668618,"res":200,"method":"GET","ver":"1.1","dst_addr":"193.171.255.2"}],"group_id":2841267,"type":"http","msm_name":"HTTPGet"}')

def test_basic(self):
expected = (
'#Version: 1.0\n'
'#Date: 2015-12-15 13:22:07\n'
'#Fields: cs-method cs-uri c-ip s-ip sc-status time-taken http-version header-bytes body-bytes\n'
'GET http://at-vie-as1120.anchors.atlas.ripe.net:80/4096 217.13.64.36 193.171.255.2 200 45.953289 1.1 131 1668618\n\n'
)
self.assertEqual(Renderer().on_result(self.basic), expected)
self.assertEqual(Renderer().on_result(self.basic).probe_id, 1)

def test_multiple(self):
expected = (
'#Version: 1.0\n'
'#Date: 2015-12-15 13:22:07\n'
'#Fields: cs-method cs-uri c-ip s-ip sc-status time-taken http-version header-bytes body-bytes\n'
'GET http://at-vie-as1120.anchors.atlas.ripe.net:80/4096 217.13.64.36 193.171.255.2 200 45.953289 1.1 131 1668618\n'
'GET http://at-vie-as1120.anchors.atlas.ripe.net:80/4096 217.13.64.36 193.171.255.2 200 45.953289 1.1 131 1668618\n\n'
)
self.assertEqual(Renderer().on_result(self.multiple), expected)
self.assertEqual(Renderer().on_result(self.basic).probe_id, 1)

0 comments on commit cb96213

Please sign in to comment.