status_web.go 1.4 KB

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