1
0

graph2.py 4.6 KB

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