status_web.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. LastEnded textTime `json:"last_ended"`
  41. LastEndedTs stampTime `json:"last_ended_ts"`
  42. Scheduled textTime `json:"next_schedule"`
  43. ScheduledTs stampTime `json:"next_schedule_ts"`
  44. Upstream string `json:"upstream"`
  45. Size string `json:"size"` // approximate size
  46. }
  47. func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus {
  48. return WebMirrorStatus{
  49. Name: m.Name,
  50. IsMaster: m.IsMaster,
  51. Status: m.Status,
  52. LastUpdate: textTime{m.LastUpdate},
  53. LastUpdateTs: stampTime{m.LastUpdate},
  54. LastEnded: textTime{m.LastEnded},
  55. LastEndedTs: stampTime{m.LastEnded},
  56. Scheduled: textTime{m.Scheduled},
  57. ScheduledTs: stampTime{m.Scheduled},
  58. Upstream: m.Upstream,
  59. Size: m.Size,
  60. }
  61. }