LiDa_Search/js/dataMgr.js

204 lines
6.1 KiB
JavaScript
Raw Normal View History

2025-01-28 17:22:52 +01:00
import * as duckdb from 'https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.29.0/+esm';
2025-01-31 14:22:31 +01:00
2025-01-28 17:22:52 +01:00
//import * as arrow from 'https://cdn.skypack.dev/apache-arrow@9'
2025-01-31 14:58:34 +01:00
var rif_commentatori = [];
var rif_autori = [];
var rif_areetematiche = [];
var rif_fonti = [];
var listaidversi = []
2025-01-31 14:22:31 +01:00
const cantobadgeclassBis = 'badge badge-outline-warning badge-pill'
2025-01-30 14:53:57 +01:00
2025-01-28 17:22:52 +01:00
const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);
const worker_url = URL.createObjectURL(
2025-01-31 14:58:34 +01:00
new Blob([`importScripts("${bundle.mainWorker}");`], { type: 'text/javascript' })
2025-01-28 17:22:52 +01:00
);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(worker_url);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
URL.revokeObjectURL(worker_url);
2025-01-31 14:58:34 +01:00
console.log('datamgr init done')
2025-01-28 17:22:52 +01:00
//await db.registerFileURL('remote.parquet', 'http://localhost:8000/js/data/riferimenti_3.parquet', DuckDBDataProtocol.HTTP, false);
/*const res = await fetch('http://localhost:8000/js/data/riferimenti_3.parquet');
await db.registerFileBuffer('buffer.parquet', new Uint8Array(await res.arrayBuffer()));*/
const conn = await db.connect();
await conn.query(`
CREATE TABLE riferimenti AS
SELECT * FROM "https://lida.dantenetwork.it/js/data/riferimenti_3.parquet";
`);
2025-01-31 14:58:34 +01:00
var table = await conn.query(`
2025-01-30 14:53:57 +01:00
select distinct Aut from riferimenti order by Aut COLLATE NOCASE;
2025-01-28 17:22:52 +01:00
`);
const result = table.toArray().map((row) => row.toJSON());
2025-01-31 14:58:34 +01:00
rif_autori = result.map((x) => x['Aut']);
2025-01-30 14:53:57 +01:00
//console.log(rif_autori)
2025-01-31 14:58:34 +01:00
var table = await conn.query(`
2025-01-30 14:53:57 +01:00
select distinct Com from riferimenti;
2025-01-28 17:22:52 +01:00
`);
const result2 = table.toArray().map((row) => row.toJSON());
2025-01-31 14:58:34 +01:00
rif_commentatori = result2.map((x) => x['Com']);
2025-01-28 17:22:52 +01:00
2025-01-31 10:50:55 +01:00
2025-01-31 14:58:34 +01:00
var table = await conn.query(`
2025-01-30 14:53:57 +01:00
select distinct CA from riferimenti order by CA;
2025-01-28 17:22:52 +01:00
`);
const result3 = table.toArray().map((row) => row.toJSON());
2025-01-31 14:58:34 +01:00
rif_areetematiche = result3.map((x) => x['CA']);
2025-01-31 10:50:55 +01:00
2025-01-31 14:58:34 +01:00
var table1 = await conn.query(`
2025-01-31 17:01:27 +01:00
select distinct TiFo from riferimenti order by TiFo COLLATE NOCASE;
2025-01-31 10:50:55 +01:00
`);
const result4 = table1.toArray().map((row) => row.toJSON());
2025-02-02 17:42:22 +01:00
//console.log(result4)
2025-01-31 14:58:34 +01:00
rif_fonti = result4.map((x) => x['TiFo']);
2025-01-31 10:50:55 +01:00
//console.log(rif_fonti)
2025-01-30 14:53:57 +01:00
await conn.close();
2025-01-28 17:22:52 +01:00
console.log('done')
2025-01-31 14:58:34 +01:00
export function getListaRis(){
return listaidversi;
}
2025-01-31 17:01:27 +01:00
export function cleanListaRis(){
listaidversi=[];
}
2025-01-31 10:50:55 +01:00
export function getFontiS() {
2025-01-31 14:58:34 +01:00
2025-01-31 10:50:55 +01:00
return rif_fonti
}
2025-01-30 14:53:57 +01:00
export function getAreeTematicheS() {
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
return rif_areetematiche
}
export function getAutoriFontiCitazioniS() {
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
return rif_autori
}
export function getCommentatoriS() {
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
return rif_commentatori
}
export async function getVersiConCitazioni(canticapar = '', cantopar = '') {
2025-01-31 14:58:34 +01:00
2025-01-31 10:50:55 +01:00
//let citcantiche = ['Inferno', 'Purgatorio', 'Paradiso']
2025-01-30 14:53:57 +01:00
if (canticapar == '') {
return
}
2025-01-31 14:58:34 +01:00
const tconn = await db.connect();
var table = await tconn.query(`
2025-01-30 14:53:57 +01:00
select Ann as Annotazione, Com as Commentario, FrN as frammentoNota, AC as AutoreCitazione,
F as FonteCitazione, LF as LuogoFonteCitazione, NF as NotaFonteCitazione,
TF as TestoFonteCitazione, UF as URLFonteCitazione, NaRi as NaturaRiferimento,
RCC as RapportoCommentoCommentatoreText, RSO as RapportoSoggettoOggetto,
Aut as NomeAutoreCitazione, TiFo as TitoloFonteCitazione, Verso as VersoCitazione, CA as AreaTematica,
TiCi as TipoCitazione, CEP as CitEpisodi, CIM as CitImmagini, CTE as CitTeorie,
2025-02-02 17:42:22 +01:00
CMO as CitMotivi, CST as CitStilemi, CTO as CitTopografie, CPER as CitPersonaggi, IdRiferimento as Id
2025-01-30 14:53:57 +01:00
from riferimenti
where Cantica='${canticapar}' and Canto='${cantopar}' order by verso;
`);
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
var result4 = table.toArray().map((row) => row.toJSON());
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
await tconn.close();
return result4
2025-01-31 14:58:34 +01:00
2025-01-30 14:53:57 +01:00
}
2025-01-31 14:58:34 +01:00
export async function queryRiferimenti(filtro, cid) {
if (filtro == null) {
2025-01-31 10:50:55 +01:00
return
}
console.log(filtro)
2025-01-31 14:58:34 +01:00
const tconn = await db.connect();
var querytl = 'select Cantica, Canto, Verso from riferimenti'
var queryfi = ' where '
if (filtro.Commentatore != null && filtro.Commentatore.trim() != 'all')
queryfi += "Com='" + filtro.Commentatore.trim() + "'"
2025-01-31 10:50:55 +01:00
else
2025-01-31 14:58:34 +01:00
queryfi += "Com like '%'"
if (filtro.Autore_Fonte != null && filtro.Autore_Fonte.trim() != 'all')
queryfi += " and Aut='" + filtro.Autore_Fonte.trim() + "'"
2025-01-31 17:01:27 +01:00
if (filtro.Fonte != null && filtro.Fonte.trim() != 'all')
queryfi += " and TiFo='" + filtro.Fonte.trim() + "'"
if (filtro.Area_Tematica != null && filtro.Area_Tematica.trim() != 'all')
queryfi += " and CA='" + filtro.Area_Tematica.trim() + "'"
if (filtro.Tipo_Riferimento != null && filtro.Tipo_Riferimento.trim() != 'all')
queryfi += " and NaRi='" + filtro.Tipo_Riferimento.trim().toUpperCase() + "'"
2025-01-31 14:58:34 +01:00
var rs = await tconn.query(querytl + queryfi)
2025-01-31 10:50:55 +01:00
var result4 = rs.toArray().map((row) => row.toJSON());
2025-01-31 14:58:34 +01:00
2025-01-31 10:50:55 +01:00
await tconn.close();
2025-01-31 14:58:34 +01:00
var resxcantica = Object.groupBy(result4, ({ Cantica }) => Cantica);
2025-01-31 14:22:31 +01:00
2025-01-31 17:01:27 +01:00
listaidversi=[]
2025-01-31 14:58:34 +01:00
var numcid = cid.split('_')[1]
for (var cantica in resxcantica) {
2025-01-31 14:22:31 +01:00
var $nctags = $(' .navig-canto')
2025-01-31 10:50:55 +01:00
console.log(resxcantica[cantica].length)
2025-01-31 14:58:34 +01:00
var i = 1;
if (cantica == 'Purgatorio')
i = 2
if (cantica == 'Paradiso')
i = 3
2025-01-31 10:50:55 +01:00
var spanbadgecantica = $('<span />');
spanbadgecantica.attr('class', 'badge badge-warning badge-pill mx-2');
spanbadgecantica.append(resxcantica[cantica].length)
spanbadgecantica.appendTo($('#cantica' + i + '-badge'))
console.log(spanbadgecantica)
2025-01-31 14:58:34 +01:00
var tmp = resxcantica[cantica]
var resxcanto = Object.groupBy(tmp, ({ Canto }) => Canto);
for (var canto in resxcanto) {
var tmpv = resxcanto[canto]
2025-01-31 14:22:31 +01:00
var spanbadge = $('<span />');
spanbadge.attr('class', cantobadgeclassBis);
spanbadge.append(resxcanto[canto].length)
2025-01-31 14:58:34 +01:00
if (canto.startsWith('0')) {
canto = canto.replace('0', '')
2025-01-31 14:22:31 +01:00
}
spanbadge.appendTo($nctags.filter('[name="' + cantica + 'Canto ' + canto + '"]'))
2025-01-31 14:58:34 +01:00
for (const tmpvi of tmpv) {
let myveid = '#' + cantica + '_Canto_' + canto + '_' + tmpvi.Verso.split('-')[0]
2025-01-31 17:01:27 +01:00
//console.log(myveid)
2025-01-31 14:58:34 +01:00
listaidversi.push(myveid)
if (cid.includes(cantica) && numcid == canto)
2025-01-31 17:01:27 +01:00
$(myveid).addClass("mark ")
2025-01-31 14:22:31 +01:00
}
2025-01-31 14:58:34 +01:00
2025-01-31 14:22:31 +01:00
}
drawMinimap(cid, true)
2025-01-31 14:58:34 +01:00
2025-01-31 10:50:55 +01:00
}
/*console.log(resxcantica)*/
2025-01-31 14:58:34 +01:00
return listaidversi
2025-01-31 10:50:55 +01:00
}