<commit_before>package netflix.nebula.interactive

import com.fasterxml.jackson.databind.ObjectMapper
import groovy.transform.Canonical
import groovy.transform.EqualsAndHashCode
import io.netty.handler.codec.http.HttpHeaders
import io.reactivex.netty.RxNetty
import io.reactivex.netty.protocol.http.server.RequestHandler
import io.reactivex.netty.protocol.http.server.RequestHandlerWithErrorMapper
import io.reactivex.netty.protocol.http.server.file.ClassPathFileRequestHandler
import io.reactivex.netty.protocol.http.server.file.FileErrorResponseMapper
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.tasks.TaskAction

import java.awt.Desktop
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class InteractiveDependenciesTask extends DefaultTask {
    private final int PORT = 8641

    int recurseDependencies(Set artifacts, Set links, ResolvedDependency dep, int index) {
        def artifact = new Artifact(dep)
        if(artifacts.add(artifact)) {
            artifact.index = index++
            for (ResolvedDependency child : dep.children) {
                index = recurseDependencies(artifacts, links, child, index)
                links.add(new Link(artifact, new Artifact(child)))
            }
        }
        return index
    }

    ResolvedDependency projectAsResolvedDependency() {
        [
                getModuleGroup: { project.group },
                getModuleName: { project.name },
                getModuleVersion: { project.version },
                getChildren: { project.configurations.compile.resolvedConfiguration.firstLevelModuleDependencies }
        ] as ResolvedDependency
    }

    @TaskAction
    def serveInteractiveContent() {
        def links = new HashSet()
        def nodes = new LinkedHashSet()

        recurseDependencies(nodes, links, projectAsResolvedDependency(), 0)

        def results = [
            nodes: new ArrayList(nodes),
            links: links.collect { link -> [source: link.source.index, target: nodes.find { it == link.target }.index] }
        ]

        def resultJson = new ObjectMapper().writeValueAsString(results)

        def server = null
        try {
            def latch = new CountDownLatch(1)

            server = RxNetty.createHttpServer(PORT,
                new HttpRouter()
                    .get('/dependencies',
                        { request, response ->
                            response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, 'application/json');
                            response.writeString(resultJson)
                            latch.countDown()
                        } as RequestHandler
                    )
                    .noMatch(
                        RequestHandlerWithErrorMapper.from(
                            new ClassPathFileRequestHandler('static'),
                            new FileErrorResponseMapper())
                    )
            ).start()

            Desktop.getDesktop().browse(new URI("http://localhost:$PORT/index.html"))<commit_msg>Add logging to debug problem with published artifact<commit_after>package netflix.nebula.interactive

import com.fasterxml.jackson.databind.ObjectMapper
import groovy.transform.Canonical
import groovy.transform.EqualsAndHashCode
import io.netty.handler.codec.http.HttpHeaders
import io.reactivex.netty.RxNetty
import io.reactivex.netty.protocol.http.server.RequestHandler
import io.reactivex.netty.protocol.http.server.RequestHandlerWithErrorMapper
import io.reactivex.netty.protocol.http.server.file.ClassPathFileRequestHandler
import io.reactivex.netty.protocol.http.server.file.FileErrorResponseMapper
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.tasks.TaskAction

import java.awt.Desktop
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class InteractiveDependenciesTask extends DefaultTask {
    private final int PORT = 8641

    int recurseDependencies(Set artifacts, Set links, ResolvedDependency dep, int index) {
        def artifact = new Artifact(dep)
        if(artifacts.add(artifact)) {
            artifact.index = index++
            for (ResolvedDependency child : dep.children) {
                index = recurseDependencies(artifacts, links, child, index)
                links.add(new Link(artifact, new Artifact(child)))
            }
        }
        return index
    }

    ResolvedDependency projectAsResolvedDependency() {
        [
                getModuleGroup: { project.group },
                getModuleName: { project.name },
                getModuleVersion: { project.version },
                getChildren: { project.configurations.compile.resolvedConfiguration.firstLevelModuleDependencies }
        ] as ResolvedDependency
    }

    @TaskAction
    def serveInteractiveContent() {
        def links = new HashSet()
        def nodes = new LinkedHashSet()

        recurseDependencies(nodes, links, projectAsResolvedDependency(), 0)

        def results = [
            nodes: new ArrayList(nodes),
            links: links.collect { link -> [source: link.source.index, target: nodes.find { it == link.target }.index] }
        ]

        def resultJson = new ObjectMapper().writeValueAsString(results)

        def server = null
        try {
            def latch = new CountDownLatch(1)

            def staticHandler = RequestHandlerWithErrorMapper.from(
                    new ClassPathFileRequestHandler('static'),
                    new FileErrorResponseMapper())

            server = RxNetty.createHttpServer(PORT,
                new HttpRouter()
                    .get('/dependencies',
                        { request, response ->
                            println 'Rest request'
                            response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, 'application/json');
                            response.writeString(resultJson)
                            latch.countDown()
                        } as RequestHandler
                    )
                    .noMatch({ request, response ->
                        println 'Static request: ' + request.getUri()
                        staticHandler.handle(request, response)
                    } as RequestHandler)
            ).start()

            println "Starting browser"
            Desktop.getDesktop().browse(new URI("http://localhost:$PORT/index.html"))
{'fraction_non_alphanumeric': 0.08450486636799011, 'fraction_numerical': 0.0018538544724239147, 'mean_word_length': 2.2208955223880595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4558201', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1539, 'n_words': 320}