Search in sources :

Example 1 with UserAgentAnalyzer

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

the class ApiYamlOutput 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)) {
            if (input.startsWith("#")) {
                result.add(input);
            } else {
                UserAgent userAgent = userAgentAnalyzer.parse(input);
                result.add(userAgent.toYamlTestCase(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 2 with UserAgentAnalyzer

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

the class RunTests method getRunTests.

// ===========================================
@Operation(description = "Fire all available test cases against the analyzer and return 200 if all tests were good")
@ApiResponse(// HttpStatus.OK
responseCode = "200", description = "All tests were good", content = @Content(mediaType = TEXT_PLAIN_VALUE, examples = @ExampleObject("All 3866 tests passed in 2994ms (average 0.775ms per testcase).")))
@ApiResponse(// HttpStatus.INTERNAL_SERVER_ERROR
responseCode = "500", description = "A test failed", content = @Content(mediaType = TEXT_PLAIN_VALUE, examples = @ExampleObject("Extensive text describing what went wrong in the test that failed")))
@GetMapping(value = "/runtests", produces = TEXT_PLAIN_VALUE)
public String getRunTests() {
    UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
    List<TestCase> testCases = userAgentAnalyzer.getTestCases();
    long start = System.nanoTime();
    List<TestCase> failedTests = testCases.stream().filter(testCase -> !testCase.verify(userAgentAnalyzer)).collect(Collectors.toList());
    long stop = System.nanoTime();
    if (failedTests.isEmpty()) {
        return String.format("All %d tests passed in %dms (average %4.3fms per testcase).", testCases.size(), (stop - start) / 1_000_000, ((stop - start) / 1_000_000D / testCases.size()));
    }
    throw new YauaaTestsFailed("There were " + failedTests.size() + " failed tests " + "(~" + ((100.0D * failedTests.size()) / testCases.size()) + "%)");
}
Also used : TestCase(nl.basjes.parse.useragent.config.TestCase) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) ParseService(nl.basjes.parse.useragent.servlet.ParseService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) TEXT_PLAIN_VALUE(org.springframework.http.MediaType.TEXT_PLAIN_VALUE) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) List(java.util.List) YauaaTestsFailed(nl.basjes.parse.useragent.servlet.exceptions.YauaaTestsFailed) ParseService.ensureStartedForApis(nl.basjes.parse.useragent.servlet.ParseService.ensureStartedForApis) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Tag(io.swagger.v3.oas.annotations.tags.Tag) GetMapping(org.springframework.web.bind.annotation.GetMapping) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) TestCase(nl.basjes.parse.useragent.config.TestCase) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) YauaaTestsFailed(nl.basjes.parse.useragent.servlet.exceptions.YauaaTestsFailed) GetMapping(org.springframework.web.bind.annotation.GetMapping) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 3 with UserAgentAnalyzer

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

the class RunTests method getPreHeat.

@Operation(description = "Fire all available test cases against the analyzer to heat up the JVM")
@ApiResponse(// HttpStatus.OK
responseCode = "200", description = "The number of reported tests were done to preheat the engine", content = @Content(mediaType = APPLICATION_JSON_VALUE, examples = @ExampleObject("{\n" + "  \"status\": \"Ran tests\",\n" + "  \"testsDone\": 2337,\n" + "  \"timeInMs\": 3123\n" + "}")))
@GetMapping(value = "/preheat", produces = APPLICATION_JSON_VALUE)
public String getPreHeat() {
    ensureStartedForApis(OutputType.JSON);
    UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
    final int cacheSize = userAgentAnalyzer.getCacheSize();
    userAgentAnalyzer.disableCaching();
    long start = System.nanoTime();
    final long testsDone = userAgentAnalyzer.preHeat();
    long stop = System.nanoTime();
    userAgentAnalyzer.setCacheSize(cacheSize);
    if (testsDone == 0) {
        return "{ \"status\": \"No testcases available\", \"testsDone\": 0 , \"timeInMs\" : -1 } ";
    }
    return "{ \"status\": \"Ran tests\", \"testsDone\": " + testsDone + " , \"timeInMs\" : " + (stop - start) / 1000000 + " } ";
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) GetMapping(org.springframework.web.bind.annotation.GetMapping) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 4 with UserAgentAnalyzer

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

the class TestYauaaProcessor method testExtraRules.

@Test
void testExtraRules() {
    Map<String, Object> document = new HashMap<>();
    document.put(SOURCE_FIELD, "Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/53.0.2785.124 Mobile Safari/537.36");
    IngestDocument ingestDocument = new IngestDocument("index", "type", "id", null, 42L, VersionType.EXTERNAL, document);
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().withFields(Arrays.asList("DeviceClass", "DeviceBrand", "DeviceName", "AgentNameVersionMajor", "FirstProductName")).withCache(10).preheat(10).addYamlRule("config:\n- matcher:\n    extract:\n      - 'FirstProductName     : 1 :agent.(1)product.(1)name'\n").build();
    YauaaProcessor processor = new YauaaProcessor("tag", "description", SOURCE_FIELD, TARGET_FIELD, userAgentAnalyzer);
    Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();
    MatcherAssert.assertThat(data, hasKey(TARGET_FIELD));
    Object targetFieldData = data.get(TARGET_FIELD);
    assertTrue(targetFieldData instanceof Map, "Wrong type");
    Map<?, ?> results = (Map<?, ?>) targetFieldData;
    // The EXPLICITLY requested fields
    assertHasKValue(results, "FirstProductName", "Mozilla");
    assertHasKValue(results, "DeviceClass", "Phone");
    assertHasKValue(results, "DeviceBrand", "Google");
    assertHasKValue(results, "DeviceName", "Google Nexus 6");
    assertHasKValue(results, "AgentNameVersionMajor", "Chrome 53");
    // The IMPLICITLY requested fields (i.e. partials of the actually requested ones)
    assertHasKValue(results, "AgentName", "Chrome");
    assertHasKValue(results, "AgentVersion", "53.0.2785.124");
    assertHasKValue(results, "AgentVersionMajor", "53");
    // The NOT requested fields
    assertHasNotKey(results, "OperatingSystemClass");
    assertHasNotKey(results, "OperatingSystemName");
    assertHasNotKey(results, "OperatingSystemNameVersion");
    assertHasNotKey(results, "OperatingSystemNameVersionMajor");
    assertHasNotKey(results, "OperatingSystemVersion");
    assertHasNotKey(results, "OperatingSystemVersionBuild");
    assertHasNotKey(results, "OperatingSystemVersionMajor");
    assertHasNotKey(results, "LayoutEngineClass");
    assertHasNotKey(results, "LayoutEngineName");
    assertHasNotKey(results, "LayoutEngineNameVersion");
    assertHasNotKey(results, "LayoutEngineNameVersionMajor");
    assertHasNotKey(results, "LayoutEngineVersion");
    assertHasNotKey(results, "LayoutEngineVersionMajor");
    assertHasNotKey(results, "AgentClass");
    assertHasNotKey(results, "AgentNameVersion");
}
Also used : HashMap(java.util.HashMap) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) IngestDocument(org.elasticsearch.ingest.IngestDocument) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with UserAgentAnalyzer

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

the class TestYauaaProcessor method testThatProcessorWorks.

@Test
void testThatProcessorWorks() {
    Map<String, Object> document = new HashMap<>();
    document.put(SOURCE_FIELD, "Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/53.0.2785.124 Mobile Safari/537.36");
    IngestDocument ingestDocument = new IngestDocument("index", "type", "id", null, 42L, VersionType.EXTERNAL, document);
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().build();
    YauaaProcessor processor = new YauaaProcessor("tag", "description", SOURCE_FIELD, TARGET_FIELD, userAgentAnalyzer);
    Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();
    MatcherAssert.assertThat(data, hasKey(TARGET_FIELD));
    Object targetFieldData = data.get(TARGET_FIELD);
    assertTrue(targetFieldData instanceof Map, "Wrong type");
    Map<?, ?> results = (Map<?, ?>) targetFieldData;
    assertHasKValue(results, "DeviceClass", "Phone");
    assertHasKValue(results, "DeviceBrand", "Google");
    assertHasKValue(results, "DeviceName", "Google Nexus 6");
    assertHasKValue(results, "OperatingSystemClass", "Mobile");
    assertHasKValue(results, "OperatingSystemName", "Android");
    assertHasKValue(results, "OperatingSystemNameVersion", "Android 7.0");
    assertHasKValue(results, "OperatingSystemNameVersionMajor", "Android 7");
    assertHasKValue(results, "OperatingSystemVersion", "7.0");
    assertHasKValue(results, "OperatingSystemVersionBuild", "NBD90Z");
    assertHasKValue(results, "OperatingSystemVersionMajor", "7");
    assertHasKValue(results, "LayoutEngineClass", "Browser");
    assertHasKValue(results, "LayoutEngineName", "Blink");
    assertHasKValue(results, "LayoutEngineNameVersion", "Blink 53.0");
    assertHasKValue(results, "LayoutEngineNameVersionMajor", "Blink 53");
    assertHasKValue(results, "LayoutEngineVersion", "53.0");
    assertHasKValue(results, "LayoutEngineVersionMajor", "53");
    assertHasKValue(results, "AgentClass", "Browser");
    assertHasKValue(results, "AgentName", "Chrome");
    assertHasKValue(results, "AgentNameVersion", "Chrome 53.0.2785.124");
    assertHasKValue(results, "AgentNameVersionMajor", "Chrome 53");
    assertHasKValue(results, "AgentVersion", "53.0.2785.124");
    assertHasKValue(results, "AgentVersionMajor", "53");
}
Also used : HashMap(java.util.HashMap) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) IngestDocument(org.elasticsearch.ingest.IngestDocument) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

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