├── CHANGELOG.md ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── cn │ └── thinkingdata │ └── analytics │ ├── BatchConsumer.java │ ├── DebugConsumer.java │ ├── LoggerConsumer.java │ ├── TDAnalytics.java │ ├── TDBatchConsumer.java │ ├── TDConstData.java │ ├── TDDebugConsumer.java │ ├── TDExample.java │ ├── TDLoggerConsumer.java │ ├── ThinkingDataAnalytics.java │ ├── exception │ ├── IllegalDataException.java │ ├── InvalidArgumentException.java │ └── NeedRetryException.java │ ├── inter │ ├── DynamicSuperPropertiesTracker.java │ ├── ITALogger.java │ ├── ITDAnalytics.java │ ├── ITDConsumer.java │ ├── ITDLogger.java │ └── IThinkingDataAnalytics.java │ ├── request │ ├── TDBaseRequest.java │ ├── TDBatchRequest.java │ ├── TDDebugRequest.java │ └── TDHttpRequestClient.java │ └── util │ ├── TDCommonUtil.java │ ├── TDLogger.java │ └── TDPropertyUtil.java └── test └── java └── cn └── thinkingdata └── analytics └── test ├── LoggerConsumerTest.java ├── functionTest ├── AssembleEventTest.java ├── TemporaryConsumer.java ├── TemporaryEvent.java └── UtilTest.java └── performanceTest └── PerformanceTest.java /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### v3.0.2-beta.2 2 | **Date:** 2024/08/08 3 | 4 | **Notes:** 5 | 6 | * Improve SDK stability 7 | 8 | ### v3.0.2-beta.1 9 | **Date:** 2024/06/06 10 | 11 | **Notes:** 12 | 13 | * The minimum supported JDK is 8 14 | * Upgrade to fastjson2 15 | 16 | ### v3.0.1 17 | **Date:** 2024/05/21 18 | 19 | **Notes:** 20 | 21 | * The debug mode supports data format verification 22 | 23 | ### v3.0.0 24 | **Date:** 2023/11/29 25 | 26 | **Notes:** 27 | 28 | * Release the official version 29 | 30 | ### v3.0.0-beta.1 31 | **Date:** 2023/09/20 32 | 33 | **Notes:** 34 | 35 | * Using the new API 36 | 37 | ### v2.1.2 38 | **Date:** 2023/09/19 39 | 40 | **Notes:** 41 | 42 | * Set SDK to official version 43 | 44 | ### v2.1.2-beta.3 45 | **Date:** 2023/07/17 46 | 47 | **Notes:** 48 | 49 | * Improve SDK stability 50 | 51 | ### v2.1.2-beta.2 52 | **Date:** 2023/04/20 53 | 54 | **Notes:** 55 | 56 | * Tidy test case 57 | 58 | ### v2.1.1 59 | **Date:** 2022/11/15 60 | 61 | **Notes:** 62 | 63 | * Add debug model 64 | 65 | ### v2.1.0 66 | **Date:** 2022/08/08 67 | 68 | **Notes:** 69 | 70 | * In LoggerConsumer and BatchConsumer mode, the correctness of attributes is not checked by default 71 | * Added log switch 72 | * Optimize some codes 73 | 74 | ### v2.0.4 75 | **Date:** 2022/05/27 76 | 77 | **Notes:** 78 | 79 | * Update to concurrentHasMap 80 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 thinking-analytics / data-collector / client-sdk 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ThinkingData SDK for Java 2 | ![output](https://user-images.githubusercontent.com/53337625/205621683-ed9b97ef-6a52-4903-a2c0-a955dddebb7d.png) 3 | 4 | This is the [ThinkingData](https://www.thinkingdata.cn)™ SDK for Java. Documentation is available on our help center in the following languages: 5 | 6 | - [English](https://docs.thinkingdata.cn/ta-manual/latest/en/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html) 7 | - [中文](https://docs.thinkingdata.cn/ta-manual/latest/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html) 8 | - [日本語](https://docs.thinkingdata.cn/ta-manual/latest/ja/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html) 9 | 10 | --- -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | cn.thinkingdata 6 | thinkingdatasdk 7 | 8 | 3.0.2 9 | 10 | 11 | 12 | thinkingdatasdk 13 | http://www.thinkinggame.cn 14 | 15 | org.sonatype.oss 16 | oss-parent 17 | 7 18 | 19 | 20 | 21 | The Apache Software License, Version 2.0 22 | http://www.apache.org/licenses/LICENSE-2.0.txt 23 | 24 | 25 | 26 | 27 | https://github.com/ThinkingDataAnalytics/java-sdk 28 | https://github.com/ThinkingDataAnalytics/java-sdk 29 | https://github.com/ThinkingDataAnalytics/java-sdk 30 | 31 | 32 | 33 | thinkingdata 34 | sdk@thinkingdata.cn 35 | https://www.thinkingdata.cn 36 | 37 | 38 | 39 | 40 | UTF-8 41 | 42 | 43 | 44 | 45 | com.alibaba 46 | fastjson 47 | 2.0.32 48 | 49 | 50 | org.anarres.lzo 51 | lzo-core 52 | 1.0.5 53 | 54 | 55 | org.apache.httpcomponents 56 | httpclient 57 | 4.5.13 58 | 59 | 60 | net.jpountz.lz4 61 | lz4 62 | 1.3.0 63 | 64 | 65 | 66 | junit 67 | junit 68 | 4.12 69 | test 70 | 71 | 72 | 73 | 74 | 75 | deploy 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | 3.5.1 81 | 82 | 8 83 | 8 84 | UTF-8 85 | 86 | 87 | 88 | org.apache.maven.plugins 89 | maven-javadoc-plugin 90 | 2.10.4 91 | 92 | true 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | ossrh 101 | 102 | 103 | releases 104 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 105 | 106 | 107 | snapshots 108 | https://oss.sonatype.org/content/repositories/snapshots/ 109 | 110 | 111 | 112 | 113 | compile 114 | 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-source-plugin 119 | 3.0.1 120 | 121 | 122 | package 123 | 124 | jar-no-fork 125 | 126 | 127 | 128 | 129 | 130 | 131 | org.apache.maven.plugins 132 | maven-javadoc-plugin 133 | 2.10.4 134 | 135 | 136 | package 137 | 138 | jar 139 | 140 | 141 | 142 | 143 | 144 | 145 | org.apache.maven.plugins 146 | maven-gpg-plugin 147 | 1.6 148 | 149 | 150 | sign-artifacts 151 | verify 152 | 153 | sign 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/BatchConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import java.net.URISyntaxException; 4 | 5 | /** 6 | * @deprecated please use TDBatchConsumer instead 7 | */ 8 | @Deprecated 9 | public class BatchConsumer extends TDBatchConsumer { 10 | 11 | public BatchConsumer(String serverUrl, String appId) throws URISyntaxException { 12 | super(serverUrl, appId, 20, 0, false, 0, "gzip", 0, true); 13 | } 14 | 15 | /** 16 | * init BatchConsumer with serverUrl and appId 17 | * 18 | * @param serverUrl serverUrl 19 | * @param appId APP ID 20 | * @param isThrowException throw exception or not 21 | * @throws URISyntaxException exception 22 | */ 23 | public BatchConsumer(String serverUrl, String appId, boolean isThrowException) throws URISyntaxException { 24 | super(serverUrl, appId, 20, 0, false, 0, "gzip", 0, isThrowException); 25 | } 26 | 27 | /** 28 | * @param serverUrl serverUrl 29 | * @param appId APP ID 30 | * @param config BatchConsumer config 31 | * @throws URISyntaxException exception 32 | */ 33 | public BatchConsumer(String serverUrl, String appId, Config config) throws URISyntaxException { 34 | super(serverUrl, appId, config.batchSize, config.timeout, config.autoFlush, config.interval, config.compress, config.maxCacheSize, config.isThrowException); 35 | } 36 | 37 | /** 38 | * init BatchConsumer 39 | * 40 | * @param serverUrl serverUrl 41 | * @param appId APP ID 42 | * @param batchSize flush event count each time 43 | * @param timeout http timeout (Unit: mill second) 44 | * @param autoFlush is auto flush or not 45 | * @param interval auto flush spacing (Unit: second) 46 | * @throws URISyntaxException exception 47 | */ 48 | public BatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval) throws URISyntaxException { 49 | super(serverUrl, appId, batchSize, timeout, autoFlush, interval, "gzip", 0, true); 50 | } 51 | 52 | /** 53 | * init BatchConsumer 54 | * 55 | * @param serverUrl serverUrl 56 | * @param appId APP ID 57 | * @param batchSize flush event count each time 58 | * @param timeout http timeout (Unit: mill second) 59 | * @param autoFlush is auto flush or not 60 | * @param interval auto flush spacing (Unit: second) 61 | * @param compress compress type 62 | * @throws URISyntaxException exception 63 | */ 64 | public BatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval, String compress) throws URISyntaxException { 65 | super(serverUrl, appId, batchSize, timeout, autoFlush, interval, compress, 0, true); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/DebugConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import java.net.URISyntaxException; 4 | 5 | /** 6 | * @deprecated please use TDDebugConsumer instead 7 | */ 8 | @Deprecated 9 | public class DebugConsumer extends TDDebugConsumer { 10 | public DebugConsumer(String serverUrl, String appId) throws URISyntaxException { 11 | super(serverUrl, appId, true); 12 | } 13 | 14 | public DebugConsumer(String serverUrl, String appId, String deviceId) throws URISyntaxException { 15 | super(serverUrl, appId, true, deviceId); 16 | } 17 | 18 | public DebugConsumer(String serverUrl, String appId, boolean writeData) throws URISyntaxException { 19 | super(serverUrl, appId, writeData, null); 20 | } 21 | 22 | public DebugConsumer(String serverUrl, String appId, boolean writeData, String deviceId) throws URISyntaxException { 23 | super(serverUrl, appId, writeData, deviceId); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/LoggerConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | /** 4 | * @deprecated please use TDLoggerConsumer instead 5 | */ 6 | @Deprecated 7 | public class LoggerConsumer extends TDLoggerConsumer { 8 | 9 | /** 10 | * init LoggerConsumer with config 11 | * 12 | * @param config LoggerConsumer.Config instance. 13 | */ 14 | public LoggerConsumer(final Config config) { 15 | super(config); 16 | } 17 | 18 | /** 19 | * init LoggerConsumer 20 | * 21 | * @param logDirectory directory of file 22 | */ 23 | public LoggerConsumer(final String logDirectory) { 24 | this(new Config(logDirectory)); 25 | } 26 | 27 | /** 28 | * init LoggerConsumer 29 | * 30 | * @param logDirectory directory of file 31 | * @param fileSize max size of single log file (MByte), default infinite 32 | */ 33 | public LoggerConsumer(final String logDirectory, int fileSize) { 34 | this(new Config(logDirectory, fileSize)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDAnalytics.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 4 | import cn.thinkingdata.analytics.inter.DynamicSuperPropertiesTracker; 5 | import cn.thinkingdata.analytics.inter.ITDAnalytics; 6 | import cn.thinkingdata.analytics.inter.ITDConsumer; 7 | import cn.thinkingdata.analytics.inter.ITDLogger; 8 | import cn.thinkingdata.analytics.util.TDCommonUtil; 9 | import cn.thinkingdata.analytics.util.TDLogger; 10 | import cn.thinkingdata.analytics.util.TDPropertyUtil; 11 | import org.apache.http.util.TextUtils; 12 | 13 | import java.util.Date; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | import java.util.UUID; 17 | import java.util.concurrent.ConcurrentHashMap; 18 | 19 | import static cn.thinkingdata.analytics.TDConstData.LIB_NAME; 20 | import static cn.thinkingdata.analytics.TDConstData.LIB_VERSION; 21 | 22 | /** 23 | * Entry of SDK 24 | */ 25 | public class TDAnalytics implements ITDAnalytics { 26 | /** 27 | * consumer 28 | */ 29 | private final ITDConsumer consumer; 30 | /** 31 | * common properties 32 | */ 33 | private final Map superProperties; 34 | private final boolean enableUUID; 35 | /** 36 | * Incorrect data will be reported to the TE when it is false; true would be not. default false 37 | */ 38 | boolean isStrictModel; 39 | /** 40 | * dynamic common properties 41 | */ 42 | private DynamicSuperPropertiesTracker dynamicSuperProperties = null; 43 | /** 44 | * Construct function 45 | * 46 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer 47 | */ 48 | public TDAnalytics(final ITDConsumer consumer) { 49 | this(consumer,false); 50 | } 51 | 52 | /** 53 | * Construct function 54 | * 55 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer 56 | * @param enableUUID Whether to allow uuid creation for events automatically 57 | */ 58 | public TDAnalytics(final ITDConsumer consumer, final boolean enableUUID) { 59 | this(consumer, enableUUID, consumer instanceof TDDebugConsumer); 60 | } 61 | 62 | /** 63 | * Construct function 64 | * 65 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer 66 | * @param enableUUID Whether to allow uuid creation for events automatically 67 | * @param isStrictModel Whether to enable property verification 68 | */ 69 | public TDAnalytics(final ITDConsumer consumer, final boolean enableUUID, final boolean isStrictModel) { 70 | this.consumer = consumer; 71 | this.enableUUID = enableUUID; 72 | this.superProperties = new ConcurrentHashMap<>(); 73 | this.isStrictModel = isStrictModel; 74 | } 75 | 76 | /** 77 | * Is enabled log or not. it is enabled in DebugConsumer 78 | * 79 | * @param isPrintLog enabled 80 | */ 81 | public static void enableLog(boolean isPrintLog) 82 | { 83 | TDLogger.enableLog(isPrintLog); 84 | } 85 | 86 | /** 87 | * Developers can set up a custom logging system based on requirements 88 | * 89 | * @param logger custom logger 90 | */ 91 | public static void setLogger(ITDLogger logger) 92 | { 93 | TDLogger.setLogger(logger); 94 | } 95 | 96 | /** 97 | * Report normal event 98 | * 99 | * @param accountId account ID 100 | * @param distinctId distinct ID 101 | * @param eventName name 102 | * @param properties properties 103 | * @throws InvalidArgumentException exception 104 | */ 105 | @Override 106 | public void track(String accountId, String distinctId, String eventName, Map properties) throws InvalidArgumentException { 107 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK, eventName, null, properties); 108 | } 109 | 110 | /** 111 | * Report first event 112 | * 113 | * @param accountId account ID 114 | * @param distinctId distinct ID 115 | * @param eventName event name 116 | * @param properties properties (must add "#first_check_id" in properties, because it is flag of the first event) 117 | * @throws InvalidArgumentException exception 118 | */ 119 | @Override 120 | public void trackFirst(String accountId, String distinctId, String eventName, String firstCheckId, Map properties) throws InvalidArgumentException { 121 | if (firstCheckId != null) { 122 | properties.put("#first_check_id", firstCheckId); 123 | } 124 | if (properties.containsKey("#first_check_id")) { 125 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK, eventName, null, properties); 126 | } else { 127 | throw new InvalidArgumentException("#first_check_id key must set"); 128 | } 129 | } 130 | 131 | /** 132 | * Report update event 133 | * 134 | * @param accountId account ID 135 | * @param distinctId distinct ID 136 | * @param eventName event name 137 | * @param eventId event id 138 | * @param properties properties 139 | * @throws InvalidArgumentException exception 140 | */ 141 | @Override 142 | public void trackUpdate(String accountId, String distinctId, String eventName, String eventId, Map properties) throws InvalidArgumentException { 143 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK_UPDATE, eventName, eventId, properties); 144 | } 145 | 146 | /** 147 | * Report overwrite event 148 | * 149 | * @param accountId account ID 150 | * @param distinctId distinct ID 151 | * @param eventName event name 152 | * @param eventId event id 153 | * @param properties properties 154 | * @throws InvalidArgumentException exception 155 | */ 156 | @Override 157 | public void trackOverwrite(String accountId, String distinctId, String eventName, String eventId, Map properties) throws InvalidArgumentException { 158 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK_OVERWRITE, eventName, eventId, properties); 159 | } 160 | 161 | /** 162 | * Sets the properties that each event carries 163 | * 164 | * @param properties common properties 165 | */ 166 | @Override 167 | public void setSuperProperties(Map properties) { 168 | this.superProperties.putAll(properties); 169 | } 170 | 171 | @Override 172 | public Map getSuperProperties() { 173 | return this.superProperties; 174 | } 175 | 176 | @Override 177 | public void unsetSuperProperties(String key) { 178 | if (!key.isEmpty()) { 179 | this.superProperties.remove(key); 180 | } 181 | } 182 | 183 | @Override 184 | public void clearSuperProperties() { 185 | this.superProperties.clear(); 186 | } 187 | 188 | @Override 189 | public void setDynamicSuperPropertiesTracker(DynamicSuperPropertiesTracker dynamicSuperPropertiesTracker) { 190 | this.dynamicSuperProperties = dynamicSuperPropertiesTracker; 191 | } 192 | 193 | /** 194 | * Delete a user, This operation is not reversible 195 | * 196 | * @param accountId account ID 197 | * @param distinctId distinct ID 198 | * @throws InvalidArgumentException exception 199 | */ 200 | @Override 201 | public void userDelete(String accountId, String distinctId) throws InvalidArgumentException { 202 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_DEL, null); 203 | } 204 | 205 | /** 206 | * To accumulate operations against the property 207 | * 208 | * @param accountId account ID 209 | * @param distinctId distinct ID 210 | * @param properties properties 211 | * @throws InvalidArgumentException exception 212 | */ 213 | @Override 214 | public void userAdd(String accountId, String distinctId, Map properties) throws InvalidArgumentException { 215 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_ADD, properties); 216 | } 217 | 218 | /** 219 | * Set user properties, this message would be neglected If such property had been set before 220 | * 221 | * @param accountId account ID 222 | * @param distinctId distinct ID 223 | * @param properties properties 224 | * @throws InvalidArgumentException exception 225 | */ 226 | @Override 227 | public void userSetOnce(String accountId, String distinctId, Map properties) throws InvalidArgumentException { 228 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_SET_ONCE, properties); 229 | } 230 | 231 | /** 232 | * Set user properties. would overwrite existing names 233 | * 234 | * @param accountId account ID 235 | * @param distinctId distinct ID 236 | * @param properties properties 237 | * @throws InvalidArgumentException exception 238 | */ 239 | @Override 240 | public void userSet(String accountId, String distinctId, Map properties) throws InvalidArgumentException { 241 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_SET, properties); 242 | } 243 | 244 | /** 245 | * Clear the user properties of users 246 | * 247 | * @param accountId account ID 248 | * @param distinctId distinct ID 249 | * @param properties properties 250 | * @throws InvalidArgumentException exception 251 | */ 252 | @Override 253 | public void userUnset(String accountId, String distinctId, String... properties) throws InvalidArgumentException { 254 | if (properties == null) { 255 | return; 256 | } 257 | Map prop = new HashMap<>(); 258 | for (String s : properties) { 259 | prop.put(s, 0); 260 | } 261 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_UNSET, prop); 262 | } 263 | 264 | /** 265 | * To add user properties of array type 266 | * 267 | * @param accountId account ID 268 | * @param distinctId distinct ID 269 | * @param properties properties 270 | * @throws InvalidArgumentException exception 271 | */ 272 | @Override 273 | public void userAppend(String accountId, String distinctId, Map properties) throws InvalidArgumentException { 274 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_APPEND, properties); 275 | } 276 | 277 | /** 278 | * Append user properties to array type by unique. 279 | * 280 | * @param accountId account ID 281 | * @param distinctId distinct ID 282 | * @param properties properties 283 | * @throws InvalidArgumentException exception 284 | */ 285 | @Override 286 | public void userUniqAppend(String accountId, String distinctId, Map properties) throws InvalidArgumentException { 287 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_UNIQ_APPEND, properties); 288 | } 289 | 290 | @Override 291 | public void flush() { 292 | this.consumer.flush(); 293 | TDLogger.println("Manually flush"); 294 | } 295 | 296 | @Override 297 | public void close() { 298 | this.consumer.close(); 299 | TDLogger.println("SDK close"); 300 | } 301 | 302 | private void innerTrack(String accountId, String distinctId, TDConstData.DataType type, String eventName, String eventId, Map properties) 303 | throws InvalidArgumentException { 304 | add(distinctId, accountId, type, eventName, eventId, properties); 305 | } 306 | 307 | private void innerUserProfile(String accountId, String distinctId, TDConstData.DataType type, Map properties) 308 | throws InvalidArgumentException { 309 | add(distinctId, accountId, type, null, null, properties); 310 | } 311 | 312 | private void add(String distinctId, String accountId, TDConstData.DataType type, String eventName, String eventId, Map properties) 313 | throws InvalidArgumentException { 314 | if (isStrictModel && TextUtils.isEmpty(accountId) && TextUtils.isEmpty(distinctId)) { 315 | throw new InvalidArgumentException("accountId or distinctId must be provided."); 316 | } 317 | Map copyProperties = properties!=null ? new ConcurrentHashMap<>(properties) : new ConcurrentHashMap(); 318 | Map desProperties = new HashMap<>(); 319 | Map event = new HashMap<>(); 320 | TDCommonUtil.buildData(event,"#distinct_id", distinctId); 321 | TDCommonUtil.buildData(event,"#account_id", accountId); 322 | event.put("#time", new Date()); 323 | event.put("#type", type.getType()); 324 | // #uuid v4 is only supported 325 | if(!event.containsKey("#uuid") && enableUUID) 326 | { 327 | event.put("#uuid", UUID.randomUUID().toString()); 328 | } 329 | // Move special properties 330 | TDPropertyUtil.moveProperty(event,copyProperties,"#uuid","#time","#ip","#app_id","#first_check_id","#transaction_property","#import_tool_id"); 331 | if (type.getType().contains("track")) { 332 | if (isStrictModel) { 333 | TDCommonUtil.throwEmptyException(eventName,"The event name must be provided."); 334 | } 335 | if (type.getType().contains("track_")) { 336 | if (isStrictModel) { 337 | TDCommonUtil.throwEmptyException(eventId,"The event id must be provided."); 338 | } 339 | TDCommonUtil.buildData(event,"#event_id", eventId); 340 | } 341 | TDPropertyUtil.mergeProperties(desProperties,superProperties,dynamicSuperProperties!=null ? dynamicSuperProperties.getDynamicSuperProperties():null,copyProperties); 342 | event.put("#event_name", eventName); 343 | desProperties.put("#lib", LIB_NAME); 344 | desProperties.put("#lib_version", LIB_VERSION); 345 | } else { 346 | TDPropertyUtil.mergeProperties(desProperties,copyProperties); 347 | } 348 | if (isStrictModel) { 349 | TDPropertyUtil.assertProperties(desProperties,type); 350 | } 351 | event.put("properties",desProperties); 352 | try { 353 | this.consumer.add(event); 354 | } catch (Exception e) { 355 | throw new InvalidArgumentException(e); 356 | } 357 | } 358 | } 359 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDBatchConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.exception.IllegalDataException; 4 | import cn.thinkingdata.analytics.exception.NeedRetryException; 5 | import cn.thinkingdata.analytics.inter.ITDConsumer; 6 | import cn.thinkingdata.analytics.request.TDBatchRequest; 7 | import cn.thinkingdata.analytics.util.TDCommonUtil; 8 | import cn.thinkingdata.analytics.util.TDLogger; 9 | import com.alibaba.fastjson2.JSON; 10 | 11 | import java.net.URI; 12 | import java.net.URISyntaxException; 13 | import java.util.*; 14 | 15 | import static cn.thinkingdata.analytics.TDConstData.DEFAULT_DATE_FORMAT; 16 | 17 | /** 18 | * Data is reported using http 19 | */ 20 | public class TDBatchConsumer implements ITDConsumer { 21 | 22 | private final int batchSize; // flush event count each time 23 | private final int maxCacheSize; 24 | private final boolean isThrowException; 25 | private Timer autoFlushTimer; 26 | 27 | private final static int MAX_BATCH_SIZE = 1000; // max limit of flush event count 28 | private final Object messageLock = new Object(); 29 | private final Object cacheLock = new Object(); 30 | private final Object sdkCloseLock = new Object(); 31 | private List> messageChannel; 32 | private final LinkedList>> cacheBuffer = new LinkedList>>(); 33 | private final TDBatchRequest httpService; 34 | private boolean isClose; 35 | 36 | /** 37 | * Construct BatchConsumer 38 | * 39 | * @param serverUrl serverUrl 40 | * @param appId appId 41 | * @throws URISyntaxException exception 42 | */ 43 | public TDBatchConsumer(String serverUrl, String appId) throws URISyntaxException { 44 | this(serverUrl, appId, 20, 0, false, 0, "gzip", 0, false); 45 | } 46 | 47 | /** 48 | * Construct BatchConsumer 49 | * 50 | * @param serverUrl serverUrl 51 | * @param appId APP ID 52 | * @param config BatchConsumer config 53 | * @throws URISyntaxException exception 54 | */ 55 | public TDBatchConsumer(String serverUrl, String appId, Config config) throws URISyntaxException { 56 | this(serverUrl, appId, config.batchSize, config.timeout, config.autoFlush, config.interval, config.compress, config.maxCacheSize, config.isThrowException); 57 | } 58 | 59 | /** 60 | * Construct BatchConsumer 61 | * 62 | * @param serverUrl serverUrl 63 | * @param appId APP ID 64 | * @param isThrowException throw exception or not 65 | * @throws URISyntaxException exception 66 | */ 67 | @Deprecated 68 | protected TDBatchConsumer(String serverUrl, String appId, boolean isThrowException) throws URISyntaxException { 69 | this(serverUrl, appId, 20, 0, false, 0, "gzip", 0, isThrowException); 70 | } 71 | 72 | /** 73 | * Construct BatchConsumer 74 | * 75 | * @param serverUrl serverUrl 76 | * @param appId APP ID 77 | * @param batchSize flush event count each time 78 | * @param timeout http timeout (Unit: mill second) 79 | * @param autoFlush is auto flush or not 80 | * @param interval auto flush spacing (Unit: second) 81 | * @throws URISyntaxException exception 82 | */ 83 | @Deprecated 84 | protected TDBatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval) throws URISyntaxException { 85 | this(serverUrl, appId, batchSize, timeout, autoFlush, interval, "gzip", 0, false); 86 | } 87 | 88 | /** 89 | * Construct BatchConsumer 90 | * 91 | * @param serverUrl serverUrl 92 | * @param appId APP ID 93 | * @param batchSize flush event count each time 94 | * @param timeout http timeout (Unit: mill second) 95 | * @param autoFlush is auto flush or not 96 | * @param interval auto flush spacing (Unit: second) 97 | * @param compress compress type 98 | * @throws URISyntaxException exception 99 | */ 100 | @Deprecated 101 | protected TDBatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval, String compress) throws URISyntaxException { 102 | this(serverUrl, appId, batchSize, timeout, autoFlush, interval, compress, 0, false); 103 | } 104 | 105 | /** 106 | * Construct BatchConsumer 107 | * 108 | * @param serverUrl serverUrl 109 | * @param appId APP ID 110 | * @param batchSize flush event count each time 111 | * @param timeout http timeout (Unit: mill second) 112 | * @param autoFlush is auto flush or not 113 | * @param interval auto flush spacing (Unit: second) 114 | * @param compress compress type 115 | * @param maxCacheSize max buffer count 116 | * @param isThrowException is throw exception or not 117 | * @throws URISyntaxException exception 118 | */ 119 | protected TDBatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval, 120 | String compress, int maxCacheSize, boolean isThrowException) throws URISyntaxException { 121 | TDLogger.println("BatchConsumer Model,Server:"+serverUrl+" Appid:"+appId); 122 | this.messageChannel = new ArrayList>(); 123 | this.batchSize = batchSize < 0 ? 20 : Math.min(batchSize, MAX_BATCH_SIZE); 124 | this.maxCacheSize = maxCacheSize <= 0 ? 50 : maxCacheSize; 125 | this.isThrowException = isThrowException; 126 | URI uri = new URI(serverUrl); 127 | URI url = new URI(uri.getScheme(), uri.getAuthority(), 128 | "/sync_server", uri.getQuery(), uri.getFragment()); 129 | this.httpService = new TDBatchRequest(url, appId, timeout); 130 | this.httpService.setCompress(compress); 131 | if (autoFlush) { 132 | if (interval <= 0) { 133 | interval = 3; 134 | } 135 | autoFlushTimer = new Timer(); 136 | autoFlushTimer.schedule(new TimerTask() { 137 | public void run() { 138 | flushOnce(); 139 | } 140 | }, 1000, interval * 1000L); 141 | } 142 | } 143 | 144 | /** 145 | * BatchConsumer config 146 | */ 147 | public static class Config { 148 | /** 149 | * Batch size 150 | */ 151 | protected int batchSize = 20; 152 | /** 153 | * Batch time interval 154 | */ 155 | protected int interval = 3; 156 | /** 157 | * Http compress type. default is "gzip" 158 | */ 159 | protected String compress = "gzip"; 160 | /** 161 | * Http timeout 162 | */ 163 | protected int timeout = 30000; 164 | /** 165 | * Enable auto flush 166 | */ 167 | protected boolean autoFlush = false; 168 | /** 169 | * Max cache size 170 | */ 171 | protected int maxCacheSize = 50; 172 | /** 173 | * Is throw exception 174 | */ 175 | protected boolean isThrowException = false; 176 | 177 | /** 178 | * Construct batch consumer config 179 | */ 180 | public Config() { 181 | } 182 | 183 | /** 184 | * @param batchSize flush event count each time 185 | */ 186 | public void setBatchSize(int batchSize) { 187 | this.batchSize = batchSize; 188 | } 189 | 190 | /** 191 | * @param interval auto flush spacing (Unit: second) 192 | */ 193 | public void setInterval(int interval) { 194 | this.interval = interval; 195 | } 196 | 197 | /** 198 | * @param compress compress type 199 | */ 200 | public void setCompress(String compress) { 201 | this.compress = compress; 202 | } 203 | 204 | /** 205 | * network timeout 206 | * @param timeout millSecond 207 | */ 208 | public void setTimeout(int timeout) { 209 | this.timeout = timeout; 210 | } 211 | 212 | /** 213 | * Whether to allow automatic flushing 214 | * @param autoFlush enable or not 215 | */ 216 | public void setAutoFlush(boolean autoFlush) { 217 | this.autoFlush = autoFlush; 218 | } 219 | 220 | /** 221 | * max cache buffer size 222 | * @param maxCacheSize number 223 | */ 224 | public void setMaxCacheSize(int maxCacheSize) { 225 | this.maxCacheSize = maxCacheSize; 226 | } 227 | 228 | /** 229 | * Whether to throw exception 230 | * @param isThrowException enable or not 231 | */ 232 | public void setThrowException(boolean isThrowException) { 233 | this.isThrowException = isThrowException; 234 | } 235 | } 236 | 237 | @Override 238 | public void add(Map message) { 239 | synchronized (sdkCloseLock) { 240 | if (isClose) { 241 | TDLogger.println("SDK is already closed"); 242 | return; 243 | } 244 | } 245 | String formatMsg = JSON.toJSONString(message, DEFAULT_DATE_FORMAT, TDCommonUtil.fastJsonSerializerFeature()); 246 | TDLogger.println("collect data="+formatMsg); 247 | synchronized (messageLock) { 248 | messageChannel.add(message); 249 | } 250 | if (messageChannel.size() >= batchSize || !cacheBuffer.isEmpty()) { 251 | flushOnce(); 252 | } 253 | } 254 | 255 | @Override 256 | public void flush() { 257 | synchronized (sdkCloseLock) { 258 | if (isClose) { 259 | TDLogger.println("SDK is already closed"); 260 | return; 261 | } 262 | } 263 | while (!cacheBuffer.isEmpty() || !messageChannel.isEmpty()) { 264 | try { 265 | flushOnce(); 266 | } catch (Exception e) { 267 | if (isThrowException) { 268 | throw e; 269 | } 270 | } 271 | } 272 | } 273 | 274 | /** 275 | * Flush data once. 276 | */ 277 | public void flushOnce() { 278 | if (messageChannel.isEmpty() && cacheBuffer.isEmpty()) { 279 | return; 280 | } 281 | synchronized (sdkCloseLock) { 282 | if (isClose) { 283 | TDLogger.println("SDK is already closed"); 284 | return; 285 | } 286 | } 287 | synchronized (cacheLock) { 288 | synchronized (messageLock) { 289 | if (messageChannel.isEmpty() && cacheBuffer.isEmpty()) { 290 | return; 291 | } 292 | if (messageChannel.size() >= batchSize || cacheBuffer.isEmpty()) { 293 | cacheBuffer.add(messageChannel); 294 | messageChannel = new ArrayList>(); 295 | } 296 | } 297 | 298 | List> buffer = cacheBuffer.getFirst(); 299 | 300 | try { 301 | String formatMsg = com.alibaba.fastjson2.JSON.toJSONString(buffer, DEFAULT_DATE_FORMAT, TDCommonUtil.fastJsonSerializerFeature()); 302 | TDLogger.println("flush data="+formatMsg); 303 | httpSending(formatMsg, buffer.size()); 304 | cacheBuffer.removeFirst(); 305 | } catch (NeedRetryException e) { 306 | TDLogger.println(e.getLocalizedMessage()); 307 | if (isThrowException) { 308 | throw e; 309 | } 310 | } catch (IllegalDataException | NullPointerException e) { 311 | TDLogger.println(e.getLocalizedMessage()); 312 | cacheBuffer.removeFirst(); 313 | if (isThrowException) { 314 | throw e; 315 | } 316 | } finally { 317 | if (cacheBuffer.size() > maxCacheSize) { 318 | cacheBuffer.removeFirst(); 319 | } 320 | } 321 | } 322 | } 323 | 324 | /** 325 | * Send data 326 | * @param data Event data 327 | * @param dataSize Data size 328 | */ 329 | public void httpSending(final String data, final int dataSize) { 330 | httpService.send(data, dataSize); 331 | } 332 | 333 | @Override 334 | public void close() { 335 | if (autoFlushTimer != null) { 336 | try { 337 | autoFlushTimer.cancel(); 338 | } catch (Exception e) { 339 | if (isThrowException) { 340 | throw e; 341 | } 342 | } 343 | } 344 | flush(); 345 | if (httpService != null) { 346 | httpService.close(); 347 | } 348 | synchronized (sdkCloseLock) { 349 | isClose = true; 350 | } 351 | } 352 | } 353 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDConstData.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class TDConstData { 6 | public final static String LIB_VERSION = "3.0.2"; 7 | public final static String LIB_NAME = "tga_java_sdk"; 8 | public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 9 | 10 | // A string of 50 letters and digits that starts with '#' or a letter 11 | public final static Pattern KEY_PATTERN = Pattern.compile( 12 | "^(#[a-z][a-z\\d_]{0,49})|([a-z][a-z\\d_]{0,50})|(__[a-z][a-z\\d_]{0,50})$", Pattern.CASE_INSENSITIVE); 13 | 14 | public enum DataType { 15 | /** 16 | * SDK api 17 | */ 18 | TRACK("track"), 19 | USER_SET("user_set"), 20 | USER_SET_ONCE("user_setOnce"), 21 | USER_ADD("user_add"), 22 | USER_DEL("user_del"), 23 | USER_UNSET("user_unset"), 24 | USER_APPEND("user_append"), 25 | USER_UNIQ_APPEND("user_uniq_append"), 26 | TRACK_UPDATE("track_update"), 27 | TRACK_OVERWRITE("track_overwrite"); 28 | 29 | private final String type; 30 | 31 | DataType(String type) { 32 | this.type = type; 33 | } 34 | 35 | public String getType() { 36 | return type; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDDebugConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.inter.ITDConsumer; 4 | import cn.thinkingdata.analytics.request.TDDebugRequest; 5 | import cn.thinkingdata.analytics.util.TDCommonUtil; 6 | import cn.thinkingdata.analytics.util.TDLogger; 7 | import com.alibaba.fastjson2.JSON; 8 | import com.alibaba.fastjson2.JSONValidator; 9 | 10 | import java.net.URI; 11 | import java.net.URISyntaxException; 12 | import java.util.Map; 13 | 14 | import static cn.thinkingdata.analytics.TDConstData.DEFAULT_DATE_FORMAT; 15 | 16 | /** 17 | * Used to verify data before use SDK 18 | */ 19 | public class TDDebugConsumer implements ITDConsumer { 20 | TDDebugRequest httpService; 21 | 22 | /** 23 | * Construct DebugConsumer 24 | * 25 | * @param serverUrl receiver url 26 | * @param appId project ID in TE 27 | * @throws URISyntaxException exception 28 | */ 29 | public TDDebugConsumer(String serverUrl, String appId) throws URISyntaxException { 30 | this(serverUrl, appId, true); 31 | } 32 | 33 | /** 34 | * Construct DebugConsumer 35 | * 36 | * @param serverUrl receiver url 37 | * @param appId project ID in TE 38 | * @param deviceId debug device ID 39 | * @throws URISyntaxException exception 40 | */ 41 | public TDDebugConsumer(String serverUrl, String appId, String deviceId) throws URISyntaxException { 42 | this(serverUrl, appId, true, deviceId); 43 | } 44 | 45 | /** 46 | * Construct DebugConsumer 47 | * 48 | * @param serverUrl receiver url 49 | * @param appId project ID in TE 50 | * @param writeData Whether to write data to the TE database. default is false 51 | * @throws URISyntaxException exception 52 | */ 53 | public TDDebugConsumer(String serverUrl, String appId, boolean writeData) throws URISyntaxException { 54 | this(serverUrl, appId, writeData, null); 55 | } 56 | 57 | /** 58 | * Construct DebugConsumer 59 | * 60 | * @param serverUrl receiver url 61 | * @param appId project ID in TE 62 | * @param writeData Whether to write data to the TE database. default is false 63 | * @param deviceId debug device ID 64 | * @throws URISyntaxException exception 65 | */ 66 | public TDDebugConsumer(String serverUrl, String appId, boolean writeData, String deviceId) throws URISyntaxException { 67 | TDLogger.enableLog(true); 68 | TDLogger.println("DebugConsumer Model,Server:"+serverUrl+" Appid:"+appId); 69 | URI uri = new URI(serverUrl); 70 | URI restfulUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), 71 | "/data_debug", uri.getQuery(), uri.getFragment()); 72 | httpService = new TDDebugRequest(restfulUri, appId, writeData, deviceId); 73 | } 74 | 75 | @Override 76 | public void add(Map message) { 77 | String formatMsg = JSON.toJSONString(message, DEFAULT_DATE_FORMAT, TDCommonUtil.fastJsonSerializerFeature()); 78 | if (JSONValidator.from(formatMsg).validate()) { 79 | TDLogger.println("collect data=" + formatMsg); 80 | } else { 81 | String errorMsg = "The provided string is not a valid JSON."; 82 | TDLogger.println(errorMsg); 83 | throw new RuntimeException(errorMsg); 84 | } 85 | try { 86 | httpService.send(formatMsg, 1); 87 | } catch (Exception e) { 88 | TDLogger.println(e.getLocalizedMessage()); 89 | throw new RuntimeException(e); 90 | } 91 | } 92 | 93 | @Override 94 | public void flush() { 95 | 96 | } 97 | 98 | @Override 99 | public void close() { 100 | if (httpService != null) { 101 | httpService.close(); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDExample.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.inter.ITDConsumer; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Calendar; 7 | import java.util.HashMap; 8 | 9 | public class TDExample { 10 | static TDAnalytics tdAnalytics; 11 | 12 | public static ITDConsumer generateDebugConsumer() { 13 | ITDConsumer consumer = null; 14 | try { 15 | consumer = new TDDebugConsumer("url", "appId", "debug_device"); 16 | } catch (Exception ignored){} 17 | return consumer; 18 | } 19 | 20 | public static ITDConsumer generateBatchConsumer() { 21 | ITDConsumer consumer = null; 22 | try { 23 | TDBatchConsumer.Config config = new TDBatchConsumer.Config(); 24 | config.setBatchSize(20); 25 | config.setMaxCacheSize(10); 26 | consumer = new TDBatchConsumer("url", "appId", config); 27 | } catch (Exception ignored){} 28 | return consumer; 29 | } 30 | 31 | public static ITDConsumer generateLoggerConsumer() { 32 | TDLoggerConsumer.Config config = new TDLoggerConsumer.Config("./log"); 33 | config.setFilenamePrefix("TD"); 34 | return new TDLoggerConsumer(config); 35 | } 36 | 37 | public static void main(String[] args) { 38 | ITDConsumer consumer = null; 39 | 40 | // // Debug 41 | // consumer = generateDebugConsumer(); 42 | 43 | // // Batch 44 | // consumer = generateBatchConsumer(); 45 | // 46 | // Log 47 | consumer = generateLoggerConsumer(); 48 | 49 | 50 | if (consumer != null) { 51 | tdAnalytics = new TDAnalytics(consumer, false); 52 | } 53 | 54 | TDAnalytics.enableLog(true); 55 | 56 | String accountId = "accountId_123"; 57 | String distinctId = "distinctId_aaa"; 58 | 59 | try { 60 | tdAnalytics.track(accountId, distinctId, "login", null); 61 | } catch (Exception ignored) {} 62 | 63 | try { 64 | tdAnalytics.setSuperProperties(new HashMap(){ 65 | { 66 | put("super_key_1", "value_1"); 67 | put("super_key_2", "value_2"); 68 | } 69 | }); 70 | } catch (Exception ignored) {} 71 | 72 | try { 73 | tdAnalytics.trackFirst(accountId, distinctId, "event_2", "id_1", new HashMap(){ 74 | { 75 | put("name", "wang"); 76 | put("age", "18"); 77 | } 78 | }); 79 | } catch (Exception ignored) {} 80 | 81 | try { 82 | tdAnalytics.trackFirst(accountId, distinctId, "event_2", null, new HashMap(){ 83 | { 84 | put("name", "wang"); 85 | put("age", "18"); 86 | put("#first_check_id", "id_1"); 87 | } 88 | }); 89 | } catch (Exception e) { 90 | System.out.println("error: " + e); 91 | } 92 | 93 | try { 94 | tdAnalytics.trackUpdate(accountId, distinctId, "event_update", "eventId_1", new HashMap(){ 95 | { 96 | put("update_key_1", "old_value"); 97 | put("time", Calendar.getInstance().getTime()); 98 | } 99 | }); 100 | } catch (Exception ignored) {} 101 | 102 | try { 103 | tdAnalytics.trackUpdate(accountId, distinctId, "event_update", "eventId_1", new HashMap(){ 104 | { 105 | put("update_key_1", "new_value"); 106 | put("time", Calendar.getInstance().getTime()); 107 | } 108 | }); 109 | } catch (Exception ignored) {} 110 | 111 | try { 112 | tdAnalytics.trackOverwrite(accountId, distinctId, "event_overwrite", "eventId_2", new HashMap(){ 113 | { 114 | put("overwrite_key_1", "value_1"); 115 | put("time", Calendar.getInstance().getTime()); 116 | } 117 | }); 118 | } catch (Exception ignored) {} 119 | 120 | try { 121 | tdAnalytics.trackUpdate(accountId, distinctId, "event_overwrite", "eventId_2", new HashMap(){ 122 | { 123 | put("overwrite_key_2", "value_2"); 124 | put("time", Calendar.getInstance().getTime()); 125 | } 126 | }); 127 | } catch (Exception ignored) {} 128 | 129 | try { 130 | tdAnalytics.userSetOnce(accountId, distinctId, new HashMap(){ 131 | { 132 | put("name", "a"); 133 | put("userId", 1); 134 | put("age", 18); 135 | } 136 | }); 137 | } catch (Exception ignored) {} 138 | 139 | try { 140 | final ArrayList fruits = new ArrayList(); 141 | fruits.add("apple"); 142 | fruits.add("orange"); 143 | tdAnalytics.userSet(accountId, distinctId, new HashMap(){ 144 | { 145 | put("name", "b"); 146 | put("fruits", fruits); 147 | } 148 | }); 149 | } catch (Exception ignored) {} 150 | 151 | tdAnalytics.flush(); 152 | 153 | // call when the service is turned off 154 | tdAnalytics.close(); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/TDLoggerConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.inter.ITDConsumer; 4 | import cn.thinkingdata.analytics.util.TDCommonUtil; 5 | import cn.thinkingdata.analytics.util.TDLogger; 6 | import com.alibaba.fastjson2.JSON; 7 | import com.alibaba.fastjson2.JSONException; 8 | 9 | import java.io.File; 10 | import java.io.FileNotFoundException; 11 | import java.io.FileOutputStream; 12 | import java.io.IOException; 13 | import java.nio.channels.FileChannel; 14 | import java.nio.channels.FileLock; 15 | import java.nio.charset.StandardCharsets; 16 | import java.text.SimpleDateFormat; 17 | import java.util.*; 18 | import static cn.thinkingdata.analytics.TDConstData.DEFAULT_DATE_FORMAT; 19 | /** 20 | * Write data to file, it works with LogBus2 21 | */ 22 | public class TDLoggerConsumer implements ITDConsumer { 23 | /** 24 | * File rotate mode 25 | */ 26 | public enum RotateMode { 27 | /** 28 | * in days 29 | */ 30 | DAILY, 31 | /** 32 | * in hours 33 | */ 34 | HOURLY 35 | } 36 | 37 | /** 38 | * LoggerConsumer config 39 | */ 40 | public static class Config { 41 | String logDirectory; // directory of log file 42 | RotateMode rotateMode = RotateMode.DAILY; // file rotate mode, in days default 43 | String lockFileName; // lock file 44 | String fileNamePrefix; // prefix of log file 45 | int interval = 0; // auto flush interval (second) 46 | int fileSize = 0; // max size of single log file (MByte) 47 | int bufferSize = 8192; // buffer size (unit: byte) 48 | boolean autoFlush = false; // is enabled auto flush or not 49 | boolean isThrowException = false; // is enabled throw exception 50 | 51 | 52 | /** 53 | * init LoggerConsumer config 54 | * 55 | * @param logDirectory directory of log file 56 | */ 57 | public Config(String logDirectory) { 58 | this(logDirectory, 0); 59 | } 60 | 61 | /** 62 | * init LoggerConsumer config 63 | * 64 | * @param logDirectory directory of log file 65 | * @param fileSize max size of single log file (MByte), default infinite 66 | */ 67 | public Config(String logDirectory, int fileSize) { 68 | this.logDirectory = logDirectory; 69 | this.fileSize = fileSize; 70 | } 71 | 72 | /** 73 | * set file rotate mode 74 | * 75 | * @param rotateMode remote mode 76 | */ 77 | public void setRotateMode(RotateMode rotateMode) { 78 | this.rotateMode = rotateMode; 79 | } 80 | 81 | /** 82 | * set file size 83 | * 84 | * @param fileSize file size (unit: Mb) 85 | */ 86 | public void setFileSize(int fileSize) { 87 | this.fileSize = fileSize; 88 | } 89 | 90 | /** 91 | * set lock file 92 | * @param lockFileName lock file name 93 | */ 94 | public void setLockFile(String lockFileName) { 95 | this.lockFileName = lockFileName; 96 | } 97 | 98 | /** 99 | * set buffer size 100 | * 101 | * @param bufferSize buffer size (unit: byte). 102 | */ 103 | public void setBufferSize(int bufferSize) { 104 | this.bufferSize = bufferSize; 105 | } 106 | 107 | /** 108 | * prefix of file 109 | * 110 | * @param fileNamePrefix prefix 111 | */ 112 | public void setFilenamePrefix(String fileNamePrefix) { 113 | this.fileNamePrefix = fileNamePrefix; 114 | } 115 | 116 | /** 117 | * is auto flush or not 118 | * 119 | * @param autoFlush auto flush 120 | */ 121 | public void setAutoFlush(boolean autoFlush) { 122 | this.autoFlush = autoFlush; 123 | } 124 | 125 | /** 126 | * auto flush interval 127 | * 128 | * @param interval interval 129 | */ 130 | public void setInterval(int interval) { 131 | this.interval = interval; 132 | } 133 | } 134 | 135 | private final String fileName; 136 | private final String lockFileName; 137 | private final int bufferSize; 138 | private final int fileSize; 139 | private Timer autoFlushTimer; 140 | 141 | private final StringBuffer messageBuffer = new StringBuffer(); 142 | private final ThreadLocal df; 143 | 144 | private LoggerFileWriter loggerWriter; 145 | 146 | private boolean isClose; 147 | private final boolean isThrowException; 148 | 149 | /** 150 | * init LoggerConsumer with config 151 | * 152 | * @param config LoggerConsumer.Config instance. 153 | */ 154 | public TDLoggerConsumer(final Config config) { 155 | if (config.logDirectory == null || config.logDirectory.isEmpty()) { 156 | throw new RuntimeException("directory for file is not be empty!"); 157 | } 158 | TDLogger.println("LogConsumer Model,LogDirectory="+config.logDirectory); 159 | File dir = new File(config.logDirectory); 160 | if (!dir.exists()) { 161 | dir.mkdirs(); 162 | } 163 | if (!dir.isDirectory()) { 164 | throw new RuntimeException("path of file is not directory" + config.logDirectory); 165 | } 166 | String fileNamePrefix = config.fileNamePrefix == null ? config.logDirectory + File.separator : config.logDirectory + File.separator + config.fileNamePrefix + "."; 167 | this.fileName = fileNamePrefix + "log."; 168 | this.fileSize = config.fileSize; 169 | this.lockFileName = config.lockFileName; 170 | this.bufferSize = config.bufferSize; 171 | this.isThrowException = config.isThrowException; 172 | 173 | final String dataFormat = config.rotateMode == RotateMode.HOURLY ? "yyyy-MM-dd-HH" : "yyyy-MM-dd"; 174 | df = new ThreadLocal() { 175 | @Override 176 | protected SimpleDateFormat initialValue() { 177 | return new SimpleDateFormat(dataFormat); 178 | } 179 | }; 180 | 181 | if (config.autoFlush) { 182 | if (config.interval <= 0) { 183 | config.interval = 3; 184 | } 185 | autoFlushTimer = new Timer(); 186 | autoFlushTimer.schedule(new TimerTask() { 187 | public void run() { 188 | flush(); 189 | } 190 | }, 1000, config.interval * 1000L); 191 | } 192 | } 193 | 194 | /** 195 | * init LoggerConsumer 196 | * 197 | * @param logDirectory directory of file 198 | */ 199 | public TDLoggerConsumer(final String logDirectory) { 200 | this(new Config(logDirectory)); 201 | } 202 | 203 | /** 204 | * init LoggerConsumer 205 | * 206 | * @param logDirectory directory of file 207 | * @param fileSize max size of single log file (MByte), default infinite 208 | */ 209 | public TDLoggerConsumer(final String logDirectory, int fileSize) { 210 | this(new Config(logDirectory, fileSize)); 211 | } 212 | 213 | @Override 214 | public synchronized void add(Map message) { 215 | if (isClose) { 216 | TDLogger.println("SDK is already closed"); 217 | return; 218 | } 219 | try { 220 | String formatMsg = JSON.toJSONString(message, DEFAULT_DATE_FORMAT, TDCommonUtil.fastJsonSerializerFeature()); 221 | messageBuffer.append(formatMsg); 222 | messageBuffer.append("\n"); 223 | TDLogger.println("Enqueue data: " + formatMsg); 224 | } catch (JSONException e) { 225 | throw new RuntimeException("Failed to add data", e); 226 | } 227 | 228 | if (messageBuffer.length() >= bufferSize) { 229 | this.flush(); 230 | } 231 | } 232 | @Override 233 | public synchronized void flush() { 234 | if (messageBuffer.length() == 0) { 235 | return; 236 | } 237 | 238 | String fileName = getFileName(); 239 | if (loggerWriter != null && !loggerWriter.getFileName().equals(fileName)) { 240 | LoggerFileWriter.removeInstance(loggerWriter); 241 | loggerWriter = null; 242 | } 243 | 244 | if (loggerWriter == null) { 245 | try { 246 | loggerWriter = LoggerFileWriter.getInstance(fileName, lockFileName); 247 | } catch (FileNotFoundException e) { 248 | TDLogger.println("Error: " + e); 249 | if (isThrowException) { 250 | throw new RuntimeException(e); 251 | } 252 | } 253 | } 254 | 255 | try { 256 | if (loggerWriter.write(messageBuffer)) { 257 | TDLogger.print("flush data: " + messageBuffer); 258 | messageBuffer.setLength(0); 259 | } 260 | } catch (Exception e) { 261 | TDLogger.println("Error: " + e); 262 | if (isThrowException) { 263 | throw new RuntimeException(e); 264 | } 265 | } 266 | } 267 | 268 | private String getFileName() { 269 | String resultPrefix = fileName + df.get().format(new Date()) + "_"; 270 | int count = 0; 271 | String result = resultPrefix + count; 272 | if (fileSize > 0) { 273 | File target = new File(result); 274 | while (target.exists()) { 275 | if ((target.length() / (1024 * 1024)) < fileSize) { 276 | break; 277 | } 278 | result = resultPrefix + (++count); 279 | target = new File(result); 280 | } 281 | } 282 | return result; 283 | } 284 | 285 | @Override 286 | public synchronized void close() { 287 | this.flush(); 288 | if (loggerWriter != null) { 289 | LoggerFileWriter.removeInstance(loggerWriter); 290 | loggerWriter = null; 291 | } 292 | if (autoFlushTimer != null) { 293 | autoFlushTimer.cancel(); 294 | } 295 | isClose = true; 296 | } 297 | 298 | private static class LoggerFileWriter { 299 | private final String fileName; 300 | private final FileOutputStream outputStream; 301 | private final FileOutputStream lockStream; 302 | private int refCount; 303 | 304 | private static final Map instances = new HashMap(); 305 | 306 | static LoggerFileWriter getInstance(final String fileName, final String lockFileName) throws FileNotFoundException { 307 | synchronized (instances) { 308 | if (!instances.containsKey(fileName)) { 309 | instances.put(fileName, new LoggerFileWriter(fileName, lockFileName)); 310 | } 311 | LoggerFileWriter writer = instances.get(fileName); 312 | writer.refCount++; 313 | return writer; 314 | } 315 | } 316 | static void removeInstance(final LoggerFileWriter writer) { 317 | synchronized (instances) { 318 | writer.refCount--; 319 | if (writer.refCount == 0) { 320 | writer.close(); 321 | instances.remove(writer.fileName); 322 | } 323 | } 324 | } 325 | 326 | private LoggerFileWriter(final String fileName, final String lockFileName) throws FileNotFoundException { 327 | this.outputStream = new FileOutputStream(fileName, true); 328 | if (lockFileName != null) { 329 | this.lockStream = new FileOutputStream(lockFileName, true); 330 | } else { 331 | this.lockStream = this.outputStream; 332 | } 333 | 334 | this.fileName = fileName; 335 | this.refCount = 0; 336 | } 337 | 338 | private void close() { 339 | try { 340 | outputStream.close(); 341 | } catch (Exception e) { 342 | throw new RuntimeException("fail to close tga outputStream.", e); 343 | } 344 | } 345 | 346 | String getFileName() { 347 | return this.fileName; 348 | } 349 | 350 | boolean write(final StringBuffer sb) { 351 | synchronized (this.lockStream) { 352 | FileLock lock = null; 353 | try { 354 | final FileChannel channel = lockStream.getChannel(); 355 | lock = channel.lock(0, Long.MAX_VALUE, false); 356 | 357 | outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8)); 358 | } catch (Exception e) { 359 | TDLogger.println(e.getLocalizedMessage()); 360 | throw new RuntimeException("failed to write file.", e); 361 | } finally { 362 | if (lock != null) { 363 | try { 364 | lock.release(); 365 | } catch (IOException e) { 366 | throw new RuntimeException("failed to release tga file lock.", e); 367 | } 368 | } 369 | } 370 | } 371 | return true; 372 | } 373 | } 374 | } 375 | 376 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/ThinkingDataAnalytics.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics; 2 | 3 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 4 | import cn.thinkingdata.analytics.inter.ITDConsumer; 5 | import java.util.*; 6 | 7 | /** 8 | * @deprecated please use TDAnalytics instead 9 | */ 10 | @Deprecated 11 | public class ThinkingDataAnalytics extends TDAnalytics { 12 | /** 13 | * Construct SDK 14 | * 15 | * @param consumer BatchConsumer, LoggerConsumer, DebugConsumer 16 | */ 17 | public ThinkingDataAnalytics(final ITDConsumer consumer) { 18 | this(consumer,false); 19 | } 20 | 21 | /** 22 | * Construct SDK 23 | * 24 | * @param consumer BatchConsumer, LoggerConsumer, DebugConsumer 25 | * @param enableUUID enable uuid 26 | */ 27 | public ThinkingDataAnalytics(final ITDConsumer consumer, final boolean enableUUID) { 28 | this(consumer,enableUUID, consumer instanceof TDDebugConsumer); 29 | } 30 | 31 | /** 32 | * Construct SDK 33 | * 34 | * @param consumer BatchConsumer, LoggerConsumer, DebugConsumer 35 | * @param enableUUID enable uuid 36 | * @param isStrictModel check event data 37 | */ 38 | public ThinkingDataAnalytics(final ITDConsumer consumer, final boolean enableUUID, final boolean isStrictModel) { 39 | super(consumer, enableUUID, isStrictModel); 40 | } 41 | 42 | /** 43 | * Delete a user, This operation is not reversible 44 | * 45 | * @param accountId account ID 46 | * @param distinctId distinct ID 47 | * @throws InvalidArgumentException exception 48 | */ 49 | public void user_del(String accountId, String distinctId) 50 | throws InvalidArgumentException { 51 | userDelete(accountId, distinctId); 52 | } 53 | 54 | /** 55 | * To accumulate operations against the property 56 | * 57 | * @param accountId account ID 58 | * @param distinctId distinct ID 59 | * @param properties properties 60 | * @throws InvalidArgumentException exception 61 | */ 62 | public void user_add(String accountId, String distinctId, Map properties) 63 | throws InvalidArgumentException { 64 | userAdd(accountId, distinctId, properties); 65 | } 66 | 67 | /** 68 | * Set user properties, this message would be neglected If such property had been set before 69 | * 70 | * @param accountId account ID 71 | * @param distinctId distinct ID 72 | * @param properties properties 73 | * @throws InvalidArgumentException exception 74 | */ 75 | public void user_setOnce(String accountId, String distinctId, Map properties) 76 | throws InvalidArgumentException { 77 | userSetOnce(accountId, distinctId, properties); 78 | } 79 | 80 | /** 81 | * Set user properties. would overwrite existing names 82 | * 83 | * @param accountId account ID 84 | * @param distinctId distinct ID 85 | * @param properties properties 86 | * @throws InvalidArgumentException exception 87 | */ 88 | public void user_set(String accountId, String distinctId, Map properties) 89 | throws InvalidArgumentException { 90 | userSet(accountId, distinctId, properties); 91 | } 92 | 93 | /** 94 | * Clear the user properties of users 95 | * 96 | * @param accountId account ID 97 | * @param distinctId distinct ID 98 | * @param properties properties 99 | * @throws InvalidArgumentException exception 100 | */ 101 | public void user_unset(String accountId, String distinctId, String... properties) 102 | throws InvalidArgumentException { 103 | userUnset(accountId, distinctId, properties); 104 | } 105 | 106 | /** 107 | * To add user properties of array type 108 | * 109 | * @param accountId account ID 110 | * @param distinctId distinct ID 111 | * @param properties properties 112 | * @throws InvalidArgumentException exception 113 | */ 114 | public void user_append(String accountId, String distinctId, Map properties) 115 | throws InvalidArgumentException { 116 | userAppend(accountId, distinctId, properties); 117 | } 118 | 119 | /** 120 | * Append user properties to array type by unique. 121 | * 122 | * @param accountId account ID 123 | * @param distinctId distinct ID 124 | * @param properties properties 125 | * @throws InvalidArgumentException exception 126 | */ 127 | public void user_uniqAppend(String accountId, String distinctId, Map properties) 128 | throws InvalidArgumentException { 129 | userUniqAppend(accountId, distinctId, properties); 130 | } 131 | 132 | /** 133 | * Report first event 134 | * @param accountId account ID 135 | * @param distinctId distinct ID 136 | * @param eventName event name 137 | * @param properties properties (must add "#first_check_id" in properties, because it is flag of the first event) 138 | * @throws InvalidArgumentException exception 139 | */ 140 | public void track_first(String accountId, String distinctId, String eventName, Map properties) 141 | throws InvalidArgumentException { 142 | trackFirst(accountId, distinctId, eventName, null, properties); 143 | } 144 | 145 | /** 146 | * Report update event 147 | * 148 | * @param accountId account ID 149 | * @param distinctId distinct ID 150 | * @param eventName event name 151 | * @param eventId event id 152 | * @param properties properties 153 | * @throws InvalidArgumentException exception 154 | */ 155 | public void track_update(String accountId, String distinctId, String eventName, String eventId, Map properties) 156 | throws InvalidArgumentException { 157 | trackUpdate(accountId, distinctId, eventName, eventId, properties); 158 | } 159 | 160 | /** 161 | * Report overwrite event 162 | * 163 | * @param accountId account ID 164 | * @param distinctId distinct ID 165 | * @param eventName event name 166 | * @param eventId event id 167 | * @param properties properties 168 | * @throws InvalidArgumentException exception 169 | */ 170 | public void track_overwrite(String accountId, String distinctId, String eventName, String eventId, Map properties) 171 | throws InvalidArgumentException { 172 | trackOverwrite(accountId, distinctId, eventName, eventId, properties); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/exception/IllegalDataException.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.exception; 2 | 3 | /** 4 | * @author Sun Zeyu 5 | */ 6 | public class IllegalDataException extends RuntimeException { 7 | 8 | public IllegalDataException(String message) { 9 | super(message); 10 | } 11 | 12 | public IllegalDataException(String message, Throwable t) { 13 | super(message, t); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/exception/InvalidArgumentException.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.exception; 2 | 3 | public class InvalidArgumentException extends Exception { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public InvalidArgumentException(String message) { 8 | super(message); 9 | } 10 | 11 | public InvalidArgumentException(Throwable error) { 12 | super(error); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/exception/NeedRetryException.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.exception; 2 | 3 | /** 4 | * @author Sun Zeyu 5 | */ 6 | public class NeedRetryException extends RuntimeException { 7 | 8 | public NeedRetryException(String message) { 9 | super(message); 10 | } 11 | 12 | public NeedRetryException(String message, Throwable t) { 13 | super(message, t); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/DynamicSuperPropertiesTracker.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | 3 | import java.util.Map; 4 | 5 | public interface DynamicSuperPropertiesTracker { 6 | Map getDynamicSuperProperties(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/ITALogger.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | 3 | @Deprecated 4 | public interface ITALogger extends ITDLogger { 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/ITDAnalytics.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | 3 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 4 | 5 | import java.util.Map; 6 | 7 | public interface ITDAnalytics { 8 | /** 9 | * report event 10 | * 11 | * @param accountId account ID 12 | * @param distinctId distinct ID 13 | * @param eventName name 14 | * @param properties properties 15 | * @throws InvalidArgumentException data error 16 | */ 17 | public void track(String accountId, String distinctId, String eventName, Map properties) 18 | throws InvalidArgumentException; 19 | 20 | /** 21 | * first event 22 | * @param accountId account ID 23 | * @param distinctId distinct ID 24 | * @param eventName name 25 | * @param firstCheckId it is flag of the first event 26 | * @param properties properties 27 | * @throws InvalidArgumentException data error 28 | */ 29 | public void trackFirst(String accountId, String distinctId, String eventName, String firstCheckId, Map properties) 30 | throws InvalidArgumentException; 31 | 32 | /** 33 | * @param accountId account ID 34 | * @param distinctId distinct ID 35 | * @param eventName name 36 | * @param eventId event ID 37 | * @param properties properties 38 | * @throws InvalidArgumentException data error 39 | */ 40 | public void trackUpdate(String accountId, String distinctId, String eventName, String eventId, Map properties) 41 | throws InvalidArgumentException; 42 | 43 | /** 44 | * @param accountId account ID 45 | * @param distinctId distinct ID 46 | * @param eventName event name 47 | * @param eventId event ID 48 | * @param properties properties 49 | * @throws InvalidArgumentException data error 50 | */ 51 | public void trackOverwrite(String accountId, String distinctId, String eventName, String eventId, Map properties) 52 | throws InvalidArgumentException; 53 | 54 | /** 55 | * set common properties. it will add to event automatically 56 | * 57 | * @param properties common properties 58 | */ 59 | public void setSuperProperties(Map properties); 60 | 61 | /** 62 | * get super properties 63 | * @return Map 64 | */ 65 | public Map getSuperProperties(); 66 | 67 | /** 68 | * delete property by key 69 | * @param key property name 70 | */ 71 | public void unsetSuperProperties(String key); 72 | 73 | /** 74 | * clear common properties 75 | */ 76 | public void clearSuperProperties(); 77 | 78 | /** 79 | * set common properties dynamically. 80 | * not recommend to add the operation which with a lot of computation 81 | * @param dynamicSuperPropertiesTracker properties 82 | */ 83 | public void setDynamicSuperPropertiesTracker(DynamicSuperPropertiesTracker dynamicSuperPropertiesTracker); 84 | 85 | /** 86 | * delete user (not recommended) 87 | * 88 | * @param accountId account ID 89 | * @param distinctId distinct ID 90 | * @throws InvalidArgumentException data error 91 | */ 92 | public void userDelete(String accountId, String distinctId) 93 | throws InvalidArgumentException; 94 | 95 | /** 96 | * to accumulate operations against the property 97 | * 98 | * @param accountId account ID 99 | * @param distinctId distinct ID 100 | * @param properties properties 101 | * @throws InvalidArgumentException data error 102 | */ 103 | public void userAdd(String accountId, String distinctId, Map properties) 104 | throws InvalidArgumentException; 105 | /** 106 | * set user properties. If such property had been set before, this message would be neglected. 107 | * 108 | * @param accountId account ID 109 | * @param distinctId distinct ID 110 | * @param properties properties 111 | * @throws InvalidArgumentException data error 112 | */ 113 | public void userSetOnce(String accountId, String distinctId, Map properties) 114 | throws InvalidArgumentException; 115 | /** 116 | * set user properties. would overwrite existing names 117 | * 118 | * @param accountId account ID 119 | * @param distinctId distinct ID 120 | * @param properties properties 121 | * @throws InvalidArgumentException data error 122 | */ 123 | public void userSet(String accountId, String distinctId, Map properties) 124 | throws InvalidArgumentException; 125 | /** 126 | * clear the user properties of users 127 | * 128 | * @param accountId account ID 129 | * @param distinctId distinct ID 130 | * @param properties properties 131 | * @throws InvalidArgumentException data error 132 | */ 133 | public void userUnset(String accountId, String distinctId, String... properties) 134 | throws InvalidArgumentException; 135 | /** 136 | * to add user properties of array type 137 | * 138 | * @param accountId account ID 139 | * @param distinctId distinct ID 140 | * @param properties properties 141 | * @throws InvalidArgumentException data error 142 | */ 143 | public void userAppend(String accountId, String distinctId, Map properties) 144 | throws InvalidArgumentException; 145 | /** 146 | * append user properties to array type by unique. 147 | * 148 | * @param accountId account ID 149 | * @param distinctId distinct ID 150 | * @param properties properties 151 | * @throws InvalidArgumentException data error 152 | */ 153 | public void userUniqAppend(String accountId, String distinctId, Map properties) 154 | throws InvalidArgumentException; 155 | 156 | /** 157 | * report data immediately 158 | */ 159 | public void flush(); 160 | 161 | /** 162 | * close and exit sdk 163 | */ 164 | public void close(); 165 | } 166 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/ITDConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | 3 | import java.util.Map; 4 | 5 | public interface ITDConsumer { 6 | /** 7 | * track event 8 | * @param message event map 9 | */ 10 | void add(Map message); 11 | 12 | /** 13 | * upload all data in buffer immediately 14 | */ 15 | void flush(); 16 | 17 | /** 18 | * close consumer 19 | */ 20 | void close(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/ITDLogger.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | 3 | public interface ITDLogger { 4 | /** 5 | * log 6 | * */ 7 | void print(String msg); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/inter/IThinkingDataAnalytics.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.inter; 2 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 3 | 4 | import java.util.Map; 5 | 6 | @Deprecated 7 | public interface IThinkingDataAnalytics { 8 | /** 9 | * report event 10 | * 11 | * @param accountId account ID 12 | * @param distinctId distinct ID 13 | * @param eventName name 14 | * @param properties properties 15 | * @throws InvalidArgumentException data error 16 | */ 17 | public void track(String accountId, String distinctId, String eventName, Map properties) 18 | throws InvalidArgumentException; 19 | 20 | /** 21 | * first event 22 | * @param accountId account ID 23 | * @param distinctId distinct ID 24 | * @param eventName name 25 | * @param properties properties 26 | * (must add "#first_check_id" in properties, because it is flag of the first event) 27 | * @throws InvalidArgumentException data error 28 | */ 29 | public void track_first(String accountId, String distinctId, String eventName, Map properties) 30 | throws InvalidArgumentException; 31 | 32 | /** 33 | * @param accountId account ID 34 | * @param distinctId distinct ID 35 | * @param eventName name 36 | * @param eventId event ID 37 | * @param properties properties 38 | * @throws InvalidArgumentException data error 39 | */ 40 | public void track_update(String accountId, String distinctId, String eventName, String eventId, Map properties) 41 | throws InvalidArgumentException; 42 | 43 | /** 44 | * @param accountId account ID 45 | * @param distinctId distinct ID 46 | * @param eventName event name 47 | * @param eventId event ID 48 | * @param properties properties 49 | * @throws InvalidArgumentException data error 50 | */ 51 | public void track_overwrite(String accountId, String distinctId, String eventName, String eventId, Map properties) 52 | throws InvalidArgumentException; 53 | 54 | /** 55 | * clear common properties 56 | */ 57 | public void clearSuperProperties(); 58 | 59 | /** 60 | * set common properties. it will add to event automatically 61 | * 62 | * @param properties common properties 63 | */ 64 | public void setSuperProperties(Map properties); 65 | 66 | /** 67 | * set common properties dynamically. 68 | * not recommend to add the operation which with a lot of computation 69 | * @param dynamicSuperPropertiesTracker properties 70 | */ 71 | public void setDynamicSuperPropertiesTracker(DynamicSuperPropertiesTracker dynamicSuperPropertiesTracker); 72 | 73 | 74 | /** 75 | * delete user 76 | * 77 | * @param accountId account ID 78 | * @param distinctId distinct ID 79 | * @throws InvalidArgumentException data error 80 | */ 81 | public void user_del(String accountId, String distinctId) 82 | throws InvalidArgumentException; 83 | 84 | /** 85 | * to accumulate operations against the property 86 | * 87 | * @param accountId account ID 88 | * @param distinctId distinct ID 89 | * @param properties properties 90 | * @throws InvalidArgumentException data error 91 | */ 92 | public void user_add(String accountId, String distinctId, Map properties) 93 | throws InvalidArgumentException; 94 | /** 95 | * set user properties. If such property had been set before, this message would be neglected. 96 | * 97 | * @param accountId account ID 98 | * @param distinctId distinct ID 99 | * @param properties properties 100 | * @throws InvalidArgumentException data error 101 | */ 102 | public void user_setOnce(String accountId, String distinctId, Map properties) 103 | throws InvalidArgumentException; 104 | /** 105 | * set user properties. would overwrite existing names 106 | * 107 | * @param accountId account ID 108 | * @param distinctId distinct ID 109 | * @param properties properties 110 | * @throws InvalidArgumentException data error 111 | */ 112 | public void user_set(String accountId, String distinctId, Map properties) 113 | throws InvalidArgumentException; 114 | /** 115 | * clear the user properties of users 116 | * 117 | * @param accountId account ID 118 | * @param distinctId distinct ID 119 | * @param properties properties 120 | * @throws InvalidArgumentException data error 121 | */ 122 | public void user_unset(String accountId, String distinctId, String... properties) 123 | throws InvalidArgumentException; 124 | /** 125 | * to add user properties of array type 126 | * 127 | * @param accountId account ID 128 | * @param distinctId distinct ID 129 | * @param properties properties 130 | * @throws InvalidArgumentException data error 131 | */ 132 | public void user_append(String accountId, String distinctId, Map properties) 133 | throws InvalidArgumentException; 134 | /** 135 | * append user properties to array type by unique. 136 | * 137 | * @param accountId account ID 138 | * @param distinctId distinct ID 139 | * @param properties properties 140 | * @throws InvalidArgumentException data error 141 | */ 142 | public void user_uniqAppend(String accountId, String distinctId, Map properties) 143 | throws InvalidArgumentException; 144 | 145 | /** 146 | * report data immediately 147 | */ 148 | public void flush(); 149 | 150 | /** 151 | * close and exit sdk 152 | */ 153 | public void close(); 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/request/TDBaseRequest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.request; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import org.apache.http.HttpEntity; 5 | import org.apache.http.client.config.RequestConfig; 6 | import org.apache.http.client.methods.HttpPost; 7 | import org.apache.http.impl.client.CloseableHttpClient; 8 | import java.io.Closeable; 9 | import java.net.URI; 10 | 11 | import static cn.thinkingdata.analytics.TDConstData.LIB_VERSION; 12 | 13 | abstract public class TDBaseRequest implements Closeable { 14 | 15 | public URI getServerUri() { 16 | return serverUri; 17 | } 18 | private final URI serverUri; 19 | public String getAppId() { 20 | return appId; 21 | } 22 | private final String appId; 23 | public Integer getConnectTimeout() { 24 | return connectTimeout; 25 | } 26 | private Integer connectTimeout = null; 27 | public CloseableHttpClient getHttpClient() { 28 | return httpClient; 29 | } 30 | private CloseableHttpClient httpClient; 31 | public TDBaseRequest(URI server_uri, String appId, Integer timeout) { 32 | this(server_uri, appId); 33 | this.connectTimeout = timeout; 34 | } 35 | public TDBaseRequest(URI server_uri, String appId) { 36 | httpClient = TDHttpRequestClient.getHttpClient(); 37 | this.serverUri = server_uri; 38 | this.appId = appId; 39 | } 40 | 41 | public synchronized void send(final String data, int dataSize) 42 | { 43 | HttpPost httpPost = new HttpPost(getServerUri()); 44 | HttpEntity params = getHttpEntity(data); 45 | httpPost.setEntity(params); 46 | httpPost.addHeader("appid", getAppId()); 47 | httpPost.addHeader("TA-Integration-Type", "Java"); 48 | httpPost.addHeader("TA-Integration-Version", LIB_VERSION); 49 | httpPost.addHeader("TA-Integration-Count", String.valueOf(dataSize)); 50 | 51 | if (this.getConnectTimeout() != null) { 52 | RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.getConnectTimeout() + 5000).setConnectTimeout(this.getConnectTimeout()).build(); 53 | httpPost.setConfig(requestConfig); 54 | } 55 | sendRequest(httpPost); 56 | } 57 | abstract void sendRequest(HttpPost httpPost); 58 | abstract HttpEntity getHttpEntity(final String data); 59 | abstract void checkingRetCode(JSONObject resultJson); 60 | @Override 61 | public void close() { 62 | httpClient = null; 63 | } 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/request/TDBatchRequest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.request; 2 | 3 | import cn.thinkingdata.analytics.exception.IllegalDataException; 4 | import cn.thinkingdata.analytics.exception.NeedRetryException; 5 | import cn.thinkingdata.analytics.util.TDLogger; 6 | import com.alibaba.fastjson.JSONObject; 7 | import net.jpountz.lz4.LZ4BlockOutputStream; 8 | import net.jpountz.lz4.LZ4Compressor; 9 | import net.jpountz.lz4.LZ4Factory; 10 | import org.anarres.lzo.LzoAlgorithm; 11 | import org.anarres.lzo.LzoCompressor; 12 | import org.anarres.lzo.LzoLibrary; 13 | import org.anarres.lzo.LzoOutputStream; 14 | import org.apache.http.HttpEntity; 15 | import org.apache.http.client.methods.CloseableHttpResponse; 16 | import org.apache.http.client.methods.HttpPost; 17 | import org.apache.http.entity.ByteArrayEntity; 18 | import org.apache.http.util.EntityUtils; 19 | import java.io.ByteArrayOutputStream; 20 | import java.io.IOException; 21 | import java.net.URI; 22 | import java.nio.charset.StandardCharsets; 23 | import java.util.zip.GZIPOutputStream; 24 | public class TDBatchRequest extends TDBaseRequest { 25 | 26 | public String getCompress() { 27 | return compress; 28 | } 29 | 30 | public void setCompress(String compress) { 31 | this.compress = compress; 32 | } 33 | private String compress = "gzip"; 34 | 35 | public TDBatchRequest(URI server_uri, String appId, Integer timeout) { 36 | super(server_uri, appId, timeout); 37 | } 38 | public TDBatchRequest(URI server_uri, String appId) { 39 | super(server_uri, appId); 40 | } 41 | 42 | @Override 43 | void sendRequest(HttpPost httpPost) { 44 | httpPost.addHeader("compress", compress); 45 | for (int i = 0; ; ) { 46 | try (CloseableHttpResponse response = getHttpClient().execute(httpPost)) { 47 | int statusCode = response.getStatusLine().getStatusCode(); 48 | if (statusCode != 200) { 49 | throw new NeedRetryException("Cannot post message to " + getServerUri() + ", status code:" + statusCode); 50 | } 51 | String result = EntityUtils.toString(response.getEntity(), "UTF-8"); 52 | JSONObject resultJson = JSONObject.parseObject(result); 53 | checkingRetCode(resultJson); 54 | return; 55 | } catch (IOException | NeedRetryException e) { 56 | TDLogger.println(e.getLocalizedMessage()); 57 | if (i++ == 2) { 58 | throw new NeedRetryException("Cannot post message to " + getServerUri(), e); 59 | } 60 | } finally { 61 | httpPost.releaseConnection(); 62 | } 63 | } 64 | } 65 | 66 | @Override 67 | HttpEntity getHttpEntity(String data) { 68 | try { 69 | byte[] dataCompressed; 70 | byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); 71 | if ("lzo".equalsIgnoreCase(this.compress)) { 72 | dataCompressed = lzoCompress(dataBytes); 73 | } else if ("lz4".equalsIgnoreCase(this.compress)) { 74 | dataCompressed = lz4Compress(dataBytes); 75 | } else if ("none".equalsIgnoreCase(this.compress)) { 76 | dataCompressed = dataBytes; 77 | } else { 78 | dataCompressed = gzipCompress(dataBytes); 79 | } 80 | return new ByteArrayEntity(dataCompressed); 81 | } catch (IOException e) { 82 | throw new NeedRetryException("compress failed", e); 83 | } 84 | } 85 | 86 | @Override 87 | void checkingRetCode(JSONObject resultJson) { 88 | int retCode = resultJson.getInteger("code"); 89 | TDLogger.println("Response="+resultJson.toString()); 90 | if (retCode != 0) { 91 | if (retCode == -1) { 92 | throw new IllegalDataException(resultJson.containsKey("msg") ? resultJson.getString("msg") : "invalid data format"); 93 | } else if (retCode == -2) { 94 | throw new IllegalDataException(resultJson.containsKey("msg") ? resultJson.getString("msg") : "APP ID doesn't exist"); 95 | } else if (retCode == -3) { 96 | throw new IllegalDataException(resultJson.containsKey("msg") ? resultJson.getString("msg") : "invalid ip transmission"); 97 | } else { 98 | throw new IllegalDataException("Unexpected response return code: " + retCode); 99 | } 100 | } 101 | } 102 | 103 | private static byte[] lz4Compress(byte[] srcBytes) throws IOException { 104 | LZ4Factory factory = LZ4Factory.fastestInstance(); 105 | ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); 106 | LZ4Compressor compressor = factory.fastCompressor(); 107 | LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream( 108 | byteOutput, 2048, compressor); 109 | compressedOutput.write(srcBytes); 110 | compressedOutput.close(); 111 | return byteOutput.toByteArray(); 112 | } 113 | 114 | private static byte[] gzipCompress(byte[] srcBytes) throws IOException { 115 | GZIPOutputStream gzipOut = null; 116 | try { 117 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 118 | gzipOut = new GZIPOutputStream(out); 119 | gzipOut.write(srcBytes); 120 | gzipOut.close(); 121 | return out.toByteArray(); 122 | } finally { 123 | if (gzipOut != null) { 124 | gzipOut.close(); 125 | } 126 | } 127 | 128 | } 129 | private static byte[] lzoCompress(byte[] srcBytes) throws IOException { 130 | LzoCompressor compressor = LzoLibrary.getInstance().newCompressor( 131 | LzoAlgorithm.LZO1X, null); 132 | ByteArrayOutputStream os = new ByteArrayOutputStream(); 133 | LzoOutputStream cs = new LzoOutputStream(os, compressor); 134 | cs.write(srcBytes); 135 | cs.close(); 136 | return os.toByteArray(); 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/request/TDDebugRequest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.request; 2 | 3 | import cn.thinkingdata.analytics.exception.IllegalDataException; 4 | import cn.thinkingdata.analytics.util.TDLogger; 5 | import com.alibaba.fastjson.JSONObject; 6 | import org.apache.http.HttpEntity; 7 | import org.apache.http.NameValuePair; 8 | import org.apache.http.client.entity.UrlEncodedFormEntity; 9 | import org.apache.http.client.methods.CloseableHttpResponse; 10 | import org.apache.http.client.methods.HttpPost; 11 | import org.apache.http.message.BasicNameValuePair; 12 | import org.apache.http.util.EntityUtils; 13 | import java.net.URI; 14 | import java.nio.charset.StandardCharsets; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | public class TDDebugRequest extends TDBaseRequest { 19 | private Boolean writeData = true; 20 | private String deviceId; 21 | public TDDebugRequest(URI server_uri, String appId, Integer timeout) { 22 | super(server_uri, appId, timeout); 23 | } 24 | 25 | public TDDebugRequest(URI server_uri, String appId, boolean writeData, String deviceId) { 26 | super(server_uri, appId); 27 | this.writeData = writeData; 28 | this.deviceId = deviceId; 29 | } 30 | 31 | public TDDebugRequest(URI server_uri, String appId) { 32 | super(server_uri, appId); 33 | } 34 | 35 | @Override 36 | void sendRequest(HttpPost httpPost) { 37 | try (CloseableHttpResponse response = getHttpClient().execute(httpPost)) { 38 | int statusCode = response.getStatusLine().getStatusCode(); 39 | if (statusCode != 200) { 40 | throw new IllegalDataException("Cannot post message to " + this.getServerUri() + ", status code:" + statusCode); 41 | } 42 | String result = EntityUtils.toString(response.getEntity(), "UTF-8"); 43 | TDLogger.println("Response="+result); 44 | JSONObject resultJson = JSONObject.parseObject(result); 45 | checkingRetCode(resultJson); 46 | return; 47 | } catch (Exception e) { 48 | TDLogger.println(e.getLocalizedMessage()); 49 | } finally { 50 | httpPost.releaseConnection(); 51 | } 52 | } 53 | 54 | @Override 55 | HttpEntity getHttpEntity(String data) { 56 | List nameValuePairs = new ArrayList<>(); 57 | nameValuePairs.add(new BasicNameValuePair("source", "server")); 58 | nameValuePairs.add(new BasicNameValuePair("appid", getAppId())); 59 | nameValuePairs.add(new BasicNameValuePair("data", data)); 60 | if (!this.writeData) { 61 | nameValuePairs.add(new BasicNameValuePair("dryRun", String.valueOf(1))); 62 | } 63 | if (this.deviceId != null && this.deviceId.length() > 0) { 64 | nameValuePairs.add(new BasicNameValuePair("deviceId", this.deviceId)); 65 | } 66 | return new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8); 67 | } 68 | 69 | @Override 70 | void checkingRetCode(JSONObject resultJson) { 71 | if (resultJson.getInteger("errorLevel") != 0) { 72 | throw new IllegalDataException(resultJson.toJSONString()); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/request/TDHttpRequestClient.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.request; 2 | 3 | import org.apache.http.client.config.CookieSpecs; 4 | import org.apache.http.client.config.RequestConfig; 5 | import org.apache.http.impl.client.CloseableHttpClient; 6 | import org.apache.http.impl.client.HttpClients; 7 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 8 | public class TDHttpRequestClient { 9 | private static final PoolingHttpClientConnectionManager cm; 10 | private static final RequestConfig globalConfig; 11 | static { 12 | cm = new PoolingHttpClientConnectionManager(); 13 | cm.setDefaultMaxPerRoute(80); 14 | cm.setMaxTotal(100); 15 | globalConfig = RequestConfig.custom() 16 | .setCookieSpec(CookieSpecs.IGNORE_COOKIES) 17 | .setConnectTimeout(30000) 18 | .setSocketTimeout(30000).build(); 19 | } 20 | 21 | public static CloseableHttpClient getHttpClient() { 22 | return HttpClients.custom() 23 | .setConnectionManager(cm) 24 | .setDefaultRequestConfig(globalConfig) 25 | .setConnectionManagerShared(true) 26 | .build(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/util/TDCommonUtil.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.util; 2 | 3 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 4 | import com.alibaba.fastjson2.JSONWriter; 5 | import org.apache.http.util.TextUtils; 6 | 7 | import java.util.Map; 8 | 9 | public class TDCommonUtil { 10 | public static void buildData(Map data, String key, Object value) 11 | { 12 | if(!TextUtils.isEmpty(key) && value != null) 13 | { 14 | data.put(key,value); 15 | } 16 | } 17 | /** 18 | * is value empty or not 19 | * @param value data 20 | * @param exceptionMsg exception 21 | * */ 22 | public static void throwEmptyException (String value,String exceptionMsg) throws InvalidArgumentException { 23 | if(TextUtils.isEmpty(value)) 24 | throw new InvalidArgumentException(exceptionMsg); 25 | } 26 | 27 | public static JSONWriter.Feature[] fastJsonSerializerFeature() { 28 | return new JSONWriter.Feature[]{ 29 | JSONWriter.Feature.WriteEnumsUsingName, 30 | JSONWriter.Feature.MapSortField 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/util/TDLogger.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.util; 2 | 3 | import cn.thinkingdata.analytics.inter.ITDLogger; 4 | import cn.thinkingdata.analytics.TDConstData; 5 | 6 | public class TDLogger { 7 | private static ITDLogger loggerInstance; 8 | private static boolean isPrintLog = false; 9 | 10 | public static void enableLog(boolean isPrint) { 11 | isPrintLog = isPrint; 12 | } 13 | 14 | public static void setLogger(ITDLogger logger) { 15 | loggerInstance = logger; 16 | } 17 | 18 | public static void print(String msg) { 19 | if (isPrintLog) { 20 | String formatMsg = String.format("[ThinkingData] %s", msg); 21 | if (loggerInstance != null) { 22 | loggerInstance.print(formatMsg); 23 | } else { 24 | System.out.print(formatMsg); 25 | } 26 | } 27 | } 28 | 29 | public static void println(String msg) { 30 | if (isPrintLog) { 31 | String formatMsg = String.format("%s\n", msg); 32 | TDLogger.print(formatMsg); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/cn/thinkingdata/analytics/util/TDPropertyUtil.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.util; 2 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 3 | import org.apache.http.util.TextUtils; 4 | 5 | import java.util.Map; 6 | import static cn.thinkingdata.analytics.TDConstData.*; 7 | public class TDPropertyUtil { 8 | /** 9 | * check properties 10 | * @param properties properties 11 | * @param type event type 12 | * */ 13 | public static void assertProperties(final Map properties,final DataType type) throws InvalidArgumentException { 14 | if (properties.size() == 0) { 15 | return; 16 | } 17 | for (Map.Entry property : properties.entrySet()) { 18 | Object value = property.getValue(); 19 | if (null == value) { 20 | continue; 21 | } 22 | if (! isValidKey(property.getKey())) { 23 | throw new InvalidArgumentException("Invalid key format: " + property.getKey()); 24 | } 25 | if(type == DataType.USER_ADD) 26 | { 27 | if ( ! (value instanceof Number)) 28 | { 29 | throw new InvalidArgumentException("Only Number is allowed,Invalid property value " + value); 30 | } 31 | } 32 | } 33 | } 34 | /** 35 | * check property name 36 | * @param key key 37 | * */ 38 | public static boolean isValidKey(String key) 39 | { 40 | if(TextUtils.isEmpty(key)) 41 | { 42 | return false; 43 | } 44 | return KEY_PATTERN.matcher(key).matches(); 45 | } 46 | 47 | /** 48 | * combine properties by sort. The latter has the highest priority 49 | * @param desProperties target 50 | * @param sourceProperties source 51 | * */ 52 | @SafeVarargs 53 | public static void mergeProperties(Map desProperties,Map ... sourceProperties) throws InvalidArgumentException { 54 | for(Map sourceProperty:sourceProperties) 55 | { 56 | if(sourceProperty != null) 57 | { 58 | desProperties.putAll(sourceProperty); 59 | } 60 | } 61 | } 62 | /** 63 | * Move special properties 64 | * @param data target 65 | * @param properties source 66 | * @param propertyKeys properties key 67 | * */ 68 | public static void moveProperty(Map data,Map properties,String... propertyKeys) 69 | { 70 | for (String propertyKey:propertyKeys) 71 | { 72 | if(properties.containsKey(propertyKey)) 73 | { 74 | data.put(propertyKey, properties.get(propertyKey)); 75 | properties.remove(propertyKey); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/LoggerConsumerTest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test; 2 | 3 | import cn.thinkingdata.analytics.*; 4 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class LoggerConsumerTest { 12 | TDAnalytics taSDK; 13 | 14 | @Before 15 | public void init() { 16 | TDAnalytics.enableLog(true); 17 | 18 | TDLoggerConsumer.Config config = new TDLoggerConsumer.Config("./log"); 19 | config.setAutoFlush(true); 20 | config.setBufferSize(1024); 21 | 22 | taSDK = new TDAnalytics(new TDLoggerConsumer(config), false, false); 23 | } 24 | 25 | @Test 26 | public void testLogConsumer() { 27 | try { 28 | Map properties = new HashMap(); 29 | properties.put("name", "ta"); 30 | 31 | for (int i = 0; i < 20; i++) { 32 | taSDK.track("123", null, "java_event", properties); 33 | } 34 | taSDK.flush(); 35 | } catch (InvalidArgumentException e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/functionTest/AssembleEventTest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test.functionTest; 2 | 3 | import cn.thinkingdata.analytics.TDAnalytics; 4 | import cn.thinkingdata.analytics.exception.InvalidArgumentException; 5 | import org.apache.http.util.TextUtils; 6 | import org.junit.After; 7 | import org.junit.Assert; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import java.util.*; 12 | 13 | interface ISDKCase { 14 | T doAction(); 15 | } 16 | 17 | interface ISDKTest { 18 | void run(); 19 | 20 | ISDKTest addVerify(ISDKVerify userIDVerify); 21 | } 22 | 23 | interface ISDKVerify { 24 | void verify(T e); 25 | } 26 | 27 | class SDKTest implements ISDKTest { 28 | List> mCases = new ArrayList<>(); 29 | List mVerifies = new ArrayList<>(); 30 | 31 | public SDKTest addCase(ISDKCase _case) { 32 | this.mCases.add(_case); 33 | return this; 34 | } 35 | 36 | public SDKTest addVerify(ISDKVerify userIDVerify) { 37 | if (userIDVerify != null) { 38 | mVerifies.add(userIDVerify); 39 | } 40 | return this; 41 | } 42 | 43 | @Override 44 | public void run() { 45 | for (ISDKCase _case : mCases) { 46 | T e = _case.doAction(); 47 | for (ISDKVerify verify : mVerifies) { 48 | verify.verify(e); 49 | } 50 | } 51 | } 52 | 53 | public void reset() { 54 | mCases.clear(); 55 | mVerifies.clear(); 56 | } 57 | } 58 | 59 | public class AssembleEventTest { 60 | private TDAnalytics sdk; 61 | private TemporaryConsumer consumer; 62 | private boolean isStrictModel = false; 63 | 64 | @Before 65 | public void init() { 66 | System.out.println("before"); 67 | consumer = new TemporaryConsumer(); 68 | sdk = new TDAnalytics(consumer, false, isStrictModel); 69 | } 70 | 71 | @After 72 | public void after() { 73 | System.out.println("After"); 74 | } 75 | 76 | void setUpEventCase(final String accountId, final String distinctId, final String eventName, 77 | final Map properties, SDKTest sdkTest) { 78 | this.setUpEventCase(accountId, distinctId, eventName, properties, sdkTest, null, null, null, null); 79 | } 80 | 81 | void setUpEventCase(final String accountId, final String distinctId, final String eventName, 82 | final Map properties, SDKTest sdkTest, final T... result) { 83 | sdkTest.addCase(new ISDKCase() { 84 | @Override 85 | public T doAction() { 86 | try { 87 | System.out.println("[track]properties=" + properties); 88 | sdk.track(accountId, distinctId, eventName, properties); 89 | } catch (InvalidArgumentException e) { 90 | return (T) e; 91 | } 92 | return result[0]; 93 | } 94 | }).addCase(new ISDKCase() { 95 | @Override 96 | public T doAction() { 97 | try { 98 | System.out.println("[trackUpdate]properties=" + properties); 99 | sdk.trackUpdate(accountId, distinctId, eventName, "eventId", properties); 100 | } catch (InvalidArgumentException e) { 101 | return (T) e; 102 | } 103 | return result[1]; 104 | } 105 | }).addCase(new ISDKCase() { 106 | @Override 107 | public T doAction() { 108 | try { 109 | System.out.println("[trackOverwrite]properties=" + properties); 110 | sdk.trackOverwrite(accountId, distinctId, eventName, "eventId", properties); 111 | } catch (InvalidArgumentException e) { 112 | return (T) e; 113 | } 114 | return result[2]; 115 | } 116 | }).addCase(new ISDKCase() { 117 | @Override 118 | public T doAction() { 119 | try { 120 | if (!properties.containsKey("#first_check_id")) { 121 | properties.put("#first_check_id", "firstCheckId"); 122 | } 123 | System.out.println("[trackFirst]properties=" + properties); 124 | sdk.trackFirst(accountId, distinctId, eventName, "firstCheckId", properties); 125 | properties.remove("#first_check_id"); 126 | } catch (InvalidArgumentException e) { 127 | return (T) e; 128 | } 129 | return result[3]; 130 | } 131 | }); 132 | } 133 | 134 | void setUpUserPropertyCase(final String accountId, final String distinctId, final Map properties, 135 | SDKTest sdkTest) { 136 | this.setUpUserPropertyCase(accountId, distinctId, properties, sdkTest, null, null, null, null, null, 137 | null, null); 138 | } 139 | 140 | void setUpUserPropertyCase(final String accountId, final String distinctId, 141 | final Map properties, SDKTest sdkTest, final T... result) { 142 | sdkTest.addCase(new ISDKCase() { 143 | @Override 144 | public T doAction() { 145 | try { 146 | System.out.println("[userSet]properties=" + properties); 147 | sdk.userSet(accountId, distinctId, properties); 148 | } catch (InvalidArgumentException e) { 149 | return (T) e; 150 | } 151 | return result[0]; 152 | } 153 | }).addCase(new ISDKCase() { 154 | @Override 155 | public T doAction() { 156 | try { 157 | System.out.println("[userSetOnce]properties=" + properties); 158 | sdk.userSetOnce(accountId, distinctId, properties); 159 | } catch (InvalidArgumentException e) { 160 | return (T) e; 161 | } 162 | return result[1]; 163 | } 164 | }).addCase(new ISDKCase() { 165 | @Override 166 | public T doAction() { 167 | try { 168 | Map appendProperties = new HashMap<>(); 169 | appendProperties.put("list", Arrays.asList("a", "b", "c")); 170 | appendProperties.putAll(properties); 171 | System.out.println("[userAppend]properties=" + properties); 172 | sdk.userAppend(accountId, distinctId, appendProperties); 173 | 174 | } catch (InvalidArgumentException e) { 175 | return (T) e; 176 | } 177 | return result[2]; 178 | } 179 | }).addCase(new ISDKCase() { 180 | @Override 181 | public T doAction() { 182 | try { 183 | System.out.println("[user_unset]properties=" + properties); 184 | sdk.userUnset(accountId, distinctId, "a"); 185 | } catch (InvalidArgumentException e) { 186 | return (T) e; 187 | } 188 | return result[3]; 189 | } 190 | }).addCase(new ISDKCase() { 191 | @Override 192 | public T doAction() { 193 | Map addProperties = new HashMap<>(); 194 | addProperties.put("age", 1); 195 | for (String key : properties.keySet()) { 196 | Object value = properties.get(key); 197 | if (value instanceof Number || key.startsWith("#")) { 198 | addProperties.put(key, value); 199 | } 200 | } 201 | 202 | try { 203 | System.out.println("[userAdd]properties=" + properties); 204 | sdk.userAdd(accountId, distinctId, addProperties); 205 | } catch (InvalidArgumentException e) { 206 | return (T) e; 207 | } 208 | return result[4]; 209 | } 210 | }).addCase(new ISDKCase() { 211 | @Override 212 | public T doAction() { 213 | try { 214 | System.out.println("[user_del]properties=" + properties); 215 | sdk.userDelete(accountId, distinctId); 216 | } catch (InvalidArgumentException e) { 217 | return (T) e; 218 | } 219 | return result[5]; 220 | } 221 | }).addCase(new ISDKCase() { 222 | @Override 223 | public T doAction() { 224 | try { 225 | Map appendProperties = new HashMap<>(); 226 | appendProperties.put("list", Arrays.asList("a", "b", "c")); 227 | appendProperties.putAll(properties); 228 | System.out.println("[user_uniqAppend]properties=" + properties); 229 | sdk.userUniqAppend(accountId, distinctId, appendProperties); 230 | } catch (InvalidArgumentException e) { 231 | return (T) e; 232 | } 233 | return result[6]; 234 | } 235 | }); 236 | } 237 | 238 | SDKTest setUpUserAdd(final String accountId, final String distinctId, final Map properties, 239 | SDKTest sdkTest, final T result) { 240 | sdkTest.addCase(new ISDKCase() { 241 | @Override 242 | public Object doAction() { 243 | try { 244 | sdk.userAdd(accountId, distinctId, properties); 245 | } catch (InvalidArgumentException e) { 246 | return (T) e; 247 | } 248 | return result; 249 | } 250 | }); 251 | return sdkTest; 252 | } 253 | 254 | void initUserIDCase(final String accountId, final String distinctId, final String eventName, SDKTest sdkTest) { 255 | Map properties = new HashMap<>(); 256 | properties.put("age", 23); 257 | properties.put("sex", "male"); 258 | setUpEventCase(accountId, distinctId, eventName, properties, sdkTest); 259 | setUpUserPropertyCase(accountId, distinctId, properties, sdkTest); 260 | } 261 | 262 | @Test 263 | public void testEmptyAccount_EmptyDistinctId() { 264 | ISDKVerify emptyVerify = new ISDKVerify() { 265 | @Override 266 | public void verify(Exception e) { 267 | if (isStrictModel) { 268 | Assert.assertNotNull(e); 269 | Assert.assertEquals("accountId or distinctId must be provided.", e.getMessage()); 270 | } 271 | 272 | } 273 | }; 274 | SDKTest test = new SDKTest(); 275 | initUserIDCase("", "", "eventName", test); 276 | test.addVerify(emptyVerify); 277 | test.run(); 278 | test.reset(); 279 | } 280 | 281 | @Test 282 | public void testEmptyDistinctId() { 283 | ISDKVerify onlyAccountIdVerity = new ISDKVerify() { 284 | @Override 285 | public void verify(Exception e) { 286 | Assert.assertNull(e); 287 | Assert.assertEquals("accountId", consumer.getTaData().getAccountId()); 288 | Assert.assertTrue(TextUtils.isEmpty(consumer.getTaData().getDistinctId())); 289 | } 290 | }; 291 | SDKTest test = new SDKTest(); 292 | initUserIDCase("accountId", "", "eventName", test); 293 | test.addVerify(onlyAccountIdVerity); 294 | test.run(); 295 | test.reset(); 296 | } 297 | 298 | @Test 299 | public void testEmptyAccountId() { 300 | ISDKVerify onlyDistinctIdVerity = new ISDKVerify() { 301 | @Override 302 | public void verify(Exception e) { 303 | Assert.assertNull(e); 304 | Assert.assertEquals("distinctId", consumer.getTaData().getDistinctId()); 305 | Assert.assertTrue(TextUtils.isEmpty(consumer.getTaData().getAccountId())); 306 | } 307 | }; 308 | SDKTest test = new SDKTest(); 309 | initUserIDCase("", "distinctId", "eventName", test); 310 | test.addVerify(onlyDistinctIdVerity); 311 | test.run(); 312 | test.reset(); 313 | } 314 | 315 | @Test 316 | public void testDistinctIdAndAccountId() { 317 | ISDKVerify accountIdAndDistinctIdVerity = new ISDKVerify() { 318 | @Override 319 | public void verify(Exception e) { 320 | Assert.assertNull(e); 321 | Assert.assertEquals("accountId", consumer.getTaData().getAccountId()); 322 | Assert.assertEquals("distinctId", consumer.getTaData().getDistinctId()); 323 | } 324 | }; 325 | SDKTest test = new SDKTest(); 326 | initUserIDCase("accountId", "distinctId", "eventName", test); 327 | test.addVerify(accountIdAndDistinctIdVerity); 328 | test.run(); 329 | test.reset(); 330 | } 331 | 332 | @Test 333 | public void testEmptyEventName() { 334 | ISDKVerify eventNameVerify = new ISDKVerify() { 335 | @Override 336 | public void verify(Exception e) { 337 | if (isStrictModel) { 338 | Assert.assertNotNull(e); 339 | Assert.assertEquals("The event name must be provided.", e.getMessage()); 340 | } 341 | 342 | } 343 | }; 344 | SDKTest test = new SDKTest(); 345 | setUpEventCase("accountId", "", "", new HashMap(), test); 346 | test.addVerify(eventNameVerify); 347 | test.run(); 348 | test.reset(); 349 | } 350 | 351 | @Test 352 | public void testEventName() { 353 | ISDKVerify eventNameVerify = new ISDKVerify() { 354 | @Override 355 | public void verify(Exception e) { 356 | Assert.assertNull(e); 357 | Assert.assertEquals("eventName", consumer.getTaData().getEventName()); 358 | } 359 | }; 360 | SDKTest test = new SDKTest(); 361 | setUpEventCase("accountId", "", "eventName", new HashMap(), test); 362 | test.addVerify(eventNameVerify); 363 | test.run(); 364 | test.reset(); 365 | } 366 | 367 | @Test 368 | public void testEmptyEventId() { 369 | ISDKVerify eventIdVerify = new ISDKVerify() { 370 | @Override 371 | public void verify(Exception e) { 372 | if (isStrictModel) { 373 | Assert.assertNotNull(e); 374 | Assert.assertEquals("The event id must be provided.", e.getMessage()); 375 | } 376 | } 377 | }; 378 | SDKTest test = new SDKTest(); 379 | test.addCase(new ISDKCase() { 380 | @Override 381 | public Exception doAction() { 382 | try { 383 | sdk.trackUpdate("accountId", "", "eventName", "", new HashMap()); 384 | } catch (InvalidArgumentException e) { 385 | return e; 386 | } 387 | return null; 388 | } 389 | }); 390 | test.addCase(new ISDKCase() { 391 | @Override 392 | public Exception doAction() { 393 | try { 394 | sdk.trackOverwrite("accountId", "", "eventName", "", new HashMap()); 395 | } catch (InvalidArgumentException e) { 396 | return e; 397 | } 398 | return null; 399 | } 400 | }); 401 | test.addVerify(eventIdVerify); 402 | test.run(); 403 | test.reset(); 404 | 405 | test.addCase(new ISDKCase() { 406 | @Override 407 | public Exception doAction() { 408 | try { 409 | sdk.trackFirst("accountId", "", "eventName", null, new HashMap()); 410 | } catch (InvalidArgumentException e) { 411 | return e; 412 | } 413 | return null; 414 | } 415 | }); 416 | test.addVerify(new ISDKVerify() { 417 | @Override 418 | public void verify(Exception e) { 419 | Assert.assertNotNull(e); 420 | Assert.assertEquals("#first_check_id key must set", e.getMessage()); 421 | } 422 | }); 423 | test.run(); 424 | } 425 | 426 | @Test 427 | public void testEventId() { 428 | ISDKVerify eventIdVerify = new ISDKVerify() { 429 | @Override 430 | public void verify(Exception e) { 431 | Assert.assertNull(e); 432 | Assert.assertEquals("eventId", consumer.getTaData().getEventId()); 433 | } 434 | }; 435 | SDKTest test = new SDKTest(); 436 | test.addCase(new ISDKCase() { 437 | @Override 438 | public Exception doAction() { 439 | try { 440 | sdk.trackUpdate("accountId", "", "eventName", "eventId", new HashMap()); 441 | } catch (InvalidArgumentException e) { 442 | return e; 443 | } 444 | return null; 445 | } 446 | }); 447 | test.addCase(new ISDKCase() { 448 | @Override 449 | public Exception doAction() { 450 | try { 451 | sdk.trackOverwrite("accountId", "", "eventName", "eventId", new HashMap()); 452 | } catch (InvalidArgumentException e) { 453 | return e; 454 | } 455 | return null; 456 | } 457 | }); 458 | test.addVerify(eventIdVerify); 459 | test.run(); 460 | test.reset(); 461 | 462 | test.addCase(new ISDKCase() { 463 | @Override 464 | public Exception doAction() { 465 | try { 466 | sdk.trackFirst("accountId", "", "eventName", "eventId", new HashMap() { 467 | { 468 | put("#first_check_id", "eventId"); 469 | } 470 | }); 471 | } catch (InvalidArgumentException e) { 472 | return e; 473 | } 474 | return null; 475 | } 476 | }); 477 | test.addVerify(new ISDKVerify() { 478 | @Override 479 | public void verify(Exception e) { 480 | Assert.assertNull(e); 481 | Assert.assertEquals("eventId", consumer.getTaData().getFirstCheckId()); 482 | } 483 | }); 484 | test.run(); 485 | } 486 | 487 | @Test 488 | public void testType() { 489 | ISDKVerify eventTypeVerify = new ISDKVerify() { 490 | @Override 491 | public void verify(String result) { 492 | Assert.assertEquals(result, consumer.getTaData().getType()); 493 | } 494 | }; 495 | SDKTest test = new SDKTest(); 496 | setUpEventCase("accountId", "", "eventName", new HashMap(), test, "track", "track_update", 497 | "track_overwrite", "track"); 498 | setUpUserPropertyCase("accountId", "", new HashMap(), test, "user_set", "user_setOnce", 499 | "user_append", "user_unset", "user_add", "user_del", "user_uniq_append"); 500 | test.addVerify(eventTypeVerify); 501 | test.run(); 502 | } 503 | 504 | @Test 505 | public void testTimeProperty() { 506 | ISDKVerify timeVerify = new ISDKVerify() { 507 | @Override 508 | public void verify(Date result) { 509 | if (!consumer.getTaData().getType().equals("user_del") 510 | && !consumer.getTaData().getType().equals("user_unset")) { 511 | Assert.assertEquals(result.toString(), consumer.getTaData().getTime().toString()); 512 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#time")); 513 | } 514 | } 515 | }; 516 | final SDKTest test = new SDKTest(); 517 | final Date date = new Date(1657624662000L); 518 | setUpEventCase("accountId", "", "eventName", new HashMap() { 519 | { 520 | put("#time", date); 521 | } 522 | }, test, date, date, date, date); 523 | setUpUserPropertyCase("accountId", "", new HashMap() { 524 | { 525 | put("#time", date); 526 | } 527 | }, test, date, date, date, date, date, date, date); 528 | test.addVerify(timeVerify); 529 | test.run(); 530 | } 531 | 532 | @Test 533 | public void testPresetProperty() { 534 | Map properties = new HashMap<>(); 535 | properties.put("age", 23); 536 | properties.put("sex", "male"); 537 | final Date date = new Date(); 538 | properties.put("#time", date); 539 | final String uuid = UUID.randomUUID().toString(); 540 | properties.put("#uuid", uuid); 541 | final String ip = "192.168.10.10"; 542 | properties.put("#ip", ip); 543 | final String appId = "testAppid"; 544 | properties.put("#app_id", appId); 545 | final String firstCheckId = "firstCheckId"; 546 | properties.put("#first_check_id", firstCheckId); 547 | final String transactionProperty = "transactionProperty"; 548 | properties.put("#transaction_property", transactionProperty); 549 | final String importToolId = "importToolId"; 550 | properties.put("#import_tool_id", importToolId); 551 | 552 | ISDKVerify timeVerify = new ISDKVerify() { 553 | @Override 554 | public void verify(Date result) { 555 | if (!consumer.getTaData().getType().equals("user_del") 556 | && !consumer.getTaData().getType().equals("user_unset")) { 557 | if (!consumer.getTaData().getType().equals("user_add")) { 558 | Assert.assertEquals("male", consumer.getTaData().getPropertyObj().get("sex")); 559 | } 560 | Assert.assertEquals(date, consumer.getTaData().getTime()); 561 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#time")); 562 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 563 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#uuid")); 564 | Assert.assertEquals(ip, consumer.getTaData().getIp()); 565 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#ip")); 566 | Assert.assertEquals(appId, consumer.getTaData().getAppId()); 567 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#app_id")); 568 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#firstCheckId")); 569 | Assert.assertEquals(transactionProperty, consumer.getTaData().getTransactionProperty()); 570 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#transaction_property")); 571 | Assert.assertEquals(importToolId, consumer.getTaData().getImportToolId()); 572 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("#import_tool_id")); 573 | Assert.assertEquals(23, consumer.getTaData().getPropertyObj().get("age")); 574 | } 575 | if (consumer.getTaData().getFirstCheckId() != null) { 576 | Assert.assertEquals(firstCheckId, consumer.getTaData().getFirstCheckId()); 577 | } 578 | } 579 | }; 580 | final SDKTest test = new SDKTest(); 581 | setUpEventCase("accountId", "", "eventName", properties, test, date, date, date, date); 582 | setUpUserPropertyCase("accountId", "", properties, test, date, date, date, date, date, date, date); 583 | test.addVerify(timeVerify); 584 | test.run(); 585 | } 586 | 587 | @Test 588 | public void testUserAdd() { 589 | final SDKTest test = new SDKTest(); 590 | this.setUpUserAdd("accountId", "", new HashMap() { 591 | { 592 | put("a", "b"); 593 | } 594 | }, test, null); 595 | test.addVerify(new ISDKVerify() { 596 | @Override 597 | public void verify(Exception e) { 598 | if (isStrictModel) { 599 | Assert.assertNotNull(e); 600 | Assert.assertEquals("Only Number is allowed,Invalid property value b", e.getMessage()); 601 | } 602 | } 603 | }); 604 | test.run(); 605 | } 606 | @Test 607 | public void testUUID() { 608 | init(); 609 | // not open #uuid, and don't input #uuid. final data can't incloud #uuid 610 | Map properties = new HashMap<>(); 611 | properties.put("age", 23); 612 | properties.put("sex", "male"); 613 | Exception expect = null; 614 | try { 615 | sdk.track("accountId", "distinctId", "eventName", properties); 616 | } catch (Exception e) { 617 | expect = e; 618 | } 619 | Assert.assertNull(expect); 620 | Assert.assertNull(consumer.getTaData().getUuid()); 621 | 622 | expect = null; 623 | try { 624 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 625 | } catch (Exception e) { 626 | expect = e; 627 | } 628 | Assert.assertNull(expect); 629 | Assert.assertNull(consumer.getTaData().getUuid()); 630 | 631 | expect = null; 632 | try { 633 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 634 | } catch (Exception e) { 635 | expect = e; 636 | } 637 | Assert.assertNull(expect); 638 | Assert.assertNull(consumer.getTaData().getUuid()); 639 | 640 | expect = null; 641 | try { 642 | sdk.userSet("accountId", "distinctId", properties); 643 | } catch (Exception e) { 644 | expect = e; 645 | } 646 | Assert.assertNull(expect); 647 | Assert.assertNull(consumer.getTaData().getUuid()); 648 | 649 | expect = null; 650 | try { 651 | sdk.userSetOnce("accountId", "distinctId", properties); 652 | } catch (Exception e) { 653 | expect = e; 654 | } 655 | Assert.assertNull(expect); 656 | Assert.assertNull(consumer.getTaData().getUuid()); 657 | 658 | expect = null; 659 | Map appendProperties = new HashMap<>(); 660 | appendProperties.put("list", Arrays.asList("a", "b", "c")); 661 | try { 662 | sdk.userAppend("accountId", "distinctId", appendProperties); 663 | } catch (Exception e) { 664 | expect = e; 665 | } 666 | Assert.assertNull(expect); 667 | Assert.assertNull(consumer.getTaData().getUuid()); 668 | 669 | expect = null; 670 | Map addProperties = new HashMap<>(); 671 | addProperties.put("age", 1); 672 | try { 673 | sdk.userAdd("accountId", "distinctId", addProperties); 674 | } catch (Exception e) { 675 | expect = e; 676 | } 677 | Assert.assertNull(expect); 678 | Assert.assertNull(consumer.getTaData().getUuid()); 679 | 680 | String uuid = UUID.randomUUID().toString(); 681 | properties.put("#uuid", uuid); 682 | expect = null; 683 | try { 684 | sdk.track("accountId", "distinctId", "eventName", properties); 685 | } catch (Exception e) { 686 | expect = e; 687 | } 688 | Assert.assertNull(expect); 689 | System.out.println("uuid="+consumer.getTaData().getUuid()); 690 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 691 | 692 | expect = null; 693 | try { 694 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 695 | } catch (Exception e) { 696 | expect = e; 697 | } 698 | Assert.assertNull(expect); 699 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 700 | 701 | expect = null; 702 | try { 703 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 704 | } catch (Exception e) { 705 | expect = e; 706 | } 707 | Assert.assertNull(expect); 708 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 709 | 710 | expect = null; 711 | try { 712 | sdk.userSet("accountId", "distinctId", properties); 713 | } catch (Exception e) { 714 | expect = e; 715 | } 716 | Assert.assertNull(expect); 717 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 718 | 719 | expect = null; 720 | try { 721 | sdk.userSetOnce("accountId", "distinctId", properties); 722 | } catch (Exception e) { 723 | expect = e; 724 | } 725 | Assert.assertNull(expect); 726 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 727 | 728 | expect = null; 729 | appendProperties.put("#uuid", uuid); 730 | try { 731 | sdk.userAppend("accountId", "distinctId", appendProperties); 732 | } catch (Exception e) { 733 | expect = e; 734 | } 735 | Assert.assertNull(expect); 736 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 737 | 738 | expect = null; 739 | addProperties.put("#uuid", uuid); 740 | try { 741 | sdk.userAdd("accountId", "distinctId", addProperties); 742 | } catch (Exception e) { 743 | expect = e; 744 | } 745 | Assert.assertNull(expect); 746 | Assert.assertEquals(uuid, consumer.getTaData().getUuid()); 747 | 748 | properties.remove("#uuid"); 749 | sdk = new TDAnalytics(consumer, true); 750 | expect = null; 751 | try { 752 | sdk.track("accountId", "distinctId", "eventName", properties); 753 | } catch (Exception e) { 754 | expect = e; 755 | } 756 | Assert.assertNull(expect); 757 | Assert.assertNotNull(consumer.getTaData().getUuid()); 758 | 759 | expect = null; 760 | try { 761 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 762 | } catch (Exception e) { 763 | expect = e; 764 | } 765 | Assert.assertNull(expect); 766 | Assert.assertNotNull(consumer.getTaData().getUuid()); 767 | 768 | expect = null; 769 | try { 770 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 771 | } catch (Exception e) { 772 | expect = e; 773 | } 774 | Assert.assertNull(expect); 775 | Assert.assertNotNull(consumer.getTaData().getUuid()); 776 | 777 | expect = null; 778 | try { 779 | sdk.userSet("accountId", "distinctId", properties); 780 | } catch (Exception e) { 781 | expect = e; 782 | } 783 | Assert.assertNull(expect); 784 | Assert.assertNotNull(consumer.getTaData().getUuid()); 785 | 786 | expect = null; 787 | try { 788 | sdk.userSetOnce("accountId", "distinctId", properties); 789 | } catch (Exception e) { 790 | expect = e; 791 | } 792 | Assert.assertNull(expect); 793 | Assert.assertNotNull(consumer.getTaData().getUuid()); 794 | 795 | expect = null; 796 | appendProperties.remove("#uuid"); 797 | try { 798 | sdk.userAppend("accountId", "distinctId", appendProperties); 799 | } catch (Exception e) { 800 | expect = e; 801 | } 802 | Assert.assertNull(expect); 803 | Assert.assertNotNull(consumer.getTaData().getUuid()); 804 | 805 | expect = null; 806 | addProperties.remove("#uuid"); 807 | try { 808 | sdk.userAdd("accountId", "distinctId", addProperties); 809 | } catch (Exception e) { 810 | expect = e; 811 | } 812 | Assert.assertNull(expect); 813 | Assert.assertNotNull(consumer.getTaData().getUuid()); 814 | } 815 | 816 | @Test 817 | public void testPublicProperties() { 818 | init(); 819 | Map superProperties = new HashMap<>(); 820 | superProperties.put("test", "public"); 821 | sdk.setSuperProperties(superProperties); 822 | // move #uuid, #ip, #time, #app_id to out 823 | Map properties = new HashMap<>(); 824 | properties.put("age", 23); 825 | properties.put("sex", "male"); 826 | 827 | Exception expect = null; 828 | try { 829 | sdk.track("accountId", "distinctId", "eventName", properties); 830 | } catch (Exception e) { 831 | expect = e; 832 | } 833 | Assert.assertNull(expect); 834 | Assert.assertEquals("public", consumer.getTaData().getPropertyObj().get("test")); 835 | 836 | expect = null; 837 | try { 838 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 839 | } catch (Exception e) { 840 | expect = e; 841 | } 842 | Assert.assertNull(expect); 843 | Assert.assertEquals("public", consumer.getTaData().getPropertyObj().get("test")); 844 | 845 | expect = null; 846 | try { 847 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 848 | } catch (Exception e) { 849 | expect = e; 850 | } 851 | Assert.assertNull(expect); 852 | Assert.assertEquals("public", consumer.getTaData().getPropertyObj().get("test")); 853 | 854 | properties.put("test", "private"); 855 | 856 | expect = null; 857 | try { 858 | sdk.track("accountId", "distinctId", "eventName", properties); 859 | } catch (Exception e) { 860 | expect = e; 861 | } 862 | Assert.assertNull(expect); 863 | Assert.assertEquals("private", consumer.getTaData().getPropertyObj().get("test")); 864 | 865 | expect = null; 866 | try { 867 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 868 | } catch (Exception e) { 869 | expect = e; 870 | } 871 | Assert.assertNull(expect); 872 | Assert.assertEquals("private", consumer.getTaData().getPropertyObj().get("test")); 873 | 874 | expect = null; 875 | try { 876 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 877 | } catch (Exception e) { 878 | expect = e; 879 | } 880 | Assert.assertNull(expect); 881 | Assert.assertEquals("private", consumer.getTaData().getPropertyObj().get("test")); 882 | 883 | sdk.clearSuperProperties(); 884 | properties.remove("test"); 885 | 886 | expect = null; 887 | try { 888 | sdk.track("accountId", "distinctId", "eventName", properties); 889 | } catch (Exception e) { 890 | expect = e; 891 | } 892 | Assert.assertNull(expect); 893 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("test")); 894 | 895 | expect = null; 896 | try { 897 | sdk.trackUpdate("accountId", "distinctId", "eventName", "eventId", properties); 898 | } catch (Exception e) { 899 | expect = e; 900 | } 901 | Assert.assertNull(expect); 902 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("test")); 903 | 904 | expect = null; 905 | try { 906 | sdk.trackOverwrite("accountId", "distinctId", "eventName", "eventId", properties); 907 | } catch (Exception e) { 908 | expect = e; 909 | } 910 | Assert.assertNull(expect); 911 | Assert.assertNull(consumer.getTaData().getPropertyObj().get("test")); 912 | } 913 | } -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/functionTest/TemporaryConsumer.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test.functionTest; 2 | 3 | import cn.thinkingdata.analytics.inter.ITDConsumer; 4 | import cn.thinkingdata.analytics.util.TDCommonUtil; 5 | import com.alibaba.fastjson2.JSON; 6 | import com.alibaba.fastjson2.JSONObject; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * @author Sun Zeyu 12 | * @date 2021/6/9 10:36 13 | */ 14 | public class TemporaryConsumer implements ITDConsumer { 15 | 16 | private final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 17 | private TemporaryEvent taData; 18 | 19 | @Override 20 | public void add(Map message) { 21 | String formatMsg = JSON.toJSONString(message, DEFAULT_DATE_FORMAT, TDCommonUtil.fastJsonSerializerFeature()); 22 | JSONObject data = JSON.parseObject(formatMsg); 23 | this.taData = new TemporaryEvent(data); 24 | } 25 | 26 | @Override 27 | public void flush() { 28 | 29 | } 30 | 31 | @Override 32 | public void close() { 33 | 34 | } 35 | 36 | public TemporaryEvent getTaData() { 37 | return this.taData; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/functionTest/TemporaryEvent.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test.functionTest; 2 | 3 | import com.alibaba.fastjson2.JSONObject; 4 | import com.alibaba.fastjson2.annotation.JSONField; 5 | 6 | import java.util.Date; 7 | import java.util.Map; 8 | 9 | public class TemporaryEvent { 10 | @JSONField(name = "#app_id") 11 | protected String appId = null; 12 | @JSONField(name = "#account_id") 13 | protected String accountId = null; 14 | @JSONField(name = "#distinct_id") 15 | protected String distinctId = null; 16 | @JSONField(name = "#uuid") 17 | protected String uuid = null; 18 | @JSONField(name = "#type") 19 | protected String type; 20 | @JSONField(name = "#time", format = "yyyy-MM-dd HH:mm:ss.SSS") 21 | protected Date time; 22 | @JSONField(name = "#ip") 23 | private String ip = null; 24 | @JSONField(name = "#event_name") 25 | private String eventName = null; 26 | @JSONField(name = "#event_id") 27 | private String eventId = null; 28 | @JSONField(name = "#first_check_id") 29 | private String firstCheckId = null; 30 | @JSONField(name = "#transaction_property") 31 | private String transactionProperty = null; 32 | @JSONField(name = "#import_tool_id") 33 | private String importToolId; 34 | 35 | @JSONField(name = "properties") 36 | protected JSONObject propertyObj = null; 37 | 38 | public String getAppId() { 39 | return appId; 40 | } 41 | 42 | public void setAppId(String appId) { 43 | this.appId = appId; 44 | } 45 | 46 | public String getAccountId() { 47 | return accountId; 48 | } 49 | 50 | public void setAccountId(String accountId) { 51 | this.accountId = accountId; 52 | } 53 | 54 | public String getDistinctId() { 55 | return distinctId; 56 | } 57 | 58 | public void setDistinctId(String distinctId) { 59 | this.distinctId = distinctId; 60 | } 61 | 62 | public String getUuid() { 63 | return uuid; 64 | } 65 | 66 | public void setUuid(String uuid) { 67 | this.uuid = uuid; 68 | } 69 | 70 | public String getType() { 71 | return type; 72 | } 73 | 74 | public void setType(String type) { 75 | this.type = type; 76 | } 77 | 78 | public Date getTime() { 79 | return time; 80 | } 81 | 82 | public void setTime(Date time) { 83 | this.time = time; 84 | } 85 | 86 | public String getIp() { 87 | return ip; 88 | } 89 | 90 | public void setIp(String ip) { 91 | this.ip = ip; 92 | } 93 | 94 | public String getEventName() { 95 | return eventName; 96 | } 97 | 98 | public void setEventName(String eventName) { 99 | this.eventName = eventName; 100 | } 101 | 102 | public String getEventId() { 103 | return eventId; 104 | } 105 | 106 | public void setEventId(String eventId) { 107 | this.eventId = eventId; 108 | } 109 | 110 | public String getFirstCheckId() { 111 | return firstCheckId; 112 | } 113 | 114 | public void setFirstCheckId(String firstCheckId) { 115 | this.firstCheckId = firstCheckId; 116 | } 117 | 118 | public String getTransactionProperty() { 119 | return transactionProperty; 120 | } 121 | 122 | public void setTransactionProperty(String transactionProperty) { 123 | this.transactionProperty = transactionProperty; 124 | } 125 | 126 | public String getImportToolId() { 127 | return importToolId; 128 | } 129 | 130 | public void setImportToolId(String importToolId) { 131 | this.importToolId = importToolId; 132 | } 133 | 134 | public JSONObject getPropertyObj() { 135 | return propertyObj; 136 | } 137 | 138 | public void setPropertyObj(JSONObject propertyObj) { 139 | this.propertyObj = propertyObj; 140 | } 141 | 142 | public TemporaryEvent(Map mapData) 143 | { 144 | 145 | } 146 | public TemporaryEvent(JSONObject data) 147 | { 148 | this.appId = data.getString("#app_id"); 149 | this.accountId = data.getString("#account_id"); 150 | this.distinctId = data.getString("#distinct_id"); 151 | this.firstCheckId = data.getString("#first_check_id"); 152 | this.type = data.getString("#type"); 153 | this.propertyObj = data.getJSONObject("properties"); 154 | this.uuid = data.getString("#uuid"); 155 | this.time = data.getDate("#time"); 156 | this.ip = data.getString("#ip"); 157 | this.eventName = data.getString("#event_name"); 158 | this.importToolId = data.getString("#import_tool_id"); 159 | this.eventId = data.getString("#event_id"); 160 | this.transactionProperty = data.getString("#transaction_property"); 161 | } 162 | 163 | } 164 | -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/functionTest/UtilTest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test.functionTest; 2 | 3 | import cn.thinkingdata.analytics.util.TDPropertyUtil; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class UtilTest { 9 | @Before 10 | public void init() 11 | { 12 | } 13 | @Test 14 | // check rules of keys 15 | public void test1() 16 | { 17 | Assert.assertFalse( TDPropertyUtil.isValidKey(null)); 18 | Assert.assertFalse(TDPropertyUtil.isValidKey("")); 19 | Assert.assertFalse(TDPropertyUtil.isValidKey("_")); 20 | Assert.assertTrue(TDPropertyUtil.isValidKey("#a")); 21 | Assert.assertTrue(TDPropertyUtil.isValidKey("a")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/cn/thinkingdata/analytics/test/performanceTest/PerformanceTest.java: -------------------------------------------------------------------------------- 1 | package cn.thinkingdata.analytics.test.performanceTest; 2 | 3 | import cn.thinkingdata.analytics.TDAnalytics; 4 | import cn.thinkingdata.analytics.TDLoggerConsumer; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class PerformanceTest { 12 | @org.junit.Test 13 | public void trackDuration() 14 | { 15 | TDAnalytics ta = new TDAnalytics(new TDLoggerConsumer("./log")); 16 | Map map= new HashMap<>(); 17 | for(int i = 0; i < 20; i++) { 18 | map.put("#dasdadsdddffddfasadffdffdffdfdffafd"+i,"#dasdadsdddffddfasadffdffdffdfdffafd"+i); 19 | } 20 | long time1 = System.currentTimeMillis(); 21 | 22 | // add track ... 23 | try { 24 | for (int i = 0; i < 1; i++) { 25 | ta.track("a", "b", "eventName", map); 26 | } 27 | } catch (Exception e) { 28 | System.out.println(e.getLocalizedMessage()); 29 | } 30 | 31 | long time2 = System.currentTimeMillis(); 32 | System.out.println("time="+(time2-time1)); 33 | } 34 | } 35 | --------------------------------------------------------------------------------