1
0

graph3.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import argparse
  2. import cgi
  3. import datetime
  4. import pydot
  5. import simplejson as json
  6. from synapse.events import FrozenEvent
  7. from synapse.util.frozenutils import unfreeze
  8. # Copyright 2016 OpenMarket Ltd
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License");
  11. # you may not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS,
  18. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. def make_graph(file_name, room_id, file_prefix, limit):
  22. print("Reading lines")
  23. with open(file_name) as f:
  24. lines = f.readlines()
  25. print("Read lines")
  26. events = [FrozenEvent(json.loads(line)) for line in lines]
  27. print("Loaded events.")
  28. events.sort(key=lambda e: e.depth)
  29. print("Sorted events")
  30. if limit:
  31. events = events[-int(limit) :]
  32. node_map = {}
  33. graph = pydot.Dot(graph_name="Test")
  34. for event in events:
  35. t = datetime.datetime.fromtimestamp(
  36. float(event.origin_server_ts) / 1000
  37. ).strftime("%Y-%m-%d %H:%M:%S,%f")
  38. content = json.dumps(unfreeze(event.get_dict()["content"]), indent=4)
  39. content = content.replace("\n", "<br/>\n")
  40. print(content)
  41. content = []
  42. for key, value in unfreeze(event.get_dict()["content"]).items():
  43. if value is None:
  44. value = "<null>"
  45. elif isinstance(value, str):
  46. pass
  47. else:
  48. value = json.dumps(value)
  49. content.append(
  50. "<b>%s</b>: %s,"
  51. % (
  52. cgi.escape(key, quote=True).encode("ascii", "xmlcharrefreplace"),
  53. cgi.escape(value, quote=True).encode("ascii", "xmlcharrefreplace"),
  54. )
  55. )
  56. content = "<br/>\n".join(content)
  57. print(content)
  58. label = (
  59. "<"
  60. "<b>%(name)s </b><br/>"
  61. "Type: <b>%(type)s </b><br/>"
  62. "State key: <b>%(state_key)s </b><br/>"
  63. "Content: <b>%(content)s </b><br/>"
  64. "Time: <b>%(time)s </b><br/>"
  65. "Depth: <b>%(depth)s </b><br/>"
  66. ">"
  67. ) % {
  68. "name": event.event_id,
  69. "type": event.type,
  70. "state_key": event.get("state_key", None),
  71. "content": content,
  72. "time": t,
  73. "depth": event.depth,
  74. }
  75. node = pydot.Node(name=event.event_id, label=label)
  76. node_map[event.event_id] = node
  77. graph.add_node(node)
  78. print("Created Nodes")
  79. for event in events:
  80. for prev_id, _ in event.prev_events:
  81. try:
  82. end_node = node_map[prev_id]
  83. except Exception:
  84. end_node = pydot.Node(name=prev_id, label="<<b>%s</b>>" % (prev_id,))
  85. node_map[prev_id] = end_node
  86. graph.add_node(end_node)
  87. edge = pydot.Edge(node_map[event.event_id], end_node)
  88. graph.add_edge(edge)
  89. print("Created edges")
  90. graph.write("%s.dot" % file_prefix, format="raw", prog="dot")
  91. print("Created Dot")
  92. graph.write_svg("%s.svg" % file_prefix, prog="dot")
  93. print("Created svg")
  94. if __name__ == "__main__":
  95. parser = argparse.ArgumentParser(
  96. description="Generate a PDU graph for a given room by reading "
  97. "from a file with line deliminated events. \n"
  98. "Requires pydot."
  99. )
  100. parser.add_argument(
  101. "-p",
  102. "--prefix",
  103. dest="prefix",
  104. help="String to prefix output files with",
  105. default="graph_output",
  106. )
  107. parser.add_argument("-l", "--limit", help="Only retrieve the last N events.")
  108. parser.add_argument("event_file")
  109. parser.add_argument("room")
  110. args = parser.parse_args()
  111. make_graph(args.event_file, args.room, args.prefix, args.limit)