page_module.go 7.2 KB

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