Fauxton: fix missing leading zeros in logs

This adds leading zeros to the times in the log view. Before that
the times were displayed like 1:5:3 (if it were 01:05:03)
because getHours() and friends are returning a Number and not
a String with a leading 0. In the case of 1:5:3 the corrected
time is now displayed as: 01:05:03. As d3 has a nice date formatter
we do not have to roll our own.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/59b072f1
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/59b072f1
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/59b072f1

Branch: refs/heads/import-master
Commit: 59b072f1ec419ca334dd343865e14f6bd1427881
Parents: ea55677
Author: Robert Kowalski <[email protected]>
Authored: Sun Apr 13 13:48:00 2014 +0200
Committer: Garren Smith <[email protected]>
Committed: Mon Apr 14 08:50:47 2014 +0200

----------------------------------------------------------------------
 app/addons/logs/resources.js           | 15 ++++++------
 app/addons/logs/tests/baseSpec.js      | 38 +++++++++++++++++++++++++++++
 app/addons/logs/tests/logSpec.js       | 38 -----------------------------
 app/addons/logs/tests/resourcesSpec.js | 38 +++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59b072f1/app/addons/logs/resources.js
----------------------------------------------------------------------
diff --git a/app/addons/logs/resources.js b/app/addons/logs/resources.js
index 3a47b92..5cfa673 100644
--- a/app/addons/logs/resources.js
+++ b/app/addons/logs/resources.js
@@ -13,22 +13,21 @@
 define([
   "app",
   "api",
-  "backbone"
+  "backbone",
+  "d3"
 ],
 
-function (app, FauxtonAPI, Backbone) {
+function (app, FauxtonAPI, Backbone, d3) {
 
   var Log = FauxtonAPI.addon();
 
   Log.Model = Backbone.Model.extend({
 
     date: function () {
-      var date = new Date(this.get('date'));
+      var date = new Date(this.get('date')),
+          formatter = d3.time.format("%b %e %H:%M%:%S");
 
-      var formatted_time = date.getHours() + ":" + date.getMinutes() + ":" + 
date.getSeconds();
-      var formatted_date = date.toDateString().slice(4, 10);
-
-      return formatted_date + ' ' + formatted_time;
+      return formatter(date);
     },
 
     logLevel: function () {
@@ -51,7 +50,7 @@ function (app, FauxtonAPI, Backbone) {
     initialize: function (options) {
       this.params = {bytes: 5000};
     },
-    
+
     documentation: "log",
 
     url: function () {

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59b072f1/app/addons/logs/tests/baseSpec.js
----------------------------------------------------------------------
diff --git a/app/addons/logs/tests/baseSpec.js 
b/app/addons/logs/tests/baseSpec.js
new file mode 100644
index 0000000..621cc9b
--- /dev/null
+++ b/app/addons/logs/tests/baseSpec.js
@@ -0,0 +1,38 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy 
of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations 
under
+// the License.
+define([
+       'addons/logs/base',
+       'chai'
+], function (Log, chai) {
+  var expect = chai.expect;
+
+  describe('Logs Addon', function(){
+
+    describe('Log Model', function () {
+      var log;
+
+      beforeEach(function () {
+        log = new Log.Model({
+          log_level: 'DEBUG',
+          pid: '1234',
+          args: 'testing 123',
+          date: (new Date()).toString()
+        });
+      });
+
+      it('should have a log level', function () {
+        expect(log.logLevel()).to.equal('DEBUG');
+      });
+
+    });
+  });
+});

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59b072f1/app/addons/logs/tests/logSpec.js
----------------------------------------------------------------------
diff --git a/app/addons/logs/tests/logSpec.js b/app/addons/logs/tests/logSpec.js
deleted file mode 100644
index 621cc9b..0000000
--- a/app/addons/logs/tests/logSpec.js
+++ /dev/null
@@ -1,38 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License"); you may not
-// use this file except in compliance with the License. You may obtain a copy 
of
-// the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-// License for the specific language governing permissions and limitations 
under
-// the License.
-define([
-       'addons/logs/base',
-       'chai'
-], function (Log, chai) {
-  var expect = chai.expect;
-
-  describe('Logs Addon', function(){
-
-    describe('Log Model', function () {
-      var log;
-
-      beforeEach(function () {
-        log = new Log.Model({
-          log_level: 'DEBUG',
-          pid: '1234',
-          args: 'testing 123',
-          date: (new Date()).toString()
-        });
-      });
-
-      it('should have a log level', function () {
-        expect(log.logLevel()).to.equal('DEBUG');
-      });
-
-    });
-  });
-});

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59b072f1/app/addons/logs/tests/resourcesSpec.js
----------------------------------------------------------------------
diff --git a/app/addons/logs/tests/resourcesSpec.js 
b/app/addons/logs/tests/resourcesSpec.js
new file mode 100644
index 0000000..d67c677
--- /dev/null
+++ b/app/addons/logs/tests/resourcesSpec.js
@@ -0,0 +1,38 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy 
of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations 
under
+// the License.
+
+define([
+       'addons/logs/resources',
+       'testUtils'
+], function (Log, testUtils) {
+  var assert = testUtils.assert;
+
+  describe('Log Resources', function () {
+
+    describe('Date Formatter', function () {
+      it('adds leading zeros to minutes', function () {
+        var model;
+
+        model = new Log.Model({
+                  date: 'Sat, 12 Apr 2014 15:04:01 GMT',
+                  log_level: 'info',
+                  pid: '123',
+                  args: 'ente ente'
+                });
+        // timezones with daylightsaving in JS are hard
+        // and we use the current local time here
+        // so do not test for hours and the exact day
+        assert.ok(/Apr \d\d \d\d:04:01/.test(model.date()));
+      });
+    });
+  });
+});

Reply via email to