Skip to content

Commit

Permalink
Adding a restful query interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jie123108 committed Sep 24, 2020
1 parent 9114b56 commit 1836261
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 42 deletions.
73 changes: 57 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ lua-resty-stats - is a statistical module for nginx base on ngx_lua, Statistical
Table of Contents
=================

* [Name](#name)
* [Synopsis](#synopsis)
* [Variables](#variables)
* [Methods](#methods)
* [add_def_stats](#add_def_stats)
* [add_stats_config](#add_stats_config)
* [init](#init)
* [log](#log)
* [Simple Query] (#simple-query)
* [Simple Demo] (#simple-demo)
* [Authors](#authors)
* [Copyright and License](#copyright-and-license)
- [Name](#name)
- [Table of Contents](#table-of-contents)
- [Synopsis](#synopsis)
- [Variables](#variables)
- [Methods](#methods)
- [add_def_stats](#add_def_stats)
- [add_stats_config](#add_stats_config)
- [init](#init)
- [log](#log)
- [Simple Query And API](#simple-query-and-api)
- [Simple Demo](#simple-demo)
- [Authors](#authors)
- [Copyright and License](#copyright-and-license)

Synopsis
========
Expand Down Expand Up @@ -236,9 +237,9 @@ if the `stats_name` is nil, log method will collect all the statistics that have

[Back to TOC](#table-of-contents)

Simple Query
Simple Query And API
=======
lua-resty-stats comes with a simple query page, which can be used in the following steps:
lua-resty-stats with a simple query page and API interface, which can be used in the following steps:
* add location configuration to nginx.conf

```nginx
Expand All @@ -252,6 +253,46 @@ location /stats {

![docs/query-page.png](docs/query-page.png "The Simple Query")

* Access API:

```curl
# by date
curl http://127.0.0.1:8020/stats/api?table=stats_uri&date=2020-02-20&limit=100
# by date, today
curl http://127.0.0.1:8020/stats/api?table=stats_uri&date=today&limit=10
# by key(The date parameter is ignored.)
curl http://127.0.0.1:8020/stats/api?table=stats_uri&key=/path/to/uri
```

* The API response will look something like this:

```json
{
"stats": [
{
"hour_cnt": {
"19": 24
},
"count": 24,
"status": {
"200": 24
},
"total": 24,
"req_time": {
"19": 13.262,
"all": 13.262
},
"percent": 100,
"key": "/path/to/uri",
"date": "2020-09-24"
}
]
}
```

*If you've configured some other fields in your update, this will be different*

Simple Demo
========
[Simple Stats demo](docs/stats_simple_demo.conf "Simple Stats demo")
Expand All @@ -262,7 +303,7 @@ You can include it in nginx.conf using the include directive. Such as:
Authors
=======

Xiaojie Liu <[email protected]>
jie123108 <[email protected]>

[Back to TOC](#table-of-contents)

Expand All @@ -271,7 +312,7 @@ Copyright and License

This module is licensed under the BSD license.

Copyright (C) 2015, by Xiaojie Liu <[email protected]>
Copyright (C) 2020, by jie123108 <[email protected]>

All rights reserved.

Expand Down
3 changes: 0 additions & 3 deletions lib/resty/mongol/colmt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ end

function colmethods:find(query, returnfields, num_each_query)
num_each_query = num_each_query or 100
if num_each_query == 1 then
return nil, "num_each_query must larger than 1"
end
return new_cursor(self, query, returnfields, num_each_query)
end

Expand Down
43 changes: 42 additions & 1 deletion view/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ local function get_query_date(args)
return date, prev_day, next_day, today
end

local function stats_api()
local args, err = ngx.req.get_uri_args()
local table = args.table
local key = args.key
local limit = tonumber(args.limit) or 300
local date = args.date
if date == 'today' then
date = string.sub(ngx.localtime(), 1, 10)
end

local stats_list = {}
local ok = nil
local errmsg = nil
local mongo_cfg = stats.mongo_cfg
-- query stats
if not table then
errmsg = "args 'table' missing"
elseif key then
ok, stats_list = mongo.get_stats_by_key(mongo_cfg, table, key, limit)
if not ok then
ngx.log(ngx.ERR, "mongo.get_stats_by_key(", table, ",", key, ") failed! err:", tostring(stats_list))
errmsg = "error on query:" .. tostring(stats_list)
stats_list = {}
end
else
ok, stats_list = mongo.get_stats(mongo_cfg, table, date, nil, limit)
if not ok then
ngx.log(ngx.ERR, "mongo.get_stats(", table, ",", date, ") failed! err:", tostring(stats_list))
errmsg = "error on query:" .. tostring(stats_list)
stats_list = {}
end
end
local resp = {
errmsg=errmsg,
stats=stats_list,
}
ngx.header["Content-Type"] = "application/json; charset=utf-8"
ngx.say(cjson.encode(resp))
end

local function stats_def()
local args, err = ngx.req.get_uri_args()
local table = args.table
Expand Down Expand Up @@ -139,7 +179,7 @@ local function stats_key()
local mongo_cfg = stats.mongo_cfg
local ok, stats = mongo.get_stats_by_key(mongo_cfg, table, key)
if not ok then
ngx.log(ngx.ERR, "mongo.get_stats_by_key failed! err:", tostring(stats))
ngx.log(ngx.ERR, "mongo.get_stats_by_key(", table, ",", key, ") failed! err:", tostring(stats))
errmsg = "error on query:" .. tostring(stats)
else
stats_list = stats
Expand All @@ -161,6 +201,7 @@ ngx.header["Content-Type"] = 'text/html'
local uri = ngx.var.uri
local router = {
["/stats"] = stats_def,
["/stats/api"] = stats_api,
["/stats/key"] = stats_key,
}

Expand Down
30 changes: 8 additions & 22 deletions view/mongo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ date: 20151206
]]

local mongo = require "resty.mongol"
local json = require("resty.stats.json")

local function conn_get(mongo_cfg)
local conn = mongo:new()
Expand Down Expand Up @@ -60,26 +61,10 @@ local function add_percent(stats_list)
end
end

-- function mongo_query(coll, selector, page, limit)
-- page = page or 1
-- local offset = (page-1)* limit

-- local _, objs, result = coll:query(selector, nil, offset, limit, options)

-- if result and result.QueryFailure then
-- if #objs == 1 then
-- return false, objs[1]["$err"]
-- else
-- return false, "unknow-error"
-- end
-- end
-- return true, objs
-- end

function mongo_find(coll, selector, sortby, skip, limit)
local function mongo_find(coll, selector, sortby, skip, limit)
local objs = {}
skip = skip or 0
local cursor, err = coll:find(selector, nil, limit)
local cursor, err = coll:find(selector, {_id=0}, limit)
if cursor then
if skip then
cursor:skip(skip)
Expand All @@ -102,12 +87,13 @@ function mongo_find(coll, selector, sortby, skip, limit)
end
end


local function get_stats(mongo_cfg, collname, date, key_pattern, limit)
local ok, conn = conn_get(mongo_cfg)
if not ok then
return ok, conn
end
ngx.log(ngx.INFO, "-------", collname, ", date:", date)
ngx.log(ngx.INFO, "------- collection:", collname, ", date:", date)
local stats = {}
local dbname = mongo_cfg.dbname or "ngx_stats"
local db = conn:new_db_handle(dbname)
Expand All @@ -130,19 +116,19 @@ local function get_stats(mongo_cfg, collname, date, key_pattern, limit)
return true, stats
end

local function get_stats_by_key(mongo_cfg, collname, key)
local function get_stats_by_key(mongo_cfg, collname, key, limit)
local ok, conn = conn_get(mongo_cfg)
if not ok then
return ok, conn
end
ngx.log(ngx.INFO, "-------", collname, ", key:", key)
ngx.log(ngx.INFO, "------- collection:", collname, ", key:", key)
local stats = {}
local dbname = mongo_cfg.dbname or "ngx_stats"
local db = conn:new_db_handle(dbname)
local coll = db:get_col(collname)
local query = {key=key}
local skip = 0
local limit = 300
limit = limit or 300
-- local ok, tmp_stats = mongo_query(coll, query, offset, limit)
local sortby = {date=-1}
local ok, tmp_stats = mongo_find(coll, query, sortby, skip, limit)
Expand Down

0 comments on commit 1836261

Please sign in to comment.