2
0

status.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package manager
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "time"
  6. . "github.com/tuna/tunasync/internal"
  7. )
  8. type textTime struct {
  9. time.Time
  10. }
  11. func (t textTime) MarshalJSON() ([]byte, error) {
  12. return json.Marshal(t.Format("2006-01-02 15:04:05"))
  13. }
  14. func (t *textTime) UnmarshalJSON(b []byte) error {
  15. s := string(b)
  16. t2, err := time.ParseInLocation(`"2006-01-02 15:04:05"`, s, time.Local)
  17. *t = textTime{t2}
  18. return err
  19. }
  20. type stampTime struct {
  21. time.Time
  22. }
  23. func (t stampTime) MarshalJSON() ([]byte, error) {
  24. return json.Marshal(t.Unix())
  25. }
  26. func (t *stampTime) UnmarshalJSON(b []byte) error {
  27. ts, err := strconv.Atoi(string(b))
  28. if err != nil {
  29. return err
  30. }
  31. *t = stampTime{time.Unix(int64(ts), 0)}
  32. return err
  33. }
  34. // webMirrorStatus is the mirror status to be shown in the web page
  35. type webMirrorStatus struct {
  36. Name string `json:"name"`
  37. IsMaster bool `json:"is_master"`
  38. Status SyncStatus `json:"status"`
  39. LastUpdate textTime `json:"last_update"`
  40. LastUpdateTs stampTime `json:"last_update_ts"`
  41. Upstream string `json:"upstream"`
  42. Size string `json:"size"` // approximate size
  43. }
  44. func convertMirrorStatus(m MirrorStatus) webMirrorStatus {
  45. return webMirrorStatus{
  46. Name: m.Name,
  47. IsMaster: m.IsMaster,
  48. Status: m.Status,
  49. LastUpdate: textTime{m.LastUpdate},
  50. LastUpdateTs: stampTime{m.LastUpdate},
  51. Upstream: m.Upstream,
  52. Size: m.Size,
  53. }
  54. }