Search in sources :

Example 36 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class ParseUserAgentFunction method parseUserAgent.

@ScalarFunction("parse_user_agent")
@Description("Tries to parse and analyze the provided useragent string and extract as many attributes " + "as possible. Uses Yauaa (Yet Another UserAgent Analyzer) version " + Version.PROJECT_VERSION + ". " + "See https://yauaa.basjes.nl/udf/trino/ for documentation.")
@SqlType("map(varchar, varchar)")
public static Block parseUserAgent(@SqlType(StandardTypes.VARCHAR) Slice userAgentSlice) throws IllegalArgumentException {
    String userAgentStringToParse = null;
    if (userAgentSlice != null) {
        userAgentStringToParse = userAgentSlice.toStringUtf8();
    }
    UserAgentAnalyzer userAgentAnalyzer = getInstance();
    UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
    Map<String, String> resultMap = userAgent.toMap(userAgent.getAvailableFieldNamesSorted());
    MapType mapType = new MapType(VARCHAR, VARCHAR, new TypeOperators());
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, resultMap.size());
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<String, String> entry : resultMap.entrySet()) {
        VARCHAR.writeString(singleMapBlockBuilder, entry.getKey());
        VARCHAR.writeString(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Map(java.util.Map) MapType(io.trino.spi.type.MapType) TypeOperators(io.trino.spi.type.TypeOperators) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 37 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class ApiJsonOutput method createOutput.

// -------------------------------------------------
private String createOutput(String userAgentString) {
    if (userAgentString == null) {
        throw new MissingUserAgentException();
    }
    ensureStartedForApis(OutputType.JSON);
    if (userAgentAnalyzerIsAvailable()) {
        UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
        List<String> result = new ArrayList<>(2048);
        for (String input : splitPerFilledLine(userAgentString)) {
            UserAgent userAgent = userAgentAnalyzer.parse(input);
            result.add(userAgent.toJson(userAgent.getCleanedAvailableFieldNamesSorted()));
        }
        return "[" + String.join(",\n", result) + "]";
    }
    return "[{}]";
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ArrayList(java.util.ArrayList) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)

Example 38 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class Yauaa method checkConfiguration.

private void checkConfiguration() {
    List<String> configProblems = new ArrayList<>();
    UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().delayInitialization().dropTests().hideMatcherLoadStats().build();
    List<String> allFieldNames = uaa.getAllPossibleFieldNamesSorted();
    if (sourceField == null) {
        configProblems.add("The \"source\" has not been specified.\n");
    } else {
        if (sourceField.isEmpty()) {
            configProblems.add("The \"source\" is empty.\n");
        }
    }
    if (outputFields == null) {
        configProblems.add("The list of needed \"fields\" has not been specified.\n");
    } else {
        if (outputFields.isEmpty()) {
            configProblems.add("The list of needed \"fields\" is empty.\n");
        }
        for (String outputField : outputFields.keySet()) {
            if (!allFieldNames.contains(outputField)) {
                configProblems.add("The requested field \"" + outputField + "\" does not exist.\n");
            }
        }
    }
    if (configProblems.isEmpty()) {
        // All is fine
        return;
    }
    StringBuilder errorMessage = new StringBuilder();
    int maxNameLength = 0;
    for (String field : allFieldNames) {
        maxNameLength = Math.max(maxNameLength, field.length());
    }
    errorMessage.append("\nThe Yauaa filter config is invalid.\n");
    errorMessage.append("The problems we found:\n");
    configProblems.forEach(problem -> errorMessage.append("- ").append(problem).append('\n'));
    errorMessage.append("\n");
    errorMessage.append("Example of a generic valid config:\n");
    errorMessage.append("\n");
    errorMessage.append("filter {\n");
    errorMessage.append("   yauaa {\n");
    errorMessage.append("       source => \"useragent\"\n");
    errorMessage.append("       fields => {\n");
    for (String field : allFieldNames) {
        if (!isSystemField(field)) {
            errorMessage.append("           \"").append(field).append("\"");
            for (int i = field.length(); i < maxNameLength; i++) {
                errorMessage.append(' ');
            }
            errorMessage.append("  => \"userAgent").append(field).append("\"\n");
        }
    }
    errorMessage.append("       }\n");
    errorMessage.append("   }\n");
    errorMessage.append("}\n");
    errorMessage.append("\n");
    LOG.error("{}", errorMessage);
    throw new IllegalArgumentException(errorMessage.toString());
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ArrayList(java.util.ArrayList)

Aggregations

UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)38 Test (org.junit.jupiter.api.Test)24 UserAgent (nl.basjes.parse.useragent.UserAgent)11 ArrayList (java.util.ArrayList)4 Disabled (org.junit.jupiter.api.Disabled)4 Map (java.util.Map)3 UserAgentAnalyzerBuilder (nl.basjes.parse.useragent.UserAgentAnalyzer.UserAgentAnalyzerBuilder)3 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)3 Kryo (com.esotericsoftware.kryo.Kryo)2 ByteBufferInput (com.esotericsoftware.kryo.io.ByteBufferInput)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInput (java.io.ObjectInput)2 ObjectInputStream (java.io.ObjectInputStream)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 TestCase (nl.basjes.parse.useragent.config.TestCase)2 IngestDocument (org.elasticsearch.ingest.IngestDocument)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2