page_module.go 7.4 KB

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