Query the new_refs count data from es, and format it as echart needs,
with 'x_params' and 'new_ref_times'. Return it to webapi.
Signed-off-by: Li Yuanchao <lyc163mail(a)163.com>
---
container/web-backend/web-backend | 13 +++++++++++
src/lib/web_backend.rb | 38 +++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/container/web-backend/web-backend b/container/web-backend/web-backend
index ff8fcff..74eda85 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",
+# "source": [["x_params", $date1, ..., $date_today],
+# ["new_ref_times", $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..291fd9f 100644
--- a/src/lib/web_backend.rb
+++ b/src/lib/web_backend.rb
@@ -510,3 +510,41 @@ 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',
+ 'source' => [['x_params'], ['new_ref_times']]
+ }
+ dev = 1
+ # 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['source'][0][dev] = day_s
+ echart['source'][1][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