Navigation Menu

Skip to content

Commit

Permalink
table search: extract time units
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 4, 2015
1 parent 1b64442 commit 6a253c7
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Expand Up @@ -21,6 +21,7 @@
"globals": {
"angular": false,
"GroongaClient": false,
"GroongaResponse": false
"GroongaResponse": false,
"TimeUnit": false
}
}
1 change: 1 addition & 0 deletions app/index.html
Expand Up @@ -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>
Expand Down
139 changes: 10 additions & 129 deletions app/scripts/controllers/table-search-controller.js
Expand Up @@ -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;
Expand All @@ -33,7 +31,7 @@ angular.module('groongaAdminApp')
}

function initialize() {
$scope.orderedTimeColumnUnits = orderedTimeColumnUnits;
$scope.orderedTimeColumnUnits = TimeUnit.getOrderedUnits();

$scope.table = {
name: $routeParams.table,
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
};
Expand Down
143 changes: 143 additions & 0 deletions app/scripts/time-unit.js
@@ -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);

0 comments on commit 6a253c7

Please sign in to comment.