api_report.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package app
  2. import (
  3. "net/http"
  4. "time"
  5. "context"
  6. "github.com/weaveworks/scope/report"
  7. )
  8. // Raw report handler
  9. func makeRawReportHandler(rep Reporter) CtxHandlerFunc {
  10. return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  11. timestamp := deserializeTimestamp(r.URL.Query().Get("timestamp"))
  12. rawReport, err := rep.Report(ctx, timestamp)
  13. if err != nil {
  14. respondWith(ctx, w, http.StatusInternalServerError, err)
  15. return
  16. }
  17. censorCfg := report.GetCensorConfigFromRequest(r)
  18. respondWithReport(ctx, w, r, report.CensorRawReport(rawReport, censorCfg))
  19. }
  20. }
  21. type probeDesc struct {
  22. ID string `json:"id"`
  23. Hostname string `json:"hostname"`
  24. Version string `json:"version"`
  25. LastSeen time.Time `json:"lastSeen"`
  26. }
  27. // Probe handler
  28. func makeProbeHandler(rep Reporter) CtxHandlerFunc {
  29. return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  30. r.ParseForm()
  31. if _, sparse := r.Form["sparse"]; sparse {
  32. // if we have reports, we must have connected probes
  33. hasProbes, err := rep.HasReports(ctx, time.Now())
  34. if err != nil {
  35. respondWith(ctx, w, http.StatusInternalServerError, err)
  36. }
  37. respondWith(ctx, w, http.StatusOK, hasProbes)
  38. return
  39. }
  40. rpt, err := rep.Report(ctx, time.Now())
  41. if err != nil {
  42. respondWith(ctx, w, http.StatusInternalServerError, err)
  43. return
  44. }
  45. result := []probeDesc{}
  46. for _, n := range rpt.Host.Nodes {
  47. id, _ := n.Latest.Lookup(report.ControlProbeID)
  48. hostname, _ := n.Latest.Lookup(report.HostName)
  49. version, dt, _ := n.Latest.LookupEntry(report.ScopeVersion)
  50. result = append(result, probeDesc{
  51. ID: id,
  52. Hostname: hostname,
  53. Version: version,
  54. LastSeen: dt,
  55. })
  56. }
  57. respondWith(ctx, w, http.StatusOK, result)
  58. }
  59. }