put await Terminus connection inside async funciton generateKnowledgeGraph to avoid top level await

This commit is contained in:
Darren Zal 2023-02-13 14:25:07 -08:00
parent 6fe346c9a6
commit 01631078ab
9 changed files with 19 additions and 1927 deletions

11
dist/index.html vendored
View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sh*t Kanye says</title>
</head>
<body>
<script src="/main.js"></script>
</body>
</html>

1429
dist/main.js vendored

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,8 @@
"check": "svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "eslint './src/**/*.{js,ts,svelte}'",
"format": "prettier --write --plugin-search-dir=. ."
"format": "prettier --write --plugin-search-dir=. .",
"clean": "rimraf dist"
},
"devDependencies": {
"@sveltejs/adapter-static": "1.0.0-next.43",

View File

@ -1,334 +0,0 @@
<script lang="ts">
import cytoscape from 'cytoscape'
import { onMount } from 'svelte'
import { bubble } from 'svelte/internal'
import TerminusClient from '@terminusdb/terminusdb-client'
import { MeiliSearch } from 'meilisearch'
let cy
interface INodeData {
id: string
}
interface INode {
data: INodeData
}
interface IEdgeData {
id: string
source: string
target: string
label: string
}
interface IEdge {
data: IEdgeData
}
import json_graph from './knowledge_graph.json'
let knowledgeGraphJson: any = json_graph
// knowledgeGraphJson = await response.json()
// } else {
// alert(`HTTP-Error: ${response.status}`)
// }
// }
let nodes: INode[] = []
let edges: IEdge[] = []
onMount(async () => {
//await fetchData();
nodes = knowledgeGraphJson.entities.map((entity: any) => ({
data: { id: entity.id, label: entity.label }
}))
edges = knowledgeGraphJson.relations.map(
(relation: any, index: string) => ({
data: {
id: index,
source: relation.source,
target: relation.target,
label: relation.type
}
})
)
cy = cytoscape({
container: document.getElementById('cy'),
elements: {
nodes,
edges
},
style: [
{
selector: 'node',
style: {
'text-valign': 'center',
'text-halign': 'center',
'text-wrap': 'wrap',
'text-max-width': function (ele) {
return Math.max(1, Math.ceil(ele.degree() / 2)) * 30
},
'font-size': function (ele) {
return Math.max(1, Math.ceil(ele.degree() / 2)) * 6
},
'background-color': '#75f6df',
'border-color': '#223152',
'border-width': function (ele) {
return Math.max(1, Math.ceil(ele.degree() / 2))
},
label: 'data(label)',
width: function (ele) {
return Math.max(1, Math.ceil(ele.degree() / 2)) * 40
},
height: function (ele) {
return Math.max(1, Math.ceil(ele.degree() / 2)) * 40
}
}
},
{
selector: 'edge',
style: {
'font-size': 20,
width: 5,
'line-color': '#223152',
'target-arrow-color': '#223152',
'target-arrow-shape': 'triangle',
'curve-style': 'bezier',
'text-rotation': 'autorotate',
'text-offset': { x: 20, y: -20 },
'text-background-opacity': 1,
'text-background-color': '#fafafa',
'text-background-shape': 'roundrectangle',
label: 'data(label)'
}
}
],
layout: {
name: 'cose'
// infinite: true,
}
})
cy.nodes().forEach(function (node) {
node.data({
degree: node.connectedEdges().length
})
})
var nodes = cy.nodes()
nodes = nodes.sort(function (a, b) {
return b.data('degree') - a.data('degree')
})
var top100 = nodes.slice(0, 1000)
console.log(top100);
cy.nodes().forEach(function (node) {
if (!top100.includes(node)) {
node.hide()
}
})
let toggle = true
// cy.off('tap', 'node', event => {
// const node = event.target;
// const nodeId = node.data('id');
// alert('unDisplay info for ' + nodeId);
// });
cy.on('tap', 'node', function (evt) {
var node = evt.target
var connectedEdges = node.connectedEdges()
var connectedNodes = node.neighborhood().nodes()
var allElements = cy.elements()
var allNodes = cy.nodes()
var allEdges = cy.edges()
if (node.style('display') == 'element') {
// hide all nodes and edges except the selected node and its neighbors
allNodes.style('display', 'none')
allEdges.style('display', 'none')
connectedNodes.style('display', 'element')
node.style('display', 'element')
connectedEdges.style('display', 'element')
} else {
// show all nodes and edges
allNodes.style('display', 'element')
allEdges.style('display', 'element')
}
})
// Reset the state when clicking away from the node
cy.on('tap', function (e) {
if (e.target === cy) {
cy.nodes().style('display', 'element')
cy.edges().style('display', 'element')
cy.nodes().data('highlighted', false)
}
})
cy.on('tap', 'edge', event => {
const edge = event.target
const edgeId = edge.data('id')
alert('Display info for ' + edgeId)
})
// cy.on('tap', 'node', function(){
// alert("put code here"));
// });
// cy.layout({
// name: 'cola'
// }).run();
})
var searchTerm = ''
function updateSearchTerm(e) {
searchTerm = e.target.value
// Perform search in real timebased on searchTerm here
}
async function entered(e) {
alert(e.target.value.toString())
const searchclient = new MeiliSearch({
host: 'https://ms-9ea4a96f02a8-1969.sfo.meilisearch.io',
apiKey: '117c691a34b21a6651798479ebffd181eb276958'
})
const index = searchclient.index('orgs')
// this will search both keys and values
// const search = await index.search(e.target.value.toString(), { q: '*' });
// const searchResult = await index.search('orgs', {
// attributesToRetrieve: ['id']
// })
const searchResult = await index.search(e.target.value.toString());
console.log(searchResult)
// var node = cy.nodes().filter(function (ele) {
// return ele.data('id') == "Organization/" + searchResult.hits[0].id
// })
// console.log(node);
// var connectedEdges = node.connectedEdges()
// var connectedNodes = node.neighborhood().nodes()
// var allElements = cy.elements()
// var allNodes = cy.nodes()
// var allEdges = cy.edges()
// if (node.style('display') == 'element') {
// console.log("a");
// // hide all nodes and edges except the selected node and its neighbors
// allNodes.style('display', 'none')
// allEdges.style('display', 'none')
// connectedNodes.style('display', 'element')
// node.style('display', 'element')
// connectedEdges.style('display', 'element')
// } else {
// console.log("b");
// // show all nodes and edges
// allNodes.style('display', 'element')
// allEdges.style('display', 'element')
// }
// Perform search in real time based on searchTerm here
const client = new TerminusClient.WOQLClient(
'https://cloud.terminusdb.com/Myseelia/',
{
user: 'zaldarren@gmail.com',
organization: 'Myseelia',
db: 'playground3',
token:
'dGVybWludXNkYjovLy9kYXRhL2tleXNfYXBpLzJkMDU4N2IwYjgzMzhmODdjMjc0ZDdiNmM1MzgwNjFmYTYyMmZkOTcyZTc3NjI1NzIyYjY3MTllYTE3NmQxYjE=_bd6f9c37d87abcaf0c16b7a68335b31010c8dd04aac0b07bf0f31676af131888666200aac080e72cdc746197334eac4f52d821c90652b5611784878afabe1267535cbd896a00a396'
}
)
try {
await client.connect()
//console.log(schema);
// console.log("result");
// const result = await client.getDocument({as_list:true,type:"Person",query: { userName: "tester" }})
// console.log(result);
} catch (err) {
console.error('this is it' + err.message)
}
// let v = WOQL.vars('person_id', 'impactarea', '@schema:checked');
const WOQL = TerminusClient.WOQL
const query = WOQL.triple(
'v:OrganizationID',
'name',
WOQL.string('Sustainable Impact Token')
)
const query3 = WOQL.and(
WOQL.triple('v:NodeID', 'property_name', WOQL.like(`%${keyword}%`)),
WOQL.triple('v:NodeID', 'property_name', 'v:Value')
)
const result = await client.getDocument({
as_list: true,
type: 'Organization',
query: { name: 'Sustainable Impact Token' }
})
console.log('result ', result)
const results = await client.query(query)
console.log('Query Documents using WOQL: ', results.bindings)
}
</script>
<div class="pt-8 p-6 md:p-8 mx-auto">
<input
id="search"
type="text"
placeholder="Search..."
on:input={updateSearchTerm}
on:keydown={event => {
if (event.keyCode === 13) {
entered(event)
}
}}
/>
</div>
<section class="overflow-hidden text-gray-700">
<div class="cyDiv" />
<div id="cy" />
</section>
<style>
#search {
position: absolute;
top: 10px;
z-index: 100;
background-color: white;
width: 50%;
height: 40px;
border-radius: 20px;
padding: 10px 20px 10px 40px;
}
#search input[type='text'] {
position: absolute;
top: 0px;
left: 50px;
width: 80%;
height: 100%;
border: none;
background-color: transparent;
font-size: 18px;
}
#cy-div {
z-index: 99;
}
#cy {
width: 100%;
height: 95%;
position: absolute;
top: 55px;
left: 0px;
}
</style>

View File

@ -194,7 +194,7 @@
// Perform search in real timebased on searchTerm here
}
async function entered(e) {
;(async function entered(e) {
const searchclient = new MeiliSearch({
host: 'https://ms-9ea4a96f02a8-1969.sfo.meilisearch.io',
apiKey: '117c691a34b21a6651798479ebffd181eb276958'
@ -230,9 +230,9 @@
cy.add(allNodes)
cy.add(allEdges)
cy.layout({
name: 'cose',
// other layout options here
}).run();
name: 'cose'
// other layout options here
}).run()
}
)
// var node = cy.nodes().filter(function (ele) {
@ -271,16 +271,7 @@
'dGVybWludXNkYjovLy9kYXRhL2tleXNfYXBpLzJkMDU4N2IwYjgzMzhmODdjMjc0ZDdiNmM1MzgwNjFmYTYyMmZkOTcyZTc3NjI1NzIyYjY3MTllYTE3NmQxYjE=_bd6f9c37d87abcaf0c16b7a68335b31010c8dd04aac0b07bf0f31676af131888666200aac080e72cdc746197334eac4f52d821c90652b5611784878afabe1267535cbd896a00a396'
}
)
try {
await client.connect()
//console.log(schema);
// console.log("result");
// const result = await client.getDocument({as_list:true,type:"Person",query: { userName: "tester" }})
// console.log(result);
} catch (err) {
console.error('this is it' + err.message)
}
await client.connect()
// let v = WOQL.vars('person_id', 'impactarea', '@schema:checked');
// const WOQL = TerminusClient.WOQL
@ -303,7 +294,7 @@
// console.log('result ', result)
// const results = await client.query(query)
// console.log('Query Documents using WOQL: ', results.bindings)
}
})()
</script>
<div class="pt-8 p-6 md:p-8 mx-auto">

View File

@ -1,96 +0,0 @@
import { isNullOrUndefined } from 'util';
import { WOQLClient } from 'terminusdb-client';
import { resultToDF } from 'terminusdb-client/woqldataframe';
import { DataFrame } from 'pandas';
import * as fs from 'fs';
const client = new WOQLClient("https://cloud.terminusdb.com/Myseelia/");
client.connect(db="playground3", team="Myseelia", use_token=true);
export async function generateKnowledgeGraph(ids: string[]) {
const orgsRaw = client.query_document({ "@type": "Organization", "Document id": { "@in": ids } });
const df = resultToDF(orgsRaw);
const entities: {id: string, label: string, type: string}[] = [];
const relations: {source: string, target: string, type: string}[] = [];
for (let i = 0; i < df.length; i++) {
const row = df[i];
entities.push({'id': row['Document id'], 'label': row['name'], 'type': 'organization'});
if (!isNullOrUndefined(row['assignee'])) {
const assigneeId = row['assignee'];
if (!isNullOrUndefined(assigneeId) && assigneeId !== '' && !isNaN(assigneeId)) {
entities.push({'id': assigneeId, 'label': row['assignee'], 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': assigneeId, 'type': 'assignee'});
}
}
if (Array.isArray(row['blockchainecosystem'])) {
for (const ecosystem of row['blockchainecosystem']) {
const ecosystemId = ecosystem;
if (!isNullOrUndefined(ecosystemId) && ecosystemId !== '' && !isNaN(ecosystemId)) {
entities.push({'id': ecosystemId, 'label': ecosystem, 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': ecosystemId, 'type': 'blockchain ecosystem'});
}
}
} else {
const ecosystemId = row['blockchainecosystem'];
if (!isNullOrUndefined(ecosystemId) && ecosystemId !== '' && !isNaN(ecosystemId)) {
entities.push({'id': ecosystemId, 'label': row['blockchainecosystem'], 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': ecosystemId, 'type': 'blockchain ecosystem'});
}
}
if (Array.isArray(row['topic'])) {
for (const topic of row['topic']) {
const topicId = topic;
if (!isNullOrUndefined(topicId) && topicId !== '' && !isNaN(topicId)) {
entities.push({'id': topicId, 'label': topic, 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': topicId, 'type': 'topic'});
}
}
} else {
const topicId = row['topic'];
if (!isNullOrUndefined(topicId) && topicId !== '' && !isNaN(topicId)) {
entities.push({'id': topicId, 'label': row['topic'], 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': topicId, 'type': 'topic'});
}
}
if (Array.isArray(row['web3'])) {
for (const web3 of row['web3']) {
const web3Id = web3;
if (!isNullOrUndefined(web3Id) && web3Id !== '' && !isNaN(web3Id)) {
entities.push({'id': web3Id, 'label': web3, 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': web3Id, 'type': 'web3'});
}
}
} else {
const web3Id = row['web3'];
if (!isNullOrUndefined(web3Id) && web3Id !== '' && !isNaN(web3Id)) {
entities.push({'id': web3Id, 'label': row['web3'], 'type': 'attribute'});
relations.push({'source': row['Document id'], 'target': web3Id, 'type': 'web3'});
}
}
}
const knowledgeGraphJson = {
'entities': entities,
'relations': relations
};
fs.writeFileSync("knowledge_graph.json", JSON.stringify(knowledgeGraphJson), 'utf-8');
export function getData(client: WOQLClient, ids: string[]): DataFrame {
const query = {
"@type": "Organization",
"Document id": {
"$in": ids
}
};
const orgsRaw = client.query_document(query);
return resultToDF(orgsRaw);
}

View File

@ -3,30 +3,20 @@ import TerminusClient from '@terminusdb/terminusdb-client'
import * as fs from 'fs'
const client = new TerminusClient.WOQLClient(
'https://cloud.terminusdb.com/Myseelia/',
{
user: 'zaldarren@gmail.com',
organization: 'Myseelia',
db: 'play',
token:
'dGVybWludXNkYjovLy9kYXRhL2tleXNfYXBpLzJkMDU4N2IwYjgzMzhmODdjMjc0ZDdiNmM1MzgwNjFmYTYyMmZkOTcyZTc3NjI1NzIyYjY3MTllYTE3NmQxYjE=_bd6f9c37d87abcaf0c16b7a68335b31010c8dd04aac0b07bf0f31676af131888666200aac080e72cdc746197334eac4f52d821c90652b5611784878afabe1267535cbd896a00a396'
}
)
try {
await client.connect()
//console.log(schema);
// console.log("result");
// const result = await client.getDocument({as_list:true,type:"Person",query: { userName: "tester" }})
// console.log(result);
} catch (err) {
console.error('this is it' + err.message)
}
const WOQL = TerminusClient.WOQL
export async function generateKnowledgeGraph(ids: unknown[]): Promise<object> {
const client = new TerminusClient.WOQLClient(
'https://cloud.terminusdb.com/Myseelia/',
{
user: 'zaldarren@gmail.com',
organization: 'Myseelia',
db: 'play',
token:
'dGVybWludXNkYjovLy9kYXRhL2tleXNfYXBpLzJkMDU4N2IwYjgzMzhmODdjMjc0ZDdiNmM1MzgwNjFmYTYyMmZkOTcyZTc3NjI1NzIyYjY3MTllYTE3NmQxYjE=_bd6f9c37d87abcaf0c16b7a68335b31010c8dd04aac0b07bf0f31676af131888666200aac080e72cdc746197334eac4f52d821c90652b5611784878afabe1267535cbd896a00a396'
}
)
await client.connect()
const results: unknown[] = []
const entities: { id: string; label: string; type: string }[] = []
const relations: { source: string; target: string; type: string }[] = []

View File

@ -28,18 +28,8 @@
onMount(async () => {
try{
await client.connect()
const schema = await client.getSchema("myseelia", "main")
// console.log("Schema");
// console.log(schema);
// console.log("result");
// const result = await client.getDocument({as_list:true,type:"Person",query: { userName: "tester" }})
// console.log(result);
}catch(err){
console.error("this is it" + err.message)
}
});
/**

View File

@ -28,18 +28,8 @@
onMount(async () => {
try{
await client.connect()
const schema = await client.getSchema("myseelia", "main")
// console.log("Schema");
// console.log(schema);
// console.log("result");
// const result = await client.getDocument({as_list:true,type:"Person",query: { userName: "tester" }})
// console.log(result);
}catch(err){
console.error("this is it" + err.message)
}
});
/**