Skip to content

Commit

Permalink
comments...more...comments....
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Ivy committed Sep 29, 2024
1 parent dfc9f86 commit 7c1416e
Show file tree
Hide file tree
Showing 36 changed files with 286 additions and 50 deletions.
2 changes: 2 additions & 0 deletions ent/schema/bazelinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ func (BazelInvocation) Edges() []ent.Edge {
}
}

// Indexes for Bazel Invocation.
func (BazelInvocation) Indexes() []ent.Index {
return []ent.Index{
index.Fields("change_number", "patchset_number"),
}
}

// Annotations for basel invocation.
func (BazelInvocation) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
Expand Down
7 changes: 5 additions & 2 deletions ent/schema/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/google/uuid"
)

// Build holds the schema definition for the Build.
type Build struct {
ent.Schema
}

// Fields of the BazelInvocation.
// Fields of the Build.
func (Build) Fields() []ent.Field {
return []ent.Field{
field.String("build_url").Unique().Immutable(),
Expand All @@ -23,19 +24,21 @@ func (Build) Fields() []ent.Field {
}
}

// Edges of the BazelInvocation.
// Edges of the Build.
func (Build) Edges() []ent.Edge {
return []ent.Edge{
edge.To("invocations", BazelInvocation.Type),
}
}

// Indexs of the Build.
func (Build) Indexes() []ent.Index {
return []ent.Index{
index.Fields("env"),
}
}

// Annoations of the Build.
func (Build) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
Expand Down
1 change: 1 addition & 0 deletions ent/schema/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (Metrics) Edges() []ent.Edge {
}
}

// Annotation of the Metrics.
func (Metrics) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
Expand Down
1 change: 0 additions & 1 deletion ent/schema/racestatistics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// stores dynamic execution data
package schema

import (
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/runnercount.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (RunnerCount) Edges() []ent.Edge {
}
}

// NOTE: not implemented.
// Annotations of the Runner Counts.
func (RunnerCount) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
Expand Down
5 changes: 5 additions & 0 deletions internal/api/bep_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ import (
"github.com/buildbarn/bb-portal/pkg/processing"
)

// A constant for strong mb and upload size informations.
const (
MB = 1024 * 1024
MaxUploadSize = 500 * MB
)

// Bep upload handler struct.
type bepUploadHandler struct {
client *ent.Client
blobArchiver processing.BlobMultiArchiver
}

// Constructor function for BEP upload handler.
func NewBEPUploadHandler(client *ent.Client, blobArchiver processing.BlobMultiArchiver) http.Handler {
return &bepUploadHandler{
client: client,
blobArchiver: blobArchiver,
}
}

// A function to serve HTTP.
func (b bepUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Expand Down Expand Up @@ -77,6 +81,7 @@ func (b bepUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
writeLocationResponse(w, location)
}

// A function to write location responses.
func writeLocationResponse(w http.ResponseWriter, location string) {
w.WriteHeader(http.StatusOK)
resp := struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/api/blob_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import (
"github.com/buildbarn/bb-portal/pkg/cas"
)

// A struct to handle blobs.
type blobHandler struct {
client *ent.Client
casManager *cas.ConnectionManager
}

// Constructor functio for a blob hanlder.
func NewBlobHandler(client *ent.Client, casManager *cas.ConnectionManager) http.Handler {
return &blobHandler{client: client, casManager: casManager}
}

// Serve this over http.
func (b *blobHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
blobIDPathValue := request.PathValue("blobID")
name := request.PathValue("name")
Expand All @@ -49,6 +52,7 @@ func (b *blobHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reques
b.serveBlob(writer, request, name, blobRecord)
}

// Serve a blob.
func (b *blobHandler) serveBlob(writer http.ResponseWriter, request *http.Request, name string, blobRecord *ent.Blob) {
if blobRecord.ArchivingStatus == blob.ArchivingStatusSUCCESS {
http.ServeFile(writer, request, blobRecord.ArchiveURL)
Expand Down Expand Up @@ -76,6 +80,7 @@ func (b *blobHandler) serveBlob(writer http.ResponseWriter, request *http.Reques
}
}

// Serve from bytestream function.
func (b *blobHandler) serveFromBytestream(writer http.ResponseWriter, request *http.Request, name string, uri *url.URL) {
casClient, err := b.casManager.GetClientForURI(request.Context(), uri)
if err != nil {
Expand Down Expand Up @@ -105,6 +110,7 @@ func (b *blobHandler) serveFromBytestream(writer http.ResponseWriter, request *h
http.ServeContent(writer, request, name, time.Time{}, tmpFile)
}

// A function to write an error.
func writeErr(writer http.ResponseWriter, request *http.Request, statusCode int, msg string) {
writer.WriteHeader(statusCode)
if _, err := writer.Write([]byte(msg)); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/api/grpc/bes/bes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ import (
"github.com/buildbarn/bb-portal/third_party/bazel/gen/bes"
)

// A type for the Build Event Service.
type BES struct {
db *ent.Client
blobArchiver processing.BlobMultiArchiver
}

// BES initializer function.
func New(db *ent.Client, blobArchiver processing.BlobMultiArchiver) build.PublishBuildEventServer {
return &BES{
db: db,
blobArchiver: blobArchiver,
}
}

// Publush a life cycle event.
func (b BES) PublishLifecycleEvent(ctx context.Context, request *build.PublishLifecycleEventRequest) (*emptypb.Empty, error) {
slog.InfoContext(ctx, "Received event", "event", protojson.Format(request.BuildEvent.GetEvent()))
return &emptypb.Empty{}, nil
}

// Public a build tool event stream.
func (b BES) PublishBuildToolEventStream(stream build.PublishBuildEvent_PublishBuildToolEventStreamServer) error {
slog.InfoContext(stream.Context(), "Stream started", "event", stream.Context())

Expand Down Expand Up @@ -95,6 +99,7 @@ func (b BES) PublishBuildToolEventStream(stream build.PublishBuildEvent_PublishB
return nil
}

// Process a bazel Event.
func processBazelEvent(ctx context.Context, event *build.BuildEvent, summarizer *summary.Summarizer) error {
if event.GetBazelEvent() == nil {
return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/buildbarn/bb-portal/pkg/processing"
)

// A helper type for a grpc server.
type Server = grpc.Server

// Initializes a new server.
func NewServer(db *ent.Client, blobArchiver processing.BlobMultiArchiver, opts ...grpc.ServerOption) *grpc.Server {
grpcServer := grpc.NewServer(opts...)

Expand Down
2 changes: 2 additions & 0 deletions internal/graphql/helpers/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ func graphQLIDFromString(input string) string {
return base64.URLEncoding.EncodeToString([]byte(input))
}

// Takes an id and returns the b64enc string.
func GraphQLIDFromTypeAndID(objType string, id int) string {
return graphQLIDFromString(fmt.Sprintf("%s:%d", objType, id))
}

// ID Decoder helper
func GraphQLTypeAndIntIDFromID(id string) (string, int, error) {
bytes, err := base64.URLEncoding.DecodeString(id)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/graphql/helpers/output.helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/buildbarn/bb-portal/third_party/bazel/gen/bes"
)

// A file lookup type.
type FileLookup func(ctx context.Context) (*bes.File, error)

// Blob Reference for File function.
func BlobReferenceForFile(ctx context.Context, db *ent.Client, fileLookup FileLookup) (*model.BlobReference, error) {
file, err := fileLookup(ctx)
if err != nil {
Expand All @@ -32,6 +34,7 @@ func BlobReferenceForFile(ctx context.Context, db *ent.Client, fileLookup FileLo
}, nil
}

// find blob function.
func findBlob(ctx context.Context, db *ent.Client, file *bes.File) (*ent.Blob, error) {
uri := file.GetUri()
if uri == "" {
Expand All @@ -41,6 +44,7 @@ func findBlob(ctx context.Context, db *ent.Client, file *bes.File) (*ent.Blob, e
return db.Blob.Query().Where(blob.URI(uri)).First(ctx)
}

// Get an Action.
func GetAction(ctx context.Context, problem *ent.BazelInvocationProblem) (*bes.ActionExecuted, error) {
bepEvents, err := events.FromJSONArray(problem.BepEvents)
if err != nil {
Expand All @@ -54,7 +58,3 @@ func GetAction(ctx context.Context, problem *ent.BazelInvocationProblem) (*bes.A
}
return nil, errActionNotFound
}

// func GetUser(ctx context.Context, obj *ent.BazelInvocation) (*model.User, error) {

// }
20 changes: 20 additions & 0 deletions internal/graphql/helpers/resolver.helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/buildbarn/bb-portal/third_party/bazel/gen/bes"
)

// Error helpers.
var (
ErrOnlyURLOrUUID = errors.New("either buildURL or buildUUID variable must be used, but not both")
ErrWrongType = errors.New("received unexpected type while trying to convert node to *ent.BazelInvocationProblem")
Expand All @@ -22,18 +23,22 @@ var (
errStatusNotFound = errors.New("status not found")
)

// A Helper struct.
type Helper struct {
*problemHelper
}

// Initializer for helper
func NewHelper() *Helper {
return &Helper{
problemHelper: &problemHelper{},
}
}

// A problem helper.
type problemHelper struct{}

// Convert db problem to api problem.
func (ph problemHelper) DBProblemsToAPIProblems(ctx context.Context, dbProblems []*ent.BazelInvocationProblem) ([]model.Problem, error) {
problems := make([]model.Problem, 0, len(dbProblems))
for _, dbProblem := range dbProblems {
Expand All @@ -47,6 +52,7 @@ func (ph problemHelper) DBProblemsToAPIProblems(ctx context.Context, dbProblems
return problems, nil
}

// Convert a DB problem to an API problem.
func (ph problemHelper) DBProblemToAPIProblem(ctx context.Context, problem *ent.BazelInvocationProblem) (model.Problem, error) {
switch problem.ProblemType {
case detectors.BazelInvocationActionProblem:
Expand Down Expand Up @@ -95,6 +101,7 @@ func (ph problemHelper) DBProblemToAPIProblem(ctx context.Context, problem *ent.
}
}

// Get an action type.
func (ph problemHelper) getActionType(ctx context.Context, problem *ent.BazelInvocationProblem) (string, error) {
action, err := ph.getAction(ctx, problem)
if err != nil {
Expand All @@ -103,6 +110,7 @@ func (ph problemHelper) getActionType(ctx context.Context, problem *ent.BazelInv
return action.GetType(), nil
}

// Get an action.
func (ph problemHelper) getAction(ctx context.Context, problem *ent.BazelInvocationProblem) (*bes.ActionExecuted, error) {
bepEvents, err := events.FromJSONArray(problem.BepEvents)
if err != nil {
Expand All @@ -117,6 +125,7 @@ func (ph problemHelper) getAction(ctx context.Context, problem *ent.BazelInvocat
return nil, errActionNotFound
}

// Get an action problem form a database model.
func (ph problemHelper) actionProblemFromDBModel(problem *ent.BazelInvocationProblem, actionType string) model.Problem {
return &model.ActionProblem{
ID: GraphQLIDFromTypeAndID("ActionProblem", problem.ID),
Expand All @@ -126,15 +135,18 @@ func (ph problemHelper) actionProblemFromDBModel(problem *ent.BazelInvocationPro
}
}

// A test problem helper struct
type testProblemHelper struct {
*ent.BazelInvocationProblem
}

// Get the graphql id.
func (problem testProblemHelper) GraphQLID() string {
// TODO: scalars.GraphQLIDFromString
return fmt.Sprintf("testProblem:%d", problem.ID)
}

// get the status of the problm helper.
func (problem testProblemHelper) Status() (string, error) {
bepEvents, err := events.FromJSONArray(problem.BepEvents)
if err != nil {
Expand All @@ -148,6 +160,7 @@ func (problem testProblemHelper) Status() (string, error) {
return "", errStatusNotFound
}

// The results.
func (problem testProblemHelper) Results() ([]*model.TestResult, error) {
bepEvents, err := events.FromJSONArray(problem.BepEvents)
if err != nil {
Expand Down Expand Up @@ -181,10 +194,12 @@ func (problem testProblemHelper) Results() ([]*model.TestResult, error) {
return results, nil
}

// The Progress problem helper.
type progressProblemHelper struct {
*ent.BazelInvocationProblem
}

// The Output.
func (e progressProblemHelper) Output() (string, error) {
bepEvents, err := events.FromJSONArray(e.BepEvents)
if err != nil {
Expand All @@ -198,23 +213,28 @@ func (e progressProblemHelper) Output() (string, error) {
return output.String(), nil
}

// Test Result Overview Helper.
type testResultOverviewHelper struct {
*bes.TestResult
testResultID model.TestResultID
}

// The Run property.
func (helper testResultOverviewHelper) Run() int32 {
return helper.testResultID.Run
}

// The Shard property.
func (helper testResultOverviewHelper) Shard() int32 {
return helper.testResultID.Shard
}

// The Attempt.
func (helper testResultOverviewHelper) Attempt() int32 {
return helper.testResultID.Attempt
}

// The Status.
func (helper testResultOverviewHelper) Status() string {
return helper.GetStatus().String()
}
3 changes: 3 additions & 0 deletions internal/graphql/helpers/test_result_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import (
"github.com/buildbarn/bb-portal/third_party/bazel/gen/bes"
)

// Get test result action log output.
func GetTestResultActionLogOutput(ctx context.Context, client *ent.Client, obj *model.TestResult) (*model.BlobReference, error) {
return getTestResultActionOutputByName(ctx, client, obj, "test.log")
}

// Get Test result test outputs.
func GetTestResultUndeclaredTestOutputs(ctx context.Context, client *ent.Client, obj *model.TestResult) (*model.BlobReference, error) {
return getTestResultActionOutputByName(ctx, client, obj, events.UndeclaredTestOutputsName)
}

// Get Test Result Action Outputs by name.
func getTestResultActionOutputByName(ctx context.Context, client *ent.Client, obj *model.TestResult, name string) (*model.BlobReference, error) {
fileLookup := func(_ context.Context) (*bes.File, error) {
var file *bes.File
Expand Down
Loading

0 comments on commit 7c1416e

Please sign in to comment.