2016-01-21 13:19:05 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-01-21 13:19:05 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2022-06-10 15:30:14 +03:00
|
|
|
import argparse
|
|
|
|
import datetime
|
|
|
|
import html
|
|
|
|
import json
|
|
|
|
|
|
|
|
import pydot
|
2016-01-21 13:19:05 +03:00
|
|
|
|
2022-06-10 15:30:14 +03:00
|
|
|
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
|
|
|
from synapse.events import make_event_from_dict
|
|
|
|
from synapse.util.frozenutils import unfreeze
|
|
|
|
|
|
|
|
|
|
|
|
def make_graph(file_name: str, file_prefix: str, limit: int) -> None:
|
|
|
|
"""
|
|
|
|
Generate a dot and SVG file for a graph of events in the room based on the
|
|
|
|
topological ordering by reading line-delimited JSON from a file.
|
|
|
|
"""
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Reading lines")
|
2016-01-21 13:19:05 +03:00
|
|
|
with open(file_name) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Read lines")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
2022-06-10 15:30:14 +03:00
|
|
|
# Figure out the room version, assume the first line is the create event.
|
|
|
|
room_version = KNOWN_ROOM_VERSIONS[
|
|
|
|
json.loads(lines[0]).get("content", {}).get("room_version")
|
|
|
|
]
|
|
|
|
|
|
|
|
events = [make_event_from_dict(json.loads(line), room_version) for line in lines]
|
2016-01-21 13:19:05 +03:00
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Loaded events.")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
events.sort(key=lambda e: e.depth)
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Sorted events")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
if limit:
|
|
|
|
events = events[-int(limit) :]
|
|
|
|
|
|
|
|
node_map = {}
|
|
|
|
|
|
|
|
graph = pydot.Dot(graph_name="Test")
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
t = datetime.datetime.fromtimestamp(
|
|
|
|
float(event.origin_server_ts) / 1000
|
|
|
|
).strftime("%Y-%m-%d %H:%M:%S,%f")
|
|
|
|
|
|
|
|
content = json.dumps(unfreeze(event.get_dict()["content"]), indent=4)
|
|
|
|
content = content.replace("\n", "<br/>\n")
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print(content)
|
2016-01-21 13:19:05 +03:00
|
|
|
content = []
|
|
|
|
for key, value in unfreeze(event.get_dict()["content"]).items():
|
|
|
|
if value is None:
|
|
|
|
value = "<null>"
|
2020-06-16 15:51:47 +03:00
|
|
|
elif isinstance(value, str):
|
2016-01-21 13:19:05 +03:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
value = json.dumps(value)
|
|
|
|
|
|
|
|
content.append(
|
|
|
|
"<b>%s</b>: %s,"
|
|
|
|
% (
|
2022-06-10 15:30:14 +03:00
|
|
|
html.escape(key, quote=True).encode("ascii", "xmlcharrefreplace"),
|
|
|
|
html.escape(value, quote=True).encode("ascii", "xmlcharrefreplace"),
|
2016-01-21 13:19:05 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
content = "<br/>\n".join(content)
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print(content)
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
label = (
|
|
|
|
"<"
|
|
|
|
"<b>%(name)s </b><br/>"
|
|
|
|
"Type: <b>%(type)s </b><br/>"
|
|
|
|
"State key: <b>%(state_key)s </b><br/>"
|
|
|
|
"Content: <b>%(content)s </b><br/>"
|
|
|
|
"Time: <b>%(time)s </b><br/>"
|
|
|
|
"Depth: <b>%(depth)s </b><br/>"
|
|
|
|
">"
|
|
|
|
) % {
|
|
|
|
"name": event.event_id,
|
|
|
|
"type": event.type,
|
|
|
|
"state_key": event.get("state_key", None),
|
|
|
|
"content": content,
|
|
|
|
"time": t,
|
|
|
|
"depth": event.depth,
|
|
|
|
}
|
|
|
|
|
|
|
|
node = pydot.Node(name=event.event_id, label=label)
|
|
|
|
|
|
|
|
node_map[event.event_id] = node
|
|
|
|
graph.add_node(node)
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Created Nodes")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
for event in events:
|
2022-06-10 15:30:14 +03:00
|
|
|
for prev_id in event.prev_event_ids():
|
2016-01-21 13:19:05 +03:00
|
|
|
try:
|
|
|
|
end_node = node_map[prev_id]
|
2020-07-20 23:43:49 +03:00
|
|
|
except Exception:
|
2022-06-10 15:30:14 +03:00
|
|
|
end_node = pydot.Node(name=prev_id, label=f"<<b>{prev_id}</b>>")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
node_map[prev_id] = end_node
|
|
|
|
graph.add_node(end_node)
|
|
|
|
|
|
|
|
edge = pydot.Edge(node_map[event.event_id], end_node)
|
|
|
|
graph.add_edge(edge)
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Created edges")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
graph.write("%s.dot" % file_prefix, format="raw", prog="dot")
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Created Dot")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
|
|
|
graph.write_svg("%s.svg" % file_prefix, prog="dot")
|
|
|
|
|
2019-06-17 20:21:30 +03:00
|
|
|
print("Created svg")
|
2016-01-21 13:19:05 +03:00
|
|
|
|
2019-06-20 12:32:02 +03:00
|
|
|
|
2016-01-21 13:19:05 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Generate a PDU graph for a given room by reading "
|
|
|
|
"from a file with line deliminated events. \n"
|
|
|
|
"Requires pydot."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--prefix",
|
|
|
|
dest="prefix",
|
|
|
|
help="String to prefix output files with",
|
|
|
|
default="graph_output",
|
|
|
|
)
|
|
|
|
parser.add_argument("-l", "--limit", help="Only retrieve the last N events.")
|
|
|
|
parser.add_argument("event_file")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-06-10 15:30:14 +03:00
|
|
|
make_graph(args.event_file, args.prefix, args.limit)
|