Exemplo n.º 1
0
func (conf Conf) ApiHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Add(ContentTypeHeader, JSONContentHeader)

	repo := r.FormValue("repo")
	if repo == "" {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(MissingRepoError)
		return
	}

	tokenHeader := strings.Split(r.Header.Get(AuthorizationHeader), " ")
	if len(tokenHeader) <= 1 {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(MissingTokenError)
		return
	}
	token := tokenHeader[1]

	// Get the data with the token from db.
	repoInfo, _, err := conf.Database.GetRepo(repo)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(InternalError)
		return
	}

	// if exist
	if repoInfo.Exist() {
		if err := conf.TriggerUpdateJob(repoInfo, token); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			json.NewEncoder(w).Encode(InternalError)
			return
		}
		w.WriteHeader(http.StatusOK)
		json.NewEncoder(w).Encode(repoInfo)
		return
	}

	// if doesn't exist in db, check on github
	repoInfo, err = github.GetRepoInfo(repo, token)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(InternalError)
		return
	}
	if repoInfo.Exist() {
		if err := conf.TriggerAddJob(repoInfo, token); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			json.NewEncoder(w).Encode(InternalError)
			return
		}
		w.WriteHeader(http.StatusOK)
		json.NewEncoder(w).Encode(repoInfo)
	}

	// if doesn't exist on github 404
	w.WriteHeader(http.StatusNotFound)
	json.NewEncoder(w).Encode(NotFoundError)
}
Exemplo n.º 2
0
func main() {
	flag.Parse()

	fmt.Printf("Starting github star graph of %s\n", repo)
	startDate := time.Now()
	repoInfo, err := github.GetRepoInfo(token, repo)
	if err != nil {
		fmt.Printf("An error occured while getting the repository info: %v\n", err)
		return
	}
	var timestamps []int64
	/*if concurrent {
		timestamps, err = lib.GetTimestampsDistributed(repoInfo.Count, batch, repoInfo.GetUrl(), token)
	} else {
		timestamps, err = lib.GetTimestamps(batch, repoUrl, token)
	}*/
	timestamps, err = example.GetTimestamps(batch, repoInfo.URL(), token)
	if err != nil {
		fmt.Printf("An error occured while getting the stars from Github: %v\n", err)
		return
	}
	endDate := time.Now()
	duration := endDate.Sub(startDate)
	fmt.Printf("Timestamps gotten in %v\n", duration)

	fmt.Println("Persisting them into canvas format...")
	canvasFile, err := os.Create("canvasdb.json")
	if err != nil {
		fmt.Printf("An error occured when creating the canvas file %v\n", err)
		return
	}
	defer canvasFile.Close()
	if err = lib.WriteCanvasJS(timestamps, repoInfo, canvasFile); err != nil {
		fmt.Printf("An error occured when writing the canvas file%v\n", err)
	}
	fmt.Println("Done.")

	fmt.Println("Persisting them into jqplot format...")
	jqplotFile, err := os.Create("jqplotdb.json")
	if err != nil {
		fmt.Printf("An error occured when creating the jqplot file %v\n", err)
		return
	}
	defer jqplotFile.Close()
	if err = lib.WriteCanvasJS(timestamps, repoInfo, jqplotFile); err != nil {
		fmt.Printf("An error occured when writing the jqplot file %v\n", err)
		return
	}
	fmt.Println("Done.")

	fmt.Println("Drawing the graph image...")
	graphFile, err := os.Create("graph.png")
	if err != nil {
		fmt.Printf("An error occured when creating the graph image %v\n", err)
		return
	}
	defer graphFile.Close()
	if err = lib.PlotGraph("Graph of "+repo, timestamps, graphFile); err != nil {
		fmt.Printf("An error occured when drawing the imge%v\n", err)
		return
	}
	fmt.Println("Done.")
}