chore: minor fixes

This commit is contained in:
Ferdinand Mütsch 2022-02-13 10:59:14 +01:00
parent e7f3432113
commit e21788b8b5
8 changed files with 633 additions and 577 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@ package v1
import ( import (
"strconv" "strconv"
"time"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
) )
@ -14,27 +15,26 @@ type HeartbeatsViewModel struct {
// that is actually required for the import // that is actually required for the import
type HeartbeatEntry struct { type HeartbeatEntry struct {
Id string `json:"id"` Id string `json:"id"`
Branch string `json:"branch"` Branch string `json:"branch"`
Category string `json:"category"` Category string `json:"category"`
Entity string `json:"entity"` Entity string `json:"entity"`
IsWrite bool `json:"is_write"` IsWrite bool `json:"is_write"`
Language string `json:"language"` Language string `json:"language"`
Project string `json:"project"` Project string `json:"project"`
Time float64 `json:"time"` Time float64 `json:"time"`
Type string `json:"type"` Type string `json:"type"`
UserId string `json:"user_id"` UserId string `json:"user_id"`
MachineNameId string `json:"machine_name_id"` MachineNameId string `json:"machine_name_id"`
UserAgentId string `json:"user_agent_id"` UserAgentId string `json:"user_agent_id"`
CreatedAt models.CustomTime `json:"created_at"` CreatedAt time.Time `json:"created_at"`
ModifiedAt models.CustomTime `json:"created_at",omitempty`
} }
func ToHeartbeatEntry(entries []*models.Heartbeat) []HeartbeatEntry { func HeartbeatsToCompat(entries []*models.Heartbeat) []*HeartbeatEntry {
out := make([]HeartbeatEntry, len(entries)) out := make([]*HeartbeatEntry, len(entries))
for i := 0; i < len(entries); i++ { for i := 0; i < len(entries); i++ {
entry := entries[i] entry := entries[i]
out[i] = HeartbeatEntry{ out[i] = &HeartbeatEntry{
Id: strconv.FormatUint(entry.ID, 10), Id: strconv.FormatUint(entry.ID, 10),
Branch: entry.Branch, Branch: entry.Branch,
Category: entry.Category, Category: entry.Category,
@ -47,7 +47,7 @@ func ToHeartbeatEntry(entries []*models.Heartbeat) []HeartbeatEntry {
UserId: entry.UserID, UserId: entry.UserID,
MachineNameId: entry.Machine, MachineNameId: entry.Machine,
UserAgentId: entry.UserAgent, UserAgentId: entry.UserAgent,
CreatedAt: entry.CreatedAt, CreatedAt: entry.CreatedAt.T(),
} }
} }
return out return out

View File

@ -14,10 +14,10 @@ import (
) )
type HeartbeatsResult struct { type HeartbeatsResult struct {
Data []wakatime.HeartbeatEntry `json:"data"` Data []*wakatime.HeartbeatEntry `json:"data"`
End string `json:"end"` End string `json:"end"`
Start string `json:"start"` Start string `json:"start"`
Timezone string `json:"timezone"` Timezone string `json:"timezone"`
} }
type HeartbeatHandler struct { type HeartbeatHandler struct {
@ -46,7 +46,7 @@ func (h *HeartbeatHandler) RegisterRoutes(router *mux.Router) {
// @Param date query string true "Date" // @Param date query string true "Date"
// @Param user path string true "Username (or current)" // @Param user path string true "Username (or current)"
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Success 200 {object} v1.HeartbeatEntry // @Success 200 {object} HeartbeatsResult
// @Failure 400 {string} string "bad date" // @Failure 400 {string} string "bad date"
// @Router /compat/wakatime/v1/users/{user}/heartbeats [get] // @Router /compat/wakatime/v1/users/{user}/heartbeats [get]
func (h *HeartbeatHandler) Get(w http.ResponseWriter, r *http.Request) { func (h *HeartbeatHandler) Get(w http.ResponseWriter, r *http.Request) {
@ -76,7 +76,7 @@ func (h *HeartbeatHandler) Get(w http.ResponseWriter, r *http.Request) {
} }
res := HeartbeatsResult{ res := HeartbeatsResult{
Data: wakatime.ToHeartbeatEntry(heartbeats), Data: wakatime.HeartbeatsToCompat(heartbeats),
Start: rangeFrom.UTC().Format(time.RFC3339), Start: rangeFrom.UTC().Format(time.RFC3339),
End: rangeTo.UTC().Format(time.RFC3339), End: rangeTo.UTC().Format(time.RFC3339),
Timezone: timezone.String(), Timezone: timezone.String(),

View File

@ -288,6 +288,7 @@ func mapHeartbeat(
Time: models.CustomTime(time.Unix(0, int64(entry.Time*1e9))), Time: models.CustomTime(time.Unix(0, int64(entry.Time*1e9))),
Origin: OriginWakatime, Origin: OriginWakatime,
OriginId: entry.Id, OriginId: entry.Id,
CreatedAt: models.CustomTime(entry.CreatedAt),
}).Hashed() }).Hashed()
} }

View File

@ -1,13 +1,14 @@
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag // This file was generated by swaggo/swag
package docs package docs
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"strings" "strings"
"text/template"
"github.com/alecthomas/template"
"github.com/swaggo/swag" "github.com/swaggo/swag"
) )
@ -15,7 +16,7 @@ var doc = `{
"schemes": {{ marshal .Schemes }}, "schemes": {{ marshal .Schemes }},
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"description": "{{escape .Description}}", "description": "{{.Description}}",
"title": "{{.Title}}", "title": "{{.Title}}",
"contact": { "contact": {
"name": "Ferdinand Mütsch", "name": "Ferdinand Mütsch",
@ -191,7 +192,7 @@ var doc = `{
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/v1.HeartbeatEntry" "$ref": "#/definitions/v1.HeartbeatsResult"
} }
}, },
"400": { "400": {
@ -1340,6 +1341,9 @@ var doc = `{
"machine_name_id": { "machine_name_id": {
"type": "string" "type": "string"
}, },
"modified_at": {
"type": "string"
},
"project": { "project": {
"type": "string" "type": "string"
}, },
@ -1357,6 +1361,26 @@ var doc = `{
} }
} }
}, },
"v1.HeartbeatsResult": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/v1.HeartbeatEntry"
}
},
"end": {
"type": "string"
},
"start": {
"type": "string"
},
"timezone": {
"type": "string"
}
}
},
"v1.Project": { "v1.Project": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1703,13 +1727,6 @@ func (s *s) ReadDoc() string {
a, _ := json.Marshal(v) a, _ := json.Marshal(v)
return string(a) return string(a)
}, },
"escape": func(v interface{}) string {
// escape tabs
str := strings.Replace(v.(string), "\t", "\\t", -1)
// replace " with \", and if that results in \\", replace that with \\\"
str = strings.Replace(str, "\"", "\\\"", -1)
return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1)
},
}).Parse(doc) }).Parse(doc)
if err != nil { if err != nil {
return doc return doc
@ -1724,5 +1741,5 @@ func (s *s) ReadDoc() string {
} }
func init() { func init() {
swag.Register("swagger", &s{}) swag.Register(swag.Name, &s{})
} }

View File

@ -176,7 +176,7 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/v1.HeartbeatEntry" "$ref": "#/definitions/v1.HeartbeatsResult"
} }
}, },
"400": { "400": {
@ -1325,6 +1325,9 @@
"machine_name_id": { "machine_name_id": {
"type": "string" "type": "string"
}, },
"modified_at": {
"type": "string"
},
"project": { "project": {
"type": "string" "type": "string"
}, },
@ -1342,6 +1345,26 @@
} }
} }
}, },
"v1.HeartbeatsResult": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/v1.HeartbeatEntry"
}
},
"end": {
"type": "string"
},
"start": {
"type": "string"
},
"timezone": {
"type": "string"
}
}
},
"v1.Project": { "v1.Project": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -166,6 +166,8 @@ definitions:
type: string type: string
machine_name_id: machine_name_id:
type: string type: string
modified_at:
type: string
project: project:
type: string type: string
time: time:
@ -177,6 +179,19 @@ definitions:
user_id: user_id:
type: string type: string
type: object type: object
v1.HeartbeatsResult:
properties:
data:
items:
$ref: '#/definitions/v1.HeartbeatEntry'
type: array
end:
type: string
start:
type: string
timezone:
type: string
type: object
v1.Project: v1.Project:
properties: properties:
id: id:
@ -501,7 +516,7 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/v1.HeartbeatEntry' $ref: '#/definitions/v1.HeartbeatsResult'
"400": "400":
description: bad date description: bad date
schema: schema:

View File

@ -1 +1 @@
2.1.0 2.2.0