[Groonga-commit] groonga/groonga-admin at 6a253c7 [master] table search: extract time units

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Jan 4 15:38:43 JST 2015


Kouhei Sutou	2015-01-04 15:38:43 +0900 (Sun, 04 Jan 2015)

  New Revision: 6a253c7eef075074779663cc09616e148b42b58d
  https://github.com/groonga/groonga-admin/commit/6a253c7eef075074779663cc09616e148b42b58d

  Message:
    table search: extract time units

  Added files:
    app/scripts/time-unit.js
  Modified files:
    .jshintrc
    app/index.html
    app/scripts/controllers/table-search-controller.js

  Modified: .jshintrc (+2 -1)
===================================================================
--- .jshintrc    2015-01-04 00:22:08 +0900 (9cc0105)
+++ .jshintrc    2015-01-04 15:38:43 +0900 (379713f)
@@ -21,6 +21,7 @@
   "globals": {
     "angular": false,
     "GroongaClient": false,
-    "GroongaResponse": false
+    "GroongaResponse": false,
+    "TimeUnit": false
   }
 }

  Modified: app/index.html (+1 -0)
===================================================================
--- app/index.html    2015-01-04 00:22:08 +0900 (1e7e3ee)
+++ app/index.html    2015-01-04 15:38:43 +0900 (afd9a24)
@@ -87,6 +87,7 @@
         <script src="scripts/groonga-client/response/column-list.js"></script>
         <script src="scripts/groonga-client/response/column-create.js"></script>
         <script src="scripts/groonga-client/response/column-remove.js"></script>
+        <script src="scripts/time-unit.js"></script>
         <script src="scripts/app.js"></script>
         <script src="scripts/services/schema-loader.js"></script>
         <script src="scripts/controllers/top-controller.js"></script>

  Modified: app/scripts/controllers/table-search-controller.js (+10 -129)
===================================================================
--- app/scripts/controllers/table-search-controller.js    2015-01-04 00:22:08 +0900 (3b29615)
+++ app/scripts/controllers/table-search-controller.js    2015-01-04 15:38:43 +0900 (f0cbf72)
@@ -13,8 +13,6 @@ angular.module('groongaAdminApp')
     function ($scope, $routeParams, $location, $http, $filter, schemaLoader) {
       var schema;
       var client = new GroongaClient($http);
-      var timeColumnUnits;
-      var orderedTimeColumnUnits;
 
       function findElement(array, finder) {
         var i, length;
@@ -33,7 +31,7 @@ angular.module('groongaAdminApp')
       }
 
       function initialize() {
-        $scope.orderedTimeColumnUnits = orderedTimeColumnUnits;
+        $scope.orderedTimeColumnUnits = TimeUnit.getOrderedUnits();
 
         $scope.table = {
           name: $routeParams.table,
@@ -307,123 +305,6 @@ angular.module('groongaAdminApp')
         };
       }
 
-      timeColumnUnits = {
-        hour: {
-          label: 'Hour',
-          baseTimeInMilliseconds: 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear(),
-                            now.getMonth(),
-                            now.getDate(),
-                            now.getHours());
-          }
-        },
-        day: {
-          label: 'Day',
-          baseTimeInMilliseconds: 24 * 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear(),
-                            now.getMonth(),
-                            now.getDate());
-          }
-        },
-        week: {
-          label: 'Week',
-          baseTimeInMilliseconds: 7 * 24 * 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear(),
-                            now.getMonth(),
-                            now.getDate() - 4);
-          }
-        },
-        month: {
-          label: 'Month',
-          baseTimeInMilliseconds: 12 * 24 * 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear(),
-                            now.getMonth());
-          }
-        },
-        year: {
-          label: 'Year',
-          baseTimeInMilliseconds: 365 * 24 * 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear());
-          }
-        },
-        decade: {
-          label: 'Decade',
-          baseTimeInMilliseconds: 10 * 365 * 24 * 60 * 60 * 1000,
-          baseDate: function() {
-            var now = new Date();
-            return new Date(now.getFullYear() - 10);
-          }
-        }
-      };
-
-      orderedTimeColumnUnits = [];
-      angular.forEach(timeColumnUnits, function(unit) {
-        orderedTimeColumnUnits.push(unit);
-      });
-      orderedTimeColumnUnits.sort(function(unit1, unit2) {
-        return unit1.baseTimeInMilliseconds - unit2.baseTimeInMilliseconds;
-      });
-
-      function dateInUnit(date, unit) {
-        var baseDate = unit.baseDate();
-        var baseTime = baseDate.getTime();
-        var time = date.getTime();
-        return (baseTime <= time &&
-                time <= (baseTime * unit.baseTimeInMilliseconds));
-      }
-
-      function dateRangeToUnit(start, end) {
-        if (!start && !end) {
-          return timeColumnUnits.day;
-        }
-
-        var unit = findElement(orderedTimeColumnUnits, function(unit) {
-          if (start) {
-            if (!dateInUnit(start, unit)) {
-              return false;
-            }
-          }
-          if (end) {
-            if (!dateInUnit(end, unit)) {
-              return false;
-            }
-          }
-          return true;
-        });
-        if (!unit) {
-          unit = timeColumnUnits.decade;
-        }
-        return unit;
-      }
-
-      function timeRangeValueToDate(value, unit) {
-        var baseDate = unit.baseDate();
-        var date;
-        if (value === 0) {
-          date = baseDate;
-        } else {
-          date = new Date();
-          date.setTime(baseDate.getTime() +
-                       unit.baseTimeInMilliseconds * (value / 100.0));
-        }
-        return date;
-      }
-
-      function dateToTimeRangeValue(date, unit) {
-        var diffTime = date.getTime() - unit.baseDate().getTime();
-        return (diffTime / unit.baseTimeInMilliseconds) * 100;
-      }
-
       function addTimeColumn(columnInfo) {
         if (columnInfo.type !== 'Time') {
           return;
@@ -435,38 +316,38 @@ angular.module('groongaAdminApp')
           startIncluded: true,
           end: null,
           endIncluded: true,
-          unit: timeColumnUnits.day,
+          unit: TimeUnit.units.day,
           range: [0, 0],
           syncFromRange: function() {
             if (this.range[0] === 0 && this.range[1] === 0) {
               return;
             }
-            this.start = timeRangeValueToDate(this.range[0], this.unit);
-            this.end = timeRangeValueToDate(this.range[1], this.unit);
+            this.start = this.unit.percentToDate(this.range[0] / 100);
+            this.end = this.unit.percentToDate(this.range[1] / 100);
           },
           syncToRange: function() {
-            this.unit = dateRangeToUnit(this.start, this.end);
+            this.unit = TimeUnit.findByDateRange(this.start, this.end);
             if (this.start && this.end) {
               this.range = [
-                dateToTimeRangeValue(this.start, this.unit),
-                dateToTimeRangeValue(this.end, this.unit)
+                this.unit.dateToPercent(this.start) * 100,
+                this.unit.dateToPercent(this.end) * 100
               ];
             } else if (this.start) {
               this.range = [
-                dateToTimeRangeValue(this.start, this.unit),
+                this.unit.dateToPercent(this.start) * 100,
                 100
               ];
             } else if (this.end) {
               this.range = [
                 0,
-                dateToTimeRangeValue(this.end, this.unit)
+                this.unit.dateToPercent(this.end) * 100
               ];
             } else {
               this.range = [0, 0];
             }
           },
           formater: function(value) {
-            var date = timeRangeValueToDate(value, timeColumnInfo.unit);
+            var date = timeColumnInfo.unit.percentToDate(value / 100);
             return date.toLocaleString();
           }
         };

  Added: app/scripts/time-unit.js (+143 -0) 100644
===================================================================
--- /dev/null
+++ app/scripts/time-unit.js    2015-01-04 15:38:43 +0900 (07a5884)
@@ -0,0 +1,143 @@
+'use strict';
+
+(function(global) {
+  function TimeUnit(parameters) {
+    this.label = parameters.label;
+    this.baseTimeInMilliseconds = parameters.baseTimeInMilliseconds;
+    this.getBaseDate = parameters.getBaseDate;
+  }
+  global.TimeUnit = TimeUnit;
+
+  TimeUnit.units = {};
+
+  TimeUnit.register = function(name, unit) {
+    TimeUnit.units[name] = unit;
+  };
+
+  TimeUnit.getOrderedUnits = function() {
+    var orderedUnits = [];
+    for (var name in TimeUnit.units) {
+      orderedUnits.push(TimeUnit.units[name]);
+    }
+    orderedUnits.sort(function(unit1, unit2) {
+      return unit1.baseTimeInMilliseconds - unit2.baseTimeInMilliseconds;
+    });
+    return orderedUnits;
+  };
+
+  TimeUnit.findByDateRange = function(start, end) {
+    var orderedUnits = TimeUnit.getOrderedUnits();
+
+    if (!start && !end) {
+      return orderedUnits[0];
+    }
+
+    var unit;
+    for (var i = 0; i < orderedUnits.length; i++) {
+      var candidateUnit = orderedUnits[i];
+      if (start && !candidateUnit.dateIncluded(start)) {
+        continue;
+      }
+      if (end && !candidateUnit.dateIncluded(end)) {
+        continue;
+      }
+      unit = candidateUnit;
+      break;
+    }
+    if (!unit) {
+      unit = orderedUnits[orderedUnits.length - 1];
+    }
+    return unit;
+  };
+
+  TimeUnit.prototype.dateIncluded = function(date) {
+    var baseDate = this.getBaseDate();
+    var baseTime = baseDate.getTime();
+    var time = date.getTime();
+    return (baseTime <= time &&
+            time <= (baseTime * this.baseTimeInMilliseconds));
+  };
+
+  TimeUnit.prototype.percentToDate = function(percent) {
+    var baseDate = this.getBaseDate();
+    var date;
+    date = new Date();
+    date.setTime(baseDate.getTime() +
+                 this.baseTimeInMilliseconds * percent);
+    return date;
+  };
+
+  TimeUnit.prototype.dateToPercent = function(date) {
+    var diffTime = date.getTime() - this.getBaseDate().getTime();
+    return diffTime / this.baseTimeInMilliseconds;
+  };
+
+
+  // Built-in units
+  TimeUnit.register('hour', new TimeUnit({
+    label: 'Hour',
+    baseTimeInMilliseconds: 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear(),
+                      now.getMonth(),
+                      now.getDate(),
+                      now.getHours(),
+                      now.getMinutes() - 30);
+    }
+  }));
+
+  TimeUnit.register('day', new TimeUnit({
+    label: 'Day',
+    baseTimeInMilliseconds: 24 * 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear(),
+                      now.getMonth(),
+                      now.getDate(),
+                      now.getHours() - 12);
+    }
+  }));
+
+  TimeUnit.register('week', new TimeUnit({
+    label: 'Week',
+    baseTimeInMilliseconds: 7 * 24 * 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear(),
+                      now.getMonth(),
+                      now.getDate() - 3,
+                      now.getHours() - 12);
+    }
+  }));
+
+  TimeUnit.register('month', new TimeUnit({
+    label: 'Month',
+    baseTimeInMilliseconds: 30 * 24 * 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear(),
+                      now.getMonth(),
+                      now.getDate() - 15);
+    }
+  }));
+
+  TimeUnit.register('year', new TimeUnit({
+    label: 'Year',
+    baseTimeInMilliseconds: 365 * 24 * 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear(),
+                      now.getMonth() - 6);
+    }
+  }));
+
+  TimeUnit.register('decade', new TimeUnit({
+    label: 'Decade',
+    baseTimeInMilliseconds: 10 * 365 * 24 * 60 * 60 * 1000,
+    getBaseDate: function() {
+      var now = new Date();
+      return new Date(now.getFullYear() - 5);
+    }
+  }));
+})(window);
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index