<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/ant0ine/go-json-rest"
	"log"
	"net/http"
	"strconv"
)

// Job status
const (
	Queued      = iota // 0
	Initalizing        // 1
	Running            // 2
	Finished           // 3
	Failed             // 4
	Cancelled          // 5
)

const (
	NextHostNum = 3 //
)

type Host struct {
	Name       string
	Status     int
	ReturnCode int
	Message    string
}

type Job struct {
	Pcode string
	Hosts []Host
}

type HostList struct {
	Pcode string
	Name  []string
}

type PyramidScheme struct {
	jobs []Job
}

func (this *PyramidScheme) GetHosts(id int) ([]Host, error) {
	if len(this.jobs) <= id {
		return nil, errors.New(fmt.Sprintf("ID that does not exist. max id is %+v", len(this.jobs)))
	}
	return this.jobs[id].Hosts, nil
}

func (this *PyramidScheme) NextHosts(id int) ([]Host, error) {
	if len(this.jobs) <= id {
		return nil, errors.New(fmt.Sprintf("ID that does not exist. max id is %+v", len(this.jobs)))
	}
	hosts := []Host{}
	for index, host := range this.jobs[id].Hosts {
		if host.Status == Queued {
			this.jobs[id].Hosts[index].Status = Initalizing
			hosts = append(hosts, host)
			if len(hosts) >= NextHostNum {
				break
			}
		}
	}
	return hosts, nil
}

func (this *PyramidScheme) UpdateHost(jobId int, host Host) error {
	if len(this.jobs) <= jobId {
		return errors.New(fmt.Sprintf("ID that does not exist. max jobId is %+v", len(this.jobs)))
	}
	for hostId, h := range this.jobs[jobId].Hosts {
		if h.Name == host.Name {
			this.jobs[jobId].Hosts[hostId] = host
			return nil
		}
	}
	return errors.New(fmt.Sprintf("Host can not be found is %+v", host.Name))

}

func (this *PyramidScheme) PostJob(hostList *HostList) int {
	hosts := []Host{}
	for _, name := range hostList.Name {
		hosts = append(hosts, Host{name, Queued, 0, ""})
	}
	job := Job{hostList.Pcode, hosts}
	this.jobs = append(this.jobs, job)
	return len(this.jobs) - 1 //jobId
}

// PostJob handler
func (this *PyramidScheme) PostJobHandler(w *rest.ResponseWriter, req *rest.Request) {
	hostList := HostList{}
	err := req.DecodeJsonPayload(&hostList)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteJson(this.PostJob(&hostList))
}

// GetHosts handler
func (this *PyramidScheme) GetHostsHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	if job, err := this.GetHosts(id); err == nil {
		w.WriteJson(&job)
	} else {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// NextHosts handler
func (this *PyramidScheme) PutNextHostsHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	if job, err := this.NextHosts(id); err == nil {
		w.WriteJson(&job)
	} else {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// UpdateHost handler
func (this *PyramidScheme) PutUpdateHostHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	host := Host{}
	err := req.DecodeJsonPayload(&host)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := this.UpdateHost(id, host); err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func main() {
	ps := PyramidScheme{}
	handler := rest.ResourceHandler{}
	handler.SetRoutes(
		rest.Route{"POST", "/jobs", ps.PostJobHandler},
		rest.Route{"GET", "/jobs/:id/hosts", ps.GetHostsHandler},
		rest.Route{"PUT", "/jobs/:id/nexthosts", ps.PutNextHostsHandler},
		rest.Route{"PUT", "/jobs/:id/updatehost/", ps.PutUpdateHostHandler},
	)
	log.Fatal(http.ListenAndServe(":8000", &handler))
}
<commit_msg>errorの場合にリターンするようにした<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/ant0ine/go-json-rest"
	"log"
	"net/http"
	"strconv"
)

// Job status
const (
	Queued      = iota // 0
	Initalizing        // 1
	Running            // 2
	Finished           // 3
	Failed             // 4
	Cancelled          // 5
)

const (
	NextHostNum = 3 //
)

type Host struct {
	Name       string
	Status     int
	ReturnCode int
	Message    string
}

type Job struct {
	Pcode string
	Hosts []Host
}

type HostList struct {
	Pcode string
	Name  []string
}

type PyramidScheme struct {
	jobs []Job
}

func (this *PyramidScheme) GetHosts(id int) ([]Host, error) {
	if len(this.jobs) <= id {
		return nil, errors.New(fmt.Sprintf("ID that does not exist. max id is %+v", len(this.jobs)))
	}
	return this.jobs[id].Hosts, nil
}

func (this *PyramidScheme) NextHosts(id int) ([]Host, error) {
	if len(this.jobs) <= id {
		return nil, errors.New(fmt.Sprintf("ID that does not exist. max id is %+v", len(this.jobs)))
	}
	hosts := []Host{}
	for index, host := range this.jobs[id].Hosts {
		if host.Status == Queued {
			this.jobs[id].Hosts[index].Status = Initalizing
			hosts = append(hosts, host)
			if len(hosts) >= NextHostNum {
				break
			}
		}
	}
	return hosts, nil
}

func (this *PyramidScheme) UpdateHost(jobId int, host Host) error {
	if len(this.jobs) <= jobId {
		return errors.New(fmt.Sprintf("ID that does not exist. max jobId is %+v", len(this.jobs)))
	}
	for hostId, h := range this.jobs[jobId].Hosts {
		if h.Name == host.Name {
			this.jobs[jobId].Hosts[hostId] = host
			return nil
		}
	}
	return errors.New(fmt.Sprintf("Host can not be found is %+v", host.Name))

}

func (this *PyramidScheme) PostJob(hostList *HostList) int {
	hosts := []Host{}
	for _, name := range hostList.Name {
		hosts = append(hosts, Host{name, Queued, 0, ""})
	}
	job := Job{hostList.Pcode, hosts}
	this.jobs = append(this.jobs, job)
	return len(this.jobs) - 1 //jobId
}

// PostJob handler
func (this *PyramidScheme) PostJobHandler(w *rest.ResponseWriter, req *rest.Request) {
	hostList := HostList{}
	err := req.DecodeJsonPayload(&hostList)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteJson(this.PostJob(&hostList))
}

// GetHosts handler
func (this *PyramidScheme) GetHostsHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	if job, err := this.GetHosts(id); err == nil {
		w.WriteJson(&job)
	} else {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// NextHosts handler
func (this *PyramidScheme) PutNextHostsHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	if job, err := this.NextHosts(id); err == nil {
		w.WriteJson(&job)
	} else {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// UpdateHost handler
func (this *PyramidScheme) PutUpdateHostHandler(w *rest.ResponseWriter, req *rest.Request) {
	id, _ := strconv.Atoi(req.PathParam("id"))
	host := Host{}
	err := req.DecodeJsonPayload(&host)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := this.UpdateHost(id, host); err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func main() {
	ps := PyramidScheme{}
	handler := rest.ResourceHandler{}
	handler.SetRoutes(
		rest.Route{"POST", "/jobs", ps.PostJobHandler},
		rest.Route{"GET", "/jobs/:id/hosts", ps.GetHostsHandler},
		rest.Route{"PUT", "/jobs/:id/nexthosts", ps.PutNextHostsHandler},
		rest.Route{"PUT", "/jobs/:id/updatehost/", ps.PutUpdateHostHandler},
	)
	log.Fatal(http.ListenAndServe(":8000", &handler))
}

{'fraction_non_alphanumeric': 0.15953095173166076, 'fraction_numerical': 0.003817834742296155, 'mean_word_length': 3.6898976982097187, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310339', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2724, 'n_words': 629}