Skip to content
Snippets Groups Projects
Commit 99557ecf authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

index all data from amt

parent 3dea86bb
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ This will make easier to insert this data in any AMT or other data structure. ...@@ -29,7 +29,7 @@ This will make easier to insert this data in any AMT or other data structure.
# doAllCplusCidsToAMT() # doAllCplusCidsToAMT()
# takes about 180 seconds # takes about 180 seconds
time npx tsx src/scripts/cesium-plus-import.ts time npx tsx src/scripts/cesium-plus-import.ts
# bafyreigczogsiuhaqus7eucalkwsy4vfkh3f4zg3c3rkvltxrwji6p5rnq # bafyreiffn3kkrakf7qj5m3wepsoxjwcojmesfyoexsvsovx23nhbfqu6bq
``` ```
## Start collector and indexer ## Start collector and indexer
......
...@@ -17,7 +17,8 @@ export interface CplusProfile { ...@@ -17,7 +17,8 @@ export interface CplusProfile {
issuer: string issuer: string
hash: string hash: string
signature: string signature: string
geoloc?: Geoloc city?: string
geoPoint?: Geoloc
socials?: Social[] socials?: Social[]
tags?: string[] tags?: string[]
avatar?: Avatar | CID avatar?: Avatar | CID
...@@ -37,8 +38,8 @@ interface Avatar { ...@@ -37,8 +38,8 @@ interface Avatar {
// geoloc // geoloc
interface Geoloc { interface Geoloc {
latitude: number lat: number
longitude: number lon: number
} }
// ========================= import functions // ========================= import functions
......
import { CESIUM_PLUS_PROFILE_INSERT } from '../consts' import { CESIUM_PLUS_PROFILE_IMPORT, CESIUM_PLUS_PROFILE_INSERT } from '../consts'
import type { IndexRequest } from '../types' import type { IndexRequest } from '../types'
import { CID } from 'multiformats' import { CID } from 'multiformats'
import pg from 'pg' import pg from 'pg'
import { kubo } from '../kubo' import { kubo } from '../kubo'
import type { CplusProfile } from '../cesium-plus'
// TODO proper env // TODO proper env
const env = { const env = {
...@@ -35,7 +36,10 @@ export async function getLatestIndexedCID(): Promise<CID | null> { ...@@ -35,7 +36,10 @@ export async function getLatestIndexedCID(): Promise<CID | null> {
} }
} }
export async function setLatestIndexedCID(cid: CID) { export async function setLatestIndexedCID(cid: CID) {
// TODO await client.query(
'INSERT INTO meta(key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = meta.value',
['last_indexed_cid', cid.toString()]
)
} }
// cesium plus profile query and param builder // cesium plus profile query and param builder
...@@ -52,7 +56,7 @@ const cesiumPlusProfile: QueryBuilder = { ...@@ -52,7 +56,7 @@ const cesiumPlusProfile: QueryBuilder = {
city = COALESCE(EXCLUDED.city, profiles.city), city = COALESCE(EXCLUDED.city, profiles.city),
socials = COALESCE(EXCLUDED.socials, profiles.socials); socials = COALESCE(EXCLUDED.socials, profiles.socials);
`, `,
paramBuilder: (irCID: CID, ir: IndexRequest, dataCID: CID, data: any) => [ paramBuilder: (irCID: CID, ir: IndexRequest, dataCID: CID, data: CplusProfile) => [
// $1 index_request_cid // $1 index_request_cid
irCID.toString(), irCID.toString(),
// $2 time // $2 time
...@@ -66,26 +70,50 @@ const cesiumPlusProfile: QueryBuilder = { ...@@ -66,26 +70,50 @@ const cesiumPlusProfile: QueryBuilder = {
// $6 description // $6 description
data.description, data.description,
// $7 avatar // $7 avatar
data.avatar.toString(), data.avatar?.toString(),
// $8 geoloc // $8 geoloc
data.geoloc?.latitude, data.geoPoint?.lat,
// $9 // $9
data.geoloc?.longitude, data.geoPoint?.lon,
// $10 city // $10 city
data.city, data.city,
// $11 socials // $11 socials
data.socials data.socials ? JSON.stringify(data.socials) : undefined
] ]
} }
// insert index request in database // insert index request in database
export async function handleInsertRequest(irCID: CID, ir: IndexRequest) { export async function handleInsertRequest(irCID: CID, ir: IndexRequest) {
if (ir.kind.toString() == CESIUM_PLUS_PROFILE_INSERT.toString()) { console.log('indexing ' + irCID)
switch (ir.kind.toString()) {
// insert new format
case CESIUM_PLUS_PROFILE_INSERT.toString(): {
const dataCID = ir.data
if (dataCID == null) {
console.error('no data')
return
}
const data = (await kubo.dag.get(dataCID)).value
await client.query(cesiumPlusProfile.query, cesiumPlusProfile.paramBuilder(irCID, ir, dataCID, data))
break
}
// insert old import
case CESIUM_PLUS_PROFILE_IMPORT.toString(): {
const dataCID = ir.data const dataCID = ir.data
if (dataCID == null) { if (dataCID == null) {
return // TODO log error console.error('no data')
return
} }
const data = (await kubo.dag.get(dataCID)).value const data = (await kubo.dag.get(dataCID)).value
await client.query(cesiumPlusProfile.query, cesiumPlusProfile.paramBuilder(irCID, ir, dataCID, data)) await client.query(cesiumPlusProfile.query, cesiumPlusProfile.paramBuilder(irCID, ir, dataCID, data))
break
}
// unimplemented
default:
console.log('unimplemented kind ' + ir.kind)
break
} }
} }
...@@ -31,6 +31,9 @@ export async function indexStart(cid: CID) { ...@@ -31,6 +31,9 @@ export async function indexStart(cid: CID) {
isIndexing = true isIndexing = true
// read latest indexed root CID // read latest indexed root CID
const latestCID = await getLatestIndexedCID() const latestCID = await getLatestIndexedCID()
console.log('latest indexed cid ' + latestCID)
if (latestCID?.toString() != cid.toString()) {
// if defined iterate over all index requests of diff // if defined iterate over all index requests of diff
// else iterate over all index requests of given cid // else iterate over all index requests of given cid
const iterator = latestCID ? getDiff(latestCID, cid) : getAll(cid) const iterator = latestCID ? getDiff(latestCID, cid) : getAll(cid)
...@@ -39,6 +42,10 @@ export async function indexStart(cid: CID) { ...@@ -39,6 +42,10 @@ export async function indexStart(cid: CID) {
} }
// write root CID as lastest indexed CID // write root CID as lastest indexed CID
await setLatestIndexedCID(cid) await setLatestIndexedCID(cid)
console.log('new latest indexed cid ' + cid)
} else {
console.log('already indexed ' + cid)
}
// unlock // unlock
isIndexing = false isIndexing = false
// send check queue event to see if diff came in the meanwile // send check queue event to see if diff came in the meanwile
......
...@@ -5,16 +5,15 @@ import { getAll } from '../interface' ...@@ -5,16 +5,15 @@ import { getAll } from '../interface'
import { kubo } from '../kubo' import { kubo } from '../kubo'
import type { IndexRequest } from '../types' import type { IndexRequest } from '../types'
const cid = CID.parse('bafyreiergrf2jnnh4tg4losvncm6jjgql4rsveadpxutvz2xa6zdb7azri') const cid = CID.parse('bafyreiffn3kkrakf7qj5m3wepsoxjwcojmesfyoexsvsovx23nhbfqu6bq')
await indexStart(cid)
// await client.end()
// await indexStart(cid)
// // example to insert a specific request
const irCID = CID.parse('bafyreifpzcrghku22vp5oqzqgj4v3hxfhewe5d23kqdkhnkfapnrjgdoje') // const irCID = CID.parse('bafyreifpzcrghku22vp5oqzqgj4v3hxfhewe5d23kqdkhnkfapnrjgdoje')
const ir = await kubo.dag.get(irCID) // const ir = await kubo.dag.get(irCID)
// await handleInsertRequest(irCID, ir.value)
await handleInsertRequest(irCID, ir.value)
await client.end()
// // example to iterate over all // // example to iterate over all
// for await (const leaf of getAll(cid)) { // for await (const leaf of getAll(cid)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment