Пример #1
0
// The error application
func getErrorApp(w http.ResponseWriter, r *http.Request) {
	var username string = "thoth"
	var password string = "thoth"

	// Connect to InfluxDB
	c, _ := client.NewHTTPClient(client.HTTPConfig{
		Addr:     thoth.InfluxdbApi,
		Username: username,
		Password: password,
	})
	// TODO: iterate to query for each app
	queryRes, err := profil.QueryDB(c, fmt.Sprint("SELECT last(code5xx) FROM thoth"))
	if err != nil {
		log.Fatal(err)
	}
	errorNum5xx := queryRes[0].Series[0].Values[0][1]
	fmt.Println(errorNum5xx)
	// TODO: need to implement some algorithm searching error application
}
Пример #2
0
func main() {
	// Connect InfluxDB
	influxDB, err := client.NewHTTPClient(client.HTTPConfig{
		Addr:     thoth.InfluxdbApi,
		Username: username,
		Password: password,
	})

	if err != nil {
		panic(err)
	}

	for {

		// Get all user RC
		RC := profil.GetUserRC()
		RCLen := len(RC)

		// Getting App Metric
		for i := 0; i < RCLen; i++ {
			replicas, err := profil.GetReplicas(RC[i].Namespace, RC[i].Name)

			if err != nil {
				panic(err)
			}
			fmt.Println(replicas)

			// Check Resposne time & Label & Save WPI
			var responseDay, response10Min float64
			if responseDay, err = profil.GetProfilAvg(influxDB, RC[i].Namespace, RC[i].Name, "rtime", "1d"); err != nil {
				panic(err)
				log.Println(err)
			}
			if response10Min, err = profil.GetProfilAvg(influxDB, RC[i].Namespace, RC[i].Name, "rtime", "5m"); err != nil {
				panic(err)
				log.Println(err)
			}
			// Floor
			responseDay = math.Floor(responseDay)
			response10Min = math.Floor(response10Min)
			fmt.Println("D", responseDay, " 10M", response10Min)
			//metrics := profil.GetAppResource(RC[i].Namespace, RC[i].Name)
			var cpu10Min float64
			if cpu10Min, err = profil.GetProfilAvg(influxDB, RC[i].Namespace, RC[i].Name, "cpu", "5m"); err != nil {
				panic(err)
				log.Println(err)
			}
			fmt.Println("CPU ", cpu10Min)
			qRepSpread, err := profil.QueryDB(influxDB, fmt.Sprint("SELECT spread(replicas) FROM "+RC[i].Namespace+" WHERE time > now() - 5m"))
			if err != nil {
				log.Fatal(err)
			}
			repSpread, err := strconv.ParseFloat(fmt.Sprint(qRepSpread[0].Series[0].Values[0][1]), 32)

			if repSpread < 1 {

				if cpu10Min > 70 {

					//fmt.Println("Response check")
					//if response10Min > responseDay { // TODO:Need to check WPI too
					// Save WPI
					fmt.Println("Scale+1")
					//if err := profil.WriteRPI(influxDB, RC[i].Namespace, RC[i].Name, metrics.Request, replicas); err != nil {
					//	panic(err)
					//	log.Println(err)
					//}
					// Scale +1
					// TODO: Limit
					if replicas < 10 {
						if _, err := thoth.ScaleOutViaCli(replicas+1, RC[i].Namespace, RC[i].Name); err != nil {
							panic(err)
						}
					}
					//	}
				} else if replicas > 1 {
					// = rpi/replicas
					//var rpiMax float64
					//if rpiMax, err = profil.GetAvgRPI(influxDB, RC[i].Namespace, RC[i].Name); err != nil {
					//	rpiMax = -1
					// TODO:Handler
					//panic(err)
					//}
					//fmt.Println("WPI", rpiMax)
					//if rpiMax > 0 {
					//	minReplicas := int(metrics.Request / int64(rpiMax)) // TODO: Ceil?

					//	if minReplicas < replicas {
					// Scale -1
					fmt.Println("Scale-1")
					if _, err := thoth.ScaleOutViaCli(replicas-1, RC[i].Namespace, RC[i].Name); err != nil {
						panic(err)
					}
					//	}
					//}
				}
			}
		}
		// -----Prediction-----
		// Normalize
		// Run (Predict)
		// Label
		//runFann()
		//-----------
		fmt.Println("Sleep TODO:Change to 5 Min")
		time.Sleep(1 * time.Minute)
	}
}