70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
# [[file:../sitemap.org::*Making SigmaJS Nodes][Making SigmaJS Nodes:1]]
|
|
import arcology.models
|
|
import roam.models
|
|
from arcology.cache_decorator import cache
|
|
|
|
import hashlib
|
|
|
|
def make_loc_hash(page: arcology.models.Page, salt, max_q=700):
|
|
key = page.file.path + str(salt)
|
|
hash = hashlib.sha224(key.encode("utf-8")).digest()
|
|
return int.from_bytes(hash, byteorder="big") % max_q
|
|
|
|
class Node():
|
|
@classmethod
|
|
def make_page_dict(cls, page):
|
|
@cache(key_prefix="sitemap_node", expire_secs=60*60*24)
|
|
def _make(page, hash):
|
|
link_cnt = page.file.outbound_links.count()
|
|
backlink_cnt = roam.models.Link.objects.filter(dest_heading__in=page.file.heading_set.all()).count()
|
|
return dict(
|
|
key=page.route_key,
|
|
attributes=dict(
|
|
label=page.title,
|
|
x=make_loc_hash(page, 1),
|
|
y=make_loc_hash(page, 2),
|
|
size=min((link_cnt + backlink_cnt) / 2, 20),
|
|
color=page.site.link_color,
|
|
href=page.to_url(),
|
|
)
|
|
)
|
|
return _make(page, page.file.digest)
|
|
|
|
@classmethod
|
|
def get_sigmajs_nodes(cls):
|
|
pages = arcology.models.Page.objects.all()
|
|
nodes = [
|
|
cls.make_page_dict(page)
|
|
for page in pages
|
|
]
|
|
|
|
return nodes
|
|
# Making SigmaJS Nodes:1 ends here
|
|
|
|
# [[file:../sitemap.org::*Making SigmaJS Edges][Making SigmaJS Edges:1]]
|
|
class hashabledict(dict):
|
|
def __hash__(self):
|
|
return hash(tuple(sorted(self.items())))
|
|
|
|
class Edge():
|
|
@classmethod
|
|
def get_sigmajs_edges(cls):
|
|
q_links = roam.models.Link.objects.all()
|
|
|
|
links = set()
|
|
for link in q_links:
|
|
try:
|
|
source = link.source_file.page_set.first().route_key
|
|
dest = link.dest_heading.path.page_set.first().route_key
|
|
links.add(
|
|
hashabledict(
|
|
key=f"{source}-{dest}",
|
|
source=source,
|
|
target=dest,
|
|
)
|
|
)
|
|
except roam.models.Heading.DoesNotExist:
|
|
pass
|
|
return list(links)
|
|
# Making SigmaJS Edges:1 ends here
|