<commit_before>package org.rakam.collection;

import com.mysql.jdbc.MySQLConnection;
import io.airlift.log.Level;
import org.postgresql.PGConnection;
import org.rakam.analysis.JDBCPoolDataSource;
import org.rakam.collection.util.JSCodeCompiler;
import org.rakam.server.http.annotations.Api;
import org.rakam.server.http.annotations.ApiOperation;
import org.rakam.server.http.annotations.ApiParam;
import org.rakam.server.http.annotations.Authorization;
import org.rakam.server.http.annotations.JsonRequest;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Query;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Path;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Path("/javascript-logger")
@Api(value = "/javascript-logger", nickname = "javascript-logs", description = "Javascript code logs", tags = {"collect", "javascript"})
public class JSCodeLoggerService
{
    private final DBI dbi;

    @Inject
    public JSCodeLoggerService(@Named("report.metadata.store.jdbc") JDBCPoolDataSource dataSource)
    {
        this.dbi = new DBI(dataSource);
    }

    @PostConstruct
    public void setupLogger()
    {
        try (Handle handle = dbi.open()) {
            handle.createStatement("CREATE TABLE IF NOT EXISTS javascript_logs (" +
                    "  project VARCHAR(255) NOT NULL," +
                    "  type VARCHAR(15) NOT NULL," +
                    "  prefix VARCHAR(255) NOT NULL," +
                    "  error TEXT NOT NULL," +
                    "  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    "  )")
                    .execute();
        }
    }

    @JsonRequest
    @ApiOperation(value = "Get logs", authorizations = @Authorization(value = "master_key"))
    @Path("/test")
    public List<LogEntry> getLogs(@Named("project") String project, @ApiParam(value = "start", required = false) Instant start, @ApiParam(value = "end", required = false) Instant end, @ApiParam(value = "prefix") String prefix)
    {
        String sql = "SELECT id, type, error, %s(created_at) FROM javascript_logs WHERE project = :project AND prefix = :prefix";
        if (start != null) {
            sql += " AND created_at > :start";
        }
        if (end != null) {
            sql += " AND created_at < :end";
        }

        sql += " ORDER BY created_at DESC";

        try (Handle handle = dbi.open()) {

            if (handle.getConnection() instanceof MySQLConnection) {
                sql = String.format(sql, "unix_timestamp");
            }
            else if (handle.getConnection() instanceof PGConnection) {
                sql = String.format(sql, "to_unixtime");
            }
            else {
                throw new RuntimeException("JsCodeLogger service requires Postgresql or Mysql as dependency.");
            }

            Query<Map<String, Object>> query = handle.createQuery(sql + " LIMIT 100");
            query.bind("project", project);
            query.bind("prefix", prefix);

            if (start != null) {
                query.bind("start", Timestamp.from(start));
            }
            if (end != null) {
                query.bind("end", Timestamp.from(end));
            }

            return query.map((index, r, ctx) -> {<commit_msg>Fix bug in JS code logger<commit_after>package org.rakam.collection;

import com.google.common.base.Throwables;
import com.mysql.jdbc.MySQLConnection;
import com.zaxxer.hikari.pool.HikariProxyConnection;
import io.airlift.log.Level;
import org.postgresql.PGConnection;
import org.rakam.analysis.JDBCPoolDataSource;
import org.rakam.collection.util.JSCodeCompiler;
import org.rakam.server.http.annotations.Api;
import org.rakam.server.http.annotations.ApiOperation;
import org.rakam.server.http.annotations.ApiParam;
import org.rakam.server.http.annotations.Authorization;
import org.rakam.server.http.annotations.JsonRequest;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Query;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Path;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Path("/javascript-logger")
@Api(value = "/javascript-logger", nickname = "javascript-logs", description = "Javascript code logs", tags = {"collect", "javascript"})
public class JSCodeLoggerService
{
    private final DBI dbi;
    private final boolean postgresOrMysql;

    @Inject
    public JSCodeLoggerService(@Named("report.metadata.store.jdbc") JDBCPoolDataSource dataSource)
    {
        try (Connection handle = dataSource.getConnection(true)) {
            if(handle instanceof MySQLConnection) {
                postgresOrMysql = false;
            } else if (handle instanceof PGConnection) {
                postgresOrMysql = true;
            } else {
                throw new RuntimeException("JsCodeLogger service requires Postgresql or Mysql as dependency.");
            }
        }
        catch (SQLException e) {
            throw Throwables.propagate(e);
        }
        this.dbi = new DBI(dataSource);
    }

    @PostConstruct
    public void setupLogger()
    {
        try (Handle handle = dbi.open()) {
            handle.createStatement("CREATE TABLE IF NOT EXISTS javascript_logs (" +
                    "  project VARCHAR(255) NOT NULL," +
                    "  type VARCHAR(15) NOT NULL," +
                    "  prefix VARCHAR(255) NOT NULL," +
                    "  error TEXT NOT NULL," +
                    "  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    "  )")
                    .execute();
        }
    }

    @JsonRequest
    @ApiOperation(value = "Get logs", authorizations = @Authorization(value = "master_key"))
    @Path("/test")
    public List<LogEntry> getLogs(@Named("project") String project, @ApiParam(value = "start", required = false) Instant start, @ApiParam(value = "end", required = false) Instant end, @ApiParam(value = "prefix") String prefix)
    {
        String sql = "SELECT id, type, error, %s(created_at) FROM javascript_logs WHERE project = :project AND prefix = :prefix";
        if (start != null) {
            sql += " AND created_at > :start";
        }
        if (end != null) {
            sql += " AND created_at < :end";
        }

        sql += " ORDER BY created_at DESC";

        try (Handle handle = dbi.open()) {
            if (postgresOrMysql) {
                sql = String.format(sql, "to_unixtime");
            } else {
                sql = String.format(sql, "unix_timestamp");
            }

            Query<Map<String, Object>> query = handle.createQuery(sql + " LIMIT 100");
            query.bind("project", project);
            query.bind("prefix", prefix);

            if (start != null) {
                query.bind("start", Timestamp.from(start));
            }
            if (end != null) {
                query.bind("end", Timestamp.from(end));
            }

            return query.map((index, r, ctx) -> {
{'fraction_non_alphanumeric': 0.11968741431313408, 'fraction_numerical': 0.003838771593090211, 'mean_word_length': 2.4622686283815853, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '2769095', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2052, 'n_words': 486}