Example #1
0
// Stop terminates a running timer if one exists.  If the running timer has not
// yet been associated with a timer id, it is transitioned to the AnonStopped
// state.  Otherwise it is transitioned to the NamedStopped state.
func Stop(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	u := user.Current(ctx)
	timer, err := datastore.LoadRunningTimer(ctx, u.String())
	if internalError(w, err) {
		return
	}
	if timer == nil {
		printEmptyTimer(w)
		return
	}
	if timer.State == m.AnonStopped || timer.State == m.NamedStopped {
		timer.Print(w)
		return
	}
	if timer.TimerID == "" {
		timer.State = m.AnonStopped
	} else {
		timer.State = m.NamedStopped
	}
	err = datastore.SaveRunningTimer(ctx, timer)
	if internalError(w, err) {
		return
	}
	timer.Print(w)
}
Example #2
0
// Timer renders the current running timer for a user.
func Timer(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	u := user.Current(ctx)
	timer, err := datastore.LoadRunningTimer(ctx, u.String())
	if internalError(w, err) {
		return
	}
	if timer == nil {
		printEmptyTimer(w)
		return
	}
	timer.Print(w)
}
Example #3
0
// Identify associates the current running timer with a pre-existing timer.
func Identify(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	u := user.Current(ctx)
	id := r.FormValue("id")
	if id == "" {
		userError(w, "ID is required.")
		return
	}
	running, err := datastore.LoadRunningTimer(ctx, u.String())
	if internalError(w, err) {
		return
	}
	if running == nil {
		userError(w, "No timer is running.")
		return
	}
	if running.State == m.NamedRunning || running.State == m.NamedStopped {
		userError(w, "Timer is already identified.")
		return
	}
	timer, err := datastore.LoadTimer(ctx, u.String(), id)
	if internalError(w, err) {
		return
	}
	if timer == nil {
		userError(w, "No such timer: "+id)
		return
	}
	running.TimerID = id
	if running.State == m.AnonRunning {
		running.State = m.NamedRunning
	}
	if running.State == m.AnonStopped {
		running.State = m.NamedStopped
	}
	err = datastore.SaveRunningTimer(ctx, running)
	if internalError(w, err) {
		return
	}
	running.Print(w)
}
Example #4
0
// Reset collection running timer stats for a user and deletes the timer.
func Reset(w http.ResponseWriter, r *http.Request) {
	owner := r.FormValue("owner")
	if owner == "" {
		panic("Unknown owner.")
	}
	ctx := appengine.NewContext(r)
	timer, err := datastore.LoadRunningTimer(ctx, owner)
	if err != nil {
		panic("Unable to load running timer for " + owner + " : " + err.Error())
	}
	minute, err := stats.Minute(timer)
	if err != nil {
		panic(err)
	}
	err = updateAllDimensions(ctx, timer, minute)
	if err != nil {
		panic(err)
	}
	err = datastore.DeleteRunningTimer(ctx, timer)
	if err != nil {
		panic("Unable to delete running timer for " + owner + " : " + err.Error())
	}
}
Example #5
0
// Name creates a new timer and associated it with the current running timer.
func Name(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	u := user.Current(ctx)
	name := r.FormValue("name")
	if name == "" {
		http.Error(w, "Name is required.", http.StatusBadRequest)
		return
	}
	running, err := datastore.LoadRunningTimer(ctx, u.String())
	if internalError(w, err) {
		return
	}
	if running == nil {
		userError(w, "There is no current timer.")
		return
	}
	if running.State == m.NamedRunning || running.State == m.NamedStopped {
		userError(w, "The current timer is already named.")
		return
	}
	timer, err := datastore.NewTimer(ctx, u.String(), name)
	if internalError(w, err) {
		return
	}
	running.TimerID = timer.ID
	if running.State == m.AnonRunning {
		running.State = m.NamedRunning
	}
	if running.State == m.AnonStopped {
		running.State = m.NamedStopped
	}
	err = datastore.SaveRunningTimer(ctx, running)
	if internalError(w, err) {
		return
	}
	fmt.Fprint(w, inHTMLBody(messageHTML("New timer created with id: "+timer.ID)+menu))
}