mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
fix: make stats endpoint default to user-chosen time range (resolve #508)
chore: include more properties in status model for better compatibility
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/muety/wakapi/helpers"
|
||||
"github.com/muety/wakapi/models"
|
||||
"math"
|
||||
"time"
|
||||
@@ -14,19 +15,26 @@ type StatsViewModel struct {
|
||||
}
|
||||
|
||||
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"`
|
||||
Branches []*SummariesEntry `json:"branches,omitempty"`
|
||||
Username string `json:"username"`
|
||||
UserId string `json:"user_id"`
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
Status string `json:"status"`
|
||||
TotalSeconds float64 `json:"total_seconds"`
|
||||
DailyAverage float64 `json:"daily_average"`
|
||||
DaysIncludingHolidays int `json:"days_including_holidays"`
|
||||
Range string `json:"range"`
|
||||
HumanReadableRange string `json:"human_readable_range"`
|
||||
HumanReadableTotal string `json:"human_readable_total"`
|
||||
HumanReadableDailyAverage string `json:"human_readable_daily_average"`
|
||||
IsCodingActivityVisible bool `json:"is_coding_activity_visible"`
|
||||
IsOtherUsageVisible bool `json:"is_other_usage_visible"`
|
||||
Editors []*SummariesEntry `json:"editors"`
|
||||
Languages []*SummariesEntry `json:"languages"`
|
||||
Machines []*SummariesEntry `json:"machines"`
|
||||
Projects []*SummariesEntry `json:"projects"`
|
||||
OperatingSystems []*SummariesEntry `json:"operating_systems"`
|
||||
Branches []*SummariesEntry `json:"branches,omitempty"`
|
||||
}
|
||||
|
||||
func NewStatsFrom(summary *models.Summary, filters *models.Filters) *StatsViewModel {
|
||||
@@ -38,11 +46,16 @@ func NewStatsFrom(summary *models.Summary, filters *models.Filters) *StatsViewMo
|
||||
UserId: summary.UserID,
|
||||
Start: summary.FromTime.T(),
|
||||
End: summary.ToTime.T(),
|
||||
Status: "ok",
|
||||
TotalSeconds: totalTime.Seconds(),
|
||||
DailyAverage: totalTime.Seconds() / float64(numDays),
|
||||
DaysIncludingHolidays: numDays,
|
||||
HumanReadableTotal: helpers.FmtWakatimeDuration(totalTime),
|
||||
}
|
||||
|
||||
if numDays > 0 {
|
||||
data.DailyAverage = totalTime.Seconds() / float64(numDays)
|
||||
data.HumanReadableDailyAverage = helpers.FmtWakatimeDuration(totalTime / time.Duration(numDays))
|
||||
}
|
||||
if math.IsInf(data.DailyAverage, 0) || math.IsNaN(data.DailyAverage) {
|
||||
data.DailyAverage = 0
|
||||
}
|
||||
|
@@ -1,27 +1,33 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Support Wakapi and WakaTime range / interval identifiers
|
||||
// See https://wakatime.com/developers/#summaries
|
||||
var (
|
||||
IntervalToday = &IntervalKey{"today", "Today"}
|
||||
IntervalYesterday = &IntervalKey{"day", "yesterday", "Yesterday"}
|
||||
IntervalPastDay = &IntervalKey{"24_hours", "last_24_hours", "last_day", "Last 24 Hours"} // non-official one
|
||||
IntervalThisWeek = &IntervalKey{"week", "This Week"}
|
||||
IntervalLastWeek = &IntervalKey{"Last Week"}
|
||||
IntervalLastWeek = &IntervalKey{"last_week", "Last Week"}
|
||||
IntervalThisMonth = &IntervalKey{"month", "This Month"}
|
||||
IntervalLastMonth = &IntervalKey{"Last Month"}
|
||||
IntervalThisYear = &IntervalKey{"year"}
|
||||
IntervalLastMonth = &IntervalKey{"last_month", "Last Month"}
|
||||
IntervalThisYear = &IntervalKey{"year", "This Year"}
|
||||
IntervalPast7Days = &IntervalKey{"7_days", "last_7_days", "Last 7 Days"}
|
||||
IntervalPast7DaysYesterday = &IntervalKey{"Last 7 Days from Yesterday"}
|
||||
IntervalPast14Days = &IntervalKey{"Last 14 Days"}
|
||||
IntervalPast14Days = &IntervalKey{"14_days", "last_14_days", "Last 14 Days"}
|
||||
IntervalPast30Days = &IntervalKey{"30_days", "last_30_days", "Last 30 Days"}
|
||||
IntervalPast6Months = &IntervalKey{"6_months", "last_6_months"}
|
||||
IntervalPast12Months = &IntervalKey{"12_months", "last_12_months", "last_year"}
|
||||
IntervalAny = &IntervalKey{"any", "all_time"}
|
||||
IntervalPast6Months = &IntervalKey{"6_months", "last_6_months", "Last 6 Months"}
|
||||
IntervalPast12Months = &IntervalKey{"12_months", "last_12_months", "last_year", "Last 12 Months"}
|
||||
IntervalAny = &IntervalKey{"any", "all_time", "All Time"}
|
||||
)
|
||||
|
||||
var AllIntervals = []*IntervalKey{
|
||||
IntervalToday,
|
||||
IntervalYesterday,
|
||||
IntervalPastDay,
|
||||
IntervalThisWeek,
|
||||
IntervalLastWeek,
|
||||
IntervalThisMonth,
|
||||
@@ -46,3 +52,12 @@ func (k *IntervalKey) HasAlias(s string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (k *IntervalKey) GetHumanReadable() string {
|
||||
for _, s := range *k {
|
||||
if unicode.IsUpper(rune(s[0])) {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
@@ -163,6 +163,10 @@ func (u *User) MinDataAge() time.Time {
|
||||
return time.Now().AddDate(0, -retentionMonths, 0)
|
||||
}
|
||||
|
||||
func (u *User) AnyDataShared() bool {
|
||||
return u.ShareDataMaxDays != 0 && (u.ShareEditors || u.ShareLanguages || u.ShareProjects || u.ShareOSs || u.ShareMachines || u.ShareLabels)
|
||||
}
|
||||
|
||||
func (c *CredentialsReset) IsValid() bool {
|
||||
return ValidatePassword(c.PasswordNew) &&
|
||||
c.PasswordNew == c.PasswordRepeat
|
||||
|
Reference in New Issue
Block a user