graph2.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright 2014 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 sqlite3
  15. import pydot
  16. import cgi
  17. import json
  18. import datetime
  19. import argparse
  20. from synapse.events import FrozenEvent
  21. def make_graph(db_name, room_id, file_prefix, limit):
  22. conn = sqlite3.connect(db_name)
  23. sql = (
  24. "SELECT json FROM event_json as j "
  25. "INNER JOIN events as e ON e.event_id = j.event_id "
  26. "WHERE j.room_id = ?"
  27. )
  28. args = [room_id]
  29. if limit:
  30. sql += (
  31. " ORDER BY topological_ordering DESC, stream_ordering DESC "
  32. "LIMIT ?"
  33. )
  34. args.append(limit)
  35. c = conn.execute(sql, args)
  36. events = [FrozenEvent(json.loads(e[0])) for e in c.fetchall()]
  37. events.sort(key=lambda e: e.depth)
  38. node_map = {}
  39. state_groups = {}
  40. graph = pydot.Dot(graph_name="Test")
  41. for event in events:
  42. c = conn.execute(
  43. "SELECT state_group FROM event_to_state_groups "
  44. "WHERE event_id = ?",
  45. (event.event_id,)
  46. )
  47. res = c.fetchone()
  48. state_group = res[0] if res else None
  49. if state_group is not None:
  50. state_groups.setdefault(state_group, []).append(event.event_id)
  51. t = datetime.datetime.fromtimestamp(
  52. float(event.origin_server_ts) / 1000
  53. ).strftime('%Y-%m-%d %H:%M:%S,%f')
  54. content = json.dumps(event.get_dict()["content"])
  55. label = (
  56. "<"
  57. "<b>%(name)s </b><br/>"
  58. "Type: <b>%(type)s </b><br/>"
  59. "State key: <b>%(state_key)s </b><br/>"
  60. "Content: <b>%(content)s </b><br/>"
  61. "Time: <b>%(time)s </b><br/>"
  62. "Depth: <b>%(depth)s </b><br/>"
  63. "State group: %(state_group)s<br/>"
  64. ">"
  65. ) % {
  66. "name": event.event_id,
  67. "type": event.type,
  68. "state_key": event.get("state_key", None),
  69. "content": cgi.escape(content, quote=True),
  70. "time": t,
  71. "depth": event.depth,
  72. "state_group": state_group,
  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. for event in events:
  81. for prev_id, _ in event.prev_events:
  82. try:
  83. end_node = node_map[prev_id]
  84. except:
  85. end_node = pydot.Node(
  86. name=prev_id,
  87. label="<<b>%s</b>>" % (prev_id,),
  88. )
  89. node_map[prev_id] = end_node
  90. graph.add_node(end_node)
  91. edge = pydot.Edge(node_map[event.event_id], end_node)
  92. graph.add_edge(edge)
  93. for group, event_ids in state_groups.items():
  94. if len(event_ids) <= 1:
  95. continue
  96. cluster = pydot.Cluster(
  97. str(group),
  98. label="<State Group: %s>" % (str(group),)
  99. )
  100. for event_id in event_ids:
  101. cluster.add_node(node_map[event_id])
  102. graph.add_subgraph(cluster)
  103. graph.write('%s.dot' % file_prefix, format='raw', prog='dot')
  104. graph.write_svg("%s.svg" % file_prefix, prog='dot')
  105. if __name__ == "__main__":
  106. parser = argparse.ArgumentParser(
  107. description="Generate a PDU graph for a given room by talking "
  108. "to the given homeserver to get the list of PDUs. \n"
  109. "Requires pydot."
  110. )
  111. parser.add_argument(
  112. "-p", "--prefix", dest="prefix",
  113. help="String to prefix output files with",
  114. default="graph_output"
  115. )
  116. parser.add_argument(
  117. "-l", "--limit",
  118. help="Only retrieve the last N events.",
  119. )
  120. parser.add_argument('db')
  121. parser.add_argument('room')
  122. args = parser.parse_args()
  123. make_graph(args.db, args.room, args.prefix, args.limit)