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);
}
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 "[{}]";
}
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());
}
Aggregations