Esempio n. 1
0
func History(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	page, err := cached.Get(HISTORYS_MEMCACHE_KEY,
		1*time.Hour, func() ([]byte, error) {
			currentTime := trailk.Time().Now()
			data := getMostRecentHistory(currentTime, trailk, c, keyTool)
			buf, err := template.Render("history", data)

			if err != nil {
				return []byte(""), err
			}

			return buf.Bytes(), nil
		})

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Esempio n. 2
0
File: update.go Progetto: npk/trailk
func Update(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	trailk := context.NewContext()

	keyTool := key.NewTool(c)

	fetcher := urlfetch.NewURLFetcher(c)

	subjects := source.NewSubjects(trailk, c, keyTool, "Source")
	subjectRefers := source.NewSubjects(trailk, c, keyTool, "Reference")

	bm := benchmark.NewBenchmark(
		fetcher,
		config.Benchmark_Repeat,
		config.Benchmark_Delay,
		config.Benchmark_ConnectionTimeoutMax, config.Benchmark_ConnectionTimeoutMin,
		config.Benchmark_ConnectionTimeoutIncreaseFactor)

	summaries := summary.NewSummaries(trailk, c, keyTool)
	summary.Defaults(summaries) // Build defaults if needed

	// Test reference first to warmup
	benchmark.NewBenchmarks(bm).Resulter(func(s *benchmarkcommon.Summary) {
		for _, sum := range summaries.Select("Reference").All() {
			segErr := sum.Update(s.Max(), s.Min(), s.Average(), s.Timeout())

			if segErr != nil {
				c.Errorf("Reference frame updating: %v", segErr)

				continue
			}
		}
	}).Errorer(func(e error) {
		c.Errorf(e.Error())
	}).RunAll(subjectRefers)

	benchmark.NewBenchmarks(bm).Resulter(func(s *benchmarkcommon.Summary) {
		for _, sum := range summaries.Select("Source").All() {
			segErr := sum.Update(s.Max(), s.Min(), s.Average(), s.Timeout())

			if segErr != nil {
				c.Errorf("Source segment updating: %v", segErr)

				continue
			}
		}
	}).Errorer(func(e error) {
		c.Errorf(e.Error())
	}).RunAll(subjects)

	w.WriteHeader(http.StatusOK)
}
Esempio n. 3
0
File: index.go Progetto: npk/trailk
func Index(w http.ResponseWriter, r *http.Request) {
	// Check if we need to display 404 here
	if r.URL.Path != "/" {
		NotFoundError(w, r)

		return
	}

	c := appengine.NewContext(r)
	trailk := context.NewContext()

	keyTool := key.NewTool(c)
	s := summary.NewSummaries(trailk, c, keyTool)
	cached := memcache.NewMemcache(c)

	page, err := cached.Get(INDEX_MEMCACHE_KEY, 24*time.Hour, func() ([]byte, error) {
		currentTime := trailk.Time().Now()

		sumData := &summaryData{
			Report30Days:  getSummaryReports("DEFAULT_30_DAYS", "Source", currentTime, s),
			Report24Hours: getSummaryReports("DEFAULT_24_HOURS", "Source", currentTime, s),

			Dists:    s.Select("Source").Distribution().All(),
			DistsRef: s.Select("Reference").Distribution().All(),
		}

		buf, tplErr := template.Render("landing", sumData)

		if tplErr != nil {
			return []byte(""), tplErr
		}

		return buf.Bytes(), nil
	})

	if err != nil {
		switch err {
		default:
			controller.ServerError(http.StatusInternalServerError, w, err)
		}

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)

	w.Write(page)
}
Esempio n. 4
0
func HistoryAPI(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	endTimestampStr := r.URL.Query().Get("end")

	if len(endTimestampStr) < 1 {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("End time must be specified"))

		return
	}

	endTSInt, errTS := strconv.ParseInt(endTimestampStr, 10, 64)

	if errTS != nil {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("End time must be an unix timestamp"))

		return
	}

	endTimestamp := time.Unix(endTSInt, 0)

	page, err := cached.Get(HISTORY_MEMCACHE_KEY+":"+endTimestampStr,
		1*time.Hour, func() ([]byte, error) {
			data := getMostRecentHistory(endTimestamp, trailk, c, keyTool)

			json, err := json.Marshal(data)

			if err != nil {
				return []byte(""), err
			}

			return json, nil
		})

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Esempio n. 5
0
File: add.go Progetto: npk/trailk
func Add(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)

	if !user.IsAdmin(c) {
		controller.ServerError(http.StatusForbidden, w, errors.New(
			"Only administrator can access this page"))

		return
	}

	data := &sourceAddData{
		Name: r.FormValue("Name"),
		URL:  r.FormValue("URL"),
		Type: r.FormValue("Type"),
	}

	if strings.ToUpper(r.Method) == "POST" {
		fetcher := urlfetch.NewURLFetcher(c)

		sourceKey, err := addSource(trailk, c, keyTool, fetcher, data)

		if err != nil {
			data.Error = err
		} else {
			http.Redirect(w, r, fmt.Sprintf(
				"/source?id=%d&type=%s", sourceKey.IntID(), data.Type), 301)

			return
		}
	}

	buf, tplErr := template.Render("sources-add", data)

	if tplErr != nil {
		controller.ServerError(http.StatusInternalServerError, w, tplErr)

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(buf.Bytes())
}
Esempio n. 6
0
File: list.go Progetto: npk/trailk
func List(w http.ResponseWriter, r *http.Request) {
	var page []byte
	var err error

	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	if !user.IsAdmin(c) {
		page, err = cached.Get(SOURCES_MEMCACHE_KEY, 1*time.Hour, func() ([]byte, error) {
			data := getList(trailk, c, keyTool)
			buf, err := template.Render("sources", data)

			if err != nil {
				return []byte(""), err
			}

			return buf.Bytes(), nil
		})
	} else {
		data := getList(trailk, c, keyTool)

		data.CanAddNewSource = true

		buf, err := template.Render("sources", data)

		if err == nil {
			page = buf.Bytes()
		}
	}

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Esempio n. 7
0
File: item.go Progetto: npk/trailk
func Item(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	sourceIDStr := r.URL.Query().Get("id")
	sourceTypeStr := r.URL.Query().Get("type")

	if len(sourceIDStr) < 1 {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("Source ID must be inputted"))

		return
	}

	if len(sourceTypeStr) < 1 {
		sourceTypeStr = "Source"
	}

	sourceID, strcErr := strconv.ParseInt(sourceIDStr, 10, 64)

	if strcErr != nil {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("Source ID must be numeric"))

		return
	}

	page, err := cached.Get(SOURCE_MEMCACHE_KEY+sourceTypeStr+":"+sourceIDStr, 1*time.Hour, func() ([]byte, error) {
		data, err := getItem(sourceID, sourceTypeStr, trailk, c, keyTool)

		if err != nil {
			return []byte(""), err
		}

		buf, tplErr := template.Render("source", data)

		if tplErr != nil {
			return []byte(""), tplErr
		}

		return buf.Bytes(), nil
	})

	if err != nil {
		switch err {
		case datastore.ErrNoSuchEntity:
			controller.ServerError(http.StatusNotFound, w, err)

		case ErrUnknownSourceType:
			controller.ServerError(http.StatusNotFound, w, err)

		default:
			controller.ServerError(http.StatusInternalServerError, w, err)
		}

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}