graph.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from __future__ import print_function
  2. # Copyright 2014-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 sqlite3
  16. import pydot
  17. import cgi
  18. import json
  19. import datetime
  20. import argparse
  21. import urllib2
  22. def make_name(pdu_id, origin):
  23. return "%s@%s" % (pdu_id, origin)
  24. def make_graph(pdus, room, filename_prefix):
  25. pdu_map = {}
  26. node_map = {}
  27. origins = set()
  28. colors = set(("red", "green", "blue", "yellow", "purple"))
  29. for pdu in pdus:
  30. origins.add(pdu.get("origin"))
  31. color_map = {color: color for color in colors if color in origins}
  32. colors -= set(color_map.values())
  33. color_map[None] = "black"
  34. for o in origins:
  35. if o in color_map:
  36. continue
  37. try:
  38. c = colors.pop()
  39. color_map[o] = c
  40. except:
  41. print("Run out of colours!")
  42. color_map[o] = "black"
  43. graph = pydot.Dot(graph_name="Test")
  44. for pdu in pdus:
  45. name = make_name(pdu.get("pdu_id"), pdu.get("origin"))
  46. pdu_map[name] = pdu
  47. t = datetime.datetime.fromtimestamp(float(pdu["ts"]) / 1000).strftime(
  48. "%Y-%m-%d %H:%M:%S,%f"
  49. )
  50. label = (
  51. "<"
  52. "<b>%(name)s </b><br/>"
  53. "Type: <b>%(type)s </b><br/>"
  54. "State key: <b>%(state_key)s </b><br/>"
  55. "Content: <b>%(content)s </b><br/>"
  56. "Time: <b>%(time)s </b><br/>"
  57. "Depth: <b>%(depth)s </b><br/>"
  58. ">"
  59. ) % {
  60. "name": name,
  61. "type": pdu.get("pdu_type"),
  62. "state_key": pdu.get("state_key"),
  63. "content": cgi.escape(json.dumps(pdu.get("content")), quote=True),
  64. "time": t,
  65. "depth": pdu.get("depth"),
  66. }
  67. node = pydot.Node(name=name, label=label, color=color_map[pdu.get("origin")])
  68. node_map[name] = node
  69. graph.add_node(node)
  70. for pdu in pdus:
  71. start_name = make_name(pdu.get("pdu_id"), pdu.get("origin"))
  72. for i, o in pdu.get("prev_pdus", []):
  73. end_name = make_name(i, o)
  74. if end_name not in node_map:
  75. print("%s not in nodes" % end_name)
  76. continue
  77. edge = pydot.Edge(node_map[start_name], node_map[end_name])
  78. graph.add_edge(edge)
  79. # Add prev_state edges, if they exist
  80. if pdu.get("prev_state_id") and pdu.get("prev_state_origin"):
  81. prev_state_name = make_name(
  82. pdu.get("prev_state_id"), pdu.get("prev_state_origin")
  83. )
  84. if prev_state_name in node_map:
  85. state_edge = pydot.Edge(
  86. node_map[start_name], node_map[prev_state_name], style="dotted"
  87. )
  88. graph.add_edge(state_edge)
  89. graph.write("%s.dot" % filename_prefix, format="raw", prog="dot")
  90. # graph.write_png("%s.png" % filename_prefix, prog='dot')
  91. graph.write_svg("%s.svg" % filename_prefix, prog="dot")
  92. def get_pdus(host, room):
  93. transaction = json.loads(
  94. urllib2.urlopen(
  95. "http://%s/_matrix/federation/v1/context/%s/" % (host, room)
  96. ).read()
  97. )
  98. return transaction["pdus"]
  99. if __name__ == "__main__":
  100. parser = argparse.ArgumentParser(
  101. description="Generate a PDU graph for a given room by talking "
  102. "to the given homeserver to get the list of PDUs. \n"
  103. "Requires pydot."
  104. )
  105. parser.add_argument(
  106. "-p", "--prefix", dest="prefix", help="String to prefix output files with"
  107. )
  108. parser.add_argument("host")
  109. parser.add_argument("room")
  110. args = parser.parse_args()
  111. host = args.host
  112. room = args.room
  113. prefix = args.prefix if args.prefix else "%s_graph" % (room)
  114. pdus = get_pdus(host, room)
  115. make_graph(pdus, room, prefix)