From: Marek Vavrusa Date: Wed, 8 Jun 2016 07:04:21 +0000 (-0700) Subject: modules/http: graphs, prometheus metrics, websocks X-Git-Tag: v1.1.0~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf2a18b05beb0b3b4af032db2124f97783a13306;p=thirdparty%2Fknot-resolver.git modules/http: graphs, prometheus metrics, websocks * http embeds modified lua-http server code that reuses single cqueue for all h2 client sockets, this is also because the API in upstream is unstable * http embeds rickshaw for real-time graphs over websockets, it displays latency heatmap by default and can show several other metrics * http shows a world map with pinned recently contacted authoritatives, where diameter represents number of queries sent and colour its average RTT, so you can see where the queries are going * http now exports several endpoints and websockets: /stats for statistics in JSON, and /metrics for metrics in Prometheus text format --- diff --git a/daemon/lua/sandbox.lua b/daemon/lua/sandbox.lua index 2398e8f55..aca525c11 100644 --- a/daemon/lua/sandbox.lua +++ b/daemon/lua/sandbox.lua @@ -10,6 +10,17 @@ min = minute hour = 60 * minute day = 24 * hour +-- Logging +function panic(fmt, ...) + error(string.format('error: '..fmt, ...)) +end +function warn(fmt, ...) + io.stderr:write(string.format(fmt..'\n', ...)) +end +function log(fmt, ...) + print(string.format(fmt, ...)) +end + -- Resolver bindings kres = require('kres') trust_anchors = require('trust_anchors') diff --git a/modules/http/README.rst b/modules/http/README.rst index 9fb4db8a6..9bf2c6b74 100644 --- a/modules/http/README.rst +++ b/modules/http/README.rst @@ -28,6 +28,7 @@ for starters? http = { host = 'localhost', port = 8053, + geoip = 'GeoLite2-City.mmdb' -- Optional } } @@ -71,6 +72,40 @@ the outputs of following: openssl req -new -key mykey.key -out csr.pem openssl req -x509 -days 90 -key mykey.key -in csr.pem -out mycert.crt +Built-in services +^^^^^^^^^^^^^^^^^ + +The HTTP module has several built-in services to use. + +.. csv-table:: + :header: "Endpoint", "Service", "Description" + + "``/stats``", "Statistics/metrics", "Exported metrics in JSON." + "``/metrics``", "Prometheus metrics", "Exported metrics for Prometheus_" + "``/feed``", "Most frequent queries", "List of most frequent queries in JSON." + +Enabling Prometheus metrics endpoint +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The module exposes ``/metrics`` endpoint that serves internal metrics in Prometheus_ text format. +You can use it out of the box: + +.. code-block:: bash + + $ curl -k https://localhost:8053/metrics | tail + # TYPE latency histogram + latency_bucket{le=10} 2.000000 + latency_bucket{le=50} 2.000000 + latency_bucket{le=100} 2.000000 + latency_bucket{le=250} 2.000000 + latency_bucket{le=500} 2.000000 + latency_bucket{le=1000} 2.000000 + latency_bucket{le=1500} 2.000000 + latency_bucket{le=+Inf} 2.000000 + latency_count 2.000000 + latency_sum 11.000000 + + How to expose services over HTTP ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -141,4 +176,10 @@ Dependencies * `lua-http `_ available in LuaRocks - ``$ luarocks install --server=http://luarocks.org/dev http`` \ No newline at end of file + ``$ luarocks install --server=http://luarocks.org/dev http`` + +* `mmdblua `_ available in LuaRocks + + ``$ luarocks install --server=http://luarocks.org/dev mmdblua`` + +.. _Prometheus: https://prometheus.io \ No newline at end of file diff --git a/modules/http/http.lua b/modules/http/http.lua index be2d0fab2..e30660c56 100644 --- a/modules/http/http.lua +++ b/modules/http/http.lua @@ -8,6 +8,7 @@ local server = require('http.server') local headers = require('http.headers') local websocket = require('http.websocket') local x509, pkey = require('openssl.x509'), require('openssl.pkey') +local has_mmdb, mmdb = pcall(require, 'mmdb') -- Module declaration local cq = cqueues.new() @@ -31,89 +32,54 @@ local function pgload(relpath) fp:close() -- Guess content type local ext = relpath:match('[^\\.]+$') - return {'/'..relpath, mime_types[ext] or 'text', data} + return {'/'..relpath, mime_types[ext] or 'text', data, 86400} end -- Preloaded static assets local pages = { + pgload('favicon.ico'), + pgload('rickshaw.min.css'), pgload('kresd.js'), pgload('datamaps.world.min.js'), pgload('topojson.js'), pgload('jquery.js'), - pgload('epoch.css'), - pgload('epoch.js'), - pgload('favicon.ico'), + pgload('rickshaw.min.js'), pgload('d3.js'), } -- Serve preloaded root page local function serve_root() - local _, mime_root, mime_data = unpack(pgload('main.tpl')) - mime_data = mime_data - :gsub('{{ title }}', 'kresd @ '..hostname()) - :gsub('{{ host }}', hostname()) + local data = pgload('main.tpl')[3] + data = data + :gsub('{{ title }}', 'kresd @ '..hostname()) + :gsub('{{ host }}', hostname()) return function (h, stream) - -- Return index page + -- Render snippets local rsnippets = {} for _,v in pairs(M.snippets) do table.insert(rsnippets, string.format('

%s

\n%s', v[1], v[2])) end - local data = mime_data - :gsub('{{ secure }}', stream:checktls() and 'true' or 'false') - :gsub('{{ snippets }}', table.concat(rsnippets, '\n')) - local hsend = headers.new() - hsend:append(':status', '200') - hsend:append('content/type', mime_root) - assert(stream:write_headers(hsend, false)) - assert(stream:write_chunk(data, true)) - -- Push assets - -- local path, mime, data = unpack(pages[1]) - -- local hpush = headers.new() - -- hpush:append(':scheme', h:get(':scheme')) - -- hpush:append(':method', 'GET') - -- hpush:append(':authority', h:get(':authority')) - -- hpush:append(':path', path) - -- local nstream = stream:push_promise(hpush) - -- hpush = headers.new() - -- hpush:append(':status', '200') - -- hpush:append('content/type', mime) - -- print('pushing', path) - -- assert(nstream:write_headers(hpush, false)) - -- assert(nstream:write_chunk(data, true)) - -- Do not send anything else - return false - end -end - --- Load dependent modules -if not stats then modules.load('stats') end --- Function to sort frequency list -local function stream_stats(h, ws) - local ok, prev = true, stats.list() - while ok do - -- Get current snapshot - local cur, stats_dt = stats.list(), {} - for k,v in pairs(cur) do - stats_dt[k] = v - (prev[k] or 0) - end - prev = cur - -- Publish stats updates periodically - local push = tojson({stats=stats_dt}) - ok = ws:send(push) - cqueues.sleep(0.5) + -- Return index page + return data + :gsub('{{ secure }}', stream:checktls() and 'true' or 'false') + :gsub('{{ snippets }}', table.concat(rsnippets, '\n')) end - ws:close() end -- Export HTTP service endpoints M.endpoints = { - ['/'] = {'text/html', serve_root()}, - ['/stats'] = {'application/json', stats.list, stream_stats}, - ['/feed'] = {'application/json', stats.frequent}, + ['/'] = {'text/html', serve_root()}, } + +-- Export static pages for _, pg in ipairs(pages) do - local path, mime, data = unpack(pg) - M.endpoints[path] = {mime, data} + local path, mime, data, ttl = unpack(pg) + M.endpoints[path] = {mime, data, nil, ttl} +end + +-- Export built-in prometheus interface +for k, v in pairs(require('prometheus')) do + M.endpoints[k] = v end -- Export HTTP service page snippets @@ -140,11 +106,15 @@ local function serve_get(h, stream) if type(data) == 'table' then data = tojson(data) end if not mime or type(data) ~= 'string' then hsend:append(':status', '404') - assert(stream:write_headers(hsend, false)) + assert(stream:write_headers(hsend, true)) else -- Serve content type appropriately hsend:append(':status', '200') - hsend:append('content/type', mime) + hsend:append('content-type', mime) + local ttl = entry and entry[4] + if ttl then + hsend:append('cache-control', string.format('max-age=%d', ttl)) + end assert(stream:write_headers(hsend, false)) assert(stream:write_chunk(data, true)) end @@ -174,6 +144,7 @@ local function route(endpoints) if cb then cb(h, ws) end + ws:close() return -- Handle HTTP method appropriately elseif m == 'GET' then @@ -182,13 +153,9 @@ local function route(endpoints) -- Method is not supported local hsend = headers.new() hsend:append(':status', '500') - assert(stream:write_headers(hsend, false)) + assert(stream:write_headers(hsend, true)) end stream:shutdown() - -- Close multiplexed HTTP/2 connection only when empty - if connection.version < 2 or connection.new_streams:length() == 0 then - connection:shutdown() - end end end @@ -282,18 +249,18 @@ function M.interface(host, port, endpoints, crtfile, keyfile) end -- Check loaded certificate if not crt or not key then - error(string.format('failed to load certificate "%s" - %s', crtfile, err or 'error')) + panic('failed to load certificate "%s" - %s', crtfile, err or 'error') end end -- Create TLS context and start listening local s, err = server.listen { host = host, port = port, - tls = crt ~= nil, - ctx = crt and tlscontext(crt, key), + client_timeout = 5, + ctx = crt and tlscontext(crt, key), } if not s then - error(string.format('failed to listen on %s#%d: %s', host, port, err)) + panic('failed to listen on %s#%d: %s', host, port, err) end -- Compose server handler local routes = route(endpoints) @@ -307,7 +274,7 @@ function M.interface(host, port, endpoints, crtfile, keyfile) local _, expiry = crt:getLifetime() expiry = math.max(0, expiry - (os.time() - 3 * 24 * 3600)) event.after(expiry, function (ev) - print('[http] refreshed ephemeral certificate') + log('[http] refreshed ephemeral certificate') crt, key = updatecert(crtfile, keyfile) s.ctx = tlscontext(crt, key) end) @@ -321,21 +288,21 @@ function M.deinit() end -- @function Configure module -local ffi = require('ffi') function M.config(conf) conf = conf or {} assert(type(conf) == 'table', 'config { host = "...", port = 443, cert = "...", key = "..." }') -- Configure web interface for resolver if not conf.port then conf.port = 8053 end if not conf.host then conf.host = 'localhost' end + if conf.geoip and has_mmdb then M.geoip = mmdb.open(conf.geoip) end M.interface(conf.host, conf.port, M.endpoints, conf.cert, conf.key) -- TODO: configure DNS/HTTP(s) interface if M.ev then return end -- Schedule both I/O activity notification and timeouts local poll_step - poll_step = function (ev, status, events) + poll_step = function () local ok, err, _, co = cq:step(0) - if not ok then print('[http]', err, debug.traceback(co)) end + if not ok then warn('[http] error: %s %s', err, debug.traceback(co)) end -- Reschedule timeout or create new one local timeout = cq:timeout() if timeout then diff --git a/modules/http/http.mk b/modules/http/http.mk index 1b6d45822..9c8983dcf 100644 --- a/modules/http/http.mk +++ b/modules/http/http.mk @@ -1,3 +1,5 @@ -http_SOURCES := http.lua -http_INSTALL := $(wildcard modules/http/static/*) +http_SOURCES := http.lua prometheus.lua +http_INSTALL := $(wildcard modules/http/static/*) \ + modules/http/http/h2_connection.lua \ + modules/http/http/server.lua $(call make_lua_module,http) diff --git a/modules/http/http/LICENSE b/modules/http/http/LICENSE new file mode 100644 index 000000000..ec71e4921 --- /dev/null +++ b/modules/http/http/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015-2016 Daurnimator + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/modules/http/http/README b/modules/http/http/README new file mode 100644 index 000000000..382e159c9 --- /dev/null +++ b/modules/http/http/README @@ -0,0 +1,5 @@ +Embedded unstable APIs from https://github.com/daurnimator/lua-http under MIT license, see LICENSE. + +ChangeLog: +* Marek Vavrusa : + - Modified h2_connection to reuse current cqueue context \ No newline at end of file diff --git a/modules/http/http/h2_connection.lua b/modules/http/http/h2_connection.lua new file mode 100644 index 000000000..f7a488844 --- /dev/null +++ b/modules/http/http/h2_connection.lua @@ -0,0 +1,462 @@ +local cqueues = require "cqueues" +local monotime = cqueues.monotime +local cc = require "cqueues.condition" +local ce = require "cqueues.errno" +local rand = require "openssl.rand" +local new_fifo = require "fifo" +local band = require "http.bit".band +local h2_error = require "http.h2_error" +local h2_stream = require "http.h2_stream" +local hpack = require "http.hpack" +local h2_banned_ciphers = require "http.tls".banned_ciphers +local spack = string.pack or require "compat53.string".pack +local sunpack = string.unpack or require "compat53.string".unpack + +local assert = assert +if _VERSION:match("%d+%.?%d*") < "5.3" then + assert = require "compat53.module".assert +end + +local function xor(a, b) + return (a and b) or not (a or b) +end + +local preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" + +local default_settings = { + [0x1] = 4096; -- HEADER_TABLE_SIZE + [0x2] = true; -- ENABLE_PUSH + [0x3] = math.huge; -- MAX_CONCURRENT_STREAMS + [0x4] = 65535; -- INITIAL_WINDOW_SIZE + [0x5] = 16384; -- MAX_FRAME_SIZE + [0x6] = math.huge; -- MAX_HEADER_LIST_SIZE +} + +local function merge_settings(new, old) + return { + [0x1] = new[0x1] or old[0x1]; + [0x2] = new[0x2] or old[0x2]; + [0x3] = new[0x3] or old[0x3]; + [0x4] = new[0x4] or old[0x4]; + [0x5] = new[0x5] or old[0x5]; + [0x6] = new[0x6] or old[0x6]; + } +end + +local connection_methods = {} +local connection_mt = { + __name = "http.h2_connection"; + __index = connection_methods; +} + +function connection_mt:__tostring() + return string.format("http.h2_connection{type=%q}", + self.type) +end + +local connection_main_loop + +-- An 'onerror' that doesn't throw +local function onerror(socket, op, why, lvl) -- luacheck: ignore 212 + if why == ce.EPIPE or why == ce.ETIMEDOUT then + return why + end + return string.format("%s: %s", op, ce.strerror(why)), why +end + +-- Read bytes from the given socket looking for the http2 connection preface +-- optionally ungets the bytes in case of failure +local function socket_has_preface(socket, unget, timeout) + local deadline = timeout and (monotime()+timeout) + local bytes = "" + local is_h2 = true + while #bytes < #preface do + -- read *up to* number of bytes left in preface + local ok, err, errno = socket:xread(#bytes-#preface, deadline and (deadline-monotime())) + if ok == nil then + return nil, err or ce.EPIPE, errno + end + bytes = bytes .. ok + if bytes ~= preface:sub(1, #bytes) then + is_h2 = false + break + end + end + if unget then + local ok, errno = socket:unget(bytes) + if not ok then + return nil, onerror(socket, "unget", errno, 2) + end + end + return is_h2 +end + +local function new_connection(socket, conn_type, settings, timeout, cq) + local deadline = timeout and (monotime()+timeout) + cq = assert(cq or cqueues.running()) + + socket:setmode("b", "bf") + socket:setvbuf("full", math.huge) -- 'infinite' buffering; no write locks needed + socket:onerror(onerror) + + local ssl = socket:checktls() + if ssl then + local cipher = ssl:getCipherInfo() + if h2_banned_ciphers[cipher.name] then + h2_error.errors.INADEQUATE_SECURITY("bad cipher: " .. cipher.name) + end + end + if conn_type == "client" then + local ok, err = socket:xwrite(preface, "f", timeout) + if ok == nil then return nil, err end + elseif conn_type == "server" then + local ok, err = socket_has_preface(socket, false, timeout) + if ok == nil then + return nil, err + end + if not ok then + h2_error.errors.PROTOCOL_ERROR("invalid connection preface. not an http2 client?") + end + else + error('invalid connection type. must be "client" or "server"') + end + + settings = settings or {} + + local self = setmetatable({ + socket = socket; + type = conn_type; + version = 2; -- for compat with h1_connection + + streams = setmetatable({}, {__mode="kv"}); + stream0 = nil; -- store separately with a strong reference + need_continuation = nil; -- stream + highest_odd_stream = -1; + highest_even_stream = -2; + recv_goaway_lowest = nil; + recv_goaway = cc.new(); + new_streams = new_fifo(); + new_streams_cond = cc.new(); + peer_settings = default_settings; + peer_settings_cond = cc.new(); -- signaled when the peer has changed their settings + acked_settings = default_settings; + send_settings = {n = 0}; + send_settings_ack_cond = cc.new(); -- for when server ACKs our settings + send_settings_acked = 0; + peer_flow_credits = 65535; -- 5.2.1 + peer_flow_credits_increase = cc.new(); + encoding_context = nil; + decoding_context = nil; + pongs = {}; -- pending pings we've sent. keyed by opaque 8 byte payload + }, connection_mt) + self:new_stream(0) + self.encoding_context = hpack.new(default_settings[0x1]) + self.decoding_context = hpack.new(default_settings[0x1]) + self.cq = cq:wrap(connection_main_loop, self) + + do -- send settings frame + wait for reply to complete connection + local ok, err = self:settings(settings, deadline and (deadline-monotime())) + if not ok then + return nil, err + end + end + + return self +end + +function connection_methods:pollfd() + return self.socket:pollfd() +end + +function connection_methods:events() + return self.socket:events() +end + +function connection_methods:timeout() + if not self:empty() then + return 0 + end +end + +function connection_main_loop(self) + while not self.socket:eof("r") do + local typ, flag, streamid, payload = self:read_http2_frame() + if typ == nil then + if flag == nil then -- EOF + self.socket:close() + break + else + error(flag) + end + end + local handler = h2_stream.frame_handlers[typ] + -- http2 spec section 4.1: + -- Implementations MUST ignore and discard any frame that has a type that is unknown. + if handler then + local stream = self.streams[streamid] + if stream == nil then + if xor(streamid % 2 == 1, self.type == "client") then + h2_error.errors.PROTOCOL_ERROR("Streams initiated by a client MUST use odd-numbered stream identifiers; those initiated by the server MUST use even-numbered stream identifiers") + end + -- TODO: check MAX_CONCURRENT_STREAMS + stream = self:new_stream(streamid) + self.new_streams:push(stream) + self.new_streams_cond:signal(1) + end + local ok, err = handler(stream, flag, payload) + if not ok then + if h2_error.is(err) and err.stream_error then + if not stream:write_rst_stream(err.code) then + error(err) + end + else -- connection error or unknown error + error(err) + end + end + end + end + return true +end + +local function handle_step_return(self, step_ok, last_err, errno) + if step_ok then + return true + else + if not self.socket:eof("w") then + local code, message + if step_ok then + code = h2_error.errors.NO_ERROR.code + elseif h2_error.is(last_err) then + code = last_err.code + message = last_err.message + else + code = h2_error.errors.INTERNAL_ERROR.code + end + -- ignore write failure here; there's nothing that can be done + self:write_goaway_frame(nil, code, message) + end + self:shutdown() + return nil, last_err, errno + end +end + +function connection_methods:checktls() + return self.socket:checktls() +end + +function connection_methods:localname() + return self.socket:localname() +end + +function connection_methods:peername() + return self.socket:peername() +end + +function connection_methods:shutdown() + local ok, err = self:write_goaway_frame(nil, h2_error.errors.NO_ERROR.code, "connection closed") + if not ok and err == ce.EPIPE then + -- other end already closed + ok, err = true, nil + end + for _, stream in pairs(self.streams) do + stream:shutdown() + end + self.socket:shutdown("r") + return ok, err +end + +function connection_methods:close() + local ok, err = self:shutdown() + cqueues.poll() + cqueues.poll() + self.socket:close() + return ok, err +end + +function connection_methods:new_stream(id) + if id then + assert(id % 1 == 0) + else + if self.recv_goaway_lowest then + h2_error.errors.PROTOCOL_ERROR("Receivers of a GOAWAY frame MUST NOT open additional streams on the connection") + end + if self.type == "client" then + -- Pick next free odd number + id = self.highest_odd_stream + 2 + else + -- Pick next free odd number + id = self.highest_even_stream + 2 + end + -- TODO: check MAX_CONCURRENT_STREAMS + end + assert(self.streams[id] == nil, "stream id already in use") + assert(id < 2^32, "stream id too large") + if id % 2 == 0 then + assert(id > self.highest_even_stream, "stream id too small") + self.highest_even_stream = id + else + assert(id > self.highest_odd_stream, "stream id too small") + self.highest_odd_stream = id + end + local stream = h2_stream.new(self, id) + if id == 0 then + self.stream0 = stream + else + -- Add dependency on stream 0. http2 spec, 5.3.1 + self.stream0:reprioritise(stream) + end + self.streams[id] = stream + return stream +end + +-- this function *should never throw* +function connection_methods:get_next_incoming_stream(timeout) + local deadline = timeout and (monotime()+timeout) + while self.new_streams:length() == 0 do + if self.socket:eof('r') or self.recv_goaway_lowest then + -- TODO? clarification required: can the sender of a GOAWAY subsequently start streams? + -- (with a lower stream id than they sent in the GOAWAY) + -- For now, assume not. + return nil, ce.EPIPE + end + local which = cqueues.poll(self.socket, self.new_streams_cond, self.recv_goaway, timeout) + if which == timeout then + return nil, ce.ETIMEDOUT + end + timeout = deadline and (deadline-monotime()) + end + + local stream = self.new_streams:pop() + return stream +end + +-- On success, returns type, flags, stream id and payload +-- On timeout, returns nil, ETIMEDOUT -- safe to retry +-- If the socket has been shutdown for reading, and there is no data left unread, returns EPIPE +-- Will raise an error on other errors, or if the frame is invalid +function connection_methods:read_http2_frame(timeout) + local deadline = timeout and (monotime()+timeout) + local frame_header, err, errno = self.socket:xread(9, timeout) + if frame_header == nil then + if err == ce.ETIMEDOUT then + return nil, err + elseif err == nil --[[EPIPE]] and self.socket:eof("r") then + return nil + else + return nil, err, errno + end + end + local size, typ, flags, streamid = sunpack(">I3 B B I4", frame_header) + if size > self.acked_settings[0x5] then + return nil, h2_error.errors.FRAME_SIZE_ERROR:new_traceback("frame too large") + end + -- reserved bit MUST be ignored by receivers + streamid = band(streamid, 0x7fffffff) + local payload, err2, errno2 = self.socket:xread(size, deadline and (deadline-monotime())) + if payload == nil then + if err2 == ce.ETIMEDOUT then + -- put frame header back into socket so a retry will work + local ok, errno3 = self.socket:unget(frame_header) + if not ok then + return nil, onerror(self.socket, "unget", errno3, 2) + end + end + return nil, err2, errno2 + end + return typ, flags, streamid, payload +end + +-- If this times out, it was the flushing; not the write itself +-- hence it's not always total failure. +-- It's up to the caller to take some action (e.g. closing) rather than doing it here +-- TODO: distinguish between nothing sent and partially sent? +function connection_methods:write_http2_frame(typ, flags, streamid, payload, timeout) + local deadline = timeout and (monotime()+timeout) + if #payload > self.peer_settings[0x5] then + return nil, h2_error.errors.FRAME_SIZE_ERROR:new_traceback("frame too large") + end + local header = spack(">I3 B B I4", #payload, typ, flags, streamid) + local ok, err, errno = self.socket:xwrite(header, "f", timeout) + if not ok then + return nil, err, errno + end + return self.socket:xwrite(payload, "n", deadline and (deadline-monotime())) +end + +function connection_methods:ping(timeout) + local deadline = timeout and (monotime()+timeout) + local payload + -- generate a random, unique payload + repeat -- keep generating until we don't have a collision + payload = rand.bytes(8) + until self.pongs[payload] == nil + local cond = cc.new() + self.pongs[payload] = cond + assert(self.stream0:write_ping_frame(false, payload, timeout)) + while self.pongs[payload] do + timeout = deadline and (deadline-monotime()) + local which = cqueues.poll(self, cond, timeout) + if which == self then + local ok, err, errno = self:step(0) + if not ok then + return nil, err, errno + end + elseif which == timeout then + return nil, ce.ETIMEDOUT + end + end + return true +end + +function connection_methods:write_window_update(...) + return self.stream0:write_window_update(...) +end + +function connection_methods:write_goaway_frame(last_stream_id, err_code, debug_msg) + if last_stream_id == nil then + last_stream_id = math.max(self.highest_odd_stream, self.highest_even_stream) + end + return self.stream0:write_goaway_frame(last_stream_id, err_code, debug_msg) +end + +function connection_methods:set_peer_settings(peer_settings) + self.peer_settings = merge_settings(peer_settings, self.peer_settings) + self.peer_settings_cond:signal() +end + +function connection_methods:ack_settings() + local n = self.send_settings_acked + 1 + self.send_settings_acked = n + local acked_settings = self.send_settings[n] + if acked_settings then + self.send_settings[n] = nil + self.acked_settings = merge_settings(acked_settings, self.acked_settings) + end + self.send_settings_ack_cond:signal(1) +end + +function connection_methods:settings(tbl, timeout) + local deadline = timeout and monotime()+timeout + local n, err, errno = self.stream0:write_settings_frame(false, tbl, timeout) + if not n then + return nil, err, errno + end + -- Now wait for ACK + while self.send_settings_acked < n do + timeout = deadline and (deadline-monotime()) + local which = cqueues.poll(self.send_settings_ack_cond, timeout) + if which ~= self.send_settings_ack_cond then + self:write_goaway_frame(nil, h2_error.errors.SETTINGS_TIMEOUT.code, "timeout exceeded") + return nil, ce.ETIMEDOUT + end + end + return true +end + +return { + preface = preface; + socket_has_preface = socket_has_preface; + new = new_connection; + methods = connection_methods; + mt = connection_mt; +} \ No newline at end of file diff --git a/modules/http/http/server.lua b/modules/http/http/server.lua new file mode 100644 index 000000000..49d9fe67e --- /dev/null +++ b/modules/http/http/server.lua @@ -0,0 +1,339 @@ +local cqueues = require "cqueues" +local monotime = cqueues.monotime +local cs = require "cqueues.socket" +local cc = require "cqueues.condition" +local ce = require "cqueues.errno" +local h1_connection = require "http.h1_connection" +local h2_connection = require "http.h2_connection" +local http_tls = require "http.tls" +local pkey = require "openssl.pkey" +local x509 = require "openssl.x509" +local name = require "openssl.x509.name" +local altname = require "openssl.x509.altname" + +local hang_timeout = 0.03 + +local function onerror(socket, op, why, lvl) -- luacheck: ignore 212 + if why == ce.EPIPE or why == ce.ETIMEDOUT then + return why + end + return string.format("%s: %s", op, ce.strerror(why)), why +end + +-- Sense for TLS or SSL client hello +-- returns `true`, `false` or `nil, err` +local function is_tls_client_hello(socket, timeout) + -- reading for 6 bytes should be safe, as no HTTP version + -- has a valid client request shorter than 6 bytes + local first_bytes, err, errno = socket:xread(6, timeout) + if first_bytes == nil then + return nil, err or ce.EPIPE, errno + end + local use_tls = not not ( + first_bytes:match("^\22\3...\1") or -- TLS + first_bytes:match("^[\128-\255][\9-\255]\1") -- SSLv2 + ) + local ok + ok, errno = socket:unget(first_bytes) + if not ok then + return nil, onerror(socket, "unget", errno, 2) + end + return use_tls +end + +-- Wrap a bare cqueues socket in an HTTP connection of a suitable version +-- Starts TLS if necessary +-- this function *should never throw* +local function wrap_socket(self, socket, deadline) + socket:setmode("b", "b") + socket:onerror(onerror) + local use_tls = self.tls + if use_tls == nil then + local err, errno + use_tls, err, errno = is_tls_client_hello(socket, deadline and (deadline-monotime())) + if use_tls == nil then + return nil, err, errno + end + end + local is_h2 -- tri-state + if use_tls then + local ok, err, errno = socket:starttls(self.ctx, deadline and (deadline-monotime())) + if not ok then + return nil, err, errno + end + local ssl = socket:checktls() + if ssl and http_tls.has_alpn then + local proto = ssl:getAlpnSelected() + if proto == "h2" then + is_h2 = true + elseif proto == nil or proto == "http/1.1" then + is_h2 = false + else + return nil, "unexpected ALPN protocol: " .. proto + end + end + end + -- Still not sure if incoming connection is an HTTP1 or HTTP2 connection + -- Need to sniff for the h2 connection preface to find out for sure + if is_h2 == nil then + local err, errno + is_h2, err, errno = h2_connection.socket_has_preface(socket, true, deadline and (deadline-monotime())) + if is_h2 == nil then + return nil, err, errno + end + end + local conn, err, errno + if is_h2 then + conn, err, errno = h2_connection.new(socket, "server", nil, deadline and (deadline-monotime())) + else + conn, err, errno = h1_connection.new(socket, "server", 1.1) + end + if not conn then + return nil, err, errno + end + return conn, is_h2 +end + +-- this function *should never throw* +local function handle_client(conn, on_stream) + while true do + local stream, err, errno = conn:get_next_incoming_stream() + if stream == nil then + if (err == ce.EPIPE or errno == ce.ECONNRESET or errno == ce.ENOTCONN) + and (conn.socket == nil or conn.socket:pending() == 0) then + break + else + return nil, err, errno + end + end + on_stream(stream) + end + -- wait for streams to complete? + return true +end + +-- Prefer whichever comes first +local function alpn_select(ssl, protos) -- luacheck: ignore 212 + for _, proto in ipairs(protos) do + if proto == "h2" or proto == "http/1.1" then + return proto + end + end + return nil +end + +-- create a new self signed cert +local function new_ctx(host) + local ctx = http_tls.new_server_context() + if ctx.setAlpnSelect then + ctx:setAlpnSelect(alpn_select) + end + local crt = x509.new() + -- serial needs to be unique or browsers will show uninformative error messages + crt:setSerial(os.time()) + -- use the host we're listening on as canonical name + local dn = name.new() + dn:add("CN", host) + crt:setSubject(dn) + local alt = altname.new() + alt:add("DNS", host) + crt:setSubjectAlt(alt) + -- lasts for 10 years + crt:setLifetime(os.time(), os.time()+86400*3650) + -- can't be used as a CA + crt:setBasicConstraints{CA=false} + crt:setBasicConstraintsCritical(true) + -- generate a new private/public key pair + local key = pkey.new() + crt:setPublicKey(key) + crt:sign(key) + assert(ctx:setPrivateKey(key)) + assert(ctx:setCertificate(crt)) + return ctx +end + +local server_methods = { + max_concurrent = math.huge; + client_timeout = 10; +} +local server_mt = { + __name = "http.server"; + __index = server_methods; +} + +function server_mt:__tostring() + return string.format("http.server{socket=%s;n_connections=%d}", + tostring(self.socket), self.n_connections) +end + +--[[ Creates a new server object + +Takes a table of options: + - `.socket`: A cqueues socket object + - `.tls`: `nil`: allow both tls and non-tls connections + - `true`: allows tls connections only + - `false`: allows non-tls connections only + - `.ctx`: an `openssl.ssl.context` object to use for tls connections + - ` `nil`: a self-signed context will be generated + - `.max_concurrent`: Maximum number of connections to allow live at a time (default: infinity) + - `.client_timeout`: Timeout (in seconds) to wait for client to send first bytes and/or complete TLS handshake (default: 10) +]] +local function new_server(tbl) + local socket = assert(tbl.socket) + + -- Return errors rather than throwing + socket:onerror(function(s, op, why, lvl) -- luacheck: ignore 431 212 + return why + end) + + return setmetatable({ + socket = socket; + tls = tbl.tls; + ctx = tbl.ctx; + max_concurrent = tbl.max_concurrent; + n_connections = 0; + pause_cond = cc.new(); + paused = true; + connection_done = cc.new(); -- signalled when connection has been closed + client_timeout = tbl.client_timeout; + }, server_mt) +end + +--[[ +Extra options: + - `.family`: protocol family + - `.host`: address to bind to (required if not `.path`) + - `.port`: port to bind to (optional if tls isn't `nil`, in which case defaults to 80 for `.tls == false` or 443 if `.tls == true`) + - `.path`: path to UNIX socket (required if not `.host`) + - `.v6only`: allow ipv6 only (no ipv4-mapped-ipv6) + - `.mode`: fchmod or chmod socket after creating UNIX domain socket + - `.mask`: set and restore umask when binding UNIX domain socket + - `.unlink`: unlink socket path before binding? + - `.reuseaddr`: turn on SO_REUSEADDR flag? + - `.reuseport`: turn on SO_REUSEPORT flag? +]] +local function listen(tbl) + local tls = tbl.tls + local host = tbl.host + local path = tbl.path + assert(host or path, "need host or path") + local port = tbl.port + if host and port == nil then + if tls == true then + port = "443" + elseif tls == false then + port = "80" + else + error("need port") + end + end + local ctx = tbl.ctx + if ctx == nil and tls ~= false then + if host then + ctx = new_ctx(host) + else + error("Custom OpenSSL context required when using a UNIX domain socket") + end + end + local s = assert(cs.listen{ + family = tbl.family; + host = host; + port = port; + path = path; + mode = tbl.mode; + mask = tbl.mask; + unlink = tbl.unlink; + reuseaddr = tbl.reuseaddr; + reuseport = tbl.reuseport; + v6only = tbl.v6only; + }) + return new_server { + socket = s; + tls = tls; + ctx = ctx; + max_concurrent = tbl.max_concurrent; + client_timeout = tbl.client_timeout; + } +end + +-- Actually wait for and *do* the binding +-- Don't *need* to call this, as if not it will be done lazily +function server_methods:listen(timeout) + return self.socket:listen(timeout) +end + +function server_methods:localname() + return self.socket:localname() +end + +function server_methods:pause() + self.paused = true + self.pause_cond:signal() +end + +function server_methods:close() + self:pause() + cqueues.poll() + cqueues.poll() + self.socket:close() +end + +function server_methods:run(on_stream, cq) + cq = assert(cq or cqueues.running()) + self.paused = false + repeat + if self.n_connections >= self.max_concurrent then + cqueues.poll(self.connection_done, self.pause_cond) + if self.paused then + break + end + end + local socket, accept_errno = self.socket:accept({nodelay = true;}, 0) + if socket == nil then + if accept_errno == ce.ETIMEDOUT then + -- Yield this thread until a client arrives or server paused + cqueues.poll(self.socket, self.pause_cond) + elseif accept_errno == ce.EMFILE then + -- Wait for another request to finish + if cqueues.poll(self.connection_done, self.pause_cond, hang_timeout) == hang_timeout then + -- If we're stuck waiting, run a garbage collection sweep + -- This can prevent a hang + collectgarbage() + end + else + return nil, ce.strerror(accept_errno), accept_errno + end + else + self.n_connections = self.n_connections + 1 + cq:wrap(function() + local ok, err + local conn, is_h2, errno = wrap_socket(self, socket) + if not conn then + err = is_h2 + socket:close() + if errno == ce.ECONNRESET or err == 'read: Connection reset by peer' then + ok = true + end + else + ok, err = handle_client(conn, on_stream) + conn:close() + end + self.n_connections = self.n_connections - 1 + self.connection_done:signal(1) + if not ok + and err ~= ce.EPIPE -- client closed connection + and err ~= ce.ETIMEDOUT -- an operation timed out + then + error(err) + end + end) + end + until self.paused + return true +end + +return { + new = new_server; + listen = listen; + mt = server_mt; +} \ No newline at end of file diff --git a/modules/http/prometheus.lua b/modules/http/prometheus.lua new file mode 100644 index 000000000..e50976499 --- /dev/null +++ b/modules/http/prometheus.lua @@ -0,0 +1,85 @@ +local cqueues = require('cqueues') + +-- Load dependent modules +if not stats then modules.load('stats') end + +local function getstats() + local t = stats.list() + for k,v in pairs(cache.stats()) do t['cache.'..k] = v end + for k,v in pairs(worker.stats()) do t['worker.'..k] = v end + return t +end + +-- Function to sort frequency list +local function stream_stats(h, ws) + local ok, prev = true, getstats() + while ok do + -- Get current snapshot + local cur, stats_dt = getstats(), {} + for k,v in pairs(cur) do + stats_dt[k] = v - (prev[k] or 0) + end + prev = cur + -- Calculate upstreams and geotag them if possible + local upstreams + if http.geoip then + upstreams = stats.upstreams() + for k,v in pairs(upstreams) do + local gi + if string.find(k, '.', 1, true) then + gi = http.geoip:search_ipv4(k) + else + gi = http.geoip:search_ipv6(k) + end + if gi then + upstreams[k] = {data=v, location=gi.location, country=gi.country and gi.country.iso_code} + end + end + end + -- Publish stats updates periodically + local push = tojson({stats=stats_dt,upstreams=upstreams or {}}) + ok = ws:send(push) + cqueues.sleep(1) + end +end + +-- Render stats in Prometheus text format +local function serve_prometheus() + -- First aggregate metrics list and print counters + local slist, render = getstats(), {} + local latency = {} + local counter = '# TYPE %s counter\n%s %f' + for k,v in pairs(slist) do + k = select(1, k:gsub('%.', '_')) + -- Aggregate histograms + local band = k:match('answer_([%d]+)ms') + if band then + table.insert(latency, {band, v}) + elseif k == 'answer_slow' then + table.insert(latency, {'+Inf', v}) + -- Counter as a fallback + else table.insert(render, string.format(counter, k, k, v)) end + end + -- Fill in latency histogram + local function kweight(x) return tonumber(x) or math.huge end + table.sort(latency, function (a,b) return kweight(a[1]) < kweight(b[1]) end) + table.insert(render, '# TYPE latency histogram') + local count, sum = 0.0, 0.0 + for _,e in ipairs(latency) do + -- The information about the %Inf bin is lost, so we treat it + -- as a timeout (3000ms) for metrics purposes + count = count + e[2] + sum = sum + e[2] * (math.min(tonumber(e[1]), 3000.0)) + table.insert(render, string.format('latency_bucket{le=%s} %f', e[1], count)) + end + table.insert(render, string.format('latency_count %f', count)) + table.insert(render, string.format('latency_sum %f', sum)) + return table.concat(render, '\n') +end + +-- Export endpoints +return { + ['/stats'] = {'application/json', getstats, stream_stats}, + ['/frequent'] = {'application/json', function () return stats.frequent() end}, + ['/metrics'] = {'text/plain; version=0.0.4', serve_prometheus}, +} \ No newline at end of file diff --git a/modules/http/static/kresd.js b/modules/http/static/kresd.js index e731838a8..3346ae20e 100644 --- a/modules/http/static/kresd.js +++ b/modules/http/static/kresd.js @@ -1,43 +1,92 @@ -// Colour palette -var colours = [ - 'rgb(198,219,239)', - 'rgb(158,202,225)', - 'rgb(107,174,214)', - 'rgb(66,146,198)', - 'rgb(33,113,181)', - 'rgb(8,81,156)', - 'rgb(8,48,107)', -]; -// Unit conversion -function tounit(d) { - d = parseInt(d); - if (d < 1000) return d.toFixed(0); - else if (d < 1000000) return (d / 1000.0).toFixed(1) + 'K'; - else return (d / 1000000.0).toFixed(1) + 'M'; -} -// Set up UI and pollers +var colours = ['#ffffd9','#edf8b1','#c7e9b4','#7fcdbb','#41b6c4','#1d91c0','#225ea8','#253494','#081d58']; +var latency = ['1ms', '10ms', '50ms', '100ms', '250ms', '500ms', '1000ms', '1500ms', 'slow']; +var palette = new Rickshaw.Color.Palette( { scheme: 'colorwheel' } ); + window.onload = function() { - var statsLabels = ['cached', '10ms', '100ms', '1000ms', 'slow']; - var statsHistory = []; - var now = Date.now(); - for (i = 0; i < statsLabels.length; ++i) { - statsHistory.push({ label: 'Layer ' + statsLabels[i], values: [{time: now, y:0}] }); - $('.stats-legend').append('
  • ' + statsLabels[i]); + /* Latency has its own palette */ + var series = []; + var data = []; + function pushSeries(name, color) { + data[name] = []; + var s = { + name: name, + color: color, + data: data[name], + stroke: true, + preserve: true + } + series.push(s); + return s; + } + /* Render latency metrics as sort of a heatmap */ + for (var i in latency) { + var s = pushSeries('answer.'+latency[i], colours[colours.length - i - 1]); + s.name = 'RTT '+latency[i]; + s.renderer = 'bar'; } - var statsChart = $('#stats').epoch({ - type: 'time.bar', - axes: ['right', 'bottom'], - ticks: { right: 2 }, - margins: { right: 60 }, - tickFormats: { - right: function(d) { return tounit(d) + ' pps'; }, - bottom: function(d) { return new Date(d).toTimeString().split(' ')[0]; }, + /* Render other interesting metrics as lines (hidden by default) */ + var metrics = { + 'answer.noerror': 'NOERROR', + 'answer.nodata': 'NODATA', + 'answer.nxdomain': 'NXDOMAIN', + 'answer.servfail': 'SERVFAIL', + 'answer.dnssec': 'DNSSEC', + 'cache.hit': 'Cache hit', + 'cache.miss': 'Cache miss', + 'worker.udp': 'Outgoing UDP', + 'worker.tcp': 'Outgoing TCP', + 'worker.ipv4': 'Outgoing IPv4', + 'worker.ipv6': 'Outgoing IPv6', + }; + for (var key in metrics) { + var s = pushSeries(key, palette.color()); + s.name = metrics[key]; + s.renderer = 'line'; + s.disabled = true; + } + + /* Define how graph looks like. */ + var graph = new Rickshaw.Graph( { + element: document.getElementById('chart'), + height: 350, + width: 700, + renderer: 'multi', + series: series, + }); + var x_axis = new Rickshaw.Graph.Axis.Time( { + graph: graph, + ticksTreatment: 'glow', + element: document.querySelector("#x_axis"), + } ); + var y_axis = new Rickshaw.Graph.Axis.Y( { + graph: graph, + orientation: 'left', + ticksTreatment: 'glow', + tickFormat: function (y) { + return Rickshaw.Fixtures.Number.formatKMBT(y) + ' pps'; }, - data: statsHistory + element: document.querySelector("#y_axis") + } ); + var graphHover = new Rickshaw.Graph.HoverDetail({graph: graph}); + var legend = new Rickshaw.Graph.Legend({ + graph: graph, + element: document.querySelector("#legend") }); + var highlighter = new Rickshaw.Graph.Behavior.Series.Highlight( { + graph: graph, + legend: legend + } ); + var shelving = new Rickshaw.Graph.Behavior.Series.Toggle( { + graph: graph, + legend: legend + } ); + + graph.render(); + + /* Data map */ var fills = { defaultFill: '#F5F5F5' }; for (var i in colours) { - fills['q' + i] = colours[i]; + fills['q' + i] = colours[colours.length - 1 - i]; } var map = new Datamap({ element: document.getElementById('map'), @@ -54,32 +103,102 @@ window.onload = function() { '
    Queries: ', data ? data.queries : '0', '', ''].join(''); } + }, + bubblesConfig: { + popupTemplate: function(geo, data) { + return ['
    ', + '', data.name, '', + '
    Queries: ', data ? data.queries : '0', '', + '
    Average RTT: ', data ? parseInt(data.rtt) : '0', ' ms', + '
    '].join(''); + } } }); + function colorBracket(rtt) { + for (var i in latency) { + if (rtt <= parseInt(latency[i])) { + return 'q' + i; + } + } + return 'q8'; + } + function togeokey(lon, lat) { + return lon.toFixed(0)+'#'+lat.toFixed(0); + } - /* - * Realtime updates over WebSockets - */ + /* Realtime updates over WebSockets */ function pushMetrics(resp) { - var now = Date.now(); - var next = []; - for (i = 0; i < statsLabels.length; ++i) { - var val = resp['answer.' + statsLabels[i]]; - next.push({time: now, y: val}); + var now = Date.now() / 1000; + for (var lb in resp) { + var val = resp[lb]; + /* Push new datapoints */ + if (lb in data) { + data[lb].push({x: now, y:val}); + if (data[lb].length > 100) { + data[lb].shift(); + } + } + } - statsChart.push(next); + graph.update(); } - function updateFeed(resp) { - var feed = $('#feed'); - feed.children().remove(); - feed.append('Query nameTypeFrequency') - for (i = 0; i < resp.length; ++i) { - var row = $(''); - row.append('' + resp[i].name + ''); - row.append('' + resp[i].type + ''); - row.append('' + resp[i].count + ''); - feed.append(row); + + var age = 0; + var bubbles = []; + var bubblemap = {}; + function pushUpstreams(resp) { + if (resp == null) { + return; + } + /* Get current maximum number of queries for bubble diameter adjustment */ + var maxQueries = 1; + for (var key in resp) { + var val = resp[key]; + if ('data' in val) { + maxQueries = Math.max(maxQueries, resp[key].data.length) + } + } + /* Update bubbles and prune the oldest */ + for (var key in resp) { + var val = resp[key]; + if (!val.data) { + continue; + } + var sum = val.data.reduce(function(a, b) { return a + b; }); + var avg = sum / val.data.length; + var geokey = togeokey(val.location.longitude, val.location.latitude) + var found = bubblemap[geokey]; + if (!found) { + found = { + name: [key], + longitude: val.location.longitude, + latitude: val.location.latitude, + queries: 0, + rtt: avg, + } + bubbles.push(found); + bubblemap[geokey] = found; + } + /* Update bubble parameters */ + if (!(key in found.name)) { + found.name.push(key); + } + found.rtt = (found.rtt + avg) / 2.0; + found.fillKey = colorBracket(found.rtt); + found.queries = found.queries + val.data.length; + found.radius = Math.max(5, 15*(val.data.length/maxQueries)); + found.age = age; + } + /* Prune bubbles not updated in a while. */ + for (var i in bubbles) { + var b = bubbles[i]; + if (b.age <= age - 5) { + bubbles.splice(i, 1) + bubblemap[i] = null; + } } + map.bubbles(bubbles); + age = age + 1; } /* WebSocket endpoints */ @@ -89,6 +208,6 @@ window.onload = function() { ws.onmessage = function(evt) { var data = $.parseJSON(evt.data); pushMetrics(data.stats); - updateFeed(data.freq) + pushUpstreams(data.upstreams); }; } diff --git a/modules/http/static/main.tpl b/modules/http/static/main.tpl index 7948c6781..e2faafd49 100644 --- a/modules/http/static/main.tpl +++ b/modules/http/static/main.tpl @@ -3,19 +3,48 @@ {{ title }} - + - +

    {{ title }}

    -
    -
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +

      Where do the queries go?

      {{ snippets }} diff --git a/modules/http/static/rickshaw.min.css b/modules/http/static/rickshaw.min.css new file mode 100644 index 000000000..d1b32d8eb --- /dev/null +++ b/modules/http/static/rickshaw.min.css @@ -0,0 +1 @@ +.rickshaw_graph .detail{pointer-events:none;position:absolute;top:0;z-index:2;background:rgba(0,0,0,.1);bottom:0;width:1px;transition:opacity .25s linear;-moz-transition:opacity .25s linear;-o-transition:opacity .25s linear;-webkit-transition:opacity .25s linear}.rickshaw_graph .detail.inactive{opacity:0}.rickshaw_graph .detail .item.active{opacity:1}.rickshaw_graph .detail .x_label{font-family:Arial,sans-serif;border-radius:3px;padding:6px;opacity:.5;border:1px solid #e0e0e0;font-size:12px;position:absolute;background:#fff;white-space:nowrap}.rickshaw_graph .detail .x_label.left{left:0}.rickshaw_graph .detail .x_label.right{right:0}.rickshaw_graph .detail .item{position:absolute;z-index:2;border-radius:3px;padding:.25em;font-size:12px;font-family:Arial,sans-serif;opacity:0;background:rgba(0,0,0,.4);color:#fff;border:1px solid rgba(0,0,0,.4);margin-left:1em;margin-right:1em;margin-top:-1em;white-space:nowrap}.rickshaw_graph .detail .item.left{left:0}.rickshaw_graph .detail .item.right{right:0}.rickshaw_graph .detail .item.active{opacity:1;background:rgba(0,0,0,.8)}.rickshaw_graph .detail .item:after{position:absolute;display:block;width:0;height:0;content:"";border:5px solid transparent}.rickshaw_graph .detail .item.left:after{top:1em;left:-5px;margin-top:-5px;border-right-color:rgba(0,0,0,.8);border-left-width:0}.rickshaw_graph .detail .item.right:after{top:1em;right:-5px;margin-top:-5px;border-left-color:rgba(0,0,0,.8);border-right-width:0}.rickshaw_graph .detail .dot{width:4px;height:4px;margin-left:-3px;margin-top:-3.5px;border-radius:5px;position:absolute;box-shadow:0 0 2px rgba(0,0,0,.6);box-sizing:content-box;-moz-box-sizing:content-box;background:#fff;border-width:2px;border-style:solid;display:none;background-clip:padding-box}.rickshaw_graph .detail .dot.active{display:block}.rickshaw_graph{position:relative}.rickshaw_graph svg{display:block;overflow:hidden}.rickshaw_graph .x_tick{position:absolute;top:0;bottom:0;width:0;border-left:1px dotted rgba(0,0,0,.2);pointer-events:none}.rickshaw_graph .x_tick .title{position:absolute;font-size:12px;font-family:Arial,sans-serif;opacity:.5;white-space:nowrap;margin-left:3px;bottom:1px}.rickshaw_annotation_timeline{height:1px;border-top:1px solid #e0e0e0;margin-top:10px;position:relative}.rickshaw_annotation_timeline .annotation{position:absolute;height:6px;width:6px;margin-left:-2px;top:-3px;border-radius:5px;background-color:rgba(0,0,0,.25)}.rickshaw_graph .annotation_line{position:absolute;top:0;bottom:-6px;width:0;border-left:2px solid rgba(0,0,0,.3);display:none}.rickshaw_graph .annotation_line.active{display:block}.rickshaw_graph .annotation_range{background:rgba(0,0,0,.1);display:none;position:absolute;top:0;bottom:-6px}.rickshaw_graph .annotation_range.active{display:block}.rickshaw_graph .annotation_range.active.offscreen{display:none}.rickshaw_annotation_timeline .annotation .content{background:#fff;color:#000;opacity:.9;padding:5px;box-shadow:0 0 2px rgba(0,0,0,.8);border-radius:3px;position:relative;z-index:20;font-size:12px;padding:6px 8px 8px;top:18px;left:-11px;width:160px;display:none;cursor:pointer}.rickshaw_annotation_timeline .annotation .content:before{content:"\25b2";position:absolute;top:-11px;color:#fff;text-shadow:0 -1px 1px rgba(0,0,0,.8)}.rickshaw_annotation_timeline .annotation.active,.rickshaw_annotation_timeline .annotation:hover{background-color:rgba(0,0,0,.8);cursor:none}.rickshaw_annotation_timeline .annotation .content:hover{z-index:50}.rickshaw_annotation_timeline .annotation.active .content{display:block}.rickshaw_annotation_timeline .annotation:hover .content{display:block;z-index:50}.rickshaw_graph .y_axis,.rickshaw_graph .x_axis_d3{fill:none}.rickshaw_graph .y_ticks .tick line,.rickshaw_graph .x_ticks_d3 .tick{stroke:rgba(0,0,0,.16);stroke-width:2px;shape-rendering:crisp-edges;pointer-events:none}.rickshaw_graph .y_grid .tick,.rickshaw_graph .x_grid_d3 .tick{z-index:-1;stroke:rgba(0,0,0,.2);stroke-width:1px;stroke-dasharray:1 1}.rickshaw_graph .y_grid .tick[data-y-value="0"]{stroke-dasharray:1 0}.rickshaw_graph .y_grid path,.rickshaw_graph .x_grid_d3 path{fill:none;stroke:none}.rickshaw_graph .y_ticks path,.rickshaw_graph .x_ticks_d3 path{fill:none;stroke:gray}.rickshaw_graph .y_ticks text,.rickshaw_graph .x_ticks_d3 text{opacity:.5;font-size:12px;pointer-events:none}.rickshaw_graph .x_tick.glow .title,.rickshaw_graph .y_ticks.glow text{fill:#000;color:#000;text-shadow:-1px 1px 0 rgba(255,255,255,.1),1px -1px 0 rgba(255,255,255,.1),1px 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1),0 -1px 0 rgba(255,255,255,.1),1px 0 0 rgba(255,255,255,.1),-1px 0 0 rgba(255,255,255,.1),-1px -1px 0 rgba(255,255,255,.1)}.rickshaw_graph .x_tick.inverse .title,.rickshaw_graph .y_ticks.inverse text{fill:#fff;color:#fff;text-shadow:-1px 1px 0 rgba(0,0,0,.8),1px -1px 0 rgba(0,0,0,.8),1px 1px 0 rgba(0,0,0,.8),0 1px 0 rgba(0,0,0,.8),0 -1px 0 rgba(0,0,0,.8),1px 0 0 rgba(0,0,0,.8),-1px 0 0 rgba(0,0,0,.8),-1px -1px 0 rgba(0,0,0,.8)}.rickshaw_legend{font-family:Arial;font-size:12px;color:#fff;background:#404040;display:inline-block;padding:12px 5px;border-radius:2px;position:relative}.rickshaw_legend:hover{z-index:10}.rickshaw_legend .swatch{width:10px;height:10px;border:1px solid rgba(0,0,0,.2)}.rickshaw_legend .line{clear:both;line-height:140%;padding-right:15px}.rickshaw_legend .line .swatch{display:inline-block;margin-right:3px;border-radius:2px}.rickshaw_legend .label{margin:0;white-space:nowrap;display:inline;font-size:inherit;background-color:transparent;color:inherit;font-weight:400;line-height:normal;padding:0;text-shadow:none}.rickshaw_legend .action:hover{opacity:.6}.rickshaw_legend .action{margin-right:.2em;font-size:10px;opacity:.2;cursor:pointer;font-size:14px}.rickshaw_legend .line.disabled{opacity:.4}.rickshaw_legend ul{list-style-type:none;margin:0;padding:0;margin:2px;cursor:pointer}.rickshaw_legend li{padding:0 0 0 2px;min-width:80px;white-space:nowrap}.rickshaw_legend li:hover{background:rgba(255,255,255,.08);border-radius:3px}.rickshaw_legend li:active{background:rgba(255,255,255,.2);border-radius:3px} \ No newline at end of file diff --git a/modules/http/static/rickshaw.min.js b/modules/http/static/rickshaw.min.js new file mode 100644 index 000000000..ddbaab600 --- /dev/null +++ b/modules/http/static/rickshaw.min.js @@ -0,0 +1,3 @@ +(function(root,factory){if(typeof define==="function"&&define.amd){define(["d3"],function(d3){return root.Rickshaw=factory(d3)})}else if(typeof exports==="object"){module.exports=factory(require("d3"))}else{root.Rickshaw=factory(d3)}})(this,function(d3){var Rickshaw={namespace:function(namespace,obj){var parts=namespace.split(".");var parent=Rickshaw;for(var i=1,length=parts.length;i0){var x=s.data[0].x;var y=s.data[0].y;if(typeof x!="number"||typeof y!="number"&&y!==null){throw"x and y properties of points should be numbers instead of "+typeof x+" and "+typeof y}}if(s.data.length>=3){if(s.data[2].xthis.window.xMax)isInRange=false;return isInRange}return true};this.onUpdate=function(callback){this.updateCallbacks.push(callback)};this.onConfigure=function(callback){this.configureCallbacks.push(callback)};this.registerRenderer=function(renderer){this._renderers=this._renderers||{};this._renderers[renderer.name]=renderer};this.configure=function(args){this.config=this.config||{};if(args.width||args.height){this.setSize(args)}Rickshaw.keys(this.defaults).forEach(function(k){this.config[k]=k in args?args[k]:k in this?this[k]:this.defaults[k]},this);Rickshaw.keys(this.config).forEach(function(k){this[k]=this.config[k]},this);if("stack"in args)args.unstack=!args.stack;var renderer=args.renderer||this.renderer&&this.renderer.name||"stack";this.setRenderer(renderer,args);this.configureCallbacks.forEach(function(callback){callback(args)})};this.setRenderer=function(r,args){if(typeof r=="function"){this.renderer=new r({graph:self});this.registerRenderer(this.renderer)}else{if(!this._renderers[r]){throw"couldn't find renderer "+r}this.renderer=this._renderers[r]}if(typeof args=="object"){this.renderer.configure(args)}};this.setSize=function(args){args=args||{};if(typeof window!==undefined){var style=window.getComputedStyle(this.element,null);var elementWidth=parseInt(style.getPropertyValue("width"),10);var elementHeight=parseInt(style.getPropertyValue("height"),10)}this.width=args.width||elementWidth||400;this.height=args.height||elementHeight||250;this.vis&&this.vis.attr("width",this.width).attr("height",this.height)};this.initialize(args)};Rickshaw.namespace("Rickshaw.Fixtures.Color");Rickshaw.Fixtures.Color=function(){this.schemes={};this.schemes.spectrum14=["#ecb796","#dc8f70","#b2a470","#92875a","#716c49","#d2ed82","#bbe468","#a1d05d","#e7cbe6","#d8aad6","#a888c2","#9dc2d3","#649eb9","#387aa3"].reverse();this.schemes.spectrum2000=["#57306f","#514c76","#646583","#738394","#6b9c7d","#84b665","#a7ca50","#bfe746","#e2f528","#fff726","#ecdd00","#d4b11d","#de8800","#de4800","#c91515","#9a0000","#7b0429","#580839","#31082b"];this.schemes.spectrum2001=["#2f243f","#3c2c55","#4a3768","#565270","#6b6b7c","#72957f","#86ad6e","#a1bc5e","#b8d954","#d3e04e","#ccad2a","#cc8412","#c1521d","#ad3821","#8a1010","#681717","#531e1e","#3d1818","#320a1b"];this.schemes.classic9=["#423d4f","#4a6860","#848f39","#a2b73c","#ddcb53","#c5a32f","#7d5836","#963b20","#7c2626","#491d37","#2f254a"].reverse();this.schemes.httpStatus={503:"#ea5029",502:"#d23f14",500:"#bf3613",410:"#efacea",409:"#e291dc",403:"#f457e8",408:"#e121d2",401:"#b92dae",405:"#f47ceb",404:"#a82a9f",400:"#b263c6",301:"#6fa024",302:"#87c32b",307:"#a0d84c",304:"#28b55c",200:"#1a4f74",206:"#27839f",201:"#52adc9",202:"#7c979f",203:"#a5b8bd",204:"#c1cdd1"};this.schemes.colorwheel=["#b5b6a9","#858772","#785f43","#96557e","#4682b4","#65b9ac","#73c03a","#cb513a"].reverse();this.schemes.cool=["#5e9d2f","#73c03a","#4682b4","#7bc3b8","#a9884e","#c1b266","#a47493","#c09fb5"];this.schemes.munin=["#00cc00","#0066b3","#ff8000","#ffcc00","#330099","#990099","#ccff00","#ff0000","#808080","#008f00","#00487d","#b35a00","#b38f00","#6b006b","#8fb300","#b30000","#bebebe","#80ff80","#80c9ff","#ffc080","#ffe680","#aa80ff","#ee00cc","#ff8080","#666600","#ffbfff","#00ffcc","#cc6699","#999900"]};Rickshaw.namespace("Rickshaw.Fixtures.RandomData");Rickshaw.Fixtures.RandomData=function(timeInterval){var addData;timeInterval=timeInterval||1;var lastRandomValue=200;var timeBase=Math.floor((new Date).getTime()/1e3);this.addData=function(data){var randomValue=Math.random()*100+15+lastRandomValue;var index=data[0].length;var counter=1;data.forEach(function(series){var randomVariance=Math.random()*20;var v=randomValue/25+counter++ +(Math.cos(index*counter*11/960)+2)*15+(Math.cos(index/7)+2)*7+(Math.cos(index/17)+2)*1;series.push({x:index*timeInterval+timeBase,y:v+randomVariance})});lastRandomValue=randomValue*.85};this.removeData=function(data){data.forEach(function(series){series.shift()});timeBase+=timeInterval}};Rickshaw.namespace("Rickshaw.Fixtures.Time");Rickshaw.Fixtures.Time=function(){var self=this;this.months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];this.units=[{name:"decade",seconds:86400*365.25*10,formatter:function(d){return parseInt(d.getUTCFullYear()/10,10)*10}},{name:"year",seconds:86400*365.25,formatter:function(d){return d.getUTCFullYear()}},{name:"month",seconds:86400*30.5,formatter:function(d){return self.months[d.getUTCMonth()]}},{name:"week",seconds:86400*7,formatter:function(d){return self.formatDate(d)}},{name:"day",seconds:86400,formatter:function(d){return d.getUTCDate()}},{name:"6 hour",seconds:3600*6,formatter:function(d){return self.formatTime(d)}},{name:"hour",seconds:3600,formatter:function(d){return self.formatTime(d)}},{name:"15 minute",seconds:60*15,formatter:function(d){return self.formatTime(d)}},{name:"minute",seconds:60,formatter:function(d){return d.getUTCMinutes()}},{name:"15 second",seconds:15,formatter:function(d){return d.getUTCSeconds()+"s"}},{name:"second",seconds:1,formatter:function(d){return d.getUTCSeconds()+"s"}},{name:"decisecond",seconds:1/10,formatter:function(d){return d.getUTCMilliseconds()+"ms"}},{name:"centisecond",seconds:1/100,formatter:function(d){return d.getUTCMilliseconds()+"ms"}}];this.unit=function(unitName){return this.units.filter(function(unit){return unitName==unit.name}).shift()};this.formatDate=function(d){return d3.time.format("%b %e")(d)};this.formatTime=function(d){return d.toUTCString().match(/(\d+:\d+):/)[1]};this.ceil=function(time,unit){var date,floor,year;if(unit.name=="month"){date=new Date(time*1e3);floor=Date.UTC(date.getUTCFullYear(),date.getUTCMonth())/1e3;if(floor==time)return time;year=date.getUTCFullYear();var month=date.getUTCMonth();if(month==11){month=0;year=year+1}else{month+=1}return Date.UTC(year,month)/1e3}if(unit.name=="year"){date=new Date(time*1e3);floor=Date.UTC(date.getUTCFullYear(),0)/1e3;if(floor==time)return time;year=date.getUTCFullYear()+1;return Date.UTC(year,0)/1e3}return Math.ceil(time/unit.seconds)*unit.seconds}};Rickshaw.namespace("Rickshaw.Fixtures.Time.Local");Rickshaw.Fixtures.Time.Local=function(){var self=this;this.months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];this.units=[{name:"decade",seconds:86400*365.25*10,formatter:function(d){return parseInt(d.getFullYear()/10,10)*10}},{name:"year",seconds:86400*365.25,formatter:function(d){return d.getFullYear()}},{name:"month",seconds:86400*30.5,formatter:function(d){return self.months[d.getMonth()]}},{name:"week",seconds:86400*7,formatter:function(d){return self.formatDate(d)}},{name:"day",seconds:86400,formatter:function(d){return d.getDate()}},{name:"6 hour",seconds:3600*6,formatter:function(d){return self.formatTime(d)}},{name:"hour",seconds:3600,formatter:function(d){return self.formatTime(d)}},{name:"15 minute",seconds:60*15,formatter:function(d){return self.formatTime(d)}},{name:"minute",seconds:60,formatter:function(d){return d.getMinutes()}},{name:"15 second",seconds:15,formatter:function(d){return d.getSeconds()+"s"}},{name:"second",seconds:1,formatter:function(d){return d.getSeconds()+"s"}},{name:"decisecond",seconds:1/10,formatter:function(d){return d.getMilliseconds()+"ms"}},{name:"centisecond",seconds:1/100,formatter:function(d){return d.getMilliseconds()+"ms"}}];this.unit=function(unitName){return this.units.filter(function(unit){return unitName==unit.name}).shift()};this.formatDate=function(d){return d3.time.format("%b %e")(d)};this.formatTime=function(d){return d.toString().match(/(\d+:\d+):/)[1]};this.ceil=function(time,unit){var date,floor,year;if(unit.name=="day"){var nearFuture=new Date((time+unit.seconds-1)*1e3);var rounded=new Date(0);rounded.setFullYear(nearFuture.getFullYear());rounded.setMonth(nearFuture.getMonth());rounded.setDate(nearFuture.getDate());rounded.setMilliseconds(0);rounded.setSeconds(0);rounded.setMinutes(0);rounded.setHours(0);return rounded.getTime()/1e3}if(unit.name=="month"){date=new Date(time*1e3);floor=new Date(date.getFullYear(),date.getMonth()).getTime()/1e3;if(floor==time)return time;year=date.getFullYear();var month=date.getMonth();if(month==11){month=0;year=year+1}else{month+=1}return new Date(year,month).getTime()/1e3}if(unit.name=="year"){date=new Date(time*1e3);floor=new Date(date.getUTCFullYear(),0).getTime()/1e3;if(floor==time)return time;year=date.getFullYear()+1;return new Date(year,0).getTime()/1e3}return Math.ceil(time/unit.seconds)*unit.seconds}};Rickshaw.namespace("Rickshaw.Fixtures.Number");Rickshaw.Fixtures.Number.formatKMBT=function(y){var abs_y=Math.abs(y);if(abs_y>=1e12){return y/1e12+"T"}else if(abs_y>=1e9){return y/1e9+"B"}else if(abs_y>=1e6){return y/1e6+"M"}else if(abs_y>=1e3){return y/1e3+"K"}else if(abs_y<1&&y>0){return y.toFixed(2)}else if(abs_y===0){return""}else{return y}};Rickshaw.Fixtures.Number.formatBase1024KMGTP=function(y){var abs_y=Math.abs(y);if(abs_y>=0x4000000000000){return y/0x4000000000000+"P"}else if(abs_y>=1099511627776){return y/1099511627776+"T"}else if(abs_y>=1073741824){return y/1073741824+"G"}else if(abs_y>=1048576){return y/1048576+"M"}else if(abs_y>=1024){return y/1024+"K"}else if(abs_y<1&&y>0){return y.toFixed(2)}else if(abs_y===0){return""}else{return y}};Rickshaw.namespace("Rickshaw.Color.Palette");Rickshaw.Color.Palette=function(args){var color=new Rickshaw.Fixtures.Color;args=args||{};this.schemes={};this.scheme=color.schemes[args.scheme]||args.scheme||color.schemes.colorwheel;this.runningIndex=0;this.generatorIndex=0;if(args.interpolatedStopCount){var schemeCount=this.scheme.length-1;var i,j,scheme=[];for(i=0;iself.graph.x.range()[1]){if(annotation.element){annotation.line.classList.add("offscreen");annotation.element.style.display="none"}annotation.boxes.forEach(function(box){if(box.rangeElement)box.rangeElement.classList.add("offscreen")});return}if(!annotation.element){var element=annotation.element=document.createElement("div");element.classList.add("annotation");this.elements.timeline.appendChild(element);element.addEventListener("click",function(e){element.classList.toggle("active");annotation.line.classList.toggle("active");annotation.boxes.forEach(function(box){if(box.rangeElement)box.rangeElement.classList.toggle("active")})},false)}annotation.element.style.left=left+"px";annotation.element.style.display="block";annotation.boxes.forEach(function(box){var element=box.element;if(!element){element=box.element=document.createElement("div");element.classList.add("content");element.innerHTML=box.content;annotation.element.appendChild(element);annotation.line=document.createElement("div");annotation.line.classList.add("annotation_line");self.graph.element.appendChild(annotation.line);if(box.end){box.rangeElement=document.createElement("div");box.rangeElement.classList.add("annotation_range");self.graph.element.appendChild(box.rangeElement)}}if(box.end){var annotationRangeStart=left;var annotationRangeEnd=Math.min(self.graph.x(box.end),self.graph.x.range()[1]);if(annotationRangeStart>annotationRangeEnd){annotationRangeEnd=left;annotationRangeStart=Math.max(self.graph.x(box.end),self.graph.x.range()[0])}var annotationRangeWidth=annotationRangeEnd-annotationRangeStart;box.rangeElement.style.left=annotationRangeStart+"px";box.rangeElement.style.width=annotationRangeWidth+"px";box.rangeElement.classList.remove("offscreen")}annotation.line.classList.remove("offscreen");annotation.line.style.left=left+"px"})},this)};this.graph.onUpdate(function(){self.update()})};Rickshaw.namespace("Rickshaw.Graph.Axis.Time");Rickshaw.Graph.Axis.Time=function(args){var self=this;this.graph=args.graph;this.elements=[];this.ticksTreatment=args.ticksTreatment||"plain";this.fixedTimeUnit=args.timeUnit;var time=args.timeFixture||new Rickshaw.Fixtures.Time;this.appropriateTimeUnit=function(){var unit;var units=time.units;var domain=this.graph.x.domain();var rangeSeconds=domain[1]-domain[0];units.forEach(function(u){if(Math.floor(rangeSeconds/u.seconds)>=2){unit=unit||u}});return unit||time.units[time.units.length-1]};this.tickOffsets=function(){var domain=this.graph.x.domain();var unit=this.fixedTimeUnit||this.appropriateTimeUnit();var count=Math.ceil((domain[1]-domain[0])/unit.seconds);var runningTick=domain[0];var offsets=[];for(var i=0;iself.graph.x.range()[1])return;var element=document.createElement("div");element.style.left=self.graph.x(o.value)+"px";element.classList.add("x_tick");element.classList.add(self.ticksTreatment);var title=document.createElement("div");title.classList.add("title");title.innerHTML=o.unit.formatter(new Date(o.value*1e3));element.appendChild(title);self.graph.element.appendChild(element);self.elements.push(element)})};this.graph.onUpdate(function(){self.render()})};Rickshaw.namespace("Rickshaw.Graph.Axis.X");Rickshaw.Graph.Axis.X=function(args){var self=this;var berthRate=.1;this.initialize=function(args){this.graph=args.graph;this.orientation=args.orientation||"top";this.pixelsPerTick=args.pixelsPerTick||75;if(args.ticks)this.staticTicks=args.ticks;if(args.tickValues)this.tickValues=args.tickValues;this.tickSize=args.tickSize||4;this.ticksTreatment=args.ticksTreatment||"plain";if(args.element){this.element=args.element;this._discoverSize(args.element,args);this.vis=d3.select(args.element).append("svg:svg").attr("height",this.height).attr("width",this.width).attr("class","rickshaw_graph x_axis_d3");this.element=this.vis[0][0];this.element.style.position="relative";this.setSize({width:args.width,height:args.height})}else{this.vis=this.graph.vis}this.graph.onUpdate(function(){self.render()})};this.setSize=function(args){args=args||{};if(!this.element)return;this._discoverSize(this.element.parentNode,args);this.vis.attr("height",this.height).attr("width",this.width*(1+berthRate));var berth=Math.floor(this.width*berthRate/2);this.element.style.left=-1*berth+"px"};this.render=function(){if(this._renderWidth!==undefined&&this.graph.width!==this._renderWidth)this.setSize({auto:true});var axis=d3.svg.axis().scale(this.graph.x).orient(this.orientation);axis.tickFormat(args.tickFormat||function(x){return x});if(this.tickValues)axis.tickValues(this.tickValues);this.ticks=this.staticTicks||Math.floor(this.graph.width/this.pixelsPerTick);var berth=Math.floor(this.width*berthRate/2)||0;var transform;if(this.orientation=="top"){var yOffset=this.height||this.graph.height;transform="translate("+berth+","+yOffset+")"}else{transform="translate("+berth+", 0)"}if(this.element){this.vis.selectAll("*").remove()}this.vis.append("svg:g").attr("class",["x_ticks_d3",this.ticksTreatment].join(" ")).attr("transform",transform).call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize));var gridSize=(this.orientation=="bottom"?1:-1)*this.graph.height;this.graph.vis.append("svg:g").attr("class","x_grid_d3").call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(gridSize)).selectAll("text").each(function(){this.parentNode.setAttribute("data-x-value",this.textContent)});this._renderHeight=this.graph.height};this._discoverSize=function(element,args){if(typeof window!=="undefined"){var style=window.getComputedStyle(element,null);var elementHeight=parseInt(style.getPropertyValue("height"),10);if(!args.auto){var elementWidth=parseInt(style.getPropertyValue("width"),10)}}this.width=(args.width||elementWidth||this.graph.width)*(1+berthRate);this.height=args.height||elementHeight||40};this.initialize(args)};Rickshaw.namespace("Rickshaw.Graph.Axis.Y");Rickshaw.Graph.Axis.Y=Rickshaw.Class.create({initialize:function(args){this.graph=args.graph;this.orientation=args.orientation||"right";this.pixelsPerTick=args.pixelsPerTick||75;if(args.ticks)this.staticTicks=args.ticks;if(args.tickValues)this.tickValues=args.tickValues;this.tickSize=args.tickSize||4;this.ticksTreatment=args.ticksTreatment||"plain";this.tickFormat=args.tickFormat||function(y){return y};this.berthRate=.1;if(args.element){this.element=args.element;this.vis=d3.select(args.element).append("svg:svg").attr("class","rickshaw_graph y_axis");this.element=this.vis[0][0];this.element.style.position="relative";this.setSize({width:args.width,height:args.height})}else{this.vis=this.graph.vis}var self=this;this.graph.onUpdate(function(){self.render()})},setSize:function(args){args=args||{};if(!this.element)return;if(typeof window!=="undefined"){var style=window.getComputedStyle(this.element.parentNode,null);var elementWidth=parseInt(style.getPropertyValue("width"),10);if(!args.auto){var elementHeight=parseInt(style.getPropertyValue("height"),10)}}this.width=args.width||elementWidth||this.graph.width*this.berthRate;this.height=args.height||elementHeight||this.graph.height;this.vis.attr("width",this.width).attr("height",this.height*(1+this.berthRate));var berth=this.height*this.berthRate;if(this.orientation=="left"){this.element.style.top=-1*berth+"px"}},render:function(){if(this._renderHeight!==undefined&&this.graph.height!==this._renderHeight)this.setSize({auto:true});this.ticks=this.staticTicks||Math.floor(this.graph.height/this.pixelsPerTick);var axis=this._drawAxis(this.graph.y);this._drawGrid(axis);this._renderHeight=this.graph.height},_drawAxis:function(scale){var axis=d3.svg.axis().scale(scale).orient(this.orientation);axis.tickFormat(this.tickFormat);if(this.tickValues)axis.tickValues(this.tickValues);if(this.orientation=="left"){var berth=this.height*this.berthRate;var transform="translate("+this.width+", "+berth+")"}if(this.element){this.vis.selectAll("*").remove()}this.vis.append("svg:g").attr("class",["y_ticks",this.ticksTreatment].join(" ")).attr("transform",transform).call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize));return axis},_drawGrid:function(axis){var gridSize=(this.orientation=="right"?1:-1)*this.graph.width;this.graph.vis.append("svg:g").attr("class","y_grid").call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(gridSize)).selectAll("text").each(function(){this.parentNode.setAttribute("data-y-value",this.textContent); +})}});Rickshaw.namespace("Rickshaw.Graph.Axis.Y.Scaled");Rickshaw.Graph.Axis.Y.Scaled=Rickshaw.Class.create(Rickshaw.Graph.Axis.Y,{initialize:function($super,args){if(typeof args.scale==="undefined"){throw new Error("Scaled requires scale")}this.scale=args.scale;if(typeof args.grid==="undefined"){this.grid=true}else{this.grid=args.grid}$super(args)},_drawAxis:function($super,scale){var domain=this.scale.domain();var renderDomain=this.graph.renderer.domain().y;var extents=[Math.min.apply(Math,domain),Math.max.apply(Math,domain)];var extentMap=d3.scale.linear().domain([0,1]).range(extents);var adjExtents=[extentMap(renderDomain[0]),extentMap(renderDomain[1])];var adjustment=d3.scale.linear().domain(extents).range(adjExtents);var adjustedScale=this.scale.copy().domain(domain.map(adjustment)).range(scale.range());return $super(adjustedScale)},_drawGrid:function($super,axis){if(this.grid){$super(axis)}}});Rickshaw.namespace("Rickshaw.Graph.Behavior.Series.Highlight");Rickshaw.Graph.Behavior.Series.Highlight=function(args){this.graph=args.graph;this.legend=args.legend;var self=this;var colorSafe={};var activeLine=null;var disabledColor=args.disabledColor||function(seriesColor){return d3.interpolateRgb(seriesColor,d3.rgb("#d8d8d8"))(.8).toString()};this.addHighlightEvents=function(l){l.element.addEventListener("mouseover",function(e){if(activeLine)return;else activeLine=l;self.legend.lines.forEach(function(line){if(l===line){if(self.graph.renderer.unstack&&(line.series.renderer?line.series.renderer.unstack:true)){var seriesIndex=self.graph.series.indexOf(line.series);line.originalIndex=seriesIndex;var series=self.graph.series.splice(seriesIndex,1)[0];self.graph.series.push(series)}return}colorSafe[line.series.name]=colorSafe[line.series.name]||line.series.color;line.series.color=disabledColor(line.series.color)});self.graph.update()},false);l.element.addEventListener("mouseout",function(e){if(!activeLine)return;else activeLine=null;self.legend.lines.forEach(function(line){if(l===line&&line.hasOwnProperty("originalIndex")){var series=self.graph.series.pop();self.graph.series.splice(line.originalIndex,0,series);delete line.originalIndex}if(colorSafe[line.series.name]){line.series.color=colorSafe[line.series.name]}});self.graph.update()},false)};if(this.legend){this.legend.lines.forEach(function(l){self.addHighlightEvents(l)})}};Rickshaw.namespace("Rickshaw.Graph.Behavior.Series.Order");Rickshaw.Graph.Behavior.Series.Order=function(args){this.graph=args.graph;this.legend=args.legend;var self=this;if(typeof window.jQuery=="undefined"){throw"couldn't find jQuery at window.jQuery"}if(typeof window.jQuery.ui=="undefined"){throw"couldn't find jQuery UI at window.jQuery.ui"}jQuery(function(){jQuery(self.legend.list).sortable({containment:"parent",tolerance:"pointer",update:function(event,ui){var series=[];jQuery(self.legend.list).find("li").each(function(index,item){if(!item.series)return;series.push(item.series)});for(var i=self.graph.series.length-1;i>=0;i--){self.graph.series[i]=series.shift()}self.graph.update()}});jQuery(self.legend.list).disableSelection()});this.graph.onUpdate(function(){var h=window.getComputedStyle(self.legend.element).height;self.legend.element.style.height=h})};Rickshaw.namespace("Rickshaw.Graph.Behavior.Series.Toggle");Rickshaw.Graph.Behavior.Series.Toggle=function(args){this.graph=args.graph;this.legend=args.legend;var self=this;this.addAnchor=function(line){var anchor=document.createElement("a");anchor.innerHTML="✔";anchor.classList.add("action");line.element.insertBefore(anchor,line.element.firstChild);anchor.onclick=function(e){if(line.series.disabled){line.series.enable();line.element.classList.remove("disabled")}else{if(this.graph.series.filter(function(s){return!s.disabled}).length<=1)return;line.series.disable();line.element.classList.add("disabled")}self.graph.update()}.bind(this);var label=line.element.getElementsByTagName("span")[0];label.onclick=function(e){var disableAllOtherLines=line.series.disabled;if(!disableAllOtherLines){for(var i=0;idomainX){dataIndex=Math.abs(domainX-data[i].x)0){alignables.forEach(function(el){el.classList.remove("left");el.classList.add("right")});var rightAlignError=this._calcLayoutError(alignables);if(rightAlignError>leftAlignError){alignables.forEach(function(el){el.classList.remove("right");el.classList.add("left")})}}if(typeof this.onRender=="function"){this.onRender(args)}},_calcLayoutError:function(alignables){var parentRect=this.element.parentNode.getBoundingClientRect();var error=0;var alignRight=alignables.forEach(function(el){var rect=el.getBoundingClientRect();if(!rect.width){return}if(rect.right>parentRect.right){error+=rect.right-parentRect.right}if(rect.left=self.previewWidth){frameAfterDrag[0]-=frameAfterDrag[1]-self.previewWidth;frameAfterDrag[1]=self.previewWidth}}self.graphs.forEach(function(graph){var domainScale=d3.scale.linear().interpolate(d3.interpolateNumber).domain([0,self.previewWidth]).range(graph.dataDomain());var windowAfterDrag=[domainScale(frameAfterDrag[0]),domainScale(frameAfterDrag[1])];self.slideCallbacks.forEach(function(callback){callback(graph,windowAfterDrag[0],windowAfterDrag[1])});if(frameAfterDrag[0]===0){windowAfterDrag[0]=undefined}if(frameAfterDrag[1]===self.previewWidth){windowAfterDrag[1]=undefined}graph.window.xMin=windowAfterDrag[0];graph.window.xMax=windowAfterDrag[1];graph.update()})}function onMousedown(){drag.target=d3.event.target;drag.start=self._getClientXFromEvent(d3.event,drag);self.frameBeforeDrag=self.currentFrame.slice();d3.event.preventDefault?d3.event.preventDefault():d3.event.returnValue=false;d3.select(document).on("mousemove.rickshaw_range_slider_preview",onMousemove);d3.select(document).on("mouseup.rickshaw_range_slider_preview",onMouseup);d3.select(document).on("touchmove.rickshaw_range_slider_preview",onMousemove);d3.select(document).on("touchend.rickshaw_range_slider_preview",onMouseup);d3.select(document).on("touchcancel.rickshaw_range_slider_preview",onMouseup)}function onMousedownLeftHandle(datum,index){drag.left=true;onMousedown()}function onMousedownRightHandle(datum,index){drag.right=true;onMousedown()}function onMousedownMiddleHandle(datum,index){drag.left=true;drag.right=true;drag.rigid=true;onMousedown()}function onMouseup(datum,index){d3.select(document).on("mousemove.rickshaw_range_slider_preview",null);d3.select(document).on("mouseup.rickshaw_range_slider_preview",null);d3.select(document).on("touchmove.rickshaw_range_slider_preview",null);d3.select(document).on("touchend.rickshaw_range_slider_preview",null);d3.select(document).on("touchcancel.rickshaw_range_slider_preview",null);delete self.frameBeforeDrag;drag.left=false;drag.right=false;drag.rigid=false}element.select("rect.left_handle").on("mousedown",onMousedownLeftHandle);element.select("rect.right_handle").on("mousedown",onMousedownRightHandle);element.select("rect.middle_handle").on("mousedown",onMousedownMiddleHandle);element.select("rect.left_handle").on("touchstart",onMousedownLeftHandle);element.select("rect.right_handle").on("touchstart",onMousedownRightHandle);element.select("rect.middle_handle").on("touchstart",onMousedownMiddleHandle)},_getClientXFromEvent:function(event,drag){switch(event.type){case"touchstart":case"touchmove":var touchList=event.changedTouches;var touch=null;for(var touchIndex=0;touchIndexyMax)yMax=y});if(!series.length)return;if(series[0].xxMax)xMax=series[series.length-1].x});xMin-=(xMax-xMin)*this.padding.left;xMax+=(xMax-xMin)*this.padding.right;yMin=this.graph.min==="auto"?yMin:this.graph.min||0;yMax=this.graph.max===undefined?yMax:this.graph.max;if(this.graph.min==="auto"||yMin<0){yMin-=(yMax-yMin)*this.padding.bottom}if(this.graph.max===undefined){yMax+=(yMax-yMin)*this.padding.top}return{x:[xMin,xMax],y:[yMin,yMax]}},render:function(args){args=args||{};var graph=this.graph;var series=args.series||graph.series;var vis=args.vis||graph.vis;vis.selectAll("*").remove();var data=series.filter(function(s){return!s.disabled}).map(function(s){return s.stack});var pathNodes=vis.selectAll("path.path").data(data).enter().append("svg:path").classed("path",true).attr("d",this.seriesPathFactory());if(this.stroke){var strokeNodes=vis.selectAll("path.stroke").data(data).enter().append("svg:path").classed("stroke",true).attr("d",this.seriesStrokeFactory())}var i=0;series.forEach(function(series){if(series.disabled)return;series.path=pathNodes[0][i];if(this.stroke)series.stroke=strokeNodes[0][i];this._styleSeries(series);i++},this)},_styleSeries:function(series){var fill=this.fill?series.color:"none";var stroke=this.stroke?series.color:"none";var strokeWidth=series.strokeWidth?series.strokeWidth:this.strokeWidth;var opacity=series.opacity?series.opacity:this.opacity;series.path.setAttribute("fill",fill);series.path.setAttribute("stroke",stroke);series.path.setAttribute("stroke-width",strokeWidth);series.path.setAttribute("opacity",opacity);if(series.className){d3.select(series.path).classed(series.className,true)}if(series.className&&this.stroke){d3.select(series.stroke).classed(series.className,true)}},configure:function(args){args=args||{};Rickshaw.keys(this.defaults()).forEach(function(key){if(!args.hasOwnProperty(key)){this[key]=this[key]||this.graph[key]||this.defaults()[key];return}if(typeof this.defaults()[key]=="object"){Rickshaw.keys(this.defaults()[key]).forEach(function(k){this[key][k]=args[key][k]!==undefined?args[key][k]:this[key][k]!==undefined?this[key][k]:this.defaults()[key][k]},this)}else{this[key]=args[key]!==undefined?args[key]:this[key]!==undefined?this[key]:this.graph[key]!==undefined?this.graph[key]:this.defaults()[key]}},this)},setStrokeWidth:function(strokeWidth){if(strokeWidth!==undefined){this.strokeWidth=strokeWidth}},setTension:function(tension){if(tension!==undefined){this.tension=tension}}});Rickshaw.namespace("Rickshaw.Graph.Renderer.Line");Rickshaw.Graph.Renderer.Line=Rickshaw.Class.create(Rickshaw.Graph.Renderer,{name:"line",defaults:function($super){return Rickshaw.extend($super(),{unstack:true,fill:false,stroke:true})},seriesPathFactory:function(){var graph=this.graph;var factory=d3.svg.line().x(function(d){return graph.x(d.x)}).y(function(d){return graph.y(d.y)}).interpolate(this.graph.interpolation).tension(this.tension);factory.defined&&factory.defined(function(d){return d.y!==null});return factory}});Rickshaw.namespace("Rickshaw.Graph.Renderer.Stack");Rickshaw.Graph.Renderer.Stack=Rickshaw.Class.create(Rickshaw.Graph.Renderer,{name:"stack",defaults:function($super){return Rickshaw.extend($super(),{fill:true,stroke:false,unstack:false})},seriesPathFactory:function(){var graph=this.graph;var factory=d3.svg.area().x(function(d){return graph.x(d.x)}).y0(function(d){return graph.y(d.y0)}).y1(function(d){return graph.y(d.y+d.y0)}).interpolate(this.graph.interpolation).tension(this.tension);factory.defined&&factory.defined(function(d){return d.y!==null});return factory}});Rickshaw.namespace("Rickshaw.Graph.Renderer.Bar");Rickshaw.Graph.Renderer.Bar=Rickshaw.Class.create(Rickshaw.Graph.Renderer,{name:"bar",defaults:function($super){var defaults=Rickshaw.extend($super(),{gapSize:.05,unstack:false,opacity:1});delete defaults.tension;return defaults},initialize:function($super,args){args=args||{};this.gapSize=args.gapSize||this.gapSize;$super(args)},domain:function($super){var domain=$super();var frequentInterval=this._frequentInterval(this.graph.stackedData.slice(-1).shift());domain.x[1]+=Number(frequentInterval.magnitude);return domain},barWidth:function(series){var frequentInterval=this._frequentInterval(series.stack);var barWidth=this.graph.x.magnitude(frequentInterval.magnitude)*(1-this.gapSize);return barWidth},render:function(args){args=args||{};var graph=this.graph;var series=args.series||graph.series;var vis=args.vis||graph.vis;vis.selectAll("*").remove();var barWidth=this.barWidth(series.active()[0]);var barXOffset=0;var activeSeriesCount=series.filter(function(s){return!s.disabled}).length;var seriesBarWidth=this.unstack?barWidth/activeSeriesCount:barWidth;var transform=function(d){var matrix=[1,0,0,d.y<0?-1:1,0,d.y<0?graph.y.magnitude(Math.abs(d.y))*2:0];return"matrix("+matrix.join(",")+")"};series.forEach(function(series){if(series.disabled)return;var barWidth=this.barWidth(series);var nodes=vis.selectAll("path").data(series.stack.filter(function(d){return d.y!==null})).enter().append("svg:rect").attr("x",function(d){return graph.x(d.x)+barXOffset}).attr("y",function(d){return graph.y(d.y0+Math.abs(d.y))*(d.y<0?-1:1)}).attr("width",seriesBarWidth).attr("height",function(d){return graph.y.magnitude(Math.abs(d.y))}).attr("opacity",series.opacity).attr("transform",transform);Array.prototype.forEach.call(nodes[0],function(n){n.setAttribute("fill",series.color)});if(this.unstack)barXOffset+=seriesBarWidth},this)},_frequentInterval:function(data){var intervalCounts={};for(var i=0;i0){this[0].data.forEach(function(plot){item.data.push({x:plot.x,y:0})})}else if(item.data.length===0){item.data.push({x:this.timeBase-(this.timeInterval||0),y:0})}this.push(item);if(this.legend){this.legend.addLine(this.itemByName(item.name))}},addData:function(data,x){var index=this.getIndex();Rickshaw.keys(data).forEach(function(name){if(!this.itemByName(name)){this.addItem({name:name})}},this);this.forEach(function(item){item.data.push({x:x||(index*this.timeInterval||1)+this.timeBase,y:data[item.name]||0})},this)},getIndex:function(){return this[0]&&this[0].data&&this[0].data.length?this[0].data.length:0},itemByName:function(name){for(var i=0;i1;i--){this.currentSize+=1;this.currentIndex+=1;this.forEach(function(item){item.data.unshift({x:((i-1)*this.timeInterval||1)+this.timeBase,y:0,i:i})},this)}}},addData:function($super,data,x){$super(data,x);this.currentSize+=1;this.currentIndex+=1;if(this.maxDataPoints!==undefined){while(this.currentSize>this.maxDataPoints){this.dropData()}}},dropData:function(){this.forEach(function(item){item.data.splice(0,1)});this.currentSize-=1},getIndex:function(){return this.currentIndex}});return Rickshaw});