github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/timeline.go (about) 1 package service 2 3 import ( 4 "fmt" 5 "net/http" 6 "strconv" 7 8 "github.com/evergreen-ci/evergreen" 9 "github.com/evergreen-ci/evergreen/model/patch" 10 "github.com/evergreen-ci/evergreen/model/user" 11 "github.com/evergreen-ci/evergreen/model/version" 12 "github.com/gorilla/mux" 13 "github.com/pkg/errors" 14 ) 15 16 func (uis *UIServer) timelineJson(w http.ResponseWriter, r *http.Request) { 17 projCtx := MustHaveProjectContext(r) 18 19 skip, perPage := getSkipAndLimit(r, DefaultSkip, DefaultLimit) 20 data, err := getTimelineData(projCtx.Project.Identifier, evergreen.RepotrackerVersionRequester, skip, perPage) 21 if err != nil { 22 http.Error(w, fmt.Sprintf("Error getting timeline data: %v", err.Error()), http.StatusInternalServerError) 23 return 24 } 25 26 uis.WriteJSON(w, http.StatusOK, data) 27 } 28 29 func (uis *UIServer) timeline(w http.ResponseWriter, r *http.Request) { 30 projCtx := MustHaveProjectContext(r) 31 if projCtx.Project == nil { 32 uis.ProjectNotFound(projCtx, w, r) 33 return 34 } 35 uis.WriteHTML(w, http.StatusOK, struct { 36 ProjectData projectContext 37 User *user.DBUser 38 }{projCtx, GetUser(r)}, "base", "timeline.html", "base_angular.html", "menu.html") 39 } 40 41 func (uis *UIServer) patchTimeline(w http.ResponseWriter, r *http.Request) { 42 uis.patchTimelineWrapper("", w, r) 43 } 44 45 func (uis *UIServer) myPatchesTimeline(w http.ResponseWriter, r *http.Request) { 46 author := MustHaveUser(r).Username() 47 uis.patchTimelineWrapper(author, w, r) 48 } 49 50 func (uis *UIServer) userPatchesTimeline(w http.ResponseWriter, r *http.Request) { 51 author := mux.Vars(r)["user_id"] 52 uis.patchTimelineWrapper(author, w, r) 53 } 54 55 func (uis *UIServer) patchTimelineWrapper(author string, w http.ResponseWriter, r *http.Request) { 56 projCtx := MustHaveProjectContext(r) 57 58 if projCtx.Project == nil { 59 uis.ProjectNotFound(projCtx, w, r) 60 return 61 } 62 63 uis.WriteHTML(w, http.StatusOK, struct { 64 ProjectData projectContext 65 User *user.DBUser 66 Author string 67 }{projCtx, GetUser(r), author}, "base", "patches.html", "base_angular.html", "menu.html") 68 } 69 70 func (uis *UIServer) patchTimelineJson(w http.ResponseWriter, r *http.Request) { 71 projCtx := MustHaveProjectContext(r) 72 73 pageNum, err := strconv.Atoi(r.FormValue("page")) 74 if err != nil { 75 pageNum = 0 76 } 77 skip := pageNum * DefaultLimit 78 79 user := mux.Vars(r)["user_id"] 80 var patches []patch.Patch 81 if len(user) > 0 { 82 patches, err = patch.Find(patch.ByUser(user). 83 Project(patch.ExcludePatchDiff). 84 Sort([]string{"-" + patch.CreateTimeKey}). 85 Skip(skip).Limit(DefaultLimit)) 86 } else { 87 patches, err = patch.Find(patch.ByProject(projCtx.Project.Identifier). 88 Sort([]string{"-" + patch.CreateTimeKey}). 89 Project(patch.ExcludePatchDiff). 90 Skip(skip).Limit(DefaultLimit)) 91 } 92 if err != nil { 93 uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrapf(err, 94 "Error fetching patches for %v", projCtx.Project.Identifier)) 95 return 96 } 97 98 versionIds := make([]string, 0, len(patches)) 99 uiPatches := make([]uiPatch, 0, len(patches)) 100 for _, patch := range patches { 101 if patch.Version != "" { 102 versionIds = append(versionIds, patch.Version) 103 } 104 baseVersion, err := version.FindOne(version.ByProjectIdAndRevision(patch.Project, patch.Githash)) 105 if err != nil { 106 uis.LoggedError(w, r, http.StatusInternalServerError, err) 107 return 108 } 109 var baseVersionId string 110 if baseVersion != nil { 111 baseVersionId = baseVersion.Id 112 } 113 patch.Patches = nil 114 uiPatches = append(uiPatches, uiPatch{Patch: patch, BaseVersionId: baseVersionId}) 115 } 116 versions, err := version.Find(version.ByIds(versionIds).WithoutFields(version.ConfigKey)) 117 if err != nil { 118 uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error fetching versions for patches")) 119 return 120 } 121 122 versionsMap := map[string]*uiVersion{} 123 for _, version := range versions { 124 versionUI, err := PopulateUIVersion(&version) 125 if err != nil { 126 uis.LoggedError(w, r, http.StatusInternalServerError, err) 127 return 128 } 129 versionsMap[version.Id] = versionUI 130 } 131 132 data := struct { 133 VersionsMap map[string]*uiVersion 134 UIPatches []uiPatch 135 PageNum int 136 }{versionsMap, uiPatches, pageNum} 137 138 uis.WriteJSON(w, http.StatusOK, data) 139 }