Skip to content

Commit

Permalink
feat: Support namespace in list
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege committed Sep 30, 2019
1 parent cb6de0d commit f8d05cf
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 168 deletions.
2 changes: 1 addition & 1 deletion cmd/ui/v1alpha3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
http.Handle("/katib/", http.StripPrefix("/katib/", frontend))

http.HandleFunc("/katib/fetch_hp_jobs/", kuh.FetchAllHPJobs)
http.HandleFunc("/katib/fetch_nas_jobs/", kuh.FetchNASJobs)
http.HandleFunc("/katib/fetch_nas_jobs/", kuh.FetchAllNASJobs)
http.HandleFunc("/katib/submit_yaml/", kuh.SubmitYamlJob)
http.HandleFunc("/katib/submit_hp_job/", kuh.SubmitParamsJob)
http.HandleFunc("/katib/submit_nas_job/", kuh.SubmitParamsJob)
Expand Down
161 changes: 0 additions & 161 deletions pkg/ui/v1alpha3/backend.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package v1alpha3

import (
"context"
"encoding/json"
"log"
"net/http"
"strings"
"time"

"github.com/ghodss/yaml"
"google.golang.org/grpc"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

experimentv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1alpha3"
trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3"
api_pb_v1alpha3 "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3"
common_v1alpha3 "github.com/kubeflow/katib/pkg/common/v1alpha3"
"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts"
Expand Down Expand Up @@ -42,41 +38,6 @@ func (k *KatibUIHandler) connectManager() (*grpc.ClientConn, api_pb_v1alpha3.Man
return conn, c
}

func (k *KatibUIHandler) FetchHPJobs(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
jobs, err := k.getExperimentList(consts.DefaultKatibNamespace, JobTypeHP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

response, err := json.Marshal(jobs)
if err != nil {
log.Printf("Marshal HP jobs failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)

}

// FetchAllHPJobs gets experiments in all namespaces.
func (k *KatibUIHandler) FetchAllHPJobs(w http.ResponseWriter, r *http.Request) {
// Use "" to get experiments in all namespaces.
jobs, err := k.getExperimentList("", JobTypeHP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(jobs)
if err != nil {
log.Printf("Marshal HP jobs failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}

func (k *KatibUIHandler) SubmitYamlJob(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
var data map[string]interface{}
Expand Down Expand Up @@ -155,128 +116,6 @@ func (k *KatibUIHandler) DeleteExperiment(w http.ResponseWriter, r *http.Request
}
}

func (k *KatibUIHandler) FetchHPJobInfo(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
experimentName := r.URL.Query()["experimentName"][0]
namespace := r.URL.Query()["namespace"][0]

conn, c := k.connectManager()
defer conn.Close()

resultText := "trialName"
experiment, err := k.katibClient.GetExperiment(experimentName, namespace)
if err != nil {
log.Printf("GetExperiment from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Got Experiment")
metricsList := map[string]int{}
metricsName := experiment.Spec.Objective.ObjectiveMetricName
resultText += "," + metricsName
metricsList[metricsName] = 0
for i, m := range experiment.Spec.Objective.AdditionalMetricNames {
resultText += "," + m
metricsList[m] = i + 1
}
log.Printf("Got metrics names")
paramList := map[string]int{}
for i, p := range experiment.Spec.Parameters {
resultText += "," + p.Name
paramList[p.Name] = i + len(metricsList)
}
log.Printf("Got Parameters names")

trialList, err := k.katibClient.GetTrialList(experimentName)
if err != nil {
log.Printf("GetTrialList from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Got Trial List")

for _, t := range trialList.Items {
succeeded := false
for _, condition := range t.Status.Conditions {
if condition.Type == trialsv1alpha3.TrialSucceeded {
succeeded = true
}
}
if succeeded {
obsLogResp, err := c.GetObservationLog(
context.Background(),
&api_pb_v1alpha3.GetObservationLogRequest{
TrialName: t.Name,
StartTime: "",
EndTime: "",
},
)
if err != nil {
log.Printf("GetObservationLog from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

trialResText := make([]string, len(metricsList)+len(paramList))
for _, m := range obsLogResp.ObservationLog.MetricLogs {
trialResText[metricsList[m.Metric.Name]] = m.Metric.Value

}
for _, trialParam := range t.Spec.ParameterAssignments {
trialResText[paramList[trialParam.Name]] = trialParam.Value
}
resultText += "\n" + t.Name + "," + strings.Join(trialResText, ",")
}
}
log.Printf("Logs parsed, results:\n %v", resultText)
response, err := json.Marshal(resultText)
if err != nil {
log.Printf("Marshal result text for HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}

func (k *KatibUIHandler) FetchHPJobTrialInfo(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
trialName := r.URL.Query()["trialName"][0]
conn, c := k.connectManager()
defer conn.Close()

resultText := "metricName,time,value\n"
obsLogResp, err := c.GetObservationLog(
context.Background(),
&api_pb_v1alpha3.GetObservationLogRequest{
TrialName: trialName,
StartTime: "",
EndTime: "",
},
)
if err != nil {
log.Printf("GetObservationLog failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
prevTime := ""
for _, m := range obsLogResp.ObservationLog.MetricLogs {
parsedTime, _ := time.Parse(time.RFC3339Nano, m.TimeStamp)
formatTime := parsedTime.Format("2006-01-02T15:4:5")
if formatTime != prevTime {
resultText += m.Metric.Name + "," + formatTime + "," + m.Metric.Value + "\n"
prevTime = formatTime
}
}

response, err := json.Marshal(resultText)
if err != nil {
log.Printf("Marshal result text in Trial info failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}

// FetchTrialTemplates gets the trial templates for the given namespace.
func (k *KatibUIHandler) FetchTrialTemplates(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const NASJobList = (props) => {
icon = (<HighlightOffIcon className={classes.failed}/>)
}
return (
<ListItem button key={i} component={Link} to={`/katib/nas_monitor/${job.name}`}>
<ListItem button key={i} component={Link} to={`/katib/hp_monitor/${job.namespace}/${job.name}`}>
<ListItemIcon>
{icon}
</ListItemIcon>
<ListItemText inset primary={job.name} />
<ListItemText inset primary={`${job.name}`} secondary={job.namespace} />
<ListItemSecondaryAction>
<IconButton aria-label={"Delete"} onClick={onDeleteExperiment(job.name)}>
<IconButton aria-label={"Delete"} onClick={onDeleteExperiment(job.name, job.namespace)}>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
Expand Down
152 changes: 152 additions & 0 deletions pkg/ui/v1alpha3/hp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package v1alpha3

import (
"context"
"encoding/json"
"log"
"net/http"
"strings"
"time"

trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3"
api_pb_v1alpha3 "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3"
)

// FetchAllHPJobs gets experiments in all namespaces.
func (k *KatibUIHandler) FetchAllHPJobs(w http.ResponseWriter, r *http.Request) {
// Use "" to get experiments in all namespaces.
jobs, err := k.getExperimentList("", JobTypeHP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(jobs)
if err != nil {
log.Printf("Marshal HP jobs failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}

func (k *KatibUIHandler) FetchHPJobInfo(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
experimentName := r.URL.Query()["experimentName"][0]
namespace := r.URL.Query()["namespace"][0]

conn, c := k.connectManager()
defer conn.Close()

resultText := "trialName"
experiment, err := k.katibClient.GetExperiment(experimentName, namespace)
if err != nil {
log.Printf("GetExperiment from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Got Experiment")
metricsList := map[string]int{}
metricsName := experiment.Spec.Objective.ObjectiveMetricName
resultText += "," + metricsName
metricsList[metricsName] = 0
for i, m := range experiment.Spec.Objective.AdditionalMetricNames {
resultText += "," + m
metricsList[m] = i + 1
}
log.Printf("Got metrics names")
paramList := map[string]int{}
for i, p := range experiment.Spec.Parameters {
resultText += "," + p.Name
paramList[p.Name] = i + len(metricsList)
}
log.Printf("Got Parameters names")

trialList, err := k.katibClient.GetTrialList(experimentName)
if err != nil {
log.Printf("GetTrialList from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Got Trial List")

for _, t := range trialList.Items {
succeeded := false
for _, condition := range t.Status.Conditions {
if condition.Type == trialsv1alpha3.TrialSucceeded {
succeeded = true
}
}
if succeeded {
obsLogResp, err := c.GetObservationLog(
context.Background(),
&api_pb_v1alpha3.GetObservationLogRequest{
TrialName: t.Name,
StartTime: "",
EndTime: "",
},
)
if err != nil {
log.Printf("GetObservationLog from HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

trialResText := make([]string, len(metricsList)+len(paramList))
for _, m := range obsLogResp.ObservationLog.MetricLogs {
trialResText[metricsList[m.Metric.Name]] = m.Metric.Value

}
for _, trialParam := range t.Spec.ParameterAssignments {
trialResText[paramList[trialParam.Name]] = trialParam.Value
}
resultText += "\n" + t.Name + "," + strings.Join(trialResText, ",")
}
}
log.Printf("Logs parsed, results:\n %v", resultText)
response, err := json.Marshal(resultText)
if err != nil {
log.Printf("Marshal result text for HP job failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}

func (k *KatibUIHandler) FetchHPJobTrialInfo(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
trialName := r.URL.Query()["trialName"][0]
conn, c := k.connectManager()
defer conn.Close()

resultText := "metricName,time,value\n"
obsLogResp, err := c.GetObservationLog(
context.Background(),
&api_pb_v1alpha3.GetObservationLogRequest{
TrialName: trialName,
StartTime: "",
EndTime: "",
},
)
if err != nil {
log.Printf("GetObservationLog failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
prevTime := ""
for _, m := range obsLogResp.ObservationLog.MetricLogs {
parsedTime, _ := time.Parse(time.RFC3339Nano, m.TimeStamp)
formatTime := parsedTime.Format("2006-01-02T15:4:5")
if formatTime != prevTime {
resultText += m.Metric.Name + "," + formatTime + "," + m.Metric.Value + "\n"
prevTime = formatTime
}
}

response, err := json.Marshal(resultText)
if err != nil {
log.Printf("Marshal result text in Trial info failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}
6 changes: 3 additions & 3 deletions pkg/ui/v1alpha3/nas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3"
api_pb_v1alpha3 "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3"
"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts"
)

func (k *KatibUIHandler) FetchNASJobs(w http.ResponseWriter, r *http.Request) {
func (k *KatibUIHandler) FetchAllNASJobs(w http.ResponseWriter, r *http.Request) {
//enableCors(&w)
jobs, err := k.getExperimentList(consts.DefaultKatibNamespace, JobTypeNAS)
// Use "" to get experiments in all namespaces.
jobs, err := k.getExperimentList("", JobTypeNAS)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down

0 comments on commit f8d05cf

Please sign in to comment.