| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package pricingmodel
- import (
- "net/http"
- "github.com/julienschmidt/httprouter"
- proto "github.com/opencost/opencost/core/pkg/protocol"
- )
- var protocol = proto.HTTP()
- // PipelineService exposes HTTP handlers for controlling and observing the pricing model pipeline.
- type PipelineService struct {
- pipeline *Pipeline
- }
- // NewPipelineService creates a PipelineService wrapping the given Pipeline.
- func NewPipelineService(pipeline *Pipeline) *PipelineService {
- return &PipelineService{pipeline: pipeline}
- }
- // GetStatusHandler returns an HTTP handler that serializes the status of all runners.
- func (s *PipelineService) GetStatusHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- w.Header().Set("Content-Type", "application/json")
- protocol.WriteData(w, s.pipeline.Status())
- }
- }
- // GetRebuildHandler returns an HTTP handler that triggers an immediate export
- // outside the scheduled tick. If the "sourceKey" query parameter is provided,
- // only that source is rebuilt; otherwise all sources are rebuilt.
- func (s *PipelineService) GetRebuildHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- sourceKey := r.URL.Query().Get("sourceKey")
- if sourceKey == "" {
- s.pipeline.Rebuild()
- protocol.WriteData(w, "Rebuild triggered for all pricing sources")
- return
- }
- if err := s.pipeline.RebuildSource(sourceKey); err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- protocol.WriteData(w, "Rebuild triggered for source: "+sourceKey)
- }
- }
|