Graph filtering task: there are two endpoints — GET /graph (returns a graph of vertices and edges by person) and GET /events (returns events from ClickHouse by person with a location field). You need to implement an endpoint that filters the graph, leaving only those vertices that appeared in events with a specific location (for example, Moscow). Describe the implementation logic.
sobes.tech AI
Answer from AI
To implement an endpoint that filters a graph by event location, you can use the following logic:
-
Retrieve events from the
/eventsendpoint for a person, applying a filter for the desired location (e.g., Moscow). This will provide a list of events with alocationfield. -
Extract unique vertex identifiers (e.g., place IDs or related entity IDs) from these events.
-
Obtain the full graph for the person from the
/graphendpoint. -
Filter the vertices of the graph, keeping only those whose IDs are in the list from step 2.
-
Filter the edges of the graph, keeping only those that connect the remaining vertices.
-
Return the resulting filtered graph.
An example in Go (simplified):
// Retrieve events with the desired location
events := getEvents(personID, "Moscow")
// Extract vertex IDs from events
vertexIDs := make(map[string]bool)
for _, e := range events {
vertexIDs[e.VertexID] = true
}
// Get the full graph
graph := getGraph(personID)
// Filter vertices
filteredVertices := []Vertex{}
for _, v := range graph.Vertices {
if vertexIDs[v.ID] {
filteredVertices = append(filteredVertices, v)
}
}
// Filter edges
filteredEdges := []Edge{}
for _, edge := range graph.Edges {
if vertexIDs[edge.From] && vertexIDs[edge.To] {
filteredEdges = append(filteredEdges, edge)
}
}
// Create and return the filtered graph
filteredGraph := Graph{
Vertices: filteredVertices,
Edges: filteredEdges,
}
return filteredGraph
This approach allows for efficient filtering of the graph based on events with the specified location.