Skip to content

Commit

Permalink
Merge pull request #582 from midoks/dev
Browse files Browse the repository at this point in the history
网站统计-PV,UV优化
  • Loading branch information
midoks authored May 25, 2024
2 parents 6024dbc + 786fac9 commit 15d07e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions plugins/webstats/conf/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ CREATE INDEX method_idx ON web_logs (`method`);
CREATE INDEX status_code_idx ON web_logs (`status_code`);
CREATE INDEX request_time_idx ON web_logs (`request_time`);
CREATE INDEX is_spider_idx ON web_logs (`is_spider`);
CREATE INDEX body_length_idx ON web_logs (`body_length`);
CREATE INDEX all_union_idx ON web_logs (`time`,`ip`,`method`,`status_code`,`request_time`,`is_spider`);


Expand Down Expand Up @@ -84,6 +85,7 @@ CREATE TABLE `request_stat`(
`status_402` INTEGER DEFAULT 0,
`status_403` INTEGER DEFAULT 0,
`status_404` INTEGER DEFAULT 0,
`status_499` INTEGER DEFAULT 0,
`http_get` INTEGER DEFAULT 0,
`http_post` INTEGER DEFAULT 0,
`http_put` INTEGER DEFAULT 0,
Expand Down
2 changes: 1 addition & 1 deletion plugins/webstats/js/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ function wsTableLogRequest(page){
var rdata = $.parseJSON(rdata.data);
var list = '';
var data = rdata.data.data;
console.log(data);
// console.log(data);

if (data.length > 0){
for(i in data){
Expand Down
25 changes: 25 additions & 0 deletions plugins/webstats/lua/webstats_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,32 @@ function _M.statistics_request(self, ip, is_spider, body_length)
-- 计算pv uv
local pvc = 0
local uvc = 0
local request_header = ngx.req.get_headers()
if not is_spider and ngx.status == 200 and body_length > 0 then
local ua = ''
if request_header['user-agent'] then
ua = string.lower(request_header['user-agent'])
end

pvc = 1
if ua then
local today = ngx.today()
local uv_token = ngx.md5(ip .. ua .. today)
if not cache:get(uv_token) then
uvc = 1
cache:set(uv_token,1, self:get_end_time())
end
end
end
return pvc, uvc
end

-- 仅计算GET
function _M.statistics_request_old(self, ip, is_spider, body_length)
-- 计算pv uv
local pvc = 0
local uvc = 0
local method = ngx.req.get_method()
if not is_spider and method == 'GET' and ngx.status == 200 and body_length > 512 then
local ua = ''
if request_header['user-agent'] then
Expand Down
4 changes: 2 additions & 2 deletions plugins/webstats/lua/webstats_log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ log_by_lua_block {
end
end

if ngx.re.find("500,501,502,503,504,505,506,507,509,510,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,421,422,423,424,425,426,449,451", tostring(status_code), "jo") then
if ngx.re.find("500,501,502,503,504,505,506,507,509,510,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,421,422,423,424,425,426,449,451,499", tostring(status_code), "jo") then
local field = "status_"..status_code
request_stat_fields = request_stat_fields .. ","..field.."="..field.."+1"
end
Expand Down Expand Up @@ -392,7 +392,7 @@ log_by_lua_block {
end
end

if ngx.re.find("500,501,502,503,504,505,506,507,509,510,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,421,422,423,424,425,426,449,451", tostring(status_code), "jo") then
if ngx.re.find("500,501,502,503,504,505,506,507,509,510,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,421,422,423,424,425,426,449,451,499", tostring(status_code), "jo") then
local field = "status_"..status_code
request_stat_fields[field] = 1
end
Expand Down

0 comments on commit 15d07e4

Please sign in to comment.