11import { generateGraphId } from '../idGenerator' ;
22import { Argument , Edge , Graph , GraphData } from '../../.shared/types' ;
33import { getArgumentScores } from '../../analysis/argumentScoreHandler' ;
4- import { getReactionCountsFromDb } from './reactionOperations' ;
4+ import { getReactionCounts , getUserReactionsByGraphId } from './reactionOperations' ;
55import { getTimestamp } from '../getTimestamp' ;
6- import { DbArgument , DbEdge , DbGraph , DbReaction } from '../dbTypes' ;
6+ import { DbGraph } from '../dbTypes' ;
77import { query , queryMany , queryOne } from '../db' ;
88import { memoryCache , withCache } from '../../services/cacheService' ;
99import { getArgumentsByGraphId } from './argumentOperations' ;
10- import { getEdgesFromDb } from './edgeOperations' ;
10+ import { getEdgesByGraphId } from './edgeOperations' ;
1111
1212/*
1313 Cached functions
@@ -57,44 +57,37 @@ export async function getUserGraphs(userId: string): Promise<GraphData[]> {
5757 ) ;
5858}
5959
60-
61- export async function getFullGraph ( graphId : string , userId ?: string ) : Promise < Graph > {
62- const cacheKey = `graph:${ graphId } ${ userId ? `:${ userId } ` : '' } ` ;
60+ export async function getGraphName ( graphId : string ) : Promise < string > {
61+ const cacheKey = `graph-name:${ graphId } ` ;
6362
6463 return withCache (
6564 cacheKey ,
6665 60 * 60 * 1000 , // 1 hour cache
67- ( ) => getFullGraphFromDb ( graphId , userId )
66+ async ( ) => {
67+ const result = await query ( 'SELECT name FROM graphs WHERE id = $1' , [ graphId ] ) ;
68+ return result . rows [ 0 ] . name ;
69+ }
6870 ) ;
6971}
7072
71- /*
72- Heavy functions, meant to be used from cached or rarely used functions
73- */
74-
75- async function getFullGraphFromDb ( graphId : string , userId ?: string ) : Promise < Graph > {
76- console . log ( `Loading graph into cache: ${ graphId } ` ) ;
73+ export async function getFullGraph ( graphId : string , userId ?: string ) : Promise < Graph > {
74+ console . log ( `Getting full graph ${ graphId } for user ${ userId } ` ) ;
7775 const startTime = performance . now ( ) ;
7876
7977 const [
80- { rows : [ { name } ] } ,
78+ name ,
8179 argumentsResult ,
8280 edgesResult ,
8381 reactionCounts ,
8482 argumentScores ,
8583 userReactionsResult
8684 ] = await Promise . all ( [
87- query ( 'SELECT name FROM graphs WHERE id = $1' , [ graphId ] ) ,
85+ getGraphName ( graphId ) ,
8886 getArgumentsByGraphId ( graphId ) ,
89- getEdgesFromDb ( [ graphId ] ) ,
90- getReactionCountsFromDb ( graphId ) ,
87+ getEdgesByGraphId ( graphId ) ,
88+ getReactionCounts ( graphId ) ,
9189 getArgumentScores ( graphId ) ,
92- userId ? queryMany < DbReaction > (
93- `SELECT *
94- FROM reactions
95- WHERE user_id = $1 AND argument_id IN (SELECT id FROM arguments WHERE graph_id = $2)` ,
96- [ userId , graphId ]
97- ) : Promise . resolve ( [ ] )
90+ userId ? getUserReactionsByGraphId ( userId , graphId ) : Promise . resolve ( [ ] )
9891 ] ) ;
9992
10093 if ( ! name ) {
@@ -133,7 +126,7 @@ async function getFullGraphFromDb(graphId: string, userId?: string): Promise<Gra
133126
134127 const endTime = performance . now ( ) ;
135128 const duration = ( ( endTime - startTime ) / 1000 ) . toFixed ( 2 ) ;
136- console . log ( `Loaded graph "${ name } " (${ graphId } ) in ${ duration } s` ) ;
129+ console . log ( `Loaded graph "${ name } " (${ graphId } ) for user ${ userId || 'anonymous' } in ${ duration } s` ) ;
137130
138131 return {
139132 id : graphId ,
@@ -143,6 +136,10 @@ async function getFullGraphFromDb(graphId: string, userId?: string): Promise<Gra
143136 } as Graph ;
144137}
145138
139+ /*
140+ Heavy functions, meant to be used from cached or rarely used functions
141+ */
142+
146143async function getGraphDataFromDb ( graphIds : string [ ] ) : Promise < GraphData [ ] > {
147144 const placeholders = graphIds . map ( ( _ , i ) => `$${ i + 1 } ` ) . join ( ',' ) ;
148145 const result = await query (
0 commit comments