From 83d2aa396ed1b335a8e1807d8fab1979b279a0c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Wypch=C5=82o?= Date: Thu, 10 Dec 2020 14:43:59 +0100 Subject: [PATCH] allow to include previous skynet stats (#562) * allow to include previous skynet stats * explain chunked response --- docker-compose.yml | 1 + docker/nginx/conf.d/client.conf | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index b3d2ae3d..534428aa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker/nginx/conf.d/client.conf b/docker/nginx/conf.d/client.conf index a82601f5..4d76586a 100644 --- a/docker/nginx/conf.d/client.conf +++ b/docker/nginx/conf.d/client.conf @@ -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;