Skip to content

Commit

Permalink
feat: add an upload diff endpoint for adhoc mode. (#839)
Browse files Browse the repository at this point in the history
This is part of #784.
  • Loading branch information
abeaumont committed Feb 16, 2022
1 parent 9f0fda1 commit 4a11f7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/adhoc/server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ func ConverterToFormat(f ConverterFn) string {
}
return "unknown"
}

type diffModel struct {
Base flamebearer.FlamebearerProfile `json:"base"`
Diff flamebearer.FlamebearerProfile `json:"diff"`
}
30 changes: 30 additions & 0 deletions pkg/adhoc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *server) AddRoutes(r *mux.Router) http.HandlerFunc {
r.HandleFunc("/v1/profile/{id:[0-9a-f]+}", s.Profile)
r.HandleFunc("/v1/diff/{left:[0-9a-f]+}/{right:[0-9a-f]+}", s.Diff)
r.HandleFunc("/v1/upload/", s.Upload)
r.HandleFunc("/v1/upload-diff/", s.UploadDiff)
}
return r.ServeHTTP
}
Expand Down Expand Up @@ -237,6 +238,35 @@ func (s *server) Upload(w http.ResponseWriter, r *http.Request) {
}
}

// Upload two single profiles in native JSON format and convert to a diff profile
func (s *server) UploadDiff(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)

var m diffModel
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
msg := "Unable to decode the body of the request. " +
"A JSON body with a a `base` and a `diff` profile field is expected."
s.log.WithError(err).Error(msg)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
r.Body.Close()

fb, err := DiffV1("", &m.Base, &m.Diff, s.maxNodes)
if err != nil {
s.log.WithError(err).Error("Unable to generate a diff profile")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(fb); err != nil {
s.log.WithError(err).Error("Unable to encode the response")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func (s *server) convert(p profile) (*flamebearer.FlamebearerProfile, error) {
fname := filepath.Join(s.dataDir, p.Name)
f, err := os.Open(fname)
Expand Down

0 comments on commit 4a11f7d

Please sign in to comment.