Public
Edited
Nov 27, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
prereest_plot_data = {
let thedata = await client.query(`
with chosen_emisor_data as (
select emisor as emisor, "year" as "year", principal as principal, interes as interes
from calendario_deuda_prereestructuracion
where emisor = '${chosen_emisor_prereest}'
)

unpivot chosen_emisor_data
on Principal, Interes
into
name componente_deuda
value cantidad
`)

return thedata
}
Insert cell
available_emisor_prereest = {
let thedata = await client.query(`
select distinct Emisor as emisor
from calendario_deuda_prereestructuracion
order by emisor
`)
thedata = thedata.map(d => d.emisor)

return thedata
}
Insert cell
client
from calendario_deuda_prereestructuracion
Insert cell
data_after_categoria_emisor_filter = {

// Query string
let query = `
from pagos_deuda_historicos
${chosen_categoria_emisor === 'Todos' ? '' : `where categoria_emisor = '${chosen_categoria_emisor}'`}
`;

// Save as table
await client.query(`
create or replace table data_after_categoria_emisor_filter as (${query})
`)

// Get data as JS
let data = await client.query('from data_after_categoria_emisor_filter')

return data
}
Insert cell
emisores_after_categoria_emisor_filter = {
// Run these first. For dependency graph
data_after_categoria_emisor_filter;

// Query string
let query = `
select distinct emisor_nombre_en
from data_after_categoria_emisor_filter
order by emisor_nombre_en
`

// Not saving as table

// Get data as JS
let data = await client.query(query)
data = data.map(d => d.emisor_nombre_en)

return data
}
Insert cell
data_after_emisor_filter = {
// Dependencies
chosen_emisor;

// Query string
let query = `
from data_after_categoria_emisor_filter
${chosen_emisor !== null ? `where emisor_nombre_en = '${chosen_emisor}'` : ''}
`;

// Save as table
await client.query(`
create or replace table data_after_emisor_filter as (${query})
`)

// Get data as JS
let data = await client.query('from data_after_emisor_filter')

return data
}
Insert cell
data_for_plot
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data_for_plot = {
// Dependencies
data_after_emisor_filter;

let data = await client.query(`
select "year", "componente_deuda", sum(cantidad) as cantidad
from data_after_emisor_filter
group by "year", "componente_deuda"
order by "year", componente_deuda = 'principal' desc, componente_deuda = 'interes' desc
`)

return data
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
db_instance.getVersion()
Insert cell
duckdb = import(
"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.1-dev232.0/+esm"
)
Insert cell
bundle = {
const bundles = duckdb.getJsDelivrBundles()
if (duckdb_bundle === 'Auto') {
return duckdb.selectBundle(bundles)
} else {
const bun = bundles[duckdb_bundle]
bun['pthreadWorker'] = null;
return bun;
}
// return bundles['mvp']
}
Insert cell
async function makeDB() {
const logger = new duckdb.ConsoleLogger();
const worker = await duckdb.createWorker(bundle.mainWorker);
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule);
return db
}
Insert cell
db_instance = {
const db = await makeDB()

await insertFile(db, 'pagos_deuda_historicos_file', pagos_deuda_historicos_file);
await insertFile(db, 'calendario_deuda_prereestructuracion_file', calendario_deuda_prereestructuracion_file);

return db
}
Insert cell
client = {
const client_class = (duckdb_client === 'Compatibility') ? DuckDBClientCompat : DuckDBClient;
const c = await new client_class(db_instance);

await c.query(`
create or replace table pagos_deuda_historicos as (
from pagos_deuda_historicos_file
)
`)

await c.query(`
create or replace table calendario_deuda_prereestructuracion as (
from calendario_deuda_prereestructuracion_file
)
`)
return c;
}
Insert cell
Insert cell
class DuckDBClientCompat extends DuckDBClient {
async queryStream(query, params) {
const connection = await this._db.connect();
let reader, batch;
try {
if (params?.length > 0) {
const statement = await connection.prepare(query);
reader = await statement.send(...params);
} else {
reader = await connection.send(query);
}
batch = await reader.next();
if (batch.done) throw new Error("missing first batch");
} catch (error) {
await connection.close();
throw error;
}

// Mosaic utility: convert Arrow value to Javascript value
const converters = {}
batch.value.schema.fields.forEach(d => {
console.log('Type for ', d.name, d.type)
converters[d.name] = convertArrowValue(d.type)
})
return {
schema: getArrowTableSchema(batch.value),
async *readRows() {
try {
while (!batch.done) {
let batch_array = batch.value.toArray();

// Convert all values to Javascript version
let object_array = []
for (let i = 0; i < batch_array.length; i++) {
const d_proxy = batch_array[i];
const d_obj = {}
for (let k of Object.keys(converters)) {
d_obj[k] = converters[k](d_proxy[k])
}
object_array.push(d_obj)
}
yield object_array;
batch = await reader.next();
}
} finally {
await connection.close();
}
}
};
}
}
Insert cell
Insert cell
Insert cell
getArrowTableSchema = observable_stdlib.getArrowTableSchema
Insert cell
convertArrowValue = mosaic_core.convertArrowValue
Insert cell
observable_stdlib = await import('https://cdn.jsdelivr.net/npm/@observablehq/stdlib@5.8.7/+esm');
Insert cell
mosaic_core = await import('https://cdn.jsdelivr.net/npm/@uwdata/mosaic-core@0.9.0/+esm');
Insert cell
Insert cell
pagos_deuda_historicos_file = FileAttachment("pagos_deuda_historicos.parquet")
Insert cell
calendario_deuda_prereestructuracion_file = FileAttachment("calendario_deuda_prereestructuracion.parquet")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more