sparql queries for demo

master
Niko PLP 2 weeks ago
parent 29a35f835a
commit d9de913e56
  1. 14
      ng-app/src/apps/ContactEditor.svelte
  2. 120
      ng-app/src/apps/SocialQueryDemo.svelte
  3. 3
      ng-repo/src/commit.rs
  4. 2
      ng-sdk-js/example-webapp-react-socialquery/src/Contact.tsx
  5. 2
      ng-sdk-js/example-webapp-react-socialquery/src/Contacts.tsx
  6. 16
      ng-verifier/src/inbox_processor.rs

@ -90,14 +90,22 @@
}
async function test() {
if (!has_camera) {
await on_qr_scanned("AgBPtkD9jg11uDj7FTK0VqWb_aVxYvoyjFyIWs5VwCOICwAAABsxv_FXViA-5LUMNjARLJCiS3nOc7WYdoVQYgWn2ukcB25vIG5hbWUBDmZha2VAZW1haWwuY29t");
}
await on_qr_scanned("AgBX7NyrH_8cF47eeqkkFwtHFi-LrjrxOoRLNM0EScA1NAABANpOlaBYozbkSDVJFAUG2nSWSTXXD-wiLRG6ZUU23f8GCWFjY291bnQgNAEObmlrbzRAdHJ1ZS5jb20");
}
</script>
<div class="flex-col p-5"bind:this={container}>
<h1 class="font-bold text-xl text-blue-700">Contact</h1>
<Button
on:click={test}
on:keypress={test}
class="select-none ml-2 mt-2 mb-2 text-white bg-primary-700 hover:bg-primary-700/90 focus:ring-4 focus:ring-primary-500/50 rounded-lg text-base p-2 text-center inline-flex items-center dark:focus:ring-primary-700/55"
>
<QrCode tabindex="-1" class="mr-2 focus:outline-none" />
Test
</Button><br/>
{#if !has_camera && !has_name}
<Alert class="m-2" color="red" style="word-break: break-word;" >No camera available. You cannot import with QR-code</Alert>
{/if}

@ -13,6 +13,7 @@
import { onMount, tick, onDestroy } from "svelte";
import {
sparql_update,
sparql_query,
toast_error,
toast_success,
active_session,
@ -37,17 +38,79 @@
return definer(hljs);
},
};
const ranking_query = `SELECT ?mail (SAMPLE(?n) as?name) (MAX(?rust_) as ?rust) (MAX(?svelte_) as ?svelte) (MAX(?tailwind_) as ?tailwind) (MAX(?rdf_) as ?rdf) (MAX(?yjs_) as ?yjs) (MAX(?automerge_) as ?automerge) (SUM(?total_) as ?total) WHERE {
{SELECT ?mail (SAMPLE(?name) as ?n) ?skill (AVG(?value)+1 AS ?score) WHERE {
?rating <http://www.w3.org/2006/vcard/ns#hasEmail> ?mail.
?rating <http://www.w3.org/2006/vcard/ns#fn> ?name.
?rating <did:ng:x:skills#hasRating> ?hasrating.
?hasrating <did:ng:x:skills#rated> ?value.
?hasrating <did:ng:x:skills#skill> ?skill.
} GROUP BY ?mail ?skill }
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:rust>), ?score, 0) AS ?rust_)
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:svelte>), ?score, 0) AS ?svelte_)
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:tailwind>), ?score, 0) AS ?tailwind_)
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:rdf>), ?score, 0) AS ?rdf_)
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:yjs>), ?score, 0) AS ?yjs_)
BIND (IF(sameTerm(?skill, <did:ng:k:skills:programming:yjs>), ?score, 0) AS ?automerge_)
BIND (?tailwind_+?svelte_+?rust_+?rdf_+?yjs_+?automerge_ AS ?total_)
} GROUP BY ?mail
ORDER BY DESC(?total)`;
export let commits = {graph:[]};
let source = "";
$: source = commits.graph.join(" .\r\n") + (commits.graph.length ? " .":"");
let results = [];
$: if (commits.graph.length > 4) {
sparql_query(ranking_query, false).then((res) => {
console.log(res.results?.bindings);
results = res.results?.bindings;
});
}
const query = `PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX xskills: <did:ng:x:skills#>
PREFIX ksp: <did:ng:k:skills:programming:>
PREFIX ng: <did:ng:x:ng#>
CONSTRUCT { [
vcard:hasEmail ?email;
vcard:fn ?name;
a vcard:Individual;
ng:site ?public_profile;
ng:protected ?protected_profile;
xskills:hasRating [
a xskills:Rating ;
xskills:rated ?level;
xskills:skill ?skill
]
]
}
WHERE {
?contact a vcard:Individual.
?contact vcard:fn ?name.
?contact vcard:hasEmail ?email.
OPTIONAL { ?contact ng:site ?public_profile . ?contact ng:site_inbox ?public_inbox }
OPTIONAL { ?contact ng:protected ?protected_profile . ?contact ng:protected_inbox ?prot_inbox }
?contact xskills:hasRating [
a xskills:Rating ;
xskills:rated ?level;
xskills:skill ?skill
].
?contact xskills:hasRating/xskills:skill ksp:rust.
?contact xskills:hasRating/xskills:skill ksp:svelte.
FILTER ( ?skill IN (
ksp:rust, ksp:svelte, ksp:rdf, ksp:tailwind, ksp:yjs, ksp:automerge
) )
}`;
const openQuery = async () => {
//TODO : return now if already processing (when LDO for svelte is ready)
// and even disable the button in that case
try {
await sparql_update("INSERT DATA { <> <did:ng:x:ng#social_query_sparql> \"CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }\".}");
await sparql_update(`INSERT DATA { <> <did:ng:x:ng#social_query_sparql> \"${query.replaceAll("\n"," ")}\".}`);
let commit_id = commits.heads[0];
let commit_key = commits.head_keys[0];
let session = $active_session;
@ -66,25 +129,14 @@
}
onMount(()=>{
console.log($active_session);
//console.log($active_session);
});
const info = () => {
}
</script>
<div class="flex-col">
<h1> Social Query</h1>
{#if !source}
<p class="p-3">{$t("doc.no_triples")}</p>
{/if}
<button
on:click={info}
on:keypress={info}
class="select-none ml-2 mt-2 mb-2 text-white bg-primary-700 hover:bg-primary-700/90 focus:ring-4 focus:ring-primary-500/50 rounded-lg text-base p-2 text-center inline-flex items-center dark:focus:ring-primary-700/55"
>
info
</button>
<p class="p-3">
Social Query
</p>
<Button
on:click={openQuery}
on:keypress={openQuery}
@ -95,6 +147,40 @@
Start query
</Button>
<div class="relative overflow-x-auto">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400 table-auto">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<th scope="col" class="px-6 py-3">Email</th>
<th scope="col" class="px-6 py-3">Name</th>
<th scope="col" class="px-6 py-3">Rust</th>
<th scope="col" class="px-6 py-3">Svelte</th>
<th scope="col" class="px-6 py-3">Tailwind</th>
<th scope="col" class="px-6 py-3">Rdf</th>
<th scope="col" class="px-6 py-3">Yjs</th>
<th scope="col" class="px-6 py-3">Automerge</th>
<th scope="col" class="px-6 py-3">Total</th>
</thead>
<tbody>
{#each results as res}
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 border-gray-200">
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.mail.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.name.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.rust.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.svelte.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.tailwind.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.rdf.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.yjs.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.automerge.value}</td>
<td scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">{res.total.value}</td>
</tr>
{/each}
</tbody>
</table>
</div>
<p class="p-3 mt-10">
Raw data
</p>
{#if source}
<Highlight {language} code={source} class="mb-10" let:highlighted >
<LineNumbers {highlighted} wrapLines hideBorder />

@ -10,6 +10,7 @@
//! Commit that composes the DAG of a Branch
use core::fmt;
use std::any::Any;
use std::collections::HashSet;
use std::iter::FromIterator;
@ -1585,7 +1586,7 @@ impl fmt::Display for CommitBody {
RemoveSignerCap(RemoveSignerCap),
WalletUpdate(WalletUpdate),
StoreUpdate(StoreUpdate), */
_ => unimplemented!(),
_ => write!(f, "!!!! CommitBody Display not implemented for {:?}", v0.type_id()),
}
}
}

@ -10,7 +10,7 @@ export const Contact: FunctionComponent = ({nuri}) => {
const { createData, commitData, changeData } = useLdo();
useResource(session.sessionId && nuri ? nuri : undefined, { subscribe: true });
let contact = useSubject(SocialContactShapeType, session.sessionId && nuri ? nuri.substring(0,53) : undefined);
//console.log(nuri)
const ksp = "did:ng:k:skills:programming:";
const ksp_mapping = [

@ -12,7 +12,7 @@ export const Contacts: FunctionComponent = () => {
useResource(session.sessionId ? "did:ng:"+session.privateStoreId : undefined, { subscribe: true });
let myContainer = useSubject(ContainerShapeType, session.sessionId ? "did:ng:"+(session.privateStoreId.substring(0,46)) : undefined);
//console.log(myContainer.contains?.toJSON())
if (session.sessionId) {
container_overlay = session.privateStoreId.substring(46) as string;
}

@ -25,12 +25,15 @@ use crate::verifier::*;
impl Verifier {
pub(crate) async fn post_to_inbox(&self, post: InboxPost) -> Result<(), VerifierError> {
match self.client_request::<_,()>(post).await
//log_info!("post_to_inbox {:?}",post);
let res = match self.client_request::<_,()>(post).await
{
Err(e) => Err(VerifierError::InboxError(e.to_string())),
Ok(SoS::Stream(_)) => Err(VerifierError::InboxError(NgError::InvalidResponse.to_string())),
Ok(SoS::Single(_)) => Ok(()),
}
};
//log_info!("res {:?}",res);
res
}
pub(crate) async fn create_social_query_forwarder(
@ -537,14 +540,15 @@ impl Verifier {
return Err(VerifierError::InvalidResponse);
}
// for t in triples.iter() {
// log_info!("{}",t.to_string());
// }
let overlay_id = self.repos.get(&response.query_id).ok_or(VerifierError::RepoNotFound)?.store.outer_overlay();
let nuri_ov = NuriV0::repo_graph_name(&response.query_id, &overlay_id);
let graph_name = NamedNode::new_unchecked(&nuri_ov);
let quads = triples.into_iter().map(|t| t.in_graph(graph_name.clone()) ).collect();
let commits = self.prepare_sparql_update(quads, vec![], vec![]).await?;
let commits = self.prepare_sparql_update(quads, vec![], self.get_peer_id_for_skolem()).await?;
} else {

Loading…
Cancel
Save