1
0

graph3.py 4.4 KB

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