Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Database, aql } from "arangojs";
- // Connect to ArangoDB
- const db = new Database({
- url: "http://localhost:8529",
- databaseName: "unitres",
- auth: { username: "root", password: "12345" },
- });
- const edge_weights: Record<string, number> = {
- has_email: 5,
- has_mobile: 3,
- has_device: 2,
- has_ip: 1,
- };
- // Clean up: delete all non-system collections (optional, for a fresh start)
- async function clear_and_start() {
- const collections = await db.listCollections();
- for (const coll of collections) {
- if (!coll.isSystem) {
- await db.collection(coll.name).drop();
- }
- }
- // Ensure collections and indexes exist (idempotent)
- const vertex = ['person','email','device','mobile','ip'];
- const edges = ['has_email','has_device','has_mobile','has_ip'];
- for (const name of vertex) {
- if (!(await db.collection(name).exists())) {
- await db.collection(name).create();
- }
- }
- for (const name of edges) {
- if (!(await db.collection(name).exists())) {
- await db.collection(name).create({ type: 3 }); // 3 = edge collection
- }
- }
- await db.collection('email').ensureIndex({ type: 'persistent', fields: ['email_id', 'disposable'], unique: true });
- await db.collection('device').ensureIndex({ type: 'persistent', fields: ['fingerprint'], unique: true });
- await db.collection('mobile').ensureIndex({ type: 'persistent', fields: ['mobile_number', 'prepaid', 'imei'], unique: true });
- await db.collection('ip').ensureIndex({ type: 'persistent', fields: ['ip_address'], unique: true });
- }
- async function insert(
- visitor_id: string,
- session_id: string,
- lead_id: string,
- case_id: string | null,
- risk_score: string,
- mobile_number: string,
- prepaid: number,
- imei: string,
- email_id: string,
- disposable: number,
- ip_address: string
- ): Promise<string | undefined> {
- // Insert person node
- const cursor = await db.query(aql`
- INSERT {
- visitor_id: ${visitor_id},
- session_id: ${session_id},
- lead_id: ${lead_id},
- case_id: ${case_id},
- risk_score: ${risk_score}
- } INTO person
- RETURN NEW._key
- `);
- const graph_case_id = await cursor.next();
- const attribute_specs: [string, string, Record<string, any>][] = [
- ['email', 'has_email', { email_id, disposable }],
- ['device', 'has_device', { fingerprint: session_id }],
- ['mobile', 'has_mobile', { mobile_number, prepaid, imei }],
- ['ip', 'has_ip', { ip_address }],
- ];
- for (const [coll, edge_coll, attrs] of attribute_specs) {
- const attrs_with_key = { ...attrs, _key: graph_case_id };
- // Upsert attribute node
- const nodeCursor = await db.query(aql`
- UPSERT ${attrs}
- INSERT ${attrs_with_key}
- UPDATE {}
- IN ${db.collection(coll)}
- RETURN NEW._key
- `);
- const node_key = await nodeCursor.next();
- // Insert edge
- await db.query(aql`
- INSERT {
- _from: ${`person/${graph_case_id}`},
- _to: ${`${coll}/${node_key}`},
- weight: ${edge_weights[edge_coll]}
- } INTO ${db.collection(edge_coll)}
- `);
- }
- return graph_case_id;
- }
- async function get_efr(graph_case_id: string): Promise<number | null> {
- const cursor = await db.query(aql`
- LET avgEfr = AVERAGE(
- FOR v, e, p IN 1..10 ANY ${`person/${graph_case_id}`}
- has_mobile, has_email, has_ip, has_device
- OPTIONS {order: "bfs"}
- FILTER v.risk_score == 'P1'
- LIMIT 5
- LET edgeWeights = p.edges[*].weight
- LET totalWeight = SUM(edgeWeights)
- LET efrScore = totalWeight / LENGTH(edgeWeights)
- RETURN efrScore
- )
- RETURN MIN([100, ROUND(avgEfr)])
- `);
- return await cursor.next();
- }
- async function update_caseid_riskscore(graph_case_id: string, case_id: string | number, new_risk_score: string) {
- await db.query(aql`
- UPDATE ${graph_case_id}
- WITH { risk_score: ${new_risk_score}, case_id: ${case_id} }
- IN person
- `);
- }
- async function get_subgraph(graph_case_id: string, k: number) {
- const cursor = await db.query(aql`
- FOR v, e, p IN 1..${k} ANY ${`person/${graph_case_id}`}
- has_mobile, has_email, has_ip, has_device
- OPTIONS {order: "bfs"}
- RETURN p
- `);
- return await cursor.all();
- }
- // --- Example usage ---
- (async () => {
- await clear_and_start();
- const p1 = await insert('vid1','sid1','lid1', null, 'P1','+919999999999', 1, 'imei123', '[email protected]', 0, '192.168.1.1');
- const p2 = await insert('vid2','sid2','lid2', null, 'P2', '+919999999999', 1, 'imei123', '[email protected]', 0, '192.168.1.1');
- const p3 = await insert('vid3','sid3','lid3', null, 'P5','+919999999998', 1, 'imei123', '[email protected]', 1, '192.167.1.1');
- const p4 = await insert('vid4','sid4','lid4', null, 'P4','+949999999992', 1, 'imei123', '[email protected]', 0, '192.1688.1.1');
- console.log(p1, p2, p3, p4);
- console.log(await get_efr(p1!));
- console.log(await get_efr(p2!));
- console.log(await get_efr(p3!));
- console.log(await get_efr(p4!));
- await update_caseid_riskscore(p3!, "678", "P1");
- const subgraph = await get_subgraph(p1!, 6);
- if (subgraph.length > 0) {
- console.log(subgraph[0]);
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement