Mforns has uploaded a new change for review. https://gerrit.wikimedia.org/r/216060
Change subject: Add app version to user agent map ...................................................................... Add app version to user agent map UAParser parses the app version of 'mobile app' requests into a field in the user agent map called 'app_version'. For requests other than 'mobile app', 'app_version' is '-'. Bug: T99932 Change-Id: I2800d990d203addf06bdb89c137caefdbbe97f0f --- M refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java M refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Utilities.java A refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUAParserAppVersionRecognition.java A refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUtilities.java 4 files changed, 171 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/analytics/refinery/source refs/changes/60/216060/1 diff --git a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java index 068024e..a41cdb3 100644 --- a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java +++ b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/UAParser.java @@ -21,6 +21,7 @@ import ua_parser.*; import java.io.IOException; +import java.util.regex.Pattern; import java.util.HashMap; import java.util.Map; @@ -32,6 +33,10 @@ public static final String NA = "-"; static final Logger LOG = Logger.getLogger(UAParser.class.getName()); + + private static final Pattern appVersionPattern = Pattern.compile( + "WikipediaApp/([^ ]+) " + ); private CachingParser cachingParser; private Map<String, String> result = new HashMap<String, String>(); @@ -66,8 +71,8 @@ * Function extracting browser, device and os information from the UA string. * In case the uaString is null, make it an empty String. * @param uaString the ua string to parse - * @return the ua map with browser_name, browser_major, device, os_name, - * os_minor, os_major keys and associated values. + * @return the ua map with browser_family, browser_major, device_family, + * os_family, os_major, os_minor, app_version keys and associated values. */ public Map<String, String> getUAMap(String uaString) { result.clear(); @@ -115,6 +120,11 @@ result.put("os_minor", NA); } + String appVersion = Utilities.getMatched(appVersionPattern, uaString, 1); + if (appVersion == null) + appVersion = NA; + result.put("app_version", appVersion); + return result; } diff --git a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Utilities.java b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Utilities.java index e98e714..891c3cb 100644 --- a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Utilities.java +++ b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Utilities.java @@ -16,7 +16,10 @@ package org.wikimedia.analytics.refinery.core; +import java.lang.IllegalArgumentException; +import org.apache.log4j.Logger; import java.util.regex.Pattern; +import java.util.regex.Matcher; /** * Static functions to work with Wikimedia data, broadly construed; @@ -24,6 +27,8 @@ * over and over again in other classes. */ public class Utilities { + + static final Logger LOG = Logger.getLogger(Utilities.class.getName()); /** * Check if the target is contained within string. This is @@ -50,4 +55,42 @@ public static boolean patternIsFound(Pattern pattern, String target) { return pattern.matcher(target).find(); } -} \ No newline at end of file + + /** + * Returns the string matched by the pattern. + * Returns null, if the given pattern does not match the target string. + * + * @param Pattern pattern + * @param String target + * + * @return String + */ + public static string getMatched(Pattern pattern, String target) { + return getMatched(pattern, target, 0); + } + + /** + * Returns the string matched by the specified pattern group. + * Returns null, if the given pattern does not match the target string. + * Returns null, if the specified capturing group does not exist. + * + * @param Pattern pattern + * @param String target + * @param int group + * + * @return String + */ + public static String getMatched(Pattern pattern, String target, int group) { + Matcher matcher = pattern.matcher(target); + if (matcher.find()) { + try { + return matcher.group(group); + } catch (IllegalArgumentException e) { + LOG.error(e.getMessage(), e); + return null; + } + } else { + return null; + } + } +} diff --git a/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUAParserAppVersionRecognition.java b/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUAParserAppVersionRecognition.java new file mode 100644 index 0000000..c0d208f --- /dev/null +++ b/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUAParserAppVersionRecognition.java @@ -0,0 +1,55 @@ +/** + * Copyright (C) 2015 Wikimedia Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wikimedia.analytics.refinery.core; + +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + + +public class TestUAParserAppVersionRecognition extends TestCase { + + UAParser uaParser = null; + + @Before + public void setUp() { + uaParser = new UAParser(); + } + + @Test + public void testAppRequest() { + + String ua1 = "WikipediaApp/2.0-r-2015-04-23 (Android 5.0.1; Phone) Google Play"; + Map<String, String> evaled = uaParser.getUAMap(ua1); + assertEquals("App version check", (new String("2.0-r-2015-04-23")), + evaled.get("app_version").toString()); + + String ua2 = "WikipediaApp/4.1.2 (iPhone OS 8.3; Tablet)"; + Map<String, String> evaled = uaParser.getUAMap(ua2); + assertEquals("App version check", (new String("4.1.2")), + evaled.get("app_version").toString()); + } + + @Test + public void testNonAppRequest() { + + String ua = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0"; + Map<String, String> evaled = uaParser.getUAMap(ua); + assertEquals("App version check", (new String(uaParser.NA)), + evaled.get("app_version").toString()); + } +} diff --git a/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUtilities.java b/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUtilities.java new file mode 100644 index 0000000..d0376dd --- /dev/null +++ b/refinery-core/src/test/java/org/wikimedia/analytics/refinery/core/TestUtilities.java @@ -0,0 +1,60 @@ +/** + * Copyright (C) 2015 Wikimedia Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wikimedia.analytics.refinery.core; + +import junit.framework.TestCase; +import org.junit.Test; + + +public class TestUtilitiesGetMatched extends TestCase { + + @Test + public void testMatching() { + + Pattern pattern = Pattern.compile("text"); + String matched = Utilities.getMatched(pattern, "some text"); + + assertEquals("Pattern matching check", "text", matched); + } + + @Test + public void testNotMatching() { + + Pattern pattern = Pattern.compile("not there"); + String matched = Utilities.getMatched(pattern, "some text"); + + assertEquals("Pattern not matching check", null, matched); + } + + @Test + public void testMatchingGroup() { + + Pattern pattern = Pattern.compile("te(x)t"); + String matched = Utilities.getMatched(pattern, "some text", 1); + + assertEquals("Pattern matching group check", "x", matched); + } + + @Test + public void testNotMatchingGroup() { + + Pattern pattern = Pattern.compile("not (there)"); + String matched = Utilities.getMatched(pattern, "some text", 1); + + assertEquals("Pattern not matching group check", null, matched); + } +} -- To view, visit https://gerrit.wikimedia.org/r/216060 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2800d990d203addf06bdb89c137caefdbbe97f0f Gerrit-PatchSet: 1 Gerrit-Project: analytics/refinery/source Gerrit-Branch: master Gerrit-Owner: Mforns <mfo...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits