package pc

import (
	"database/sql"
	_ "go-sqlite3-master"
	"net/http"
)

//拉取在线的算力设备
func endpoint(w http.ResponseWriter, r *http.Request) {
	db, err := sql.Open("sqlite3", "info.db")
	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()
	rows, _ := db.Query(`select endpoint.macAddress from Endpoint where endpoint.times < Average.average;`)
	defer rows.Close()
	var endpoint Endpoint
	var endpoints = []string {}
	for rows.Next() {
		rows.Scan(&endpoint.macAddress)
		endpoints.append(endpoint.macAddress)
	}
	rows.Close()
}

func shuffle(n int) []int {
	b := rand.Perm(n)
	return b
}

func request(params map[string]interface{}) error {
	var indexes = []int {0,1,2,3,4,5,6}
	var err error

	shuffle(indexes)
	maxRetryTimes := 3

	idx := 0
	for i :=0; i < maxRetryTimes; i++ {
		err = apiRequest(params, indexes[idx])
		if err == nil {
			break
		}
		idx++
	}

	if err != nil {
		//logging
		return err
	}

	return nil
}