// package page_module -- страница показа модуля. package page_module import ( _ "embed" "fmt" "strings" "github.com/gofiber/fiber/v3" mL0 "gitp78su.ipnodns.ru/svi/kern/v4/lev0" mKs "gitp78su.ipnodns.ru/svi/kern/v4/lev0/kspec" "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kctx" ) // PageModule -- страница показа модуля. type PageModule struct { ctx mKs.IKernelCtx } var page *PageModule // GetPageModule -- возвращает страницу модуля. func GetPageModule() mL0.IResult[*PageModule] { if page != nil { return mL0.NewRes(page) } kCtx := kctx.GetKernelCtx() optFiber := kCtx.Get("fiberApp") if optFiber.IsNone() { err := fmt.Errorf("GetPageModule(): not found fiberApp in kernel ctx") return mL0.NewErr[*PageModule](err) } fiberApp := optFiber.Some().Val().(*fiber.App) kCtx.Set("fiberApp", fiberApp, "fiberApp in GetPageModule()") sf := &PageModule{ ctx: kCtx, } fiberApp.Post("/module/:id", sf.postModule) fiberApp.Post("/module_state/:id", sf.postModuleState) fiberApp.Post("/module_ctx/:id", sf.postModuleCtx) fiberApp.Post("/module_log/:id", sf.postModuleLog) fiberApp.Post("/module_svg_sec/svg_sec_:id.svg", sf.postSvgSec) fiberApp.Post("/module_svg_min/svg_min_:id.svg", sf.postSvgMin) fiberApp.Post("/module_svg_day/svg_day_:id.svg", sf.postSvgDay) page = sf return mL0.NewRes(sf) } //go:embed log_block.html var strLogBlock string // Возвращает страницу лога модуля. func (sf *PageModule) postModuleLog(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, _ := sf.getModule(id) if module == nil { strOut := strings.ReplaceAll(strLogBlock, "{.id}", id) strOut = strings.ReplaceAll(strOut, "{.log}", strOut) strOut = strings.ReplaceAll(strOut, "{.name}", "not found") strOut = strings.ReplaceAll(strOut, "{.mod_state}", "") return ctx.SendString(strOut) } _log := module.Log() if module.Name().Get() == "kCtx" { _log = sf.ctx.Log() } strOut := "" for i := range 100 { msg := _log.Get(i).String() if strings.Contains(msg, "*no msg*") { continue } strOut += msg + "\n" } strOut = strings.ReplaceAll(strLogBlock, "{.log}", strOut) strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get()) strOut = strings.ReplaceAll(strOut, "{.id}", id) return ctx.SendString(strOut) } //go:embed ctx_row_val.html var strCtxRowVal string //go:embed ctx_row_block.html var strCtxRowBlock string // Возвращает блок контекста монолита. func (sf *PageModule) postModuleCtx(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, _ := sf.getModule(id) if module == nil { strOut := strings.ReplaceAll(strCtxRowBlock, "{.id}", id) strOut = strings.ReplaceAll(strOut, "{.name}", "not found") strOut = strings.ReplaceAll(strOut, "{.ctx_block}", "") return ctx.SendString(strOut) } mCtx := module.Ctx() lst := mCtx.SortedList() if module.Name().Get() == "kCtx" { lst = sf.ctx.SortedList() } strOut := "" for _, val := range lst { strRow := strCtxRowVal strRow = strings.ReplaceAll(strRow, "{.key}", val.Key()) _val := val.Val() valShort := fmt.Sprint(_val) runes := []rune(valShort) if len(runes) > 20 { runes = runes[:20] valShort = string(runes) } strRow = strings.ReplaceAll(strRow, "{.value}", valShort) _type := fmt.Sprintf("%#T", _val) _type = strings.ReplaceAll(_type, ".", ".
") strRow = strings.ReplaceAll(strRow, "{.type}", _type) strRow = strings.ReplaceAll(strRow, "{.createAt}", val.CreateAt().String()) strRow = strings.ReplaceAll(strRow, "{.updateAt}", val.UpdateAt().String()) strRow = strings.ReplaceAll(strRow, "{.comment}", val.Comment()) strOut += strRow } strOut = strings.ReplaceAll(strCtxRowBlock, "{.ctx_block}", strOut) strOut = strings.ReplaceAll(strOut, "{.id}", id) strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get()) return ctx.SendString(strOut) } //go:embed module_state.html var strStateModule string //go:embed module_state_block.html var strStateModuleBlock string // Показывает состояние модуля. func (sf *PageModule) postModuleState(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, modVal := sf.getModule(id) if module == nil { strOut := strings.ReplaceAll(strStateModuleBlock, "{.id}", "") return ctx.SendString(strOut) } dictState := map[string]any{} dictState["{.name}"] = module.Name() dictState["{.createAt}"] = modVal.CreateAt() dictState["{.updateAt}"] = modVal.UpdateAt() dictState["{.comment}"] = modVal.Comment() dictState["{.id}"] = id dictState["{.live}"] = module.Live() dictState["{.svg_sec}"] = module.Stat().SvgSec() strOut := strStateModuleBlock for key, val := range dictState { strOut = strings.ReplaceAll(strOut, key, fmt.Sprint(val)) } return ctx.SendString(strOut) } // Показывает состояние модуля. func (sf *PageModule) postModule(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, modVal := sf.getModule(id) if module == nil { strOut := strings.ReplaceAll(strStateModule, "{.name}", "unknown") strOut = strings.ReplaceAll(strOut, "{.mod_state}", "") strOut = strings.ReplaceAll(strOut, "{.id}", "") return ctx.SendString(strOut) } dictState := map[string]any{} dictState["{.name}"] = module.Name() dictState["{.createAt}"] = modVal.CreateAt() dictState["{.updateAt}"] = modVal.UpdateAt() dictState["{.comment}"] = modVal.Comment() // dictState["{.id}"] = id dictState["{.live}"] = module.Live() dictState["{.svg_sec}"] = module.Stat().SvgSec() strOut := strStateModuleBlock for key, val := range dictState { strOut = strings.ReplaceAll(strOut, key, fmt.Sprint(val)) } { strOut = strings.ReplaceAll(strStateModule, "{.mod_state}", strOut) strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get()) strOut = strings.ReplaceAll(strOut, "{.id}", id) } return ctx.SendString(strOut) } // Возвращает секундную статистику модуля. func (sf *PageModule) postSvgDay(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, _ := sf.getModule(id) if module == nil { return ctx.SendString("") } strSvgSec := module.Stat().SvgDay() ctx.Set("Content-Type", "image/svg+xml") return ctx.SendString(strSvgSec) } // Возвращает секундную статистику модуля. func (sf *PageModule) postSvgMin(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, _ := sf.getModule(id) if module == nil { return ctx.SendString("") } strSvgSec := module.Stat().SvgMin() ctx.Set("Content-Type", "image/svg+xml") return ctx.SendString(strSvgSec) } // Возвращает секундную статистику модуля. func (sf *PageModule) postSvgSec(ctx fiber.Ctx) error { id := ctx.Params("id", "1") module, _ := sf.getModule(id) if module == nil { return ctx.SendString("") } strSvgSec := module.Stat().SvgSec() ctx.Set("Content-Type", "image/svg+xml") return ctx.SendString(strSvgSec) } // Возвращает модуль. func (sf *PageModule) getModule(id string) (mKs.IKernelModule, mKs.ICtxValue) { optMonolit := sf.ctx.Get("monolit") if optMonolit.IsNone() { return nil, nil } mon := optMonolit.Some().Val().(mKs.IKernelMonolit) chLst := mon.Ctx().SortedList() var ( val mKs.ICtxValue isFind bool ) for _, val = range chLst { name := "module_" + id if name == val.Key() { isFind = true break } } if !isFind { return nil, nil } mod := val.Val().(mKs.IKernelModule) return mod, val }