func apiSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, q []Query) { p, ok := ctx.Value("http.request.query.params").(*url.Values) if !ok { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context") return } st, ok := ctx.Value("app.database").(db.Session) if !ok { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain database from context") return } limit := util.ToInt32(p.Get("limit")) if limit == 0 { limit = 1000 } delim := false w.Write([]byte(`{"result":[`)) for _, query := range q { if !ahttp.IsAlive(w) { return } if limit == 0 { break } col, err := st.Coll(query.CollName) if err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "%v", err) return } collQuery := col.Find(query.Pattern).Limit(int(limit)) if len(query.Sort) > 0 { collQuery = collQuery.Sort(query.Sort...) } iter := collQuery.Iter() for { if !ahttp.IsAlive(w) { return } if limit == 0 { break } doc := query.Iterator(iter) if doc == nil { break } msg, err := json.Marshal(doc) if err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to marshal document") return } if delim { w.Write([]byte(`,`)) } w.Write(msg) limit-- delim = true } if err := iter.Close(); err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Error iterating: %+v", err) return } } w.Write([]byte(`],"query":[`)) delim = false for _, query := range q { msg, err := json.Marshal(query.Pattern) if err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to marshal pattern") return } if delim { w.Write([]byte(`,`)) } w.Write(msg) delim = true } w.Write([]byte(`]}`)) }
func StatisticQueueHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { st, ok := ctx.Value("app.database").(db.Session) if !ok { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain database from context") return } cfg, ok := ctx.Value("app.config").(*config.Config) if !ok { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain config from context") return } ans := make(map[string]map[string]int64) for _, repo := range cfg.Builder.Repos { ans[repo] = make(map[string]int64) for _, state := range taskStates { ans[repo][state] = 0 } } q := db.QueryDoc{ "state": db.QueryDoc{"$in": []string{"new", "awaiting", "postponed", "building", "pending", "committing"}}, } col, err := st.Coll(task.CollName) if err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "%v", err) return } iter := col.Find(q).Iter() for { if !ahttp.IsAlive(w) { return } t := task.New() if !iter.Next(t) { break } state, ok := t.State.Get() if !ok { continue } repo, ok := t.Repo.Get() if !ok { continue } ans[repo][state] += int64(1) } if err := iter.Close(); err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Error iterating: %+v", err) return } msg, err := json.Marshal(ans) if err != nil { ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to marshal json: %v", err) return } w.Write([]byte(`{"result":`)) w.Write(msg) w.Write([]byte(`}`)) }