2021-12-11 08:31:58 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type wakatime struct {
|
2022-01-01 11:08:08 -08:00
|
|
|
props Properties
|
2022-01-01 11:09:52 -08:00
|
|
|
env Environment
|
2021-12-11 08:31:58 -08:00
|
|
|
|
|
|
|
wtData
|
|
|
|
}
|
|
|
|
|
|
|
|
type wtTotals struct {
|
|
|
|
Seconds float64 `json:"seconds"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type wtData struct {
|
|
|
|
CummulativeTotal wtTotals `json:"cummulative_total"`
|
|
|
|
Start string `json:"start"`
|
|
|
|
End string `json:"end"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wakatime) enabled() bool {
|
|
|
|
err := w.setAPIData()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wakatime) string() string {
|
|
|
|
segmentTemplate := w.props.getString(SegmentTemplate, "{{ secondsRound .CummulativeTotal.Seconds }}")
|
|
|
|
template := &textTemplate{
|
|
|
|
Template: segmentTemplate,
|
|
|
|
Context: w,
|
|
|
|
Env: w.env,
|
|
|
|
}
|
|
|
|
text, err := template.render()
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wakatime) setAPIData() error {
|
|
|
|
url := w.props.getString(URL, "")
|
|
|
|
cacheTimeout := w.props.getInt(CacheTimeout, DefaultCacheTimeout)
|
|
|
|
if cacheTimeout > 0 {
|
|
|
|
// check if data stored in cache
|
|
|
|
if val, found := w.env.cache().get(url); found {
|
|
|
|
err := json.Unmarshal([]byte(val), &w.wtData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httpTimeout := w.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
|
|
|
|
|
2022-01-07 10:41:58 -08:00
|
|
|
body, err := w.env.HTTPRequest(url, httpTimeout)
|
2021-12-11 08:31:58 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &w.wtData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cacheTimeout > 0 {
|
|
|
|
w.env.cache().set(url, string(body), cacheTimeout)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-01 11:09:52 -08:00
|
|
|
func (w *wakatime) init(props Properties, env Environment) {
|
2021-12-11 08:31:58 -08:00
|
|
|
w.props = props
|
|
|
|
w.env = env
|
|
|
|
}
|