Skip to content

Commit

Permalink
Export filename (#2697)
Browse files Browse the repository at this point in the history
* export filename

* download filename

* download filename

* download filename

* download filename

---------

Co-authored-by: Egor Ryashin <[email protected]>
  • Loading branch information
egor-ryashin and Egor Ryashin committed Jun 29, 2023
1 parent 1c3614d commit ab2dc9d
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions runtime/server/downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"reflect"
"strings"
"time"

runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
Expand Down Expand Up @@ -55,21 +57,54 @@ func (s *Server) downloadHandler(w http.ResponseWriter, req *http.Request) {
}

var q runtime.Query
var filename string
switch v := request.Request.(type) {
case *runtimev1.ExportRequest_MetricsViewToplistRequest:
v.MetricsViewToplistRequest.Limit = int64(request.Limit)
mvr := v.MetricsViewToplistRequest
mvr.Limit = int64(request.Limit)
mv, err := lookupMetricsView(req.Context(), s.runtime, request.InstanceId, mvr.MetricsViewName)
if err != nil {
http.Error(w, fmt.Sprintf("cannot lookup MetricsView: %s", err), http.StatusBadRequest)
return
}

filteredString := ""
if mvr.Filter != nil && (len(mvr.Filter.Exclude) > 0 || len(mvr.Filter.Include) > 0) || mvr.TimeStart != nil || mvr.TimeEnd != nil {
filteredString = "_filtered"
}

filename = fmt.Sprintf("%s%s_%s", strings.ReplaceAll(mv.Model, `"`, "_"), filteredString, time.Now().Format("20060102150405"))

q, err = createToplistQuery(req.Context(), w, v.MetricsViewToplistRequest, request.Format)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case *runtimev1.ExportRequest_MetricsViewRowsRequest:
v.MetricsViewRowsRequest.Limit = request.Limit
mvr := v.MetricsViewRowsRequest
mvr.Limit = request.Limit
mv, err := lookupMetricsView(req.Context(), s.runtime, request.InstanceId, mvr.MetricsViewName)
if err != nil {
http.Error(w, fmt.Sprintf("cannot lookup MetricsView: %s", err), http.StatusBadRequest)
return
}

filteredString := ""
if mvr.Filter != nil && (len(mvr.Filter.Exclude) > 0 || len(mvr.Filter.Include) > 0) || mvr.TimeStart != nil || mvr.TimeEnd != nil {
filteredString = "_filtered"
}

filename = fmt.Sprintf("%s%s_%s", strings.ReplaceAll(mv.Model, `"`, "_"), filteredString, time.Now().Format("20060102150405"))

q, err = createRowsQuery(req.Context(), w, v.MetricsViewRowsRequest, request.Format)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(w, fmt.Sprintf("Unsupported request type: %s", reflect.TypeOf(v).Name()), http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !auth.GetClaims(req.Context()).CanInstance(request.InstanceId, auth.ReadMetrics) {
http.Error(w, "action not allowed", http.StatusUnauthorized)
Expand All @@ -80,8 +115,10 @@ func (s *Server) downloadHandler(w http.ResponseWriter, req *http.Request) {
switch request.Format {
case runtimev1.ExportFormat_EXPORT_FORMAT_CSV:
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.csv\"", filename))
case runtimev1.ExportFormat_EXPORT_FORMAT_XLSX:
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.xlsx\"", filename))
default:
http.Error(w, fmt.Sprintf("Unsupported format %s", request.Format), http.StatusBadRequest)
return
Expand Down Expand Up @@ -129,3 +166,16 @@ func createRowsQuery(ctx context.Context, writer http.ResponseWriter, req *runti

return q, nil
}

func lookupMetricsView(ctx context.Context, rt *runtime.Runtime, instanceID, name string) (*runtimev1.MetricsView, error) {
obj, err := rt.GetCatalogEntry(ctx, instanceID, name)
if err != nil {
return nil, err
}

if obj.GetMetricsView() == nil {
return nil, err
}

return obj.GetMetricsView(), nil
}

0 comments on commit ab2dc9d

Please sign in to comment.