From 82eb516be3071782ffb5c143800c112256f529be Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lubomir.sedlar@gmail.com>
Date: Mar 04 2018 17:42:25 +0000
Subject: [PATCH 1/4] Declare all variables for stats JS


Also use function declaration instead of assigning anonymous functions
to variables. Such functions don't require a semicolon, so these can be
removed.

Semicolons are added after statements that were missing them.

---

diff --git a/pagure/static/issues_stats.js b/pagure/static/issues_stats.js
index 571f3f0..406ce7b 100644
--- a/pagure/static/issues_stats.js
+++ b/pagure/static/issues_stats.js
@@ -1,5 +1,4 @@
-
-issues_history_stats_plot = function(url, _b, _s) {
+function issues_history_stats_plot(url, _b, _s) {
   var svg = d3.select("svg"),
       margin = {top: 20, right: 20, bottom: 30, left: 50},
       width = $('#stats').width() - margin.left - margin.right,
@@ -42,7 +41,7 @@ issues_history_stats_plot = function(url, _b, _s) {
         .attr("dy", "0.71em")
         .attr("text-anchor", "end")
         .text("Open Issues");
-  };
+  }
 
   d3.json(url, function(d) {
     var _out = new Array();
@@ -56,20 +55,19 @@ issues_history_stats_plot = function(url, _b, _s) {
     _b.show();
     _s.hide();
   });
+}
 
-};
-
-wait_for_task = function(url, callback){
+function wait_for_task(url, callback) {
   $.get(url)
   .done(function(data){
     callback(data);
   })
   .fail(function(){
     window.setTimeout(wait_for_task(url, callback), 1000);
-  })
+  });
 }
 
-show_commits_authors = function(data) {
+function show_commits_authors(data) {
   var _b = $("#data_stats");
   var _s = $("#data_stats_spinner");
   var html = '<h2>Authors stats</h2><p> Since '
@@ -77,9 +75,9 @@ show_commits_authors = function(data) {
     + data.results[0] + ' commits found in this repo, from '
     + data.results[2] + ' contributors</p>\n'
     + '<div class="list-group">\n';
-  for (key in data.results[1]){
-    cnt = data.results[1][key][0];
-    for (entry in data.results[1][key][1]){
+  for (const key in data.results[1]){
+    const cnt = data.results[1][key][0];
+    for (let entry in data.results[1][key][1]){
       entry = data.results[1][key][1][entry];
       html += '  <a class="list-group-item" href="'
         + view_commits_url.replace('---', entry[1]) + '">'
@@ -94,7 +92,7 @@ show_commits_authors = function(data) {
   _s.hide();
 }
 
-commits_authors = function(url, _data) {
+function commits_authors(url, _data) {
   $.post( url, _data )
   .done(function(data) {
     wait_for_task(data.url, show_commits_authors);
@@ -104,7 +102,7 @@ commits_authors = function(url, _data) {
 };
 
 
-show_commits_history = function(data) {
+function show_commits_history(data) {
   var _b = $("#data_stats");
   var _s = $("#data_stats_spinner");
 
@@ -115,7 +113,7 @@ show_commits_history = function(data) {
     t.date = parseTime(x[0]);
     t.value = x[1];
     return t;
-  })
+  });
 
   var svg = d3.select("svg"),
       margin = {top: 20, right: 20, bottom: 30, left: 50},
@@ -158,18 +156,18 @@ show_commits_history = function(data) {
         .attr("dy", "0.71em")
         .attr("text-anchor", "end")
         .text("Number of commits");
-  };
+  }
 
   draw_graph(_out);
   _b.show();
   _s.hide();
 }
 
-commits_history = function(url, _data) {
+function commits_history (url, _data) {
   $.post( url, _data )
   .done(function(data) {
     wait_for_task(data.url, show_commits_history);
   })
   .fail(function(data) {
   })
-};
+}

From e283e7d5076292f508d5a99b2d92e3f7cb7dfa83 Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lubomir.sedlar@gmail.com>
Date: Mar 04 2018 17:43:04 +0000
Subject: [PATCH 2/4] Hide spinner from single place


Instead of each data display callback having to care about hiding
spinner we can do it from the calling function. Now `wait_for_task` will
wait for the task to finish and then hide the spinner automatically.

---

diff --git a/pagure/static/issues_stats.js b/pagure/static/issues_stats.js
index 406ce7b..06bae32 100644
--- a/pagure/static/issues_stats.js
+++ b/pagure/static/issues_stats.js
@@ -61,6 +61,7 @@ function wait_for_task(url, callback) {
   $.get(url)
   .done(function(data){
     callback(data);
+    $("#data_stats_spinner").hide();
   })
   .fail(function(){
     window.setTimeout(wait_for_task(url, callback), 1000);
@@ -69,7 +70,6 @@ function wait_for_task(url, callback) {
 
 function show_commits_authors(data) {
   var _b = $("#data_stats");
-  var _s = $("#data_stats_spinner");
   var html = '<h2>Authors stats</h2><p> Since '
     + new Date(data.results[3]*1000) + ' there has been '
     + data.results[0] + ' commits found in this repo, from '
@@ -89,7 +89,6 @@ function show_commits_authors(data) {
   html += '</div>';
   _b.html(html);
   _b.show();
-  _s.hide();
 }
 
 function commits_authors(url, _data) {
@@ -104,7 +103,6 @@ function commits_authors(url, _data) {
 
 function show_commits_history(data) {
   var _b = $("#data_stats");
-  var _s = $("#data_stats_spinner");
 
   var parseTime = d3.timeParse("%Y-%m-%d");
 
@@ -160,7 +158,6 @@ function show_commits_history(data) {
 
   draw_graph(_out);
   _b.show();
-  _s.hide();
 }
 
 function commits_history (url, _data) {

From 8a1747f275105cb81e997e845a4ea0bd118091e5 Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lubomir.sedlar@gmail.com>
Date: Mar 04 2018 17:43:04 +0000
Subject: [PATCH 3/4] Reduce duplication in stats JS


The code for asynchronous retrieval of data and processing it can be
shared both for commits and authors.

---

diff --git a/pagure/static/issues_stats.js b/pagure/static/issues_stats.js
index 06bae32..4c1f9cf 100644
--- a/pagure/static/issues_stats.js
+++ b/pagure/static/issues_stats.js
@@ -91,16 +91,6 @@ function show_commits_authors(data) {
   _b.show();
 }
 
-function commits_authors(url, _data) {
-  $.post( url, _data )
-  .done(function(data) {
-    wait_for_task(data.url, show_commits_authors);
-  })
-  .fail(function(data) {
-  })
-};
-
-
 function show_commits_history(data) {
   var _b = $("#data_stats");
 
@@ -160,11 +150,9 @@ function show_commits_history(data) {
   _b.show();
 }
 
-function commits_history (url, _data) {
-  $.post( url, _data )
+function process_async(url, _data, callback) {
+  $.post(url, _data)
   .done(function(data) {
-    wait_for_task(data.url, show_commits_history);
-  })
-  .fail(function(data) {
+    wait_for_task(data.url, callback);
   })
 }
diff --git a/pagure/templates/repo_stats.html b/pagure/templates/repo_stats.html
index 7b6fae8..17aa328 100644
--- a/pagure/templates/repo_stats.html
+++ b/pagure/templates/repo_stats.html
@@ -134,7 +134,7 @@ commits_authors_call = function() {
     username: "{{ username or '' }}",
     namespace: "{{ g.repo.namespace or '' }}",
   }
-  commits_authors(_stats_url, data);
+  process_async(_stats_url, data, show_commits_authors);
 };
 
 commits_history_call = function() {
@@ -155,7 +155,7 @@ commits_history_call = function() {
     username: "{{ username or '' }}",
     namespace: "{{ g.repo.namespace or '' }}",
   }
-  commits_history(_stats_url, data);
+  process_async(_stats_url, data, show_commits_history);
 };
 
 toggle_forks = function() {

From 5a961d794c29cdb93ca38ae09ddeb18c609e6d4b Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lubomir.sedlar@gmail.com>
Date: Mar 04 2018 17:43:04 +0000
Subject: [PATCH 4/4] Define empty array as []


This is recommended by jshint: they are identical in terms of
functionality, but the square brackets are more commonly used and thus
more readable.

---

diff --git a/pagure/static/issues_stats.js b/pagure/static/issues_stats.js
index 4c1f9cf..6cdd497 100644
--- a/pagure/static/issues_stats.js
+++ b/pagure/static/issues_stats.js
@@ -44,7 +44,7 @@ function issues_history_stats_plot(url, _b, _s) {
   }
 
   d3.json(url, function(d) {
-    var _out = new Array();
+    var _out = [];
     for (var _d in d.stats) {
       var t = {};
       t.date = parseTime(_d.split('T', 1)[0]);

