-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualization-export.ts
More file actions
204 lines (169 loc) · 7.33 KB
/
Copy pathvisualization-export.ts
File metadata and controls
204 lines (169 loc) · 7.33 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* sqlite-graph Visualization Export Example
*
* Demonstrates exporting graph data for vis.js visualization
* Run: npx ts-node examples/visualization-export.ts
*/
import { GraphDatabase } from '../src/core/Database';
import * as fs from 'fs';
import * as path from 'path';
interface VisNode {
id: number;
label: string;
title: string;
color: { background: string; border: string };
properties: Record<string, unknown>;
type: string;
}
interface VisEdge {
from: number;
to: number;
label: string;
arrows: string;
}
interface VisData {
nodes: VisNode[];
edges: VisEdge[];
}
const nodeColors: Record<string, { background: string; border: string }> = {
Job: { background: '#4CAF50', border: '#388E3C' },
Company: { background: '#2196F3', border: '#1976D2' },
Skill: { background: '#FF9800', border: '#F57C00' },
Person: { background: '#9C27B0', border: '#7B1FA2' },
Application: { background: '#E91E63', border: '#C2185B' },
};
function exportToVisJS(db: GraphDatabase): VisData {
const graphData = db.export();
const nodes: VisNode[] = graphData.nodes.map((node) => {
const props = node.properties as any;
const displayName = props.title || props.name || node.type;
return {
id: node.id,
label: node.type,
title: `${node.type}: ${displayName}`,
color: nodeColors[node.type] || { background: '#999', border: '#666' },
properties: node.properties,
type: node.type,
};
});
const edges: VisEdge[] = graphData.edges.map((edge) => ({
from: edge.from,
to: edge.to,
label: edge.type,
arrows: 'to',
}));
return { nodes, edges };
}
function createJobSearchGraph(): GraphDatabase {
console.log('\n=== Creating Job Search Graph ===\n');
const db = new GraphDatabase(':memory:');
// Create companies
const google = db.createNode('Company', { name: 'Google', industry: 'Technology' });
const startup = db.createNode('Company', { name: 'TechStartup', industry: 'SaaS' });
// Create jobs
const seniorEng = db.createNode('Job', { title: 'Senior Engineer', salary: 180000 });
const techLead = db.createNode('Job', { title: 'Tech Lead', salary: 200000 });
// Create skills
const typescript = db.createNode('Skill', { name: 'TypeScript' });
const react = db.createNode('Skill', { name: 'React' });
const nodejs = db.createNode('Skill', { name: 'Node.js' });
// Create people
const alice = db.createNode('Person', { name: 'Alice', experience: 8 });
const bob = db.createNode('Person', { name: 'Bob', experience: 5 });
// Create applications
const app1 = db.createNode('Application', { status: 'in_progress' });
const app2 = db.createNode('Application', { status: 'rejected' });
// Link jobs to companies
db.createEdge(seniorEng.id, 'POSTED_BY', google.id);
db.createEdge(techLead.id, 'POSTED_BY', startup.id);
// Link jobs to skills
db.createEdge(seniorEng.id, 'REQUIRES', typescript.id);
db.createEdge(seniorEng.id, 'REQUIRES', react.id);
db.createEdge(techLead.id, 'REQUIRES', typescript.id);
db.createEdge(techLead.id, 'REQUIRES', nodejs.id);
// Link people to skills
db.createEdge(alice.id, 'HAS_SKILL', typescript.id);
db.createEdge(alice.id, 'HAS_SKILL', react.id);
db.createEdge(bob.id, 'HAS_SKILL', typescript.id);
// Link applications
db.createEdge(alice.id, 'APPLIED', app1.id);
db.createEdge(app1.id, 'APPLIED_TO', seniorEng.id);
db.createEdge(bob.id, 'APPLIED', app2.id);
db.createEdge(app2.id, 'APPLIED_TO', techLead.id);
// Add similarity
db.createEdge(seniorEng.id, 'SIMILAR_TO', techLead.id);
const graphData = db.export();
console.log(`✅ Created: ${graphData.nodes.length} nodes, ${graphData.edges.length} edges\n`);
return db;
}
function generateHTMLVisualization(visData: VisData, filename: string): void {
const html = `<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>${filename}</title>
<script src="https://unpkg.com/vis-network@9.1.6/standalone/umd/vis-network.min.js"></script>
<style>
body{font-family:system-ui;margin:0;background:#f5f5f5}
.container{max-width:1400px;margin:0 auto;padding:20px}
header{background:#fff;padding:20px;border-radius:8px;margin-bottom:20px;box-shadow:0 2px 4px rgba(0,0,0,0.1)}
#network{width:100%;height:700px;background:#fff;border-radius:8px;box-shadow:0 2px 4px rgba(0,0,0,0.1)}
.info{background:#fff;padding:20px;border-radius:8px;margin-top:20px}
.stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:15px}
.stat{background:#f8f9fa;padding:15px;border-radius:4px;text-align:center}
.stat-value{font-size:32px;font-weight:bold;color:#007bff}
</style></head><body>
<div class="container">
<header><h1>🗺️ ${filename}</h1><p>Click nodes to see details, drag to rearrange</p></header>
<div id="network"></div>
<div class="info">
<h3>Graph Statistics</h3>
<div class="stats">
<div class="stat"><div class="stat-value">${visData.nodes.length}</div><div>Nodes</div></div>
<div class="stat"><div class="stat-value">${visData.edges.length}</div><div>Edges</div></div>
</div></div></div>
<script>
const nodes=new vis.DataSet(${JSON.stringify(visData.nodes)});
const edges=new vis.DataSet(${JSON.stringify(visData.edges)});
const network=new vis.Network(document.getElementById('network'),{nodes,edges},{
nodes:{shape:'dot',size:20,font:{size:14},borderWidth:2,shadow:true},
edges:{width:2,smooth:{type:'continuous'},arrows:{to:{enabled:true,scaleFactor:0.5}},font:{size:12}},
physics:{stabilization:{iterations:200},barnesHut:{gravitationalConstant:-8000,springConstant:0.04,springLength:150}},
interaction:{hover:true,navigationButtons:true,keyboard:true}
});
network.on('stabilizationIterationsDone',()=>network.setOptions({physics:false}));
</script></body></html>`;
const outputPath = path.join(__dirname, filename);
fs.writeFileSync(outputPath, html);
console.log(`✅ Generated: ${outputPath}\n`);
}
function main() {
console.log('╔════════════════════════════════════════════════════╗');
console.log('║ sqlite-graph Visualization Export Example ║');
console.log('╚════════════════════════════════════════════════════╝');
const db = createJobSearchGraph();
const visData = exportToVisJS(db);
console.log('=== Generating Visualizations ===\n');
generateHTMLVisualization(visData, 'job-search-visualization.html');
// Export JSON
const jsonPath = path.join(__dirname, 'job-search-data.json');
fs.writeFileSync(jsonPath, JSON.stringify(visData, null, 2));
console.log(`✅ Exported JSON: ${jsonPath}\n`);
console.log('=== Demo Query: Find Matching Jobs ===\n');
const alice = db.nodes('Person').where({ name: 'Alice' }).exec()[0];
if (alice) {
const matchingJobs = db.traverse(alice.id)
.out('HAS_SKILL')
.both('REQUIRES')
.filter((node) => node.type === 'Job')
.unique()
.toArray();
console.log(`Found ${matchingJobs.length} jobs matching Alice's skills:`);
matchingJobs.forEach((job) => {
console.log(` - ${(job.properties as any).title}`);
});
}
console.log('\n=== Complete! ===\n');
console.log('Open these files in your browser:');
console.log(' - job-search-visualization.html');
console.log(' - visualization-browser.html\n');
db.close();
}
main();