allow to include previous skynet stats (#562)

* allow to include previous skynet stats

* explain chunked response
This commit is contained in:
Karol Wypchło 2020-12-10 14:43:59 +01:00 committed by GitHub
parent cd7696c97d
commit 83d2aa396e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -72,6 +72,7 @@ services:
- ./docker/nginx/conf.d:/etc/nginx/conf.d:ro
- ./docker/data/nginx/cache:/data/nginx/cache
- ./docker/data/nginx/logs:/usr/local/openresty/nginx/logs
- ./docker/data/nginx/skynet:/data/nginx/skynet:ro
- ./docker/data/sia/apipassword:/data/sia/apipassword:ro
- webapp:/var/www/webportal:ro
networks:

View File

@ -105,6 +105,33 @@ server {
location /skynet/stats {
include /etc/nginx/conf.d/include/cors;
set $response_body ''; # we need a variable for full response body (not chuncked)
# modify the response to add numfiles and totalsize to account for node rotation
# example prevstats.lua: 'return { numfiles = 12345, totalsize = 123456789 }'
body_filter_by_lua_block {
local file_exists = io.open("/data/nginx/skynet/prevstats.lua")
if file_exists then
file_exists.close()
-- because response data is chunked, we need to concat ngx.arg[1] until
-- last chunk is received (when ngx.arg[2] is set to true)
ngx.var.response_body = ngx.var.response_body .. ngx.arg[1]
if ngx.arg[2] then
local json = require('cjson')
local prevstats = require('/data/nginx/skynet/prevstats')
local stats = json.decode(ngx.var.response_body)
stats.uploadstats.numfiles = stats.uploadstats.numfiles + prevstats.numfiles
stats.uploadstats.totalsize = stats.uploadstats.totalsize + prevstats.totalsize
ngx.arg[1] = json.encode(stats)
else
-- do not send any data in this chunk (wait for last chunk)
ngx.arg[1] = nil
end
end
}
proxy_cache skynet;
proxy_cache_valid any 10m; # cache stats for 10 minutes
proxy_set_header User-Agent: Sia-Agent;