synapse-old/contrib/graph/graph3.py

149 lines
4.1 KiB
Python
Raw Normal View History

from __future__ import print_function
2019-06-20 03:32:02 -06:00
import argparse
import cgi
import datetime
import pydot
import simplejson as json
from synapse.events import FrozenEvent
from synapse.util.frozenutils import unfreeze
2016-01-21 03:19:05 -07:00
# Copyright 2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def make_graph(file_name, room_id, file_prefix, limit):
print("Reading lines")
2016-01-21 03:19:05 -07:00
with open(file_name) as f:
lines = f.readlines()
print("Read lines")
2016-01-21 03:19:05 -07:00
events = [FrozenEvent(json.loads(line)) for line in lines]
print("Loaded events.")
2016-01-21 03:19:05 -07:00
events.sort(key=lambda e: e.depth)
print("Sorted events")
2016-01-21 03:19:05 -07:00
if limit:
2019-06-20 03:32:02 -06:00
events = events[-int(limit) :]
2016-01-21 03:19:05 -07:00
node_map = {}
graph = pydot.Dot(graph_name="Test")
for event in events:
t = datetime.datetime.fromtimestamp(
float(event.origin_server_ts) / 1000
2019-06-20 03:32:02 -06:00
).strftime("%Y-%m-%d %H:%M:%S,%f")
2016-01-21 03:19:05 -07:00
content = json.dumps(unfreeze(event.get_dict()["content"]), indent=4)
content = content.replace("\n", "<br/>\n")
print(content)
2016-01-21 03:19:05 -07:00
content = []
for key, value in unfreeze(event.get_dict()["content"]).items():
if value is None:
value = "<null>"
elif isinstance(value, str):
2016-01-21 03:19:05 -07:00
pass
else:
value = json.dumps(value)
content.append(
2019-06-20 03:32:02 -06:00
"<b>%s</b>: %s,"
% (
cgi.escape(key, quote=True).encode("ascii", "xmlcharrefreplace"),
cgi.escape(value, quote=True).encode("ascii", "xmlcharrefreplace"),
2016-01-21 03:19:05 -07:00
)
)
content = "<br/>\n".join(content)
print(content)
2016-01-21 03:19:05 -07: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,
}
2019-06-20 03:32:02 -06:00
node = pydot.Node(name=event.event_id, label=label)
2016-01-21 03:19:05 -07:00
node_map[event.event_id] = node
graph.add_node(node)
print("Created Nodes")
2016-01-21 03:19:05 -07:00
for event in events:
for prev_id, _ in event.prev_events:
try:
end_node = node_map[prev_id]
except Exception:
2019-06-20 03:32:02 -06:00
end_node = pydot.Node(name=prev_id, label="<<b>%s</b>>" % (prev_id,))
2016-01-21 03:19:05 -07: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)
print("Created edges")
2016-01-21 03:19:05 -07:00
2019-06-20 03:32:02 -06:00
graph.write("%s.dot" % file_prefix, format="raw", prog="dot")
2016-01-21 03:19:05 -07:00
print("Created Dot")
2016-01-21 03:19:05 -07:00
2019-06-20 03:32:02 -06:00
graph.write_svg("%s.svg" % file_prefix, prog="dot")
2016-01-21 03:19:05 -07:00
print("Created svg")
2016-01-21 03:19:05 -07:00
2019-06-20 03:32:02 -06:00
2016-01-21 03:19:05 -07:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate a PDU graph for a given room by reading "
2019-06-20 03:32:02 -06:00
"from a file with line deliminated events. \n"
"Requires pydot."
2016-01-21 03:19:05 -07:00
)
parser.add_argument(
2019-06-20 03:32:02 -06:00
"-p",
"--prefix",
dest="prefix",
2016-01-21 03:19:05 -07:00
help="String to prefix output files with",
2019-06-20 03:32:02 -06:00
default="graph_output",
2016-01-21 03:19:05 -07:00
)
2019-06-20 03:32:02 -06:00
parser.add_argument("-l", "--limit", help="Only retrieve the last N events.")
parser.add_argument("event_file")
parser.add_argument("room")
2016-01-21 03:19:05 -07:00
args = parser.parse_args()
make_graph(args.event_file, args.room, args.prefix, args.limit)