Skip to content

Commit

Permalink
write to influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhao.dai committed May 1, 2018
1 parent 0697185 commit ddb4d68
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lib/resty/influxdb/reporter.lua
@@ -1,19 +1,29 @@
-- Copyright (C) by Jianhao Dai (Toruneko)
local point = require "resty.influxdb.point"
local http = require "resty.http"

local setmetatable = setmetatable
local escape_uri = ngx.escape_uri
local concat = table.concat
local pairs = pairs

local DEFAULT_RETENTION_POLICY = "default"

local _M = { _VERSION = '0.01' }
local mt = { __index = _M }

function _M.new(database, tags)
return setmetatable({
function _M.new(url, username, password, database, tags)
local reporter = setmetatable({
url = url,
username = username,
password = password,
database = database,
tags = tags
}, mt)

reporter:query(database, "CREATE DATABASE \"" + database + "\" WITH DURATION 2w REPLICATION 1 NAME \"default\"")

return reporter
end

function _M.report(self, measurement)
Expand Down Expand Up @@ -42,8 +52,41 @@ function _M.report(self, measurement)
end
end

function _M.write(self, database, policy, point)
function _M.write(self, db, rp, point)
local httpc = http:new()
httpc:set_timeout(5000)

local response = httpc:request_uri(self.url, {
path = "write",
method = "POST",
headers = {
["Content-Type"] = "text/plain"
},
query = {
u = self.username,
p = self.password,
db = db,
rp = rp,
precision = "n",
consistency = "one"
},
body = point:lineProtocol()
})
end

function _M.query(self, db, q)
local httpc = http.new()
httpc:set_timeout(5000)
local response = httpc:request_uri(self.url, {
path = "query",
method = "POST",
query = {
u = self.username,
p = self.password,
db = db,
q = q
}
})
end

return _M

0 comments on commit ddb4d68

Please sign in to comment.