1
0

graph3.py 4.1 KB

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