1
0

graph3.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 pydot
  15. import cgi
  16. import simplejson as json
  17. import datetime
  18. import argparse
  19. from synapse.events import FrozenEvent
  20. from synapse.util.frozenutils import unfreeze
  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, basestring):
  46. pass
  47. else:
  48. value = json.dumps(value)
  49. content.append(
  50. "<b>%s</b>: %s," % (
  51. cgi.escape(key, quote=True).encode("ascii", 'xmlcharrefreplace'),
  52. cgi.escape(value, quote=True).encode("ascii", 'xmlcharrefreplace'),
  53. )
  54. )
  55. content = "<br/>\n".join(content)
  56. print content
  57. label = (
  58. "<"
  59. "<b>%(name)s </b><br/>"
  60. "Type: <b>%(type)s </b><br/>"
  61. "State key: <b>%(state_key)s </b><br/>"
  62. "Content: <b>%(content)s </b><br/>"
  63. "Time: <b>%(time)s </b><br/>"
  64. "Depth: <b>%(depth)s </b><br/>"
  65. ">"
  66. ) % {
  67. "name": event.event_id,
  68. "type": event.type,
  69. "state_key": event.get("state_key", None),
  70. "content": content,
  71. "time": t,
  72. "depth": event.depth,
  73. }
  74. node = pydot.Node(
  75. name=event.event_id,
  76. label=label,
  77. )
  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(
  87. name=prev_id,
  88. label="<<b>%s</b>>" % (prev_id,),
  89. )
  90. node_map[prev_id] = end_node
  91. graph.add_node(end_node)
  92. edge = pydot.Edge(node_map[event.event_id], end_node)
  93. graph.add_edge(edge)
  94. print "Created edges"
  95. graph.write('%s.dot' % file_prefix, format='raw', prog='dot')
  96. print "Created Dot"
  97. graph.write_svg("%s.svg" % file_prefix, prog='dot')
  98. print "Created svg"
  99. if __name__ == "__main__":
  100. parser = argparse.ArgumentParser(
  101. description="Generate a PDU graph for a given room by reading "
  102. "from a file with line deliminated events. \n"
  103. "Requires pydot."
  104. )
  105. parser.add_argument(
  106. "-p", "--prefix", dest="prefix",
  107. help="String to prefix output files with",
  108. default="graph_output"
  109. )
  110. parser.add_argument(
  111. "-l", "--limit",
  112. help="Only retrieve the last N events.",
  113. )
  114. parser.add_argument('event_file')
  115. parser.add_argument('room')
  116. args = parser.parse_args()
  117. make_graph(args.event_file, args.room, args.prefix, args.limit)