| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // 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.NewOk(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.NewOk(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, ".", ".<br>")
- 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
- }
|