2
0

pipelineservice.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package pricingmodel
  2. import (
  3. "net/http"
  4. "github.com/julienschmidt/httprouter"
  5. proto "github.com/opencost/opencost/core/pkg/protocol"
  6. )
  7. var protocol = proto.HTTP()
  8. // PipelineService exposes HTTP handlers for controlling and observing the pricing model pipeline.
  9. type PipelineService struct {
  10. pipeline *Pipeline
  11. }
  12. // NewPipelineService creates a PipelineService wrapping the given Pipeline.
  13. func NewPipelineService(pipeline *Pipeline) *PipelineService {
  14. return &PipelineService{pipeline: pipeline}
  15. }
  16. // GetStatusHandler returns an HTTP handler that serializes the status of all runners.
  17. func (s *PipelineService) GetStatusHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  18. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  19. w.Header().Set("Content-Type", "application/json")
  20. protocol.WriteData(w, s.pipeline.Status())
  21. }
  22. }
  23. // GetRebuildHandler returns an HTTP handler that triggers an immediate export
  24. // outside the scheduled tick. If the "sourceKey" query parameter is provided,
  25. // only that source is rebuilt; otherwise all sources are rebuilt.
  26. func (s *PipelineService) GetRebuildHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  27. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  28. sourceKey := r.URL.Query().Get("sourceKey")
  29. if sourceKey == "" {
  30. s.pipeline.Rebuild()
  31. protocol.WriteData(w, "Rebuild triggered for all pricing sources")
  32. return
  33. }
  34. if err := s.pipeline.RebuildSource(sourceKey); err != nil {
  35. http.Error(w, err.Error(), http.StatusBadRequest)
  36. return
  37. }
  38. protocol.WriteData(w, "Rebuild triggered for source: "+sourceKey)
  39. }
  40. }