<commit_before>package jaci.openrio.gradle.telemetry

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import groovy.transform.CompileStatic
import jaci.openrio.gradle.GradleRIOPlugin
import jaci.openrio.gradle.telemetry.providers.*
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project

import java.util.zip.GZIPOutputStream

/**
 * This plugin reports project statistics to OpenRIO Web Services to help with
 * further development.
 *
 * This plugin's actions can be disabled if you're paranoid.
 */
@CompileStatic
class TelemetryPlugin implements Plugin<Project> {

    Map<String, TelemetryProvider> providers = [:];

    @Override
    void apply(Project project) {
        project.extensions.create("telemetry", TelemetryExtension)

        register('uuid', new UUIDProvider())
        register('os', new OSProvider())
        register('gradle', new GradleProvider())
        register('plugins', new PluginsProvider())
        register('classpath', new ClasspathProvider())
        register('deploy', new DeployProvider())
        register('dependencies', new DependencyProvider())
        register('wpi', new WPIProvider())

        project.tasks.create('telemetry') { DefaultTask task ->
            task.group = 'GradleRIO'
            task.description = 'Render the collected telemetry to the console'

            task.doLast {
                println renderTelemetry(project, true)
            }
        }

        def thread = null as Thread
        project.afterEvaluate {
            if (project.extensions.getByType(TelemetryExtension).reportTelemetry && !project.gradle.startParameter.isOffline() && System.getenv('CI') == null) {
                thread = new Thread({
                    def telemetry = renderTelemetry(project, false)
                    def reportFile = new File(GradleRIOPlugin.globalDirectory, 'lastreport.telemetry')
                    if (!reportFile.exists() || reportFile.text != telemetry || project.hasProperty('dirty-telemetry')) {
                        // Report telemetry to web
                        def baos = new ByteArrayOutputStream()
                        def gzstr = new GZIPOutputStream(baos)
                        gzstr.write(telemetry.bytes)
                        gzstr.close()

                        def b64 = baos.toByteArray().encodeBase64().toString()
                        baos.close()

                        try {
                            def url = new URL("http://openrio.imjac.in/gradlerio/telemetry/report")
                            url.openConnection().with { URLConnection conn ->
                                def http = conn as HttpURLConnection
                                http.doOutput = true
                                http.requestMethod = 'POST'
                                http.outputStream.withWriter { writer ->
                                    writer << b64
                                }
                                http.content
                            }
                            reportFile.text = telemetry
                        } catch (Exception e) { }
                    }
                })
                thread.start()
            }
        }

        project.gradle.buildFinished {
            if (thread != null) thread.join()
        }
    }

    JsonObject telemetryReport(Project project) {
        def obj = new JsonObject()
        providers.each { String name, TelemetryProvider prov ->
            obj.add(name, prov.telemetry(project))
        }
        return obj
    }

    String renderTelemetry(Project project, boolean pretty) {
        def builder = new GsonBuilder()<commit_msg>Remove checks for existing telemetry - makes it hard to track certain deps over time<commit_after>package jaci.openrio.gradle.telemetry

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import groovy.transform.CompileStatic
import jaci.openrio.gradle.GradleRIOPlugin
import jaci.openrio.gradle.telemetry.providers.*
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.slf4j.LoggerFactory

import java.util.zip.GZIPOutputStream

/**
 * This plugin reports project statistics to OpenRIO Web Services to help with
 * further development.
 *
 * This plugin's actions can be disabled if you're paranoid.
 */
@CompileStatic
class TelemetryPlugin implements Plugin<Project> {

    Map<String, TelemetryProvider> providers = [:];

    @Override
    void apply(Project project) {
        project.extensions.create("telemetry", TelemetryExtension)

        register('uuid', new UUIDProvider())
        register('os', new OSProvider())
        register('gradle', new GradleProvider())
        register('plugins', new PluginsProvider())
        register('classpath', new ClasspathProvider())
        register('deploy', new DeployProvider())
        register('dependencies', new DependencyProvider())
        register('wpi', new WPIProvider())

        project.tasks.create('telemetry') { DefaultTask task ->
            task.group = 'GradleRIO'
            task.description = 'Render the collected telemetry to the console'

            task.doLast {
                println renderTelemetry(project, true)
            }
        }

        def thread = null as Thread
        project.afterEvaluate {
            if (project.extensions.getByType(TelemetryExtension).reportTelemetry && !project.gradle.startParameter.isOffline() && System.getenv('CI') == null) {
                thread = new Thread({
                    def log = LoggerFactory.getLogger('gradlerio_telemetry')
                    def start = System.currentTimeMillis()

                    def telemetry = renderTelemetry(project, false)
                    def reportFile = new File(GradleRIOPlugin.globalDirectory, 'lastreport.telemetry')
                    // Report telemetry to web
                    def baos = new ByteArrayOutputStream()
                    def gzstr = new GZIPOutputStream(baos)
                    gzstr.write(telemetry.bytes)
                    gzstr.close()

                    def b64 = baos.toByteArray().encodeBase64().toString()
                    baos.close()

                    def start_upload = System.currentTimeMillis()
                    try {
                        def url = new URL("http://openrio.imjac.in/gradlerio/telemetry/report")
                        url.openConnection().with { URLConnection conn ->
                            def http = conn as HttpURLConnection
                            http.doOutput = true
                            http.requestMethod = 'POST'
                            http.outputStream.withWriter { writer ->
                                writer << b64
                            }
                            http.content
                        }
                        reportFile.text = telemetry

                    } catch (Exception e) {
                        def s = new StringWriter()
                        def pw = new PrintWriter(s)
                        e.printStackTrace(pw)
                        log.info("Could not run upload Telemetry...")
                        log.info(s.toString())
                    } finally {
                        log.info("Telemetry Upload took ${System.currentTimeMillis() - start_upload}ms. (Upload only)")
                    }
                    log.info("Telemetry Report took ${System.currentTimeMillis() - start}ms")
                })
                thread.start()
            }
        }

        project.gradle.buildFinished {
            if (thread != null) thread.join()
        }
    }

    JsonObject telemetryReport(Project project) {
        def obj = new JsonObject()
        providers.each { String name, TelemetryProvider prov ->
            obj.add(name, prov.telemetry(project))
        }
        return obj
    }

    String renderTelemetry(Project project, boolean pretty) {
        def builder = new GsonBuilder()
{'fraction_non_alphanumeric': 0.08936595107338992, 'fraction_numerical': 0.0016225661507738391, 'mean_word_length': 1.7135116830342025, 'pattern_counts': {'":': 0, '<': 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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1919138', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1867, 'n_words': 450}