graph.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "net/http"
  19. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/conf"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func resetGraph(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. graph := conf.NewGlobalGraph()
  29. model.Conf.Graph.Global = graph
  30. model.Conf.Save()
  31. ret.Data = map[string]interface{}{
  32. "conf": graph,
  33. }
  34. }
  35. func resetLocalGraph(c *gin.Context) {
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. graph := conf.NewLocalGraph()
  39. model.Conf.Graph.Local = graph
  40. model.Conf.Save()
  41. ret.Data = map[string]interface{}{
  42. "conf": graph,
  43. }
  44. }
  45. func getGraph(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. arg, ok := util.JsonArg(c, ret)
  49. if !ok {
  50. return
  51. }
  52. reqId := arg["reqId"]
  53. ret.Data = map[string]interface{}{"reqId": reqId}
  54. query := arg["k"].(string)
  55. graphConf, err := gulu.JSON.MarshalJSON(arg["conf"])
  56. if nil != err {
  57. ret.Code = -1
  58. ret.Msg = err.Error()
  59. return
  60. }
  61. global := conf.NewGlobalGraph()
  62. if err = gulu.JSON.UnmarshalJSON(graphConf, global); nil != err {
  63. ret.Code = -1
  64. ret.Msg = err.Error()
  65. return
  66. }
  67. model.Conf.Graph.Global = global
  68. model.Conf.Save()
  69. boxID, nodes, links := model.BuildGraph(query)
  70. ret.Data = map[string]interface{}{
  71. "nodes": nodes,
  72. "links": links,
  73. "conf": global,
  74. "box": boxID,
  75. "reqId": arg["reqId"],
  76. }
  77. util.RandomSleep(200, 500)
  78. }
  79. func getLocalGraph(c *gin.Context) {
  80. ret := gulu.Ret.NewResult()
  81. defer c.JSON(http.StatusOK, ret)
  82. arg, ok := util.JsonArg(c, ret)
  83. if !ok {
  84. return
  85. }
  86. reqId := arg["reqId"]
  87. ret.Data = map[string]interface{}{"reqId": reqId}
  88. if nil == arg["id"] {
  89. return
  90. }
  91. keyword := arg["k"].(string)
  92. id := arg["id"].(string)
  93. graphConf, err := gulu.JSON.MarshalJSON(arg["conf"])
  94. if nil != err {
  95. ret.Code = -1
  96. ret.Msg = err.Error()
  97. return
  98. }
  99. local := conf.NewLocalGraph()
  100. if err = gulu.JSON.UnmarshalJSON(graphConf, local); nil != err {
  101. ret.Code = -1
  102. ret.Msg = err.Error()
  103. return
  104. }
  105. model.Conf.Graph.Local = local
  106. model.Conf.Save()
  107. boxID, nodes, links := model.BuildTreeGraph(id, keyword)
  108. ret.Data = map[string]interface{}{
  109. "id": id,
  110. "box": boxID,
  111. "nodes": nodes,
  112. "links": links,
  113. "conf": local,
  114. "reqId": arg["reqId"],
  115. }
  116. util.RandomSleep(200, 500)
  117. }