diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 78ea8ea72..65db818a0 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -48,6 +48,8 @@ var ( const BENCHMARK_TYPE_CODE = "repo.cloudbrain.benchmark.types" +var benchmarkTypesMap = make(map[string]*models.BenchmarkTypes, 0) + var jobNamePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-_]{1,34}[a-z0-9-]$`) // MustEnableDataset check if repository enable internal cb @@ -133,12 +135,7 @@ func cloudBrainNewDataPrepare(ctx *context.Context) error { } ctx.Data["benchmark_categories"] = categories.Category - if benchmarkTypes == nil { - if err := json.Unmarshal([]byte(i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE)), &benchmarkTypes); err != nil { - log.Error("json.Unmarshal BenchmarkTypes(%s) failed:%v", i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE), err, ctx.Data["MsgID"]) - } - } - ctx.Data["benchmark_types"] = benchmarkTypes.BenchmarkType + ctx.Data["benchmark_types"] = GetBenchmarkTypes(ctx).BenchmarkType if gpuInfos == nil { json.Unmarshal([]byte(setting.GpuTypes), &gpuInfos) @@ -342,13 +339,6 @@ func CloudBrainRestart(ctx *context.Context) { } func CloudBrainBenchMarkShow(ctx *context.Context) { - if benchmarkTypes == nil { - if err := json.Unmarshal([]byte(i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE)), &benchmarkTypes); err != nil { - log.Error("json.Unmarshal BenchmarkTypes(%s) failed:%v", i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE), err, ctx.Data["MsgID"]) - ctx.ServerError(err.Error(), err) - return - } - } cloudBrainShow(ctx, tplCloudBrainBenchmarkShow) } @@ -415,7 +405,7 @@ func cloudBrainShow(ctx *context.Context, tpName base.TplName) { duration = int64(task.UpdatedUnix) - int64(task.CreatedUnix) } if task.BenchmarkTypeID > 0 { - for _, benchmarkType := range benchmarkTypes.BenchmarkType { + for _, benchmarkType := range GetBenchmarkTypes(ctx).BenchmarkType { if task.BenchmarkTypeID == benchmarkType.Id { ctx.Data["BenchmarkTypeName"] = benchmarkType.First for _, benchmarkChildType := range benchmarkType.Second { @@ -1062,13 +1052,6 @@ func CloudBrainBenchmarkIndex(ctx *context.Context) { return } - if benchmarkTypes == nil { - if err := json.Unmarshal([]byte(i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE)), &benchmarkTypes); err != nil { - ctx.ServerError("Get BenchmarkTypes faild:", err) - return - } - } - for i, task := range ciTasks { ciTasks[i].CanDel = cloudbrain.CanDeleteJob(ctx, &task.Cloudbrain) ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource @@ -1081,7 +1064,7 @@ func CloudBrainBenchmarkIndex(ctx *context.Context) { ciTasks[i].TrainJobDuration = util.AddZero(duration/3600000) + ":" + util.AddZero(duration%3600000/60000) + ":" + util.AddZero(duration%60000/1000) ciTasks[i].BenchmarkTypeName = "" if task.BenchmarkTypeID > 0 { - for _, benchmarkType := range benchmarkTypes.BenchmarkType { + for _, benchmarkType := range GetBenchmarkTypes(ctx).BenchmarkType { if task.BenchmarkTypeID == benchmarkType.Id { ciTasks[i].BenchmarkTypeRankLink = benchmarkType.RankLink ciTasks[i].BenchmarkTypeName = benchmarkType.First @@ -1105,15 +1088,8 @@ func GetChildTypes(ctx *context.Context) { benchmarkTypeID := ctx.QueryInt("benchmark_type_id") re := make(map[string]interface{}) for { - if benchmarkTypes == nil { - if err := json.Unmarshal([]byte(i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE)), &benchmarkTypes); err != nil { - log.Error("json.Unmarshal BenchmarkTypes(%s) failed:%v", i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE), err, ctx.Data["MsgID"]) - re["errMsg"] = "system error" - break - } - } var isExist bool - for _, benchmarkType := range benchmarkTypes.BenchmarkType { + for _, benchmarkType := range GetBenchmarkTypes(ctx).BenchmarkType { if benchmarkTypeID == benchmarkType.Id { isExist = true re["child_types"] = benchmarkType.Second @@ -1146,15 +1122,9 @@ func CloudBrainBenchmarkNew(ctx *context.Context) { func getBenchmarkAttachment(benchmarkTypeID, benchmarkChildTypeID int, ctx *context.Context) (*models.BenchmarkDataset, error) { var childInfo *models.BenchmarkDataset - if benchmarkTypes == nil { - if err := json.Unmarshal([]byte(i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE)), &benchmarkTypes); err != nil { - log.Error("json.Unmarshal BenchmarkTypes(%s) failed:%v", i18n.Tr(ctx.Locale.Language(), BENCHMARK_TYPE_CODE), err) - return childInfo, err - } - } var isExist bool - for _, benchmarkType := range benchmarkTypes.BenchmarkType { + for _, benchmarkType := range GetBenchmarkTypes(ctx).BenchmarkType { if benchmarkType.Id == benchmarkTypeID { for _, childType := range benchmarkType.Second { if childType.Id == benchmarkChildTypeID { @@ -1398,3 +1368,17 @@ func BenchmarkDel(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain/benchmark") } } + +func GetBenchmarkTypes(ctx *context.Context) *models.BenchmarkTypes { + var lang = ctx.Locale.Language() + if benchmarkTypesMap[lang] == nil { + var val = i18n.Tr(lang, BENCHMARK_TYPE_CODE) + var tempType *models.BenchmarkTypes + if err := json.Unmarshal([]byte(val), &tempType); err != nil { + log.Error("json.Unmarshal BenchmarkTypes(%s) failed:%v", val, err, ctx.Data["MsgID"]) + return &models.BenchmarkTypes{} + } + benchmarkTypesMap[lang] = tempType + } + return benchmarkTypesMap[lang] +}