page_module.go 7.0 KB

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