2020-09-12 00:24:51 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2020-09-12 00:24:51 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"math"
|
2020-09-12 01:20:16 +03:00
|
|
|
"sync"
|
2020-09-12 00:24:51 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://wakatime.com/developers#summaries
|
|
|
|
// https://pastr.de/v/736450
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
type SummariesViewModel struct {
|
2022-11-13 21:52:15 +03:00
|
|
|
Data []*SummariesData `json:"data"`
|
|
|
|
End time.Time `json:"end"`
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
CumulativeTotal *SummariesCumulativeTotal `json:"cummulative_total"` // typo is intended
|
|
|
|
}
|
|
|
|
|
|
|
|
type SummariesCumulativeTotal struct {
|
|
|
|
Decimal string `json:"decimal"`
|
|
|
|
Digital string `json:"digital"`
|
|
|
|
Seconds float64 `json:"seconds"`
|
|
|
|
Text string `json:"text"`
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
type SummariesData struct {
|
|
|
|
Categories []*SummariesEntry `json:"categories"`
|
|
|
|
Dependencies []*SummariesEntry `json:"dependencies"`
|
|
|
|
Editors []*SummariesEntry `json:"editors"`
|
|
|
|
Languages []*SummariesEntry `json:"languages"`
|
|
|
|
Machines []*SummariesEntry `json:"machines"`
|
|
|
|
OperatingSystems []*SummariesEntry `json:"operating_systems"`
|
|
|
|
Projects []*SummariesEntry `json:"projects"`
|
2022-01-02 23:21:29 +03:00
|
|
|
Branches []*SummariesEntry `json:"branches,omitempty"`
|
2023-03-22 22:45:27 +03:00
|
|
|
Entities []*SummariesEntry `json:"entities,omitempty"`
|
2021-02-05 20:47:28 +03:00
|
|
|
GrandTotal *SummariesGrandTotal `json:"grand_total"`
|
|
|
|
Range *SummariesRange `json:"range"`
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
type SummariesEntry struct {
|
2020-09-12 00:24:51 +03:00
|
|
|
Digital string `json:"digital"`
|
|
|
|
Hours int `json:"hours"`
|
|
|
|
Minutes int `json:"minutes"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Percent float64 `json:"percent"`
|
|
|
|
Seconds int `json:"seconds"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
TotalSeconds float64 `json:"total_seconds"`
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
type SummariesGrandTotal struct {
|
2020-09-12 00:24:51 +03:00
|
|
|
Digital string `json:"digital"`
|
|
|
|
Hours int `json:"hours"`
|
|
|
|
Minutes int `json:"minutes"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
TotalSeconds float64 `json:"total_seconds"`
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
type SummariesRange struct {
|
2020-09-12 00:24:51 +03:00
|
|
|
Date string `json:"date"`
|
|
|
|
End time.Time `json:"end"`
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Timezone string `json:"timezone"`
|
|
|
|
}
|
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
func NewSummariesFrom(summaries []*models.Summary) *SummariesViewModel {
|
2021-02-05 20:47:28 +03:00
|
|
|
data := make([]*SummariesData, len(summaries))
|
2020-09-12 00:24:51 +03:00
|
|
|
minDate, maxDate := time.Now().Add(1*time.Second), time.Time{}
|
|
|
|
|
|
|
|
for i, s := range summaries {
|
|
|
|
data[i] = newDataFrom(s)
|
|
|
|
|
2020-10-16 15:49:22 +03:00
|
|
|
if s.FromTime.T().Before(minDate) {
|
|
|
|
minDate = s.FromTime.T()
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
2020-10-16 15:49:22 +03:00
|
|
|
if s.ToTime.T().After(maxDate) {
|
|
|
|
maxDate = s.ToTime.T()
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:52:15 +03:00
|
|
|
var totalTime time.Duration
|
|
|
|
for _, s := range summaries {
|
|
|
|
totalTime += s.TotalTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
totalHrs, totalMins, totalSecs := totalTime.Hours(), (totalTime - time.Duration(totalTime.Hours())*time.Hour).Minutes(), totalTime.Seconds()
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
return &SummariesViewModel{
|
2020-09-12 00:24:51 +03:00
|
|
|
Data: data,
|
|
|
|
End: maxDate,
|
|
|
|
Start: minDate,
|
2022-11-13 21:52:15 +03:00
|
|
|
CumulativeTotal: &SummariesCumulativeTotal{
|
|
|
|
Decimal: fmt.Sprintf("%.2f", totalHrs),
|
|
|
|
Digital: fmt.Sprintf("%d:%d", int(totalHrs), int(totalMins)),
|
|
|
|
Seconds: totalSecs,
|
2022-12-01 12:57:07 +03:00
|
|
|
Text: helpers.FmtWakatimeDuration(totalTime),
|
2022-11-13 21:52:15 +03:00
|
|
|
},
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func newDataFrom(s *models.Summary) *SummariesData {
|
2020-09-12 00:24:51 +03:00
|
|
|
zone, _ := time.Now().Zone()
|
|
|
|
total := s.TotalTime()
|
|
|
|
totalHrs, totalMins := int(total.Hours()), int((total - time.Duration(total.Hours())*time.Hour).Minutes())
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
data := &SummariesData{
|
|
|
|
Categories: make([]*SummariesEntry, 0),
|
|
|
|
Dependencies: make([]*SummariesEntry, 0),
|
|
|
|
Editors: make([]*SummariesEntry, len(s.Editors)),
|
|
|
|
Languages: make([]*SummariesEntry, len(s.Languages)),
|
|
|
|
Machines: make([]*SummariesEntry, len(s.Machines)),
|
|
|
|
OperatingSystems: make([]*SummariesEntry, len(s.OperatingSystems)),
|
|
|
|
Projects: make([]*SummariesEntry, len(s.Projects)),
|
2022-01-02 15:39:20 +03:00
|
|
|
Branches: make([]*SummariesEntry, len(s.Branches)),
|
2021-02-05 20:47:28 +03:00
|
|
|
GrandTotal: &SummariesGrandTotal{
|
2020-09-12 00:24:51 +03:00
|
|
|
Digital: fmt.Sprintf("%d:%d", totalHrs, totalMins),
|
|
|
|
Hours: totalHrs,
|
|
|
|
Minutes: totalMins,
|
2022-12-01 12:57:07 +03:00
|
|
|
Text: helpers.FmtWakatimeDuration(total),
|
2020-09-12 00:24:51 +03:00
|
|
|
TotalSeconds: total.Seconds(),
|
|
|
|
},
|
2021-02-05 20:47:28 +03:00
|
|
|
Range: &SummariesRange{
|
2020-09-12 00:24:51 +03:00
|
|
|
Date: time.Now().Format(time.RFC3339),
|
2020-10-16 15:49:22 +03:00
|
|
|
End: s.ToTime.T(),
|
|
|
|
Start: s.FromTime.T(),
|
2020-09-12 00:24:51 +03:00
|
|
|
Text: "",
|
|
|
|
Timezone: zone,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:20:16 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2021-02-05 20:47:28 +03:00
|
|
|
go func(data *SummariesData) {
|
2020-09-12 01:20:16 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Projects {
|
|
|
|
data.Projects[i] = convertEntry(e, s.TotalTimeBy(models.SummaryProject))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2021-02-05 20:47:28 +03:00
|
|
|
go func(data *SummariesData) {
|
2020-09-12 01:20:16 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Editors {
|
|
|
|
data.Editors[i] = convertEntry(e, s.TotalTimeBy(models.SummaryEditor))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2021-02-05 20:47:28 +03:00
|
|
|
go func(data *SummariesData) {
|
2020-09-12 01:20:16 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Languages {
|
|
|
|
data.Languages[i] = convertEntry(e, s.TotalTimeBy(models.SummaryLanguage))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2021-02-05 20:47:28 +03:00
|
|
|
go func(data *SummariesData) {
|
2020-09-12 01:20:16 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.OperatingSystems {
|
|
|
|
data.OperatingSystems[i] = convertEntry(e, s.TotalTimeBy(models.SummaryOS))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2021-02-05 20:47:28 +03:00
|
|
|
go func(data *SummariesData) {
|
2020-09-12 01:20:16 +03:00
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Machines {
|
|
|
|
data.Machines[i] = convertEntry(e, s.TotalTimeBy(models.SummaryMachine))
|
|
|
|
}
|
|
|
|
}(data)
|
2020-09-12 00:24:51 +03:00
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
2022-01-02 15:39:20 +03:00
|
|
|
go func(data *SummariesData) {
|
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Branches {
|
|
|
|
data.Branches[i] = convertEntry(e, s.TotalTimeBy(models.SummaryBranch))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2023-03-22 22:45:27 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(data *SummariesData) {
|
|
|
|
defer wg.Done()
|
|
|
|
for i, e := range s.Entities {
|
|
|
|
data.Entities[i] = convertEntry(e, s.TotalTimeBy(models.SummaryEntity))
|
|
|
|
}
|
|
|
|
}(data)
|
|
|
|
|
2022-01-02 15:39:20 +03:00
|
|
|
if s.Branches == nil {
|
|
|
|
data.Branches = nil
|
|
|
|
}
|
2023-03-22 22:45:27 +03:00
|
|
|
if s.Entities == nil {
|
|
|
|
data.Entities = nil
|
|
|
|
}
|
2022-01-02 15:39:20 +03:00
|
|
|
|
2020-09-12 01:20:16 +03:00
|
|
|
wg.Wait()
|
2020-09-12 00:24:51 +03:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func convertEntry(e *models.SummaryItem, entityTotal time.Duration) *SummariesEntry {
|
2021-04-30 15:07:14 +03:00
|
|
|
total := e.TotalFixed()
|
2020-09-12 00:24:51 +03:00
|
|
|
hrs := int(total.Hours())
|
|
|
|
mins := int((total - time.Duration(hrs)*time.Hour).Minutes())
|
|
|
|
secs := int((total - time.Duration(hrs)*time.Hour - time.Duration(mins)*time.Minute).Seconds())
|
2020-10-16 16:16:43 +03:00
|
|
|
percentage := math.Round((total.Seconds()/entityTotal.Seconds())*1e4) / 100
|
|
|
|
if math.IsNaN(percentage) || math.IsInf(percentage, 0) {
|
|
|
|
percentage = 0
|
|
|
|
}
|
2020-09-12 00:24:51 +03:00
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
return &SummariesEntry{
|
2020-09-12 00:24:51 +03:00
|
|
|
Digital: fmt.Sprintf("%d:%d:%d", hrs, mins, secs),
|
|
|
|
Hours: hrs,
|
|
|
|
Minutes: mins,
|
|
|
|
Name: e.Key,
|
2020-10-16 16:16:43 +03:00
|
|
|
Percent: percentage,
|
2020-09-12 00:24:51 +03:00
|
|
|
Seconds: secs,
|
2022-12-01 12:57:07 +03:00
|
|
|
Text: helpers.FmtWakatimeDuration(total),
|
2020-09-12 00:24:51 +03:00
|
|
|
TotalSeconds: total.Seconds(),
|
|
|
|
}
|
|
|
|
}
|