mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Properly redraw charts.
Add pre-defined interval for today. Fix week interval.
This commit is contained in:
parent
e23e7520d0
commit
899b2fff64
@ -13,6 +13,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
IntervalToday string = "today"
|
||||
IntervalLastDay string = "day"
|
||||
IntervalLastWeek string = "week"
|
||||
IntervalLastMonth string = "month"
|
||||
@ -44,10 +45,12 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user := r.Context().Value(models.UserKey).(*models.User)
|
||||
params := r.URL.Query()
|
||||
interval := params.Get("interval")
|
||||
from, err := utils.ParseDate(params.Get("from"))
|
||||
if err != nil {
|
||||
interval := params.Get("interval")
|
||||
switch interval {
|
||||
case IntervalToday:
|
||||
from = utils.StartOfDay()
|
||||
case IntervalLastDay:
|
||||
from = utils.StartOfDay().Add(-24 * time.Hour)
|
||||
case IntervalLastWeek:
|
||||
@ -63,7 +66,7 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
live := params.Get("live") != "" && params.Get("live") != "false"
|
||||
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
|
||||
to := utils.StartOfDay()
|
||||
if live {
|
||||
to = time.Now()
|
||||
|
@ -56,7 +56,8 @@
|
||||
<input type="text" class="input" id="user-input" name="user" placeholder="Enter Username">
|
||||
<label for="pw">API Key: </label>
|
||||
<input type="password" class="input" id="password-input" name="pw" placeholder="Enter Password">
|
||||
<button onclick="load('day', true)">Day (live)</button>
|
||||
<button onclick="load('today', true)">Today (live)</button>
|
||||
<button onclick="load('day', false)">Day</button>
|
||||
<button onclick="load('week', false)">Week</button>
|
||||
<button onclick="load('month', false)">Month</button>
|
||||
<button onclick="load('year', false)">Year</button>
|
||||
@ -85,6 +86,13 @@
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
|
||||
|
||||
<script>
|
||||
const projectsCanvas = document.getElementById("chart-projects")
|
||||
const osCanvas = document.getElementById("chart-os")
|
||||
const editorsCanvas = document.getElementById("chart-editor")
|
||||
const languagesCanvas = document.getElementById("chart-language")
|
||||
|
||||
let charts = []
|
||||
|
||||
String.prototype.toHHMMSS = function () {
|
||||
var sec_num = parseInt(this, 10);
|
||||
var hours = Math.floor(sec_num / 3600);
|
||||
@ -101,6 +109,7 @@
|
||||
let user = document.getElementById('user-input').value
|
||||
let password = document.getElementById('password-input').value
|
||||
let hashed = btoa(`${user}:${password}`)
|
||||
|
||||
fetch(`${window.location.href}/api/summary?interval=${interval}&live=${live}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
@ -139,7 +148,9 @@
|
||||
}
|
||||
}
|
||||
|
||||
let projectChart = new Chart(document.getElementById("chart-projects").getContext('2d'), {
|
||||
charts.forEach(c => c.destroy())
|
||||
|
||||
let projectChart = new Chart(projectsCanvas.getContext('2d'), {
|
||||
type: 'horizontalBar',
|
||||
data: {
|
||||
datasets: data.projects
|
||||
@ -160,7 +171,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
let osChart = new Chart(document.getElementById("chart-os").getContext('2d'), {
|
||||
let osChart = new Chart(osCanvas.getContext('2d'), {
|
||||
type: 'pie',
|
||||
data: {
|
||||
datasets: [{
|
||||
@ -175,7 +186,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
let editorChart = new Chart(document.getElementById("chart-editor").getContext('2d'), {
|
||||
let editorChart = new Chart(editorsCanvas.getContext('2d'), {
|
||||
type: 'pie',
|
||||
data: {
|
||||
datasets: [{
|
||||
@ -190,7 +201,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
let languageChart = new Chart(document.getElementById("chart-language").getContext('2d'), {
|
||||
let languageChart = new Chart(languagesCanvas.getContext('2d'), {
|
||||
type: 'pie',
|
||||
data: {
|
||||
datasets: [{
|
||||
@ -209,6 +220,8 @@
|
||||
|
||||
getTotal(data.operating_systems)
|
||||
document.getElementById('grid-container').style.visibility = 'visible';
|
||||
|
||||
charts = [projectChart, osChart, editorChart, languageChart]
|
||||
}
|
||||
|
||||
function getRandomColor(seed) {
|
||||
|
@ -9,7 +9,8 @@ func StartOfDay() time.Time {
|
||||
|
||||
func StartOfWeek() time.Time {
|
||||
ref := time.Now()
|
||||
return firstDayOfISOWeek(ref.Year(), ref.Day(), ref.Location())
|
||||
year, week := ref.ISOWeek()
|
||||
return firstDayOfISOWeek(year, week, ref.Location())
|
||||
}
|
||||
|
||||
func StartOfMonth() time.Time {
|
||||
|
Loading…
Reference in New Issue
Block a user