Example #1
0
// CleanupDatastore is to remove all data in datastore
func CleanupDatastore(ctx context.Context, namespaces ...string) error {
	var dummy []interface{}
	logger := wcg.NewLogger(nil)
	logger.Debugf("[Fixture] --------- CleanupDatastore ---------")
	namespaceList := append(namespaces, "")
	for _, ns := range namespaceList {
		logger.Debugf("[Fixture] Cleanup: ns=%q", ns)
		var _ctx = ctx
		if ns != "" {
			_ctx, _ = appengine.Namespace(_ctx, ns)
		}
		err := wcg.RetryUntil(func() error {
			var keys []*datastore.Key
			var err error
			if keys, err = datastore.NewQuery("").KeysOnly().GetAll(_ctx, dummy); err != nil {
				return err
			}
			if err := datastore.DeleteMulti(_ctx, keys); err != nil {
				return err
			}
			count, _ := datastore.NewQuery("").KeysOnly().Count(_ctx)
			if count == 0 {
				return nil
			}
			return fmt.Errorf("Still have %d keys.", count)
		}, 10*time.Second, 100*time.Millisecond)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func cleanup(s *testerator.Setup) error {
	t := datastore.NewQuery("__kind__").KeysOnly().Run(s.Context)
	kinds := make([]string, 0)
	for {
		key, err := t.Next(nil)
		if err == datastore.Done {
			break
		}
		if err != nil {
			return err
		}
		kinds = append(kinds, key.StringID())
	}

	for _, kind := range kinds {
		q := datastore.NewQuery(kind).KeysOnly()
		keys, err := q.GetAll(s.Context, nil)
		if err != nil {
			return err
		}
		err = datastore.DeleteMulti(s.Context, keys)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #3
0
// Dump exports entities from the context c using the specified Options o and
// writing the generated JSON representations to the io.Writer w. You can configure
// how the dump will run by using the Options parameter. If there is an error
// generating the output, or writting to the writer, it is returned. This method
// may return an error after writting bytes to w: the output is not buffered.
func Dump(c context.Context, w io.Writer, o *Options) error {
	var (
		comma        = []byte(",")
		openBracket  = []byte("[")
		closeBracket = []byte("]")
		lineFeed     = []byte("\n")
		indent       = "  "
	)

	w.Write(openBracket)
	count := 0
	last := 0
	batchSize := o.BatchSize
	if batchSize <= 0 {
		batchSize = 100
	}

	q := datastore.NewQuery(o.Kind).Limit(batchSize)
	for i := q.Run(c); ; {
		var e Entity
		k, err := i.Next(&e)
		e.Key = k
		if err == datastore.Done {
			log.Infof(c, "datastore.Done: last=%d, count=%d", last, count)
			if last == count || count-last < batchSize {
				break
			}
			// This 100 batch is done, but more can be found in the next one
			last = count
			cur, err := i.Cursor()
			if err != nil {
				return err
			}
			log.Infof(c, "restarting the query: cursor=%v", cur)
			i = datastore.NewQuery(o.Kind).Limit(batchSize).Start(cur).Run(c)
			continue
		}
		if err != nil {
			return err
		}
		if count > 0 {
			w.Write(comma)
			w.Write(lineFeed)
		}
		var b []byte
		if o.PrettyPrint {
			b, err = json.MarshalIndent(&e, "", indent)
		} else {
			b, err = json.Marshal(&e)
		}
		if err != nil {
			return err
		}
		w.Write(b)
		count++
	}
	w.Write(closeBracket)
	return nil
}
Example #4
0
// DeleteAll deletes across all roots
// DeleteAll deletes by kind alone.
func (fs *dsFileSys) DeleteAll() (string, error) {

	msg := ""
	{
		q := datastore.NewQuery(tfil).KeysOnly()
		var files []DsFile
		keys, err := q.GetAll(fs.Ctx(), &files)
		if err != nil {
			msg += "could not get file keys\n"
			return msg, err
		}
		if len(keys) >= 500 {
			msg += "limited to 500 files. REPEAT operation.\n"
			keys = keys[:500]
		}

		err = datastore.DeleteMulti(fs.Ctx(), keys)
		if err != nil {
			msg += "error deleting files\n"
			return msg, err
		}

		msg += spf("%v files deleted\n", len(keys))

	}

	{
		q := datastore.NewQuery(tdir).KeysOnly()
		var dirs []DsDir
		keys, err := q.GetAll(fs.Ctx(), &dirs)
		if err != nil {
			msg += "could not get dir keys\n"
			return msg, err
		}
		if len(keys) >= 500 {
			msg += "limited to 500 directories. REPEAT operation.\n"
			keys = keys[:500]
		}

		err = datastore.DeleteMulti(fs.Ctx(), keys)
		if err != nil {
			msg += "error deleting directories\n"
			return msg, err
		}

		msg += spf("%v directories deleted\n", len(keys))
	}

	err := memcache.Flush(fs.Ctx())
	if err != nil {
		msg += "error flushing memcache\n"
		return msg, err
	} else {
		msg += "memcache flushed \n"
	}

	return msg, nil
}
Example #5
0
// subtreeByPath retrieves a subdirectories of a given directory.
// It is relying on an indexed string property "Dir"
// containing a string representation of the full path.
//
// It might be fast for deep, uncached directory subtree,
// that have been saved in nested manner.
//
// However, it might not find recently added directories.
// Upon finding nothing, it therefore returns the
// "warning" fsi.EmptyQueryResult
//
// The func could easily be enhanced chunked scanning.
//
// It is currently used by ReadDir and by the test package.
// It is public for the test package.
func (fs *dsFileSys) SubtreeByPath(name string, onlyDirectChildren bool) ([]DsDir, error) {

	dir, bname := fs.SplitX(name)
	name = dir + common.Filify(bname)
	if !strings.HasSuffix(name, sep) {
		name += sep
	}

	var q *datastore.Query

	if onlyDirectChildren {
		q = datastore.NewQuery(tdir).
			Filter("Dir=", name).
			Order("Dir")
		//  Limit(4)
	} else {
		pathInc := IncrementString(name)
		q = datastore.NewQuery(tdir).
			Filter("Dir>=", name).
			Filter("Dir<", pathInc).
			Order("Dir")
	}

	// log.Printf("%v", q)

	var children []DsDir
	keys, err := q.GetAll(fs.Ctx(), &children)
	if err != nil {
		aelog.Errorf(fs.Ctx(), "Error getting all children of %v => %v", dir+bname, err)
		return children, err
	}

	if len(children) < 1 {
		return children, fsi.EmptyQueryResult
	}

	// Very evil: We filter out root node, since it's
	// has the same dir as the level-1 directories.
	keyRoot := datastore.NewKey(fs.Ctx(), tdir, fs.RootDir(), 0, nil)
	idxRoot := -1

	for i := 0; i < len(children); i++ {
		children[i].fSys = fs
		children[i].Key = keys[i]
		if keys[i].Equal(keyRoot) {
			// log.Printf("root idx %v", i)
			idxRoot = i
		}
	}

	if idxRoot > -1 {
		children = append(children[:idxRoot], children[idxRoot+1:]...)
	}

	return children, nil

}
Example #6
0
func example4() {
	// [START sort_order_example]
	// Order alphabetically by last name:
	q := datastore.NewQuery("Person").Order("LastName")

	// Order by height, tallest to shortest:
	q = datastore.NewQuery("Person").Order("-Height")
	// [END sort_order_example]
	_ = q
}
Example #7
0
func GetTestResultsForSubmission(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
	if r.Method != "GET" {
		return http.StatusMethodNotAllowed, nil
	}

	// TODO(victorbalan): Check if user is company or if user is parent of result
	// else return http.StatusUnauthorized
	_, ok := passenger.FromContext(ctx)
	if !ok {
		return http.StatusUnauthorized, nil
	}

	submissionKey, err := datastore.DecodeKey(mux.Vars(r)["key"])
	if err != nil {
		return http.StatusNotFound, err
	}

	keys, err := datastore.NewQuery("").
		Ancestor(submissionKey).
		Filter("__key__ >", submissionKey).
		KeysOnly().
		GetAll(ctx, nil)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	if len(keys) == 0 {
		json.NewEncoder(w).Encode([]string{})
		return http.StatusOK, nil
	}

	switch keys[0].Kind() {
	case model.JunitTestResultKind:
		var results model.JunitTestResults
		_, err = datastore.NewQuery(keys[0].Kind()).
			Ancestor(submissionKey).
			GetAll(ctx, &results)
		if err != nil {
			return http.StatusInternalServerError, err
		}
		json.NewEncoder(w).Encode(results)
	case model.DiffTestResultKind:
		var results model.DiffTestResults
		_, err = datastore.NewQuery(keys[0].Kind()).
			Ancestor(submissionKey).
			GetAll(ctx, &results)
		if err != nil {
			return http.StatusInternalServerError, err
		}
		json.NewEncoder(w).Encode(results)
	default:
		w.Write([]byte("[]"))
	}
	return http.StatusOK, nil
}
Example #8
0
func getRelations(req *http.Request, username string) ([]Relation, []Relation, error) {
	ctx := appengine.NewContext(req)
	var Following, FollowingMe []Relation
	q := datastore.NewQuery("Relation")
	_, err := q.Filter("Follower =", username).Order("Following").GetAll(ctx, &Following)
	if err != nil {
		return []Relation{}, []Relation{}, err
	}
	q = datastore.NewQuery("Relation")
	_, err = q.Filter("Following =", username).Order("Follower").GetAll(ctx, &FollowingMe)
	if err != nil {
		return []Relation{}, []Relation{}, err
	}
	return Following, FollowingMe, nil
}
Example #9
0
func profile(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	sd := sessionInfo(req)
	var user User
	user.UserName = ps.ByName("name")

	ctx := appengine.NewContext(req)

	//Get ppl they're following
	q := datastore.NewQuery("Follow").Filter("Follower =", user.UserName)
	var f []Follow
	_, err := q.GetAll(ctx, &f)
	if err != nil {
		panic(err)
	}
	for _, val := range f {
		user.Following = append(user.Following, val.Following)
	}

	//Get ppl they're followed by
	q2 := datastore.NewQuery("Follow").Filter("Following =", user.UserName)
	var f2 []Follow
	_, err2 := q2.GetAll(ctx, &f2)
	if err2 != nil {
		panic(err)
	}
	for _, val := range f2 {
		user.FollowedBy = append(user.FollowedBy, val.Follower)
	}

	//Get ppl they're following
	q3 := datastore.NewQuery("Follow").Filter("Follower =", sd.UserName)
	var f3 []Follow
	_, err = q3.GetAll(ctx, &f3)
	if err != nil {
		panic(err)
	}
	for _, val := range f3 {
		sd.Following = append(sd.Following, val.Following)
	}

	sd.FollowingUser = stringInSlice(user.UserName, sd.Following)
	if user.UserName == sd.UserName {
		sd.FollowingUser = true
	}
	sd.ViewingUser = user

	tpl.ExecuteTemplate(res, "profile.html", &sd)
}
Example #10
0
func projections(c context.Context, u User, days int) (int64, int64, error) {
	var projected, earned int64

	g := syncutil.Group{}

	g.Go(func() error {
		q := datastore.NewQuery("Task").
			Filter("Disabled = ", false).
			Filter("Assignee = ", u.Email)

		for t := q.Run(c); ; {
			var x Task
			_, err := t.Next(&x)
			if err == datastore.Done {
				return nil
			} else if err != nil {
				return err
			}

			log.Debugf(c, "Item worth %v every %v", x.Value, x.Period)

			projected += int64(float64(x.Value) * (float64(days) / float64(x.Period)))
		}
	})

	g.Go(func() error {
		q := datastore.NewQuery("LoggedTask").
			Filter("User = "******"Completed >=", time.Now().Add(-24*time.Hour*time.Duration(days)))

		for t := q.Run(c); ; {
			var x LoggedTask
			_, err := t.Next(&x)
			if err == datastore.Done {
				return nil
			} else if err != nil {
				return err
			}

			log.Debugf(c, "Logged task worth %v", x.Amount)

			earned += int64(x.Amount)
		}
	})
	g.Wait()

	return projected, earned, g.Err()
}
Example #11
0
func getWord(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	term := req.FormValue("term")

	q := datastore.NewQuery("Word").Order("Term").Filter("Term =", term)

	html := " "

	iterator := q.Run(ctx)

	for {
		var entity Word
		_, err := iterator.Next(&entity)
		if err == datastore.Done {
			break
		} else if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		html += `<dt>` + entity.Term + `</dt>
				 <dd>` + entity.Def + `</dd>`
	}

	res.Header().Set("Content-Type", "text/html")
	fmt.Fprintln(res, `
	<dl>`+html+`<dl>
	<form method="POST" action="">
		Enter a term to see its definition <input type="text" name="term">
		<input type="submit">
	</form>
		`)
}
Example #12
0
func (s *TestSuite) TestAddLocation() {
	t := s.T()

	loc, err := json.Marshal(&Location{
		Name: "Cambridge Fresh Pond",
		Lat:  42.5,
		Lng:  -71.5,
	})
	assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)

	req, err := s.inst.NewRequest("POST", "/add-location",
		ioutil.NopCloser(bytes.NewBuffer(loc)))
	require.Nil(t, err, "Error preparing request: %v", err)
	rec := httptest.NewRecorder()
	s.rt.ServeHTTP(rec, req)

	const expectedResponse = `{"addLocation":"success"}`
	assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
		"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))

	dbReq, err := s.inst.NewRequest("GET", "/", nil)
	require.Nil(t, err, "Error preparing request: %v", err)
	ctx := appengine.NewContext(dbReq)

	q := datastore.NewQuery("Location")
	numLocs, err := q.Count(ctx)
	require.Nil(t, err, "Error preparing request: %v", err)
	assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
Example #13
0
func TestAddLocationTestify(t *testing.T) {
	inst, err := aetest.NewInstance(
		&aetest.Options{StronglyConsistentDatastore: true})
	require.Nil(t, err, "Error creating aetest instance: %v", err)
	defer inst.Close()
	rt := initRouter()

	loc, err := json.Marshal(&Location{
		Name: "Cambridge Fresh Pond",
		Lat:  42.5,
		Lng:  -71.5,
	})
	assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)

	req, err := inst.NewRequest("POST", "/add-location",
		ioutil.NopCloser(bytes.NewBuffer(loc)))
	require.Nil(t, err, "Error preparing request: %v", err)
	rec := httptest.NewRecorder()
	rt.ServeHTTP(rec, req)

	const expectedResponse = `{"addLocation":"success"}`
	assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
		"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))

	dbReq, err := inst.NewRequest("GET", "/", nil)
	require.Nil(t, err, "Error preparing request: %v", err)
	ctx := appengine.NewContext(dbReq)

	q := datastore.NewQuery("Location")
	numLocs, err := q.Count(ctx)
	require.Nil(t, err, "Error preparing request: %v", err)
	assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
func handlePersonalPage(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	session := getSession(ctx, req)
	username := strings.SplitN(req.URL.Path, "/", 3)[2]
	if username == "" {
		http.Redirect(res, req, "/userslist", 302)
		return
	}
	q := datastore.NewQuery("Image").Filter("Username ="******"List", username, 0, nil)
	var list List
	datastore.Get(ctx, key, &list)
	listItems := make([]string, 0)
	if strings.TrimSpace(list.List) != "" {
		listItems = strings.Split(list.List, "\n")
	}
	model := &personalModel{
		Session:   session,
		Username:  username,
		ListItems: listItems,
		Images:    images,
	}
	err := tpls.ExecuteTemplate(res, "personalpage", model)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
}
Example #15
0
func (cdb ComplaintDB) LoadGlobalStats() (*GlobalStats, error) {
	gs := GlobalStats{}

	fgs := []FrozenGlobalStats{}
	q := datastore.NewQuery(kGlobalStatsKind).Limit(10)

	if keys, err := q.GetAll(cdb.Ctx(), &fgs); err != nil {
		return nil, err
	} else if len(fgs) != 1 {
		return nil, fmt.Errorf("LoadGlobalStats: found %d, expected 1", len(fgs))
	} else {
		buf := bytes.NewBuffer(fgs[0].Bytes)
		err := gob.NewDecoder(buf).Decode(&gs)
		gs.DatastoreKey = keys[0].Encode() // Store this, so we can overwrite

		// Pick out the high water marks ...
		if len(gs.Counts) > 0 {
			iMaxComplaints, iMaxComplainers := 0, 0
			maxComplaints, maxComplainers := 0, 0
			for i, _ := range gs.Counts {
				if gs.Counts[i].NumComplaints > maxComplaints {
					iMaxComplaints, maxComplaints = i, gs.Counts[i].NumComplaints
				}
				if gs.Counts[i].NumComplainers > maxComplainers {
					iMaxComplainers, maxComplainers = i, gs.Counts[i].NumComplainers
				}
			}
			gs.Counts[iMaxComplaints].IsMaxComplaints = true
			gs.Counts[iMaxComplainers].IsMaxComplainers = true
		}

		return &gs, err
	}
}
Example #16
0
func TestCreateSpot(t *testing.T) {
	opt := aetest.Options{AppID: "t2jp-2015", StronglyConsistentDatastore: true}
	inst, err := aetest.NewInstance(&opt)
	defer inst.Close()
	input, err := json.Marshal(Spot{SpotName: "foo", Body: "bar"})
	req, err := inst.NewRequest("POST", "/edit/v1/spots", bytes.NewBuffer(input))
	if err != nil {
		t.Fatalf("Failed to create req: %v", err)
	}
	loginUser := user.User{Email: "*****@*****.**", Admin: false, ID: "111111"}
	aetest.Login(&loginUser, req)
	ctx := appengine.NewContext(req)
	res := httptest.NewRecorder()
	c := web.C{}
	spotCreateHandler(c, res, req)
	if res.Code != http.StatusCreated {
		t.Fatalf("Fail to request spots create, status code: %v", res.Code)
	}
	spots := []Spot{}
	_, err = datastore.NewQuery("Spot").Order("-UpdatedAt").GetAll(ctx, &spots)
	for i := 0; i < len(spots); i++ {
		t.Logf("SpotCode:%v", spots[i].SpotCode)
		t.Logf("SpotName:%v", spots[i].SpotName)
	}
	if spots[0].SpotName != "foo" {
		t.Fatalf("not expected value! :%v", spots[0].SpotName)
	}

}
Example #17
0
func requestAllImage(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	//user has requested to see all images
	sess, _ := getSession(req) // get login info if exists.
	var sd Session
	json.Unmarshal(sess.Value, &sd)

	ctx := appengine.NewContext(req)
	var links []blobbedImage
	for t := datastore.NewQuery("Images").Run(ctx); ; { /// get all images in Images from datastore
		var x blobbedImage
		_, err := t.Next(&x)
		if err == datastore.Done {
			break
		}
		if err != nil {
			serveTemplateWithParams(res, req, "falure.html", "INTERNAL DATASTORE ERROR, IMAGE REQUEST FAILED\nERROR: "+err.Error())
			return
		}
		links = append(links, x)
	}

	sd.Viewing = links  // add the images to our session data.
	if sd.Email != "" { // do a quick logged in check.
		sd.LoggedIn = true
	} else {
		sd.LoggedIn = false
	}

	serveTemplateWithParams(res, req, "imageMulti.html", sd) // serve.
}
Example #18
0
// NewSampleQueryBuilderWithKind create new SampleQueryBuilder with specific kind.
func NewSampleQueryBuilderWithKind(kind string) *SampleQueryBuilder {
	q := datastore.NewQuery(kind)
	bldr := &SampleQueryBuilder{q: q}
	bldr.ID = &SampleQueryProperty{
		bldr: bldr,
		name: "__key__",
	}
	bldr.FooUrl = &SampleQueryProperty{
		bldr: bldr,
		name: "FooURL",
	}
	bldr.StartAt = &SampleQueryProperty{
		bldr: bldr,
		name: "Start",
	}
	bldr.LimitProperty = &SampleQueryProperty{
		bldr: bldr,
		name: "Limit",
	}
	bldr.CreatedAt = &SampleQueryProperty{
		bldr: bldr,
		name: "CreatedAt",
	}

	if plugger, ok := interface{}(bldr).(qbgutils.Plugger); ok {
		bldr.plugin = plugger.Plugin()
		bldr.plugin.Init("Sample")
	}
	return bldr
}
Example #19
0
func fing(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)
	// get session
	memItem, err := getSession(req)
	if err != nil {
		log.Infof(ctx, "Attempt to see following from logged out user")
		http.Error(res, "You must be logged in", http.StatusForbidden)
		return
	}
	var sd SessionData
	if err == nil {
		// logged in
		json.Unmarshal(memItem.Value, &sd)
		sd.LoggedIn = true
	}
	// declare a variable of type user
	// initialize user with values from memcache item
	var user User
	json.Unmarshal(memItem.Value, &user)
	// Get followees
	// get the datastore key for the follower
	followerKey := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
	var XF []F
	_, err = datastore.NewQuery("Follows").Ancestor(followerKey).Project("Following").GetAll(ctx, &XF)
	log.Errorf(ctx, "here is type %T \n and value %v", XF, XF)
	if err != nil {
		log.Errorf(ctx, "error getting followees: %v", err)
		http.Error(res, err.Error(), 500)
		return
	}
	sd.Following = XF
	tpl.ExecuteTemplate(res, "follow.html", &sd)
}
Example #20
0
// Remove safely deletes an account and all its associated information in the datastore. This includes
// any objects that are descendants of the Account (i.e., a cascading delete).
func Remove(ctx context.Context, account *Account) error {

	return datastore.RunInTransaction(ctx, func(txCtx context.Context) error {

		acctKey := account.Key(txCtx)
		q := datastore.NewQuery("").
			Ancestor(acctKey).
			KeysOnly()

		if changed, err := HasChanged(txCtx, account); err != nil {
			return err
		} else if changed {
			return ErrConflict
		}

		keys, err := q.GetAll(txCtx, nil)
		if err != nil {
			return err
		}

		keys = append(keys, acctKey)
		return nds.DeleteMulti(txCtx, keys)

	}, nil)

}
// Count retrieves the value of the named counter.
func Count(c context.Context, name string) (int, error) {
	total := 0
	mkey := memcacheKey(name)
	if _, err := memcache.JSON.Get(c, mkey, &total); err == nil {
		return total, nil
	}
	pKey := datastore.NewKey(c, configKind, name, 0, nil)
	q := datastore.NewQuery(shardKind).Ancestor(pKey).Filter("Name =", name)
	for t := q.Run(c); ; {
		var s shard
		_, err := t.Next(&s)
		if err == datastore.Done {
			break
		}
		if err != nil {
			return total, err
		}
		total += s.Count
	}
	memcache.JSON.Set(c, &memcache.Item{
		Key:    mkey,
		Object: &total,
	})
	return total, nil
}
Example #22
0
func (s *ViewService) BySpace(ctx context.Context, sp *Space) ([]*View, error) {
	span := trace.FromContext(ctx).NewChild("trythings.view.BySpace")
	defer span.Finish()

	var vs []*View
	_, err := datastore.NewQuery("View").
		Ancestor(datastore.NewKey(ctx, "Root", "root", 0, nil)).
		Filter("SpaceID =", sp.ID).
		GetAll(ctx, &vs)
	if err != nil {
		return nil, err
	}

	var ac []*View
	for _, v := range vs {
		ok, err := s.IsVisible(ctx, v)
		if err != nil {
			// TODO use multierror
			return nil, err
		}

		if ok {
			ac = append(ac, v)
		}
	}

	return ac, nil
}
Example #23
0
// Attempt to test timeouts in deployed appengine; but proved elusive.
func slowHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	d, _ := time.ParseDuration(r.FormValue("d"))

	tStart := time.Now()

	start, end := date.WindowForTime(tStart)
	end = end.Add(-1 * time.Second)
	str := ""

	for time.Since(tStart) < d {
		q := datastore.
			NewQuery(oldfgae.KFlightKind).
			Filter("EnterUTC >= ", start).
			Filter("EnterUTC < ", end).
			KeysOnly()
		keys, err := q.GetAll(c, nil)
		if err != nil {
			log.Errorf(c, "batch/day: GetAll: %v", err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		str += fmt.Sprintf("Found %d flight objects at %s\n", len(keys), time.Now())
		time.Sleep(2 * time.Second)
	}

	w.Header().Set("Content-Type", "text/plain")
	w.Write([]byte(fmt.Sprintf("OK, waited for %s !\n%s", r.FormValue("d"), str)))
}
Example #24
0
func buttonClickedPage(w http.ResponseWriter, r *http.Request) {
	click := &clickData{
		IPAddress: r.RemoteAddr,
	}

	c := appengine.NewContext(r)
	key := datastore.NewIncompleteKey(c, "Click", clickKey(c))
	_, err := datastore.Put(c, key, click)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	clickCount, err := datastore.NewQuery("Click").Count(c)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	stats := &buttonStats{
		Clicks: clickCount,
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Cache-Control", "no-cache")
	err = json.NewEncoder(w).Encode(stats)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Example #25
0
func (cdb ComplaintDB) QueryAllByEmailAddress(email string) *datastore.Query {
	return datastore.
		NewQuery(kComplaintKind).
		Ancestor(cdb.emailToRootKey(email)).
		Order("Timestamp").
		Limit(-1)
}
Example #26
0
File: api.go Project: jordonwii/hms
// General handler function for all requests to the API
// In addition to calling the handler function according to the API routes,
// verifies that a valid API key was provided as a parameter, and
// sets the response content-type to JSON
func APIHandler(w http.ResponseWriter, r *http.Request) *appError {
	apiKey := r.FormValue("apiKey")
	if apiKey == "" {
		return &appError{nil, "Invalid API Key", 401}
	}

	c := appengine.NewContext(r)
	results := make([]APIKey, 0, 1)
	_, err := datastore.NewQuery("APIKey").Filter("APIKey =", apiKey).GetAll(c, &results)
	if err != nil {
		return &appError{err, "Error validating API key", 500}
	} else if len(results) == 0 {
		return &appError{nil, "Invalid API key.", 401}
	}

	apiKeyStruct := results[0]
	handler, ok := apiRoutes[r.URL.Path]
	if !ok {
		return &appError{nil, fmt.Sprintf("No API handler for %s", r.URL.Path), 404}
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	return handler(w, r, apiKeyStruct)
}
Example #27
0
// Returns an array of all todo items
func listTodoItems(ctx context.Context, u *user.User) *MaybeError {
	var result = new(MaybeError)
	var resultList = make([]TodoItem, 0)
	// filter by user
	log(fmt.Sprintf("Making query, email = %s", u.Email))

	q := datastore.NewQuery("TodoItem").Filter("OwnerEmail=", u.Email).Order("DueDate")
	keys, err := q.GetAll(ctx, &resultList)
	if err != nil {
		log(fmt.Sprintf("listTodoItems got %d keys err = %s", len(keys), err.Error()))
		*result = E(err.Error())
	} else {
		log(fmt.Sprintf("got %d items %s [%s] / [%s]\n", len(keys), u.Email, keys, resultList))
		// *assuming* these are going to be in the same order...
		var matches = make([]Match, 0)
		// this is a bit silly since we already did the database query, but...
		// if we call readTodoItem we get the more-recent item in the cache
		for _, k := range keys {
			anItem := readTodoItem(ctx, TodoID(*k))
			switch (*anItem).(type) {
			case TodoItem:
				matches = append(matches, Match{k, (*anItem).(TodoItem)})
			default:
				*result = (E("readTodoItem returned weird result in listTodoItems"))
				return result
			}
		}
		*result = Matches(matches)
	}
	return result
}
// get the first or last value from the range by querying the datastore
// so we can split the range effectively
func (q *Query) getExtremePropertyValue(c context.Context, equality []filter, property string, dir Direction) (interface{}, error) {
	// note, we're already working against a specific namespace at this point
	// so our context will already be for that (because this happens within the
	// namespace task)
	dq := datastore.NewQuery(q.kind)
	for _, f := range equality {
		dq = dq.Filter(f.FieldName+" "+f.Op.String(), f.Value)
	}
	switch dir {
	case Ascending:
		dq = dq.Order(property)
	case Descending:
		dq = dq.Order("-" + property)
	}
	dq = dq.Limit(1)

	pl := make([]*datastore.PropertyList, 1)
	if _, err := dq.GetAll(c, pl); err != nil {
		return nil, err
	}
	for _, p := range *pl[0] {
		if p.Name == property {
			return p.Value, nil
		}
	}
	return nil, fmt.Errorf("property %s not found", property)
}
Example #29
0
func index(w http.ResponseWriter, r *http.Request, ps router.Params) {
	c := appengine.NewContext(r)
	var voteResults []Show

	//Retrieve Shows from datastore
	showKeys, err := datastore.NewQuery("Show").Order("-Votes").Order("Title").GetAll(c, &voteResults)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Errorf(c, "GetAll: %v", err)
		return
	}

	//Create input to pass to html template
	tempInput := make([]struct {
		Show Show
		Key  string
	}, len(showKeys))

	//Fill input object with data
	for key, val := range voteResults {
		tempInput[key].Key = showKeys[key].Encode()
		tempInput[key].Show = val
	}

	myTmpl.ExecuteTemplate(w, "index", tempInput)
}
Example #30
0
func showList(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	tpl, err := template.ParseFiles("assets/templates/templates.gohtml")
	if err != nil {
		panic(err)
	}

	ctx := appengine.NewContext(req)
	u := user.Current(ctx)
	qp := datastore.NewQuery("ToDo")
	ToDos := []ToDo{}
	keys, error := qp.GetAll(ctx, &ToDos)
	for _, value := range ToDos {

	}

	if error != nil {
		log.Infof(ctx, "%v", error.Error())
	}

	json.NewEncoder(res).Encode(ToDos)

	err = tpl.ExecuteTemplate(res, "todo-list", ToDos)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}