Skip to content

Commit fd8692b

Browse files
committed
deploy cozy code viewer
1 parent 3fb7814 commit fd8692b

File tree

8 files changed

+833
-1
lines changed

8 files changed

+833
-1
lines changed

cozy/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
build

cozy/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Cozy
2+
3+
Uses [CozoDB](https://github.com/cozodb/cozo) for graph queries
4+
5+
```
6+
npm i -g rollup sirv
7+
8+
(cd eyg; gleam run cli cozo ./saved/saved.json)
9+
cp wisdom/tmp.db.json cozy/build/db.json
10+
11+
(cd cozy;
12+
rollup --config rollup.config.mjs && \
13+
cp index.html build/index.html && \
14+
cp node_modules/cozo-lib-wasm/cozo_lib_wasm_bg.wasm build && \
15+
16+
npx sirv ./build --dev --host 0.0.0.0 --port 5000
17+
)
18+
```

cozy/index.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>view</title>
7+
<script type="module" src="./bundle.js"></script>
8+
<link
9+
href="https://unpkg.com/[email protected]/dist/tailwind.min.css"
10+
rel="stylesheet"
11+
/>
12+
<link href="https://eyg.run/layout.css" rel="stylesheet" />
13+
<link href="https://eyg.run/neo.css" rel="stylesheet" />
14+
</head>
15+
<body>
16+
<div class="vstack">
17+
<header class="cover max-w-3xl p-2">
18+
<h1 class="text-2xl px-2">AST Query</h1>
19+
<p>
20+
Set up to run over all the full database of EYG source code;
21+
<span id="count"></span>
22+
</p>
23+
</header>
24+
<div class="expand cover max-w-3xl">
25+
<form class="px-2" id="form">
26+
<textarea
27+
class="border-2 neo-shadow w-full"
28+
rows="5"
29+
id="query"
30+
spellcheck="false"
31+
>
32+
?[label] := *eav[id, 'label', label],
33+
*eav[id, 'expression', 'Let'],
34+
*eav[id, 'value', vid],
35+
*eav[valueId, 'expression', 'String'],
36+
valueId == vid</textarea
37+
>
38+
<div class="text-right">
39+
<button
40+
class="my-2 border-1 border-purple-500 purple-gradient neo-shadow px-2"
41+
type="submit"
42+
>
43+
Run
44+
</button>
45+
</div>
46+
</form>
47+
<div class="px-2">
48+
<div class="bg-gray-100 min-h-4 p-1" id="output"></div>
49+
</div>
50+
<div class="px-2">
51+
<h3 class="text-lg">Examples</h3>
52+
<p class="italic">Click to load</p>
53+
<p class="mt-4">The name of all strings in let assignments</p>
54+
<pre class="p-1 border cursor-pointer">
55+
?[label] := *eav[id, 'label', label],
56+
*eav[id, 'expression', 'Let'],
57+
*eav[id, 'value', vid],
58+
*eav[valueId, 'expression', 'String'],
59+
valueId == vid</pre
60+
>
61+
<p class="mt-4">Type of every expression assigned to a variable x</p>
62+
<pre class="p-1 border cursor-pointer">
63+
?[vid, exp] := *eav[id, 'label', label],
64+
*eav[id, 'expression', 'Let'],
65+
*eav[id, 'value', vid],
66+
*eav[valueId, 'expression', exp],
67+
valueId == vid,
68+
label == 'x'</pre
69+
>
70+
<p class="mt-4">
71+
The largest let statments order by number of AST nodes in the
72+
assignment expression
73+
</p>
74+
<pre class="p-1 border cursor-pointer">
75+
parent[id, child] := *eav[id, 'expression', 'Let'],
76+
*eav[id, 'value', child] or *eav[id, 'then', child]
77+
78+
?[x, label, d] := parent[x,y],*eav[x, 'label', label],
79+
d = y - x,
80+
d > 1
81+
:sort -d
82+
:limit 20</pre
83+
>
84+
</div>
85+
</div>
86+
</div>
87+
</body>
88+
</html>

cozy/main.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// TODO ask in DB channel about loading triples
2+
// Have a pattern of building the front end and setting up a database
3+
// DB source toggles in the various pages ?source=yaml dir= dir not possible in web so instead ?source="./dfdf" workds for file or source can be postMessage
4+
import init, { CozoDb } from "cozo-lib-wasm";
5+
6+
(async function main() {
7+
await init();
8+
let db = CozoDb.new();
9+
let response = await fetch("./db.json");
10+
let rows = await response.json();
11+
let $count = document.getElementById("count");
12+
$count.innerText = `${rows.length} triples`;
13+
console.log(db.run(":create eav {e: Int, a: String, v: Any}", ""));
14+
db.import_relations(
15+
JSON.stringify({
16+
eav: { headers: ["e", "a", "v"], rows: rows },
17+
})
18+
);
19+
20+
let $query = document.getElementById("query");
21+
let $form = document.getElementById("form");
22+
let $output = document.getElementById("output");
23+
$form.onsubmit = function (event) {
24+
event.preventDefault();
25+
let { ok, headers, rows, message } = JSON.parse(db.run($query.value, ""));
26+
27+
$output.innerHTML = "";
28+
if (ok) {
29+
let table = document.createElement("table");
30+
let thead = document.createElement("thead");
31+
let tr = document.createElement("tr");
32+
tr.classList.add("border-b");
33+
tr.classList.add("border-black");
34+
tr.classList.add("text-left");
35+
headers.forEach((element) => {
36+
let th = document.createElement("th");
37+
th.innerText = element;
38+
tr.append(th);
39+
});
40+
thead.append(tr);
41+
table.append(thead);
42+
43+
let tbody = document.createElement("tbody");
44+
rows.forEach((row) => {
45+
let tr = document.createElement("tr");
46+
tr.classList.add("border-b");
47+
48+
row.forEach((element) => {
49+
let td = document.createElement("td");
50+
td.innerText = element;
51+
tr.append(td);
52+
});
53+
tbody.append(tr);
54+
});
55+
table.append(tbody);
56+
$output.append(table);
57+
} else {
58+
let span = document.createElement("span");
59+
span.classList.add("border-l-4");
60+
span.classList.add("border-red-700");
61+
span.classList.add("px-1");
62+
span.classList.add("text-red-400");
63+
64+
span.innerHTML = message;
65+
$output.append(span);
66+
}
67+
};
68+
document.querySelectorAll("pre").forEach((element) => {
69+
element.onclick = function (_event) {
70+
$query.value = element.innerText;
71+
};
72+
});
73+
})();

0 commit comments

Comments
 (0)