pandora/static/js/pandora/statisticsDialog.js

531 lines
26 KiB
JavaScript
Raw Normal View History

2012-03-22 20:08:51 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
pandora.ui.statisticsDialog = function() {
var colors = {
firstseen: [64, 192, 64],
lastseen: [64, 64, 192],
2012-03-25 21:12:13 +00:00
day: [64, 192, 192],
hour: [64, 192, 192],
2012-03-22 20:08:51 +00:00
continent: [64, 192, 64],
region: [64, 192, 192],
country: [64, 64, 192],
city: [192, 64, 64],
system: [64, 192, 64],
browser: [64, 64, 192],
systemandbrowser: [64, 192, 192]
},
dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
dialogWidth = Math.round(window.innerWidth * 0.9),
names = {
system: [
'Android', 'iOS', 'Linux', 'Mac OS X', 'Windows'
],
browser: [
'Chrome Frame', 'Chrome', 'Firefox',
'Internet Explorer', 'Opera', 'Safari'
]
},
tabs = [
{id: 'seen', title: 'First Seen and Last Seen', selected: true},
{id: 'locations', title: 'Locations'},
{id: 'systems', title: 'Operating Systems and Browsers'}
],
$dialog = Ox.Dialog({
buttons: [
Ox.Button({
id: 'manageUsers',
title: 'Manage Users...'
}).bindEvent({
click: function() {
$dialog.close();
pandora.$ui.usersDialog = pandora.ui.usersDialog().open();
}
}),
{},
2012-03-22 20:08:51 +00:00
Ox.Button({
id: 'close',
title: 'Close'
}).bindEvent({
click: function() {
$dialog.close();
}
})
],
closeButton: true,
content: Ox.Element().append(
$('<img>')
.attr({src: Ox.UI.getImageURL('symbolLoadingAnimated')})
.css({
position: 'absolute',
width: '32px',
height: '32px',
left: 0,
top: 0,
right: 0,
bottom: 0,
margin: 'auto'
})
),
2012-03-22 20:08:51 +00:00
height: dialogHeight,
2012-03-22 20:30:31 +00:00
maximizeButton: true,
2012-03-22 20:08:51 +00:00
minHeight: 256,
minWidth: 512,
removeOnClose: true,
title: 'Statistics',
width: dialogWidth
})
.bindEvent({
resizeend: function(data) {
dialogWidth = data.width;
$tabPanel.$element.replaceElement(1,
$tabPanel.options('content')($tabPanel.selected())
);
}
}),
$tabPanel;
2012-03-22 20:08:51 +00:00
//Ox.getJSON('/static/json/deleteme.json', function(result) {
///*
2012-03-22 20:08:51 +00:00
pandora.api.findUsers({
keys: ['browser', 'email', 'firstseen', 'lastseen', 'level', 'location', 'system'],
2012-03-22 20:08:51 +00:00
range: [0, 1000000],
sort: [{key: 'username', operator: '+'}]
}, function(result) {
//*/
2012-03-22 20:08:51 +00:00
var data = {},
2012-03-22 20:08:51 +00:00
flagCountry = {},
$guestsCheckbox;
2012-03-22 20:08:51 +00:00
['all', 'registered'].forEach(function(mode) {
data[mode] = {
year: {},
month: {},
2012-03-25 21:12:13 +00:00
day: {},
hour: {},
2012-03-22 20:08:51 +00:00
continent: {},
region: {},
country: {},
city: {},
system: {},
browser: {},
systemandbrowser: {},
systemversion: {},
browserversion: {},
systemandbrowserversion: {}
};
result.data.items.forEach(function(item) {
2012-03-25 21:12:13 +00:00
var city, continent, country, countryData, name = {}, region, split;
2012-03-22 20:08:51 +00:00
if (mode == 'all' || item.level != 'guest') {
['firstseen', 'lastseen'].forEach(function(key, i) {
var year = item[key].substr(0, 4) + '-' + key,
2012-03-25 21:12:13 +00:00
month = item[key].substr(0, 7) + '-' + key,
day = Ox.formatDate(item[key], '%u'),
hour = item[key].substr(11, 2);
2012-03-22 20:08:51 +00:00
data[mode].year[year] = (data[mode].year[year] || 0) + 1;
2012-03-25 21:12:13 +00:00
data[mode].month[month] = (data[mode].month[month] || 0) + 1;
data[mode].day[day] = (data[mode].day[day] || 0) + 1;
data[mode].hour[hour] = (data[mode].hour[hour] || 0) + 1;
2012-03-22 20:08:51 +00:00
});
if (!item.location) {
item.location = (Ox.getCountryByCode(
item.email.split('.').pop().replace(/(edu|gov|mil)/i, 'us')
) || {}).name || '';
}
2012-03-22 20:08:51 +00:00
if (item.location) {
split = item.location.split(', ')
if (split.length == 1) {
country = item.location;
} else {
country = split[1];
2012-03-25 21:12:13 +00:00
city = split[0];
2012-03-22 20:08:51 +00:00
}
2012-03-26 10:58:19 +00:00
countryData = Ox.getCountryByName(country) || {continent: '', region: ''};
2012-03-25 21:12:13 +00:00
continent = countryData.continent;
region = [continent, countryData.region].join(', ');
country = [region, country].join(', ')
city = city ? [country, city].join(', ') : '';
2012-03-22 20:08:51 +00:00
data[mode].continent[continent] = (data[mode].continent[continent] || 0) + 1;
2012-03-25 21:12:13 +00:00
data[mode].region[region] = (data[mode].region[region] || 0) + 1;
data[mode].country[country] = (data[mode].country[country] || 0) + 1;
if (city) {
data[mode].city[city] = (data[mode].city[city] || 0) + 1;
}
2012-03-22 20:08:51 +00:00
}
['system', 'browser'].forEach(function(key) {
var version = item[key];
if (version) {
name[key] = '';
Ox.forEach(names[key], function(v) {
if (new RegExp('^' + v).test(version)) {
name[key] = v;
return false;
}
});
if (name[key]) {
data[mode][key][name[key]] = (data[mode][key][name[key]] || 0) + 1;
key = key + 'version';
data[mode][key][version] = (data[mode][key][version] || 0) + 1;
}/* else {
Ox.print("^^^^^^", version)
}*/
}
});
if (name.system && name.browser) {
name = name.system + ' / ' + name.browser;
data[mode].systemandbrowser[name] = (data[mode].systemandbrowser[name] || 0) + 1;
}
}
});
var keys, firstKey, lastKey;
keys = Object.keys(data[mode].month).map(function(key) {
return key.substr(0, 7)
}).sort();
firstKey = keys[0].split('-').map(function(str) {
return parseInt(str, 10);
});
lastKey = keys[keys.length - 1].split('-').map(function(str) {
return parseInt(str, 10);
});
Ox.loop(firstKey[0], lastKey[0] + 1, function(year) {
['firstseen', 'lastseen'].forEach(function(key) {
var key = [year, key].join('-');
data[mode].year[key] = data[mode].year[key] || 0;
});
Ox.loop(
year == firstKey[0] ? firstKey[1] : 1,
year == lastKey[0] ? lastKey[1] + 1 : 13,
function(month) {
['firstseen', 'lastseen'].forEach(function(key) {
var key = [year, Ox.pad(month, 2), key].join('-');
data[mode].month[key] = data[mode].month[key] || 0;
});
}
);
});
flagCountry[mode] = {};
['continent', 'region'].forEach(function(key) {
flagCountry[mode][key] = {};
Ox.forEach(data[mode][key], function(regionValue, regionKey) {
2012-03-25 21:12:13 +00:00
regionKey = regionKey.split(', ').pop();
2012-03-22 20:08:51 +00:00
var max = 0;
Ox.forEach(data[mode].country, function(countryValue, countryKey) {
2012-03-25 21:12:13 +00:00
countryKey = countryKey.split(', ').pop();
2012-03-22 20:08:51 +00:00
if (
2012-03-26 10:55:49 +00:00
(Ox.getCountryByName(countryKey) || {})[key] == regionKey
2012-03-22 20:08:51 +00:00
&& countryValue > max
) {
flagCountry[mode][key][regionKey] = countryKey;
max = countryValue;
}
});
});
});
});
2012-03-25 21:12:13 +00:00
data.all.city['Antarctica, Antarctica, Neutral Zone, Other'] = 0;
2012-03-22 20:08:51 +00:00
Ox.forEach(data.all.city, function(value, key) {
if (value < 2) {
2012-03-25 21:12:13 +00:00
data.all.city['Antarctica, Antarctica, Neutral Zone, Other']++;
2012-03-22 20:08:51 +00:00
delete data.all.city[key];
}
});
$guestsCheckbox = Ox.Checkbox({
title: 'Include Guests',
value: false
})
.css({float: 'left', margin: '4px'})
.bindEvent({
change: function() {
$tabPanel.$element.replaceElement(1,
$tabPanel.options('content')($tabPanel.selected())
);
}
});
$tabPanel = Ox.TabPanel({
content: function(id) {
var chartWidth = dialogWidth - 32 - Ox.UI.SCROLLBAR_SIZE,
mode = $guestsCheckbox.options('value') ? 'all' : 'registered',
2012-03-22 20:08:51 +00:00
top = 16,
$content = Ox.Element()
.css({
padding: '16px',
overflowY: 'auto',
background: Ox.Theme() == 'classic'
? 'rgb(240, 240, 240)'
: 'rgb(16, 16, 16)'
});
if (id == 'seen') {
2012-03-25 21:12:13 +00:00
['year', 'month', 'day', 'hour'].forEach(function(key) {
var isYearOrMonth = ['year', 'month'].indexOf(key) > -1;
2012-03-22 20:08:51 +00:00
Ox.Chart({
2012-03-25 21:12:13 +00:00
color: isYearOrMonth
? function(value) {
return colors[value.split('-').pop()];
}
: colors[key],
2012-03-22 20:08:51 +00:00
data: data[mode][key],
formatKey: function(value) {
2012-03-25 21:12:13 +00:00
var ret, split;
if (isYearOrMonth) {
split = value.split('-');
ret = (split.pop() == 'firstseen' ? 'First' : 'Last') + ': '
+ (key == 'year' ? '' : Ox.MONTHS[parseInt(split[1], 10) - 1])
+ ' ' + split[0];
} else {
ret = key == 'day'
? Ox.WEEKDAYS[parseInt(value) - 1]
: value + ':00';
}
return ret;
2012-03-22 20:08:51 +00:00
},
keyAlign: 'right',
keyWidth: 128,
2012-03-25 21:12:13 +00:00
rows: isYearOrMonth ? 2 : 1,
sort: {key: 'key', operator: isYearOrMonth ? '-' : '+'},
title: Ox.toTitleCase(key) + 's',
2012-03-22 20:08:51 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode][key]) * 16 + 32;
});
} else if (id == 'locations') {
['continent', 'region', 'country', 'city'].forEach(function(key) {
Ox.Chart({
color: colors[key],
data: data[mode][key],
formatKey: function(value) {
2012-03-25 21:12:13 +00:00
var city, country, split = value.split(', ');
if (key == 'continent' || key == 'region') {
country = flagCountry[mode][key][Ox.last(split)];
2012-03-22 20:08:51 +00:00
} else {
2012-03-25 21:12:13 +00:00
country = split[2];
city = key == 'city' ? split[3] : ''
2012-03-22 20:08:51 +00:00
}
return $('<div>')
.append(
$('<div>')
.css({
float: 'left',
width: '104px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(
Ox.last(split)
2012-03-22 20:08:51 +00:00
)
)
.append(
2012-03-25 21:12:13 +00:00
Ox.Element({
element: '<img>',
tooltip: mode == 'all' && (key == 'continent' || key == 'region')
? Ox.wordwrap(
Ox.map(Ox.COUNTRIES, function(country) {
return country[key] == split[key == 'continent' ? 0 : 1]
&& country.code.length == 2
&& ['EU', 'UK'].indexOf(country.code) == -1
&& !country.disputed
&& !country.dissolved
? country.name : null;
}).sort().join(', '),
64, '<br>', true
).split(', ').map(function(country) {
return Ox.values(Ox.map(data.all.country, function(value, key) {
return key.split(', ').pop()
})).indexOf(country.replace(/<br>/g, '')) > -1
? '<span class="OxBright">' + country + '</span>'
: country
}).join(', ')
: ''
})
2012-03-22 20:08:51 +00:00
.attr({
src: Ox.getFlagByGeoname(country, 16)
})
.css({
float: 'left',
width: '14px',
height: '14px',
borderRadius: '4px',
margin: '0 1px 0 1px'
})
);
},
keyAlign: 'right',
keyWidth: 128,
sort: {key: 'value', operator: '-'},
title: Ox.endsWith(key, 'y')
? Ox.sub(Ox.toTitleCase(key), 0, -1) + 'ies'
: Ox.toTitleCase(key) + 's',
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode][key]) * 16 + 32;
});
} else if (id == 'systems') {
['', 'version'].forEach(function(version, i) {
['system', 'browser'].forEach(function(key) {
Ox.Chart({
color: colors[key],
data: data[mode][key + version],
formatKey: function(value) {
var index,
name = value,
$element = $('<div>');
if (version) {
Ox.forEach(names[key], function(v) {
if (new RegExp('^' + v).test(value)) {
name = v;
return false;
}
});
/*
text = value.substr(name.length + 1);
index = text.indexOf('(')
if (index > -1) {
text = Ox.sub(text, index + 1, -1);
}
*/
}
$element.append(
$('<div>')
.css({
float: 'left',
width: '168px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(value.replace('(Windows ', '('))
).append(
$('<img>')
.attr({
src: Ox.UI.PATH + 'png/' + key
+ name.replace(/ /g, '') + '128.png'
})
.css({
float: 'left',
width: '14px',
height: '14px',
margin: '0 1px 0 1px'
})
);
return $element;
},
keyWidth: 192,
sort: version == ''
? {key: 'value', operator: '-'}
: {key: 'key', operator: '+'},
title: key == 'system'
? (version == '' ? 'Operating Systems' : 'Operating System Versions')
: (version == '' ? 'Browsers' : 'Browser Versions'),
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode][key + version]) * 16 + 32;
});
if (i == 0) {
Ox.Chart({
color: [64, 192, 192],
data: data[mode].systemandbrowser,
formatKey: function(value) {
var $element = $('<div>')
.append(
$('<div>')
.css({
float: 'left',
width: '152px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(value)
);
value.split(' / ').forEach(function(value, i) {
$element.append(
$('<img>')
.attr({
src: Ox.UI.PATH + 'png/'
+ (i == 0 ? 'system' : 'browser')
+ value.replace(/ /g, '') + '128.png'
})
.css({
width: '14px',
height: '14px',
margin: '0 1px 0 1px'
})
);
});
return $element;
},
keyWidth: 192,
sort: {key: 'value', operator: '-'},
2012-03-22 20:23:57 +00:00
title: 'Operating Systems and Browsers',
2012-03-22 20:08:51 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode].systemandbrowser) * 16 + 32;
}
});
}
$('<div>')
.css({
position: 'absolute',
top: top - 16 + 'px',
width: '1px',
height: '16px'
})
.appendTo($content);
return $content;
},
tabs: tabs
});
$tabPanel.$element.find('.OxButtonGroup').css({width: '512px'});
$guestsCheckbox.appendTo($tabPanel.children('.OxBar'));
$dialog.options({content: $tabPanel});
});
return $dialog;
};