-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_load.py
More file actions
162 lines (134 loc) · 3.21 KB
/
Copy path2_load.py
File metadata and controls
162 lines (134 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Prototype the use of KùzuDB
"""
import json
import pathlib
import sys
import tomllib
import typing
from icecream import ic
from pyinstrument import Profiler
import kuzu
import pandas as pd
from nyddu import db_connect
def verify_page (
page: dict,
) -> dict:
"""
Format the data for one row, so that neither Polars nor Pandas loose their minds.
"""
if page["status"] is not None:
page["status"] = str(page["status"])
if page["slug"] is not None:
page["slug"] = page["slug"].strip().lstrip("/s/")
return {
"uri": page["uri"],
"status": page["status"],
"type": page["type"],
"path": page["path"],
"slug": page["slug"],
"redirect": page["redirect"],
"title": page["title"],
"summary": page["summary"],
"thumbnail": page["thumbnail"],
"error": page["error"],
"timing": page["timing"]
}
def main (
) -> int:
"""
Main entry point
"""
config_path: pathlib.Path = pathlib.Path("config.toml")
config: dict = {}
with open(config_path, mode = "rb") as fp:
config = tomllib.load(fp)
conn: kuzu.Connection = db_connect(
db_path = pathlib.Path(config["db"]["db_path"]),
)
## start code profiling
profiler: Profiler = Profiler()
profiler.start()
######################################################################
## define schema
conn.execute("""
DROP TABLE IF EXISTS Link;
DROP TABLE IF EXISTS Page;
""")
conn.execute("""
CREATE NODE TABLE Page(
id SERIAL PRIMARY KEY,
uri STRING,
status STRING,
type STRING,
path STRING,
slug STRING,
redirect STRING,
title STRING,
summary STRING,
thumbnail STRING,
error STRING,
timing DOUBLE
);
""")
conn.execute("""
CREATE REL TABLE Link(
FROM Page TO Page,
sym BOOLEAN
);
""")
######################################################################
## load JSON data
with open(pathlib.Path("report"), "r", encoding = "utf-8") as fp:
dat: list = json.load(fp)
df_page = pd.DataFrame([
verify_page(page)
for page in dat
])
ic(df_page)
conn.execute("""
COPY Page FROM df_page
""")
df_ref = pd.DataFrame([
{
"src": ref,
"dst": page["uri"],
}
for page in dat
for ref in page["refs"]
])
ic(df_ref)
for row in df_ref.to_dict(orient = "records"):
conn.execute(
"""
MATCH (src:Page {path: $src})
MATCH (dst:Page {uri: $dst})
CREATE (src)-[:Link {sym: true}]->(dst);
""",
row,
)
df_raw = pd.DataFrame([
{
"src": ref,
"dst": page["uri"],
}
for page in dat
for ref in page["raw"]
])
ic(df_raw)
for row in df_raw.to_dict(orient = "records"):
conn.execute(
"""
MATCH (src:Page {path: $src})
MATCH (dst:Page {uri: $dst})
CREATE (src)-[:Link {sym: false}]->(dst);
""",
row,
)
## end code profiling
profiler.stop()
profiler.print()
if __name__ == "__main__":
main()