2020-08-30 17:51:37 +03:00
|
|
|
const CHART_TARGET_SIZE = 200
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2020-03-31 11:47:22 +03:00
|
|
|
const projectsCanvas = document.getElementById('chart-projects')
|
|
|
|
const osCanvas = document.getElementById('chart-os')
|
|
|
|
const editorsCanvas = document.getElementById('chart-editor')
|
|
|
|
const languagesCanvas = document.getElementById('chart-language')
|
2020-08-29 23:03:01 +03:00
|
|
|
const machinesCanvas = document.getElementById('chart-machine')
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2020-08-30 17:51:37 +03:00
|
|
|
const projectContainer = document.getElementById('project-container')
|
|
|
|
const osContainer = document.getElementById('os-container')
|
|
|
|
const editorContainer = document.getElementById('editor-container')
|
|
|
|
const languageContainer = document.getElementById('language-container')
|
|
|
|
const machineContainer = document.getElementById('machine-container')
|
|
|
|
|
|
|
|
const containers = [projectContainer, osContainer, editorContainer, languageContainer, machineContainer]
|
|
|
|
const canvases = [projectsCanvas, osCanvas, editorsCanvas, languagesCanvas, machinesCanvas]
|
|
|
|
const data = [wakapiData.projects, wakapiData.operatingSystems, wakapiData.editors, wakapiData.languages, wakapiData.machines]
|
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let topNPickers = [...document.getElementsByClassName('top-picker')]
|
|
|
|
topNPickers.sort(((a, b) => parseInt(a.attributes['data-entity'].value) - parseInt(b.attributes['data-entity'].value)))
|
|
|
|
topNPickers.forEach(e => {
|
|
|
|
const idx = parseInt(e.attributes['data-entity'].value)
|
|
|
|
e.max = Math.min(data[idx].length, 10)
|
|
|
|
e.value = e.max
|
|
|
|
})
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
let charts = []
|
2021-01-05 14:41:01 +03:00
|
|
|
let showTopN = []
|
2020-03-31 11:47:22 +03:00
|
|
|
let resizeCount = 0
|
2020-02-20 16:28:55 +03:00
|
|
|
|
|
|
|
String.prototype.toHHMMSS = function () {
|
|
|
|
var sec_num = parseInt(this, 10)
|
|
|
|
var hours = Math.floor(sec_num / 3600)
|
|
|
|
var minutes = Math.floor((sec_num - (hours * 3600)) / 60)
|
|
|
|
var seconds = sec_num - (hours * 3600) - (minutes * 60)
|
|
|
|
|
|
|
|
if (hours < 10) {
|
|
|
|
hours = '0' + hours
|
|
|
|
}
|
|
|
|
if (minutes < 10) {
|
|
|
|
minutes = '0' + minutes
|
|
|
|
}
|
|
|
|
if (seconds < 10) {
|
|
|
|
seconds = '0' + seconds
|
|
|
|
}
|
|
|
|
return hours + ':' + minutes + ':' + seconds
|
|
|
|
}
|
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
function draw(subselection) {
|
2020-02-20 16:28:55 +03:00
|
|
|
function getTooltipOptions(key, type) {
|
|
|
|
return {
|
|
|
|
mode: 'single',
|
|
|
|
callbacks: {
|
|
|
|
label: (item) => {
|
|
|
|
let idx = type === 'pie' ? item.index : item.datasetIndex
|
|
|
|
let d = wakapiData[key][idx]
|
|
|
|
return `${d.key}: ${d.total.toString().toHHMMSS()}`
|
2020-12-03 01:16:12 +03:00
|
|
|
},
|
|
|
|
title: () => 'Total Time'
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
function shouldUpdate(index) {
|
|
|
|
return !subselection || (subselection.includes(index) && data[index].length >= showTopN[index])
|
|
|
|
}
|
|
|
|
|
|
|
|
charts
|
|
|
|
.filter((c, i) => shouldUpdate(i))
|
|
|
|
.forEach(c => c.destroy())
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let projectChart = !projectsCanvas.classList.contains('hidden') && shouldUpdate(0)
|
2020-08-30 17:51:37 +03:00
|
|
|
? new Chart(projectsCanvas.getContext('2d'), {
|
|
|
|
type: 'horizontalBar',
|
|
|
|
data: {
|
|
|
|
datasets: wakapiData.projects
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[0], wakapiData.projects.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => {
|
|
|
|
return {
|
|
|
|
label: p.key,
|
|
|
|
data: [parseInt(p.total) / 60],
|
|
|
|
backgroundColor: getRandomColor(p.key)
|
|
|
|
}
|
|
|
|
})
|
2020-08-30 00:16:21 +03:00
|
|
|
},
|
2020-08-30 17:51:37 +03:00
|
|
|
options: {
|
|
|
|
tooltips: getTooltipOptions('projects', 'bar'),
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
xAxes: [{
|
|
|
|
scaleLabel: {
|
|
|
|
display: true,
|
|
|
|
labelString: 'Minutes'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
onResize: onChartResize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: null
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let osChart = !osCanvas.classList.contains('hidden') && shouldUpdate(1)
|
2020-08-30 17:51:37 +03:00
|
|
|
? new Chart(osCanvas.getContext('2d'), {
|
|
|
|
type: 'pie',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
data: wakapiData.operatingSystems
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => parseInt(p.total)),
|
|
|
|
backgroundColor: wakapiData.operatingSystems.map(p => getRandomColor(p.key))
|
|
|
|
}],
|
|
|
|
labels: wakapiData.operatingSystems
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => p.key)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
tooltips: getTooltipOptions('operatingSystems', 'pie'),
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
onResize: onChartResize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: null
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let editorChart = !editorsCanvas.classList.contains('hidden') && shouldUpdate(2)
|
2020-08-30 17:51:37 +03:00
|
|
|
? new Chart(editorsCanvas.getContext('2d'), {
|
|
|
|
type: 'pie',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
data: wakapiData.editors
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[2], wakapiData.editors.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => parseInt(p.total)),
|
|
|
|
backgroundColor: wakapiData.editors.map(p => getRandomColor(p.key))
|
|
|
|
}],
|
|
|
|
labels: wakapiData.editors
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[2], wakapiData.editors.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => p.key)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
tooltips: getTooltipOptions('editors', 'pie'),
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
onResize: onChartResize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: null
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let languageChart = !languagesCanvas.classList.contains('hidden') && shouldUpdate(3)
|
2020-08-30 17:51:37 +03:00
|
|
|
? new Chart(languagesCanvas.getContext('2d'), {
|
|
|
|
type: 'pie',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
data: wakapiData.languages
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[3], wakapiData.languages.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => parseInt(p.total)),
|
|
|
|
backgroundColor: wakapiData.languages.map(p => languageColors[p.key.toLowerCase()] || getRandomColor(p.key))
|
|
|
|
}],
|
|
|
|
labels: wakapiData.languages
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[3], wakapiData.languages.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => p.key)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
tooltips: getTooltipOptions('languages', 'pie'),
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
onResize: onChartResize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: null
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
let machineChart = !machinesCanvas.classList.contains('hidden') && shouldUpdate(4)
|
2020-08-30 17:51:37 +03:00
|
|
|
? new Chart(machinesCanvas.getContext('2d'), {
|
|
|
|
type: 'pie',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
data: wakapiData.machines
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[4], wakapiData.machines.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => parseInt(p.total)),
|
|
|
|
backgroundColor: wakapiData.machines.map(p => getRandomColor(p.key))
|
|
|
|
}],
|
|
|
|
labels: wakapiData.machines
|
2021-01-05 14:41:01 +03:00
|
|
|
.slice(0, Math.min(showTopN[4], wakapiData.machines.length))
|
2020-08-30 17:51:37 +03:00
|
|
|
.map(p => p.key)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
tooltips: getTooltipOptions('machines', 'pie'),
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
onResize: onChartResize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: null
|
2020-08-29 23:03:01 +03:00
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
getTotal(wakapiData.operatingSystems)
|
|
|
|
|
2020-08-30 17:51:37 +03:00
|
|
|
charts = [projectChart, osChart, editorChart, languageChart, machineChart].filter(c => !!c)
|
2020-03-31 11:47:22 +03:00
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
if (!subselection) {
|
|
|
|
charts.forEach(c => c.options.onResize(c.chart))
|
|
|
|
equalizeHeights()
|
|
|
|
}
|
2020-03-31 11:47:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 14:41:01 +03:00
|
|
|
function parseTopN() {
|
|
|
|
showTopN = topNPickers.map(e => parseInt(e.value))
|
2020-08-30 17:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function togglePlaceholders(mask) {
|
|
|
|
const placeholderElements = containers.map(c => c.querySelector('.placeholder-container'))
|
|
|
|
|
|
|
|
for (let i = 0; i < mask.length; i++) {
|
|
|
|
if (!mask[i]) {
|
|
|
|
canvases[i].classList.add('hidden')
|
|
|
|
placeholderElements[i].classList.remove('hidden')
|
|
|
|
} else {
|
|
|
|
canvases[i].classList.remove('hidden')
|
|
|
|
placeholderElements[i].classList.add('hidden')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPresentDataMask() {
|
2020-11-28 22:50:35 +03:00
|
|
|
return data.map(list => (list ? list.reduce((acc, e) => acc + e.total, 0) : 0) > 0)
|
2020-08-30 17:51:37 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 11:47:22 +03:00
|
|
|
function getContainer(chart) {
|
|
|
|
return chart.canvas.parentNode
|
|
|
|
}
|
|
|
|
|
|
|
|
function onChartResize(chart) {
|
|
|
|
let container = getContainer(chart)
|
|
|
|
let targetHeight = Math.min(chart.width, CHART_TARGET_SIZE)
|
|
|
|
let actualHeight = chart.height - chart.chartArea.top
|
|
|
|
let containerTargetHeight = container.clientHeight += (targetHeight - actualHeight)
|
|
|
|
container.style.height = parseInt(containerTargetHeight) + 'px'
|
|
|
|
|
|
|
|
resizeCount++
|
|
|
|
watchEqualize()
|
|
|
|
}
|
|
|
|
|
|
|
|
function watchEqualize() {
|
|
|
|
if (resizeCount === charts.length) {
|
|
|
|
equalizeHeights()
|
|
|
|
resizeCount = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function equalizeHeights() {
|
|
|
|
let maxHeight = 0
|
|
|
|
charts.forEach(c => {
|
|
|
|
let container = getContainer(c)
|
|
|
|
if (maxHeight < container.clientHeight) {
|
|
|
|
maxHeight = container.clientHeight
|
|
|
|
}
|
|
|
|
})
|
|
|
|
charts.forEach(c => {
|
|
|
|
let container = getContainer(c)
|
|
|
|
container.style.height = parseInt(maxHeight) + 'px'
|
|
|
|
})
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 22:37:16 +03:00
|
|
|
function getTotal(items) {
|
|
|
|
let total = items.reduce((acc, d) => acc + d.total, 0)
|
2020-03-31 11:47:22 +03:00
|
|
|
document.getElementById('total-span').innerText = total.toString().toHHMMSS()
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomColor(seed) {
|
2020-03-31 11:47:22 +03:00
|
|
|
seed = seed ? seed : '1234567'
|
|
|
|
Math.seedrandom(seed)
|
|
|
|
var letters = '0123456789ABCDEF'.split('')
|
|
|
|
var color = '#'
|
2020-02-20 16:28:55 +03:00
|
|
|
for (var i = 0; i < 6; i++) {
|
2020-03-31 11:47:22 +03:00
|
|
|
color += letters[Math.floor(Math.random() * 16)]
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
2020-03-31 11:47:22 +03:00
|
|
|
return color
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 22:19:05 +03:00
|
|
|
function showApiKeyPopup(event) {
|
|
|
|
const el = document.getElementById('api-key-popup')
|
|
|
|
el.classList.remove('hidden')
|
|
|
|
el.classList.add('block')
|
|
|
|
event.stopPropagation()
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyApiKey(event) {
|
|
|
|
const el = document.getElementById('api-key-container')
|
|
|
|
el.select()
|
|
|
|
el.setSelectionRange(0, 9999)
|
|
|
|
document.execCommand('copy')
|
|
|
|
event.stopPropagation()
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
// https://koddsson.com/posts/emoji-favicon/
|
2020-03-31 11:47:22 +03:00
|
|
|
const favicon = document.querySelector('link[rel=icon]')
|
2020-02-20 16:28:55 +03:00
|
|
|
if (favicon) {
|
2020-03-31 11:47:22 +03:00
|
|
|
const emoji = favicon.getAttribute('data-emoji')
|
2020-02-20 16:28:55 +03:00
|
|
|
if (emoji) {
|
2020-03-31 11:47:22 +03:00
|
|
|
const canvas = document.createElement('canvas')
|
|
|
|
canvas.height = 64
|
|
|
|
canvas.width = 64
|
|
|
|
const ctx = canvas.getContext('2d')
|
|
|
|
ctx.font = '64px serif'
|
|
|
|
ctx.fillText(emoji, 0, 64)
|
|
|
|
favicon.href = canvas.toDataURL()
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:19:05 +03:00
|
|
|
// Click outside
|
2020-08-30 17:51:37 +03:00
|
|
|
window.addEventListener('click', function (event) {
|
2020-05-24 22:19:05 +03:00
|
|
|
if (event.target.classList.contains('popup')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
document.querySelectorAll('.popup').forEach(el => {
|
|
|
|
el.classList.remove('block')
|
|
|
|
el.classList.add('hidden')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-31 11:47:22 +03:00
|
|
|
window.addEventListener('load', function () {
|
2021-01-05 14:41:01 +03:00
|
|
|
topNPickers.forEach(e => e.addEventListener('change', () => {
|
|
|
|
parseTopN()
|
|
|
|
draw([parseInt(e.attributes['data-entity'].value)])
|
|
|
|
}))
|
|
|
|
|
|
|
|
parseTopN()
|
2020-08-30 17:51:37 +03:00
|
|
|
togglePlaceholders(getPresentDataMask())
|
2020-02-20 16:28:55 +03:00
|
|
|
draw()
|
2020-10-25 09:22:10 +03:00
|
|
|
})
|
2020-10-26 05:00:24 +03:00
|
|
|
|