Skip to content

Commit

Permalink
Update report serving to support dashboards + propagate static asset dir
Browse files Browse the repository at this point in the history
  • Loading branch information
bcspragu committed Jan 16, 2025
1 parent 00cef75 commit 142c406
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions async/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ func (r *TaskEnv) asEnvVars() []string {
"DASHBOARD_OUTPUT_DIR=" + r.pathForDir(DashboardOutputDir),
"DASHBOARD_DATA_DIR=" + filepath.Join(r.pathForDir(DashboardOutputDir), "data"),
"SUMMARY_OUTPUT_DIR=" + r.pathForDir(SummaryOutputDir),
// Make sure the command gets the relevant values from the env
"DASHBOARD_SKELETON_FILES_DIR=" + os.Getenv("DASHBOARD_SKELETON_FILES_DIR"),
}
}

Expand Down
1 change: 1 addition & 0 deletions reportsrv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//session",
"@com_github_go_chi_chi_v5//:chi",
"@org_uber_go_zap//:zap",
"@org_uber_go_zap_exp//zapfield",
],
)

Expand Down
44 changes: 29 additions & 15 deletions reportsrv/reportsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"fmt"
"io"
"net/http"
"path"
"strings"

"github.com/RMI/pacta/blob"
"github.com/RMI/pacta/db"
"github.com/RMI/pacta/pacta"
"github.com/RMI/pacta/session"
"github.com/go-chi/chi/v5"
chi "github.com/go-chi/chi/v5"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapfield"
)

type Config struct {
Expand Down Expand Up @@ -95,6 +97,31 @@ func (s *Server) serveReport(w http.ResponseWriter, r *http.Request) {
}
ctx := r.Context()

a, err := s.db.Analysis(s.db.NoTxn(ctx), aID)
if err != nil {
if strings.HasPrefix(string(aID), "analysis") {
s.logger.Error("failed to load analysis", zap.String("analysis_id", string(aID)), zap.Error(err))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
s.logger.Info("poorly constructed analysis id", zap.String("path", string(aID)), zap.Error(err))
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
}

var reportPath string
switch a.AnalysisType {
case pacta.AnalysisType_Report:
reportPath = "report-output/report/"
case pacta.AnalysisType_Dashboard:
reportPath = "dashboard-output/"
default:
s.logger.Error("unsupported analysis type", zap.String("analysis_id", string(aID)), zapfield.Str("analysis_type", a.AnalysisType))
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

artifacts, err := s.db.AnalysisArtifactsForAnalysis(s.db.NoTxn(ctx), aID)
if err != nil {
s.logger.Error("failed to load artifacts for analysis", zap.String("analysis_id", string(aID)), zap.Error(err))
Expand All @@ -113,27 +140,14 @@ func (s *Server) serveReport(w http.ResponseWriter, r *http.Request) {
return
}

a, err := s.db.Analysis(s.db.NoTxn(ctx), aID)
if err != nil {
if strings.HasPrefix(string(aID), "analysis") {
s.logger.Error("failed to load analysis", zap.String("analysis_id", string(aID)), zap.Error(err))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
s.logger.Info("poorly constructed analysis id", zap.String("path", string(aID)), zap.Error(err))
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
}

subPath := strings.TrimPrefix(r.URL.Path, "/report/"+string(aID))
if strings.HasPrefix(subPath, "/") {
subPath = subPath[1:]
}
if subPath == "" {
subPath = "index.html"
}
subPath = "report-output/report/" + subPath
subPath = path.Join(reportPath, subPath)

for _, aa := range artifacts {
// Container is just 'reports', we can ignore that.
Expand Down

0 comments on commit 142c406

Please sign in to comment.