<commit_before>package com.northernwall.hadrian.handler;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import com.northernwall.hadrian.db.DataAccess;
import com.northernwall.hadrian.domain.Network;
import com.northernwall.hadrian.domain.ServiceRefView;
import com.northernwall.hadrian.domain.VersionView;
import com.northernwall.hadrian.formData.Color;
import com.northernwall.hadrian.formData.Edge;
import com.northernwall.hadrian.formData.Node;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GraphHandler extends AbstractHandler {

    private final static Logger logger = LoggerFactory.getLogger(GraphHandler.class);

    private final DataAccess dataAccess;
    private final Gson gson;

    public GraphHandler(DataAccess dataAccess, Gson gson) {
        this.dataAccess = dataAccess;
        this.gson = gson;
    }

    @Override
    public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse response) throws IOException, ServletException {
        try {
            if (target.startsWith("/servicesGraph.json")) {
                logger.info("Handling {} request {}", request.getMethod(), target);
                getServicesGraph(response);
                request.setHandled(true);
                response.setStatus(200);
            }
        } catch (Exception e) {
            logger.error("Exception {} while handling request for {}", e.getMessage(), target, e);
            response.setStatus(400);
        }
    }

    private void getServicesGraph(HttpServletResponse response) throws IOException {
        Network network = new Network();
        List<String> indexes = new LinkedList<>();
        response.setContentType("application/json;charset=utf-8");
        try (JsonWriter jw = new JsonWriter(new OutputStreamWriter(response.getOutputStream()))) {
            List<VersionView> versions = dataAccess.getVersionVeiw();
            if (versions != null && !versions.isEmpty()) {
                for (VersionView version : versions) {
                    Node node = new Node();
                    node.id = indexes.size();
                    node.label = version.serviceId + "-v" + version.versionId;
                    if (version.type != null && version.type.equals("Application")) {
                        node.shape = "box";
                    } else {
                        node.shape = "ellipse";
                    }
                    node.title = buildNodeTitle(version);
                    node.color = new Color();
                    if (version.access != null && version.access.equals("Internal")) {
                        node.color.background = "white";
                    } else {
                        node.color.background = "yellow";
                    }
                    if (version.status != null && (version.status.equals("Retiring") || version.status.equals("Retired"))) {
                        node.color.border = "red";
                        node.borderWidth = 3;
                    } else {
                        node.color.border = "black";
                        node.borderWidth = 1;
                    }
                    indexes.add(version.serviceId + "-v" + version.versionId);
                    network.nodes.add(node);
                }
            }
            List<ServiceRefView> refs = dataAccess.getServiceRefVeiw();
            if (refs != null && !refs.isEmpty()) {
                for (ServiceRefView ref : refs) {
                    int i = indexes.indexOf(ref.serviceId + "-v" + ref.versionId);
                    int ii = indexes.indexOf(ref.refServiceId + "-v" + ref.refVersionId);
                    if (i >= 0 && i >= 0) {
                        Edge edge = new Edge();
                        edge.from = i;
                        edge.to = ii;
                        edge.style = "arrow";
                        switch (ref.scope) {
                            case "sync":
                                edge.color = "blue";
                                edge.width = 2;
                                break;
                            case "async":
                                edge.color = "black";
                                edge.width = 1;
                                break;
                            case "indirect":
                                edge.color = "brown";
                                edge.width = 1;
                                break;
                            case "support":
                                edge.color = "green";
                                edge.width = 1;<commit_msg>Retired services no longer appear on the graph<commit_after>package com.northernwall.hadrian.handler;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import com.northernwall.hadrian.db.DataAccess;
import com.northernwall.hadrian.domain.Network;
import com.northernwall.hadrian.domain.ServiceRefView;
import com.northernwall.hadrian.domain.VersionView;
import com.northernwall.hadrian.formData.Color;
import com.northernwall.hadrian.formData.Edge;
import com.northernwall.hadrian.formData.Node;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GraphHandler extends AbstractHandler {

    private final static Logger logger = LoggerFactory.getLogger(GraphHandler.class);

    private final DataAccess dataAccess;
    private final Gson gson;

    public GraphHandler(DataAccess dataAccess, Gson gson) {
        this.dataAccess = dataAccess;
        this.gson = gson;
    }

    @Override
    public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse response) throws IOException, ServletException {
        try {
            if (target.startsWith("/servicesGraph.json")) {
                logger.info("Handling {} request {}", request.getMethod(), target);
                getServicesGraph(response);
                request.setHandled(true);
                response.setStatus(200);
            }
        } catch (Exception e) {
            logger.error("Exception {} while handling request for {}", e.getMessage(), target, e);
            response.setStatus(400);
        }
    }

    private void getServicesGraph(HttpServletResponse response) throws IOException {
        Network network = new Network();
        List<String> indexes = new LinkedList<>();
        response.setContentType("application/json;charset=utf-8");
        try (JsonWriter jw = new JsonWriter(new OutputStreamWriter(response.getOutputStream()))) {
            List<VersionView> versions = dataAccess.getVersionVeiw();
            if (versions != null && !versions.isEmpty()) {
                for (VersionView version : versions) {
                    if (version.status != null && !version.status.equals("Retired")) {
                        Node node = new Node();
                        node.id = indexes.size();
                        node.label = version.serviceId + "-v" + version.versionId;
                        if (version.type != null && version.type.equals("Application")) {
                            node.shape = "box";
                        } else {
                            node.shape = "ellipse";
                        }
                        node.title = buildNodeTitle(version);
                        node.color = new Color();
                        if (version.access != null && version.access.equals("Internal")) {
                            node.color.background = "white";
                        } else {
                            node.color.background = "yellow";
                        }
                        if (version.status.equals("Retiring")) {
                            node.color.border = "red";
                            node.borderWidth = 3;
                        } else {
                            node.color.border = "black";
                            node.borderWidth = 1;
                        }
                        indexes.add(version.serviceId + "-v" + version.versionId);
                        network.nodes.add(node);
                    }
                }
            }
            List<ServiceRefView> refs = dataAccess.getServiceRefVeiw();
            if (refs != null && !refs.isEmpty()) {
                for (ServiceRefView ref : refs) {
                    int i = indexes.indexOf(ref.serviceId + "-v" + ref.versionId);
                    int ii = indexes.indexOf(ref.refServiceId + "-v" + ref.refVersionId);
                    if (i >= 0 && i >= 0) {
                        Edge edge = new Edge();
                        edge.from = i;
                        edge.to = ii;
                        edge.style = "arrow";
                        switch (ref.scope) {
                            case "sync":
                                edge.color = "blue";
                                edge.width = 2;
                                break;
                            case "async":
                                edge.color = "black";
                                edge.width = 1;
                                break;
                            case "indirect":
                                edge.color = "brown";
                                edge.width = 1;
                                break;
                            case "support":
                                edge.color = "green";
                                edge.width = 1;
{'fraction_non_alphanumeric': 0.10064935064935066, 'fraction_numerical': 0.0033451397087760726, 'mean_word_length': 1.4684312773190868, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1462809', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2381, 'n_words': 515}