Sobes.tech
Senior

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:

  1. Retrieve events from the /events endpoint for a person, applying a filter for the desired location (e.g., Moscow). This will provide a list of events with a location field.

  2. Extract unique vertex identifiers (e.g., place IDs or related entity IDs) from these events.

  3. Obtain the full graph for the person from the /graph endpoint.

  4. Filter the vertices of the graph, keeping only those whose IDs are in the list from step 2.

  5. Filter the edges of the graph, keeping only those that connect the remaining vertices.

  6. 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.

Graph filtering task: there are two endpoints — GET… - sobes.tech