github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/grid.go (about)

     1  package service
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  
     7  	"github.com/evergreen-ci/evergreen"
     8  	"github.com/evergreen-ci/evergreen/model/grid"
     9  	"github.com/evergreen-ci/evergreen/model/user"
    10  	"github.com/evergreen-ci/evergreen/model/version"
    11  	"github.com/gorilla/mux"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  var (
    16  	// how many prior versions to fetch by default
    17  	defaultGridDepth = 20
    18  )
    19  
    20  func (uis *UIServer) grid(w http.ResponseWriter, r *http.Request) {
    21  	projCtx := MustHaveProjectContext(r)
    22  	if projCtx.Project == nil {
    23  		uis.ProjectNotFound(projCtx, w, r)
    24  		return
    25  	}
    26  
    27  	// If no version was specified in the URL, grab the latest version on the project
    28  	if projCtx.Version == nil {
    29  		v, err := version.Find(version.ByMostRecentForRequester(projCtx.Project.Identifier, evergreen.RepotrackerVersionRequester).Limit(1))
    30  		if err != nil {
    31  			uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error finding version"))
    32  			return
    33  		}
    34  		if len(v) > 0 {
    35  			projCtx.Version = &v[0]
    36  		}
    37  	}
    38  
    39  	var versions map[string]version.Version
    40  	var cells grid.Grid
    41  	var failures grid.Failures
    42  	var revisionFailures grid.RevisionFailures
    43  	var depth int
    44  	var err error
    45  
    46  	d := mux.Vars(r)["depth"]
    47  	if d == "" {
    48  		depth = defaultGridDepth
    49  	} else {
    50  		depth, err = strconv.Atoi(d)
    51  		if err != nil {
    52  			uis.LoggedError(w, r, http.StatusBadRequest, errors.Wrap(err, "Error converting depth"))
    53  			return
    54  		}
    55  		if depth < 0 {
    56  			uis.LoggedError(w, r, http.StatusBadRequest, errors.Errorf("Depth must be non-negative, got %v", depth))
    57  			return
    58  		}
    59  	}
    60  
    61  	if projCtx.Version != nil {
    62  		recentVersions, err := version.Find(version.
    63  			ByProjectIdAndOrder(projCtx.Version.Identifier, projCtx.Version.RevisionOrderNumber).
    64  			WithFields(version.IdKey, version.RevisionKey, version.RevisionOrderNumberKey, version.MessageKey, version.AuthorKey, version.CreateTimeKey).
    65  			Sort([]string{"-" + version.RevisionOrderNumberKey}).
    66  			Limit(depth + 1))
    67  		if err != nil {
    68  			uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error fetching versions"))
    69  			return
    70  		}
    71  
    72  		versions = make(map[string]version.Version, len(recentVersions))
    73  		for _, v := range recentVersions {
    74  			versions[v.Revision] = v
    75  		}
    76  
    77  		cells, err = grid.FetchCells(*projCtx.Version, depth)
    78  		if err != nil {
    79  			uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error fetching builds"))
    80  			return
    81  		}
    82  
    83  		failures, err = grid.FetchFailures(*projCtx.Version, depth)
    84  		if err != nil {
    85  			uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error fetching builds"))
    86  			return
    87  		}
    88  
    89  		revisionFailures, err = grid.FetchRevisionOrderFailures(*projCtx.Version, depth)
    90  		if err != nil {
    91  			uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error fetching revision failures"))
    92  			return
    93  		}
    94  	} else {
    95  		versions = make(map[string]version.Version)
    96  		cells = make(grid.Grid, 0)
    97  		failures = make(grid.Failures, 0)
    98  		revisionFailures = make(grid.RevisionFailures, 0)
    99  	}
   100  	uis.WriteHTML(w, http.StatusOK, struct {
   101  		ProjectData      projectContext
   102  		Versions         map[string]version.Version
   103  		GridCells        grid.Grid
   104  		Failures         grid.Failures
   105  		RevisionFailures grid.RevisionFailures
   106  		User             *user.DBUser
   107  	}{projCtx, versions, cells, failures, revisionFailures, GetUser(r)}, "base", "grid.html", "base_angular.html", "menu.html")
   108  }