Skip to content

Commit

Permalink
generate full graph
Browse files Browse the repository at this point in the history
  • Loading branch information
electron271 committed Dec 3, 2024
1 parent 97d349f commit b80372f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
docker-compose.yml
data/graph.gexf
data/graph-full.gexf
postgres-data/
.env
node_modules/
3 changes: 2 additions & 1 deletion docker-compose.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ services:
restart: unless-stopped
ports:
- 8080:8080 # this is a really common port and should be changed most likely to something else
depends_on:
- app
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./public/:/usr/share/nginx/html/public
Expand All @@ -34,6 +36,5 @@ services:
- ./data/:/app/data
depends_on:
- db
- nginx
environment:
- DATABASE_URL=postgresql://postgres:slightlysecurepassword123@db:5432/postgres
7 changes: 6 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ http {
proxy_pass http://app:8000;
}

# Serve static files in /data
# Serve static files in /data and block /data/graph-full.gexf as 403
location /data {
alias /usr/share/nginx/html/data;
}

location /data/graph-full.gexf {
deny all;
return 403;
}
}
}
34 changes: 34 additions & 0 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ async function generateGEXF() {
}
}

async function generateFullGEXF() { // generate full graph with all users
await console.log('Generating FULL graph...');

// Create a new graph
const graph = new Graph();

// Fetch all mentions and user lookups
const mentions = await prisma.mention.findMany();
const userLookups = await prisma.userLookup.findMany();

// add nodes to the graph
userLookups.forEach((user) => {
graph.addNode(user.id, { label: user.username });
});

// add edges to the graph
mentions.forEach((mention) => {
if (graph.hasEdge(mention.user1Id, mention.user2Id)) {
graph.updateEdgeAttribute(mention.user1Id, mention.user2Id, 'weight', (w) => w + mention.count);
} else {
graph.addEdge(mention.user1Id, mention.user2Id, { weight: mention.count });
}
});

// Export the graph
try {
fs.writeFileSync('data/graph-full.gexf', gexf.write(graph));
console.log('Graph successfully exported to data/graph-full.gexf');
} catch (err) {
console.error('Error exporting graph:', err);
}
}

// On client ready
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
Expand Down Expand Up @@ -249,6 +282,7 @@ client.on(Events.MessageCreate, async (message) => {
if (mentionsCounter >= MENTIONS_THRESHOLD) {
mentionsCounter = 0; // Reset counter
await generateGEXF();
await generateFullGEXF();
}
});

Expand Down

0 comments on commit b80372f

Please sign in to comment.