2021-02-06 18:05:34 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/muety/wakapi/models"
|
2021-04-26 22:26:23 +03:00
|
|
|
"math"
|
2021-02-06 18:05:34 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://wakatime.com/api/v1/users/current/stats/last_7_days
|
|
|
|
// https://pastr.de/p/f2fxg6ragj7z5e7fhsow9rb6
|
|
|
|
|
|
|
|
type StatsViewModel struct {
|
|
|
|
Data *StatsData `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatsData struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
UserId string `json:"user_id"`
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
End time.Time `json:"end"`
|
|
|
|
TotalSeconds float64 `json:"total_seconds"`
|
|
|
|
DailyAverage float64 `json:"daily_average"`
|
|
|
|
DaysIncludingHolidays int `json:"days_including_holidays"`
|
|
|
|
Editors []*SummariesEntry `json:"editors"`
|
|
|
|
Languages []*SummariesEntry `json:"languages"`
|
|
|
|
Machines []*SummariesEntry `json:"machines"`
|
|
|
|
Projects []*SummariesEntry `json:"projects"`
|
|
|
|
OperatingSystems []*SummariesEntry `json:"operating_systems"`
|
2022-01-02 23:21:29 +03:00
|
|
|
Branches []*SummariesEntry `json:"branches,omitempty"`
|
2021-02-06 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatsFrom(summary *models.Summary, filters *models.Filters) *StatsViewModel {
|
|
|
|
totalTime := summary.TotalTime()
|
|
|
|
numDays := int(summary.ToTime.T().Sub(summary.FromTime.T()).Hours() / 24)
|
|
|
|
|
|
|
|
data := &StatsData{
|
|
|
|
Username: summary.UserID,
|
|
|
|
UserId: summary.UserID,
|
|
|
|
Start: summary.FromTime.T(),
|
|
|
|
End: summary.ToTime.T(),
|
|
|
|
TotalSeconds: totalTime.Seconds(),
|
|
|
|
DailyAverage: totalTime.Seconds() / float64(numDays),
|
|
|
|
DaysIncludingHolidays: numDays,
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:32:26 +03:00
|
|
|
if math.IsInf(data.DailyAverage, 0) || math.IsNaN(data.DailyAverage) {
|
|
|
|
data.DailyAverage = 0
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:05:34 +03:00
|
|
|
editors := make([]*SummariesEntry, len(summary.Editors))
|
|
|
|
for i, e := range summary.Editors {
|
|
|
|
editors[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryEditor))
|
|
|
|
}
|
|
|
|
|
|
|
|
languages := make([]*SummariesEntry, len(summary.Languages))
|
|
|
|
for i, e := range summary.Languages {
|
|
|
|
languages[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryLanguage))
|
|
|
|
}
|
|
|
|
|
|
|
|
machines := make([]*SummariesEntry, len(summary.Machines))
|
|
|
|
for i, e := range summary.Machines {
|
|
|
|
machines[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryMachine))
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := make([]*SummariesEntry, len(summary.Projects))
|
|
|
|
for i, e := range summary.Projects {
|
|
|
|
projects[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryProject))
|
|
|
|
}
|
|
|
|
|
|
|
|
oss := make([]*SummariesEntry, len(summary.OperatingSystems))
|
|
|
|
for i, e := range summary.OperatingSystems {
|
|
|
|
oss[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryOS))
|
|
|
|
}
|
|
|
|
|
2022-01-02 15:39:20 +03:00
|
|
|
branches := make([]*SummariesEntry, len(summary.Branches))
|
|
|
|
for i, e := range summary.Branches {
|
|
|
|
branches[i] = convertEntry(e, summary.TotalTimeBy(models.SummaryBranch))
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:05:34 +03:00
|
|
|
data.Editors = editors
|
|
|
|
data.Languages = languages
|
|
|
|
data.Machines = machines
|
|
|
|
data.Projects = projects
|
|
|
|
data.OperatingSystems = oss
|
2022-01-02 15:39:20 +03:00
|
|
|
data.Branches = branches
|
|
|
|
|
|
|
|
if summary.Branches == nil {
|
|
|
|
data.Branches = nil
|
|
|
|
}
|
2021-02-06 18:05:34 +03:00
|
|
|
|
|
|
|
return &StatsViewModel{
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|