2
0

status_web.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. Upstream string `json:"upstream"`
  43. Size string `json:"size"` // approximate size
  44. }
  45. func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus {
  46. return WebMirrorStatus{
  47. Name: m.Name,
  48. IsMaster: m.IsMaster,
  49. Status: m.Status,
  50. LastUpdate: textTime{m.LastUpdate},
  51. LastUpdateTs: stampTime{m.LastUpdate},
  52. LastEnded: textTime{m.LastEnded},
  53. LastEndedTs: stampTime{m.LastEnded},
  54. Upstream: m.Upstream,
  55. Size: m.Size,
  56. }
  57. }