Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fixed graph populating #2270

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
splashDataLoading,
runningProjectId,
setRunningProjectMessages,
isFetching,
} = useDataStore((s) => s)

const { setAiSummaryAnswer, getKeyExist, aiRefId } = useAiSummaryStore((s) => s)
Expand Down Expand Up @@ -138,6 +139,10 @@

const handleNewNodeCreated = useCallback(
(data: FetchDataResponse) => {
if (isFetching) {
return
}

if (!queueRef.current) {
queueRef.current = { nodes: [], edges: [] }
}
Expand All @@ -162,7 +167,7 @@
addNewNode(batchedData) // Call the original addNewNode function with batched data
}, 3000) // Adjust delay as necessary
},
[addNewNode],
[addNewNode, isFetching],
)

const handleAiSummaryAnswer = useCallback(
Expand Down Expand Up @@ -290,7 +295,7 @@
}

ws.onmessage = (event) => {
console.log('Message from server:', event.data)

Check warning on line 298 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 298 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

const data = JSON.parse(event.data)

Expand All @@ -312,7 +317,7 @@
}

ws.onclose = () => {
console.log('WebSocket connection closed')

Check warning on line 320 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 320 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement
}
}, [runningProjectId, setRunningProjectMessages])

Expand Down
56 changes: 41 additions & 15 deletions src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import { useAiSummaryStore } from '../useAiSummaryStore'
import { useAppStore } from '../useAppStore'

const deduplicateByRefId = (items) => {
const uniqueMap = new Map()

items.forEach((item) => {
if (!uniqueMap.has(item.ref_id)) {
uniqueMap.set(item.ref_id, item)
}
})

return Array.from(uniqueMap.values())
}

export type GraphStyle = 'sphere' | 'force' | 'split' | 'earth'

export const graphStyles: GraphStyle[] = ['sphere', 'force', 'split', 'earth']
Expand Down Expand Up @@ -251,7 +263,7 @@
sidebarFilterCounts,
})
} catch (error) {
console.log(error)

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/initialRender.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentAnalysis.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/searchAndRender.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNode.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sanity.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentChart.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNodeType.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)

Unexpected console statement

Check warning on line 266 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)

Unexpected console statement

if (error !== 'abort') {
set({ isLoadingNew: false, isFetching: false })
Expand Down Expand Up @@ -303,7 +315,7 @@
setHideNodeDetails: (hideNodeDetails) => set({ hideNodeDetails }),
setSeedQuestions: (questions) => set({ seedQuestions: questions }),
updateNode: (updatedNode) => {
console.log(updatedNode)

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/initialRender.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentAnalysis.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/searchAndRender.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNode.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sanity.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentChart.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNodeType.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)

Unexpected console statement

Check warning on line 318 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)

Unexpected console statement
},
addNewNode: (data) => {
const { dataInitial: existingData } = get()
Expand All @@ -312,34 +324,48 @@
return
}

const currentNodes = [...(existingData?.nodes || [])]
const currentLinks = [...(existingData?.links || [])]
const uniqueIncomingNodes = deduplicateByRefId(data.nodes || [])
const uniqueIncomingEdges = deduplicateByRefId(data.edges || [])

const newNodes = (data?.nodes || []).filter((n) => !currentNodes.some((c) => c.ref_id === n.ref_id))
// Step 2: Existing nodes and links from the current state
const currentNodes = existingData?.nodes ? [...existingData.nodes] : []
const currentLinks = existingData?.links ? [...existingData.links] : []

currentNodes.push(...newNodes)
// Step 3: Use Sets to track unique ref_ids of existing data
const nodeRefIds = new Set(currentNodes.map((node) => node.ref_id))
const linkRefIds = new Set(currentLinks.map((link) => link.ref_id))

const newLinks = (data?.edges || [])
.filter((n) => !currentLinks.some((c) => c.ref_id === n.ref_id))
.filter((c) => {
const { target, source } = c
// Step 4: Filter new nodes and add only unique ones
const newNodes = uniqueIncomingNodes.filter((node) => !nodeRefIds.has(node.ref_id))
const updatedNodes = [...currentNodes, ...newNodes]

return currentNodes.some((n) => n.ref_id === target) && currentNodes.some((n) => n.ref_id === source)
})
// Update `nodeRefIds` with the new nodes added
newNodes.forEach((node) => nodeRefIds.add(node.ref_id))

currentLinks.push(...newLinks)
// Step 5: Filter new links based on unique ref_ids and node presence
const newLinks = uniqueIncomingEdges
.filter((link) => !linkRefIds.has(link.ref_id)) // Check for unique `ref_id`
.filter((link) => {
const { source, target } = link

return nodeRefIds.has(source) && nodeRefIds.has(target) // Ensure nodes exist
})

const nodeTypes = [...new Set(currentNodes.map((i) => i.node_type))]
const updatedLinks = [...currentLinks, ...newLinks]

const sidebarFilters = ['all', ...nodeTypes.map((i) => i.toLowerCase())]
// Step 6: Extract unique node types and create sidebar filters
const nodeTypes = [...new Set(updatedNodes.map((node) => node.node_type))]
const sidebarFilters = ['all', ...nodeTypes.map((type) => type.toLowerCase())]

// Step 7: Calculate sidebar filter counts
const sidebarFilterCounts = sidebarFilters.map((filter) => ({
name: filter,
count: currentNodes.filter((node) => filter === 'all' || node.node_type?.toLowerCase() === filter).length,
count: updatedNodes.filter((node) => filter === 'all' || node.node_type?.toLowerCase() === filter).length,
}))

// Step 8: Update the state with the new data
set({
dataInitial: { nodes: currentNodes, links: currentLinks },
dataInitial: { nodes: updatedNodes, links: updatedLinks },
dataNew: { nodes: newNodes, links: newLinks },
nodeTypes,
sidebarFilters,
Expand All @@ -347,7 +373,7 @@
})
},
removeNode: (id) => {
console.log(id)

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/initialRender.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentAnalysis.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/graph/searchAndRender.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNode.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sanity.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sentimentChart/sentimentChart.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNodeType.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)

Unexpected console statement

Check warning on line 376 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)

Unexpected console statement
},

setRunningProjectId: (runningProjectId) => set({ runningProjectId, runningProjectMessages: [] }),
Expand Down
Loading