Query the new_refs count data from es, and format it as echart needs,
with 'x_params' and 'y_params'. Return it to webapi.
Signed-off-by: Li Yuanchao <lyc163mail(a)163.com>
---
container/web-backend/web-backend | 13 +++++++++++
src/lib/web_backend.rb | 39 +++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/container/web-backend/web-backend b/container/web-backend/web-backend
index ff8fcff..6f85f35 100755
--- a/container/web-backend/web-backend
+++ b/container/web-backend/web-backend
@@ -133,3 +133,16 @@ end
get '/get_tbox_state' do
get_tbox_state(params)
end
+
+# GET /get_repo_statistics?git_repo=$git_repo
+# Response:
+# - {
+# "title": "new refs statistics",
+# "unit": "times",
+# "x_name": "date",
+# "x_params": [$date1, ..., $date_today],
+# "y_params": [$times_1, ..., $times_today]
+# }
+get '/get_repo_statistics' do
+ new_refs_statistics(params)
+end
diff --git a/src/lib/web_backend.rb b/src/lib/web_backend.rb
index 61e3334..1c29c88 100644
--- a/src/lib/web_backend.rb
+++ b/src/lib/web_backend.rb
@@ -510,3 +510,42 @@ def get_tbox_state(params)
end
[200, headers.merge('Access-Control-Allow-Origin' => '*'), body]
end
+
+def get_echart(statistics)
+ echart = {
+ 'title' => 'new refs statistics',
+ 'unit' => 'times',
+ 'x_name' => 'date',
+ 'x_params' => [],
+ 'y_params' => []
+ }
+ dev = 0
+ # The day will be 2021-01-01
+ day = Date.new(2021, 1, 1)
+ today = Date.today
+ while day <= today
+ day_s = day.to_s
+ echart['x_params'][dev] = day_s
+ echart['y_params'][dev] = statistics[day_s] || 0
+ day += 1
+ dev += 1
+ end
+ echart
+end
+
+def query_repo_statistics(params)
+ query = { "query": { "match": { "_id": params[:git_repo] } } }
+ result = ES_CLIENT.search(index: 'repo', body: query)['hits']
+ statistics = result['total'].positive? ? result['hits'][0]['_source']['new_refs_count'] : {}
+ get_echart(statistics)
+end
+
+def new_refs_statistics(params)
+ begin
+ body = query_repo_statistics(params)
+ rescue StandardError => e
+ warn e.message
+ return [500, headers.merge('Access-Control-Allow-Origin' => '*'), 'new refs statistics error']
+ end
+ [200, headers.merge('Access-Control-Allow-Origin' => '*'), body]
+end
--
2.23.0