page_module.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // package page_module -- страница показа модуля.
  2. package page_module
  3. import (
  4. _ "embed"
  5. "fmt"
  6. "strings"
  7. "github.com/gofiber/fiber/v3"
  8. mL0 "gitp78su.ipnodns.ru/svi/kern/v4/lev0"
  9. "gitp78su.ipnodns.ru/svi/kern/v4/lev1/comp_spec"
  10. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kctx"
  11. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kspec"
  12. )
  13. // PageModule -- страница показа модуля.
  14. type PageModule struct {
  15. ctx kspec.IKernelCtx
  16. }
  17. var page *PageModule
  18. // GetPageModule -- возвращает страницу модуля.
  19. func GetPageModule() mL0.IResult[*PageModule] {
  20. if page != nil {
  21. return mL0.NewOk(page)
  22. }
  23. kCtx := kctx.GetKernelCtx()
  24. optFiber := kCtx.Get("fiberApp")
  25. if optFiber.IsNone() {
  26. err := fmt.Errorf("GetPageModule(): not found fiberApp in kernel ctx")
  27. return mL0.NewErr[*PageModule](err)
  28. }
  29. fiberApp := optFiber.Some().Val().(*fiber.App)
  30. kCtx.Set("fiberApp", fiberApp, "fiberApp in GetPageModule()")
  31. sf := &PageModule{
  32. ctx: kCtx,
  33. }
  34. fiberApp.Post("/module/:id", sf.postModule)
  35. fiberApp.Post("/module_state/:id", sf.postModuleState)
  36. fiberApp.Post("/module_ctx/:id", sf.postModuleCtx)
  37. fiberApp.Post("/module_log/:id", sf.postModuleLog)
  38. fiberApp.Post("/module_svg_sec/svg_sec_:id.svg", sf.postSvgSec)
  39. fiberApp.Post("/module_svg_min/svg_min_:id.svg", sf.postSvgMin)
  40. fiberApp.Post("/module_svg_day/svg_day_:id.svg", sf.postSvgDay)
  41. page = sf
  42. return mL0.NewOk(sf)
  43. }
  44. //go:embed log_block.html
  45. var strLogBlock string
  46. // Возвращает страницу лога модуля.
  47. func (sf *PageModule) postModuleLog(ctx fiber.Ctx) error {
  48. id := ctx.Params("id", "1")
  49. module, _ := sf.getModule(id)
  50. if module == nil {
  51. strOut := strings.ReplaceAll(strLogBlock, "{.id}", id)
  52. strOut = strings.ReplaceAll(strOut, "{.log}", strOut)
  53. strOut = strings.ReplaceAll(strOut, "{.name}", "not found")
  54. strOut = strings.ReplaceAll(strOut, "{.mod_state}", "")
  55. return ctx.SendString(strOut)
  56. }
  57. _log := module.Log()
  58. if module.Name().Get() == "kCtx" {
  59. _log = sf.ctx.Log()
  60. }
  61. strOut := ""
  62. for i := range 100 {
  63. msg := _log.Get(i).String()
  64. if strings.Contains(msg, "*no msg*") {
  65. continue
  66. }
  67. strOut += msg + "\n"
  68. }
  69. strOut = strings.ReplaceAll(strLogBlock, "{.log}", strOut)
  70. strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get())
  71. strOut = strings.ReplaceAll(strOut, "{.id}", id)
  72. return ctx.SendString(strOut)
  73. }
  74. //go:embed ctx_row_val.html
  75. var strCtxRowVal string
  76. //go:embed ctx_row_block.html
  77. var strCtxRowBlock string
  78. // Возвращает блок контекста монолита.
  79. func (sf *PageModule) postModuleCtx(ctx fiber.Ctx) error {
  80. id := ctx.Params("id", "1")
  81. module, _ := sf.getModule(id)
  82. if module == nil {
  83. strOut := strings.ReplaceAll(strCtxRowBlock, "{.id}", id)
  84. strOut = strings.ReplaceAll(strOut, "{.name}", "not found")
  85. strOut = strings.ReplaceAll(strOut, "{.ctx_block}", "")
  86. return ctx.SendString(strOut)
  87. }
  88. mCtx := module.Ctx()
  89. lst := mCtx.SortedList()
  90. if module.Name().Get() == "kCtx" {
  91. lst = sf.ctx.SortedList()
  92. }
  93. strOut := ""
  94. for _, val := range lst {
  95. strRow := strCtxRowVal
  96. strRow = strings.ReplaceAll(strRow, "{.key}", val.Key())
  97. _val := val.Val()
  98. valShort := fmt.Sprint(_val)
  99. runes := []rune(valShort)
  100. if len(runes) > 20 {
  101. runes = runes[:20]
  102. valShort = string(runes)
  103. }
  104. strRow = strings.ReplaceAll(strRow, "{.value}", valShort)
  105. _type := fmt.Sprintf("%#T", _val)
  106. _type = strings.ReplaceAll(_type, ".", ".<br>")
  107. strRow = strings.ReplaceAll(strRow, "{.type}", _type)
  108. strRow = strings.ReplaceAll(strRow, "{.createAt}", val.CreateAt().String())
  109. strRow = strings.ReplaceAll(strRow, "{.updateAt}", val.UpdateAt().String())
  110. strRow = strings.ReplaceAll(strRow, "{.comment}", val.Comment())
  111. strOut += strRow
  112. }
  113. strOut = strings.ReplaceAll(strCtxRowBlock, "{.ctx_block}", strOut)
  114. strOut = strings.ReplaceAll(strOut, "{.id}", id)
  115. strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get())
  116. return ctx.SendString(strOut)
  117. }
  118. //go:embed module_state.html
  119. var strStateModule string
  120. //go:embed module_state_block.html
  121. var strStateModuleBlock string
  122. // Показывает состояние модуля.
  123. func (sf *PageModule) postModuleState(ctx fiber.Ctx) error {
  124. id := ctx.Params("id", "1")
  125. module, modVal := sf.getModule(id)
  126. if module == nil {
  127. strOut := strings.ReplaceAll(strStateModuleBlock, "{.id}", "")
  128. return ctx.SendString(strOut)
  129. }
  130. dictState := map[string]any{}
  131. dictState["{.name}"] = module.Name()
  132. dictState["{.createAt}"] = modVal.CreateAt()
  133. dictState["{.updateAt}"] = modVal.UpdateAt()
  134. dictState["{.comment}"] = modVal.Comment()
  135. dictState["{.id}"] = id
  136. dictState["{.live}"] = module.Live()
  137. dictState["{.svg_sec}"] = module.Stat().SvgSec()
  138. strOut := strStateModuleBlock
  139. for key, val := range dictState {
  140. strOut = strings.ReplaceAll(strOut, key, fmt.Sprint(val))
  141. }
  142. return ctx.SendString(strOut)
  143. }
  144. // Показывает состояние модуля.
  145. func (sf *PageModule) postModule(ctx fiber.Ctx) error {
  146. id := ctx.Params("id", "1")
  147. module, modVal := sf.getModule(id)
  148. if module == nil {
  149. strOut := strings.ReplaceAll(strStateModule, "{.name}", "unknown")
  150. strOut = strings.ReplaceAll(strOut, "{.mod_state}", "")
  151. strOut = strings.ReplaceAll(strOut, "{.id}", "")
  152. return ctx.SendString(strOut)
  153. }
  154. dictState := map[string]any{}
  155. dictState["{.name}"] = module.Name()
  156. dictState["{.createAt}"] = modVal.CreateAt()
  157. dictState["{.updateAt}"] = modVal.UpdateAt()
  158. dictState["{.comment}"] = modVal.Comment()
  159. // dictState["{.id}"] = id
  160. dictState["{.live}"] = module.Live()
  161. dictState["{.svg_sec}"] = module.Stat().SvgSec()
  162. strOut := strStateModuleBlock
  163. for key, val := range dictState {
  164. strOut = strings.ReplaceAll(strOut, key, fmt.Sprint(val))
  165. }
  166. {
  167. strOut = strings.ReplaceAll(strStateModule, "{.mod_state}", strOut)
  168. strOut = strings.ReplaceAll(strOut, "{.name}", module.Name().Get())
  169. strOut = strings.ReplaceAll(strOut, "{.id}", id)
  170. }
  171. return ctx.SendString(strOut)
  172. }
  173. // Возвращает секундную статистику модуля.
  174. func (sf *PageModule) postSvgDay(ctx fiber.Ctx) error {
  175. id := ctx.Params("id", "1")
  176. module, _ := sf.getModule(id)
  177. if module == nil {
  178. return ctx.SendString("")
  179. }
  180. strSvgSec := module.Stat().SvgDay()
  181. ctx.Set("Content-Type", "image/svg+xml")
  182. return ctx.SendString(strSvgSec)
  183. }
  184. // Возвращает секундную статистику модуля.
  185. func (sf *PageModule) postSvgMin(ctx fiber.Ctx) error {
  186. id := ctx.Params("id", "1")
  187. module, _ := sf.getModule(id)
  188. if module == nil {
  189. return ctx.SendString("")
  190. }
  191. strSvgSec := module.Stat().SvgMin()
  192. ctx.Set("Content-Type", "image/svg+xml")
  193. return ctx.SendString(strSvgSec)
  194. }
  195. // Возвращает секундную статистику модуля.
  196. func (sf *PageModule) postSvgSec(ctx fiber.Ctx) error {
  197. id := ctx.Params("id", "1")
  198. module, _ := sf.getModule(id)
  199. if module == nil {
  200. return ctx.SendString("")
  201. }
  202. strSvgSec := module.Stat().SvgSec()
  203. ctx.Set("Content-Type", "image/svg+xml")
  204. return ctx.SendString(strSvgSec)
  205. }
  206. // Возвращает модуль.
  207. func (sf *PageModule) getModule(id string) (kspec.IKernelModule, comp_spec.ICtxValue) {
  208. optMonolit := sf.ctx.Get("monolit")
  209. if optMonolit.IsNone() {
  210. return nil, nil
  211. }
  212. mon := optMonolit.Some().Val().(kspec.IKernelMonolit)
  213. chLst := mon.Ctx().SortedList()
  214. var (
  215. val comp_spec.ICtxValue
  216. isFind bool
  217. )
  218. for _, val = range chLst {
  219. name := "module_" + id
  220. if name == val.Key() {
  221. isFind = true
  222. break
  223. }
  224. }
  225. if !isFind {
  226. return nil, nil
  227. }
  228. mod := val.Val().(kspec.IKernelModule)
  229. return mod, val
  230. }