Skip to content

Commit

Permalink
Added Google Analytics for telemetry - Review zinggAI#41
Browse files Browse the repository at this point in the history
  • Loading branch information
navinrathore committed Dec 11, 2021
1 parent f38e810 commit a900ba8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
40 changes: 21 additions & 19 deletions client/src/main/java/zingg/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zingg.client;

import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -108,8 +110,8 @@ public static void printAnalyticsBanner(boolean collectMetrics) {
LOG.info("* Please note that Zingg captures a few metrics about application's *");
LOG.info("* runtime parameters. However, no user's personal data or application *");
LOG.info("* data is captured. If you want to switch off this feature, please *");
LOG.info("* set the flag collectMetrics to false in config. For details, *");
LOG.info("* please refer to the zingg docs (https://docs.zingg.ai/docs/analytics.html) *");
LOG.info("* set the flag collectMetrics to false in config. For details, please *");
LOG.info("* refer to the zingg docs (https://docs.zingg.ai/docs/analytics.html) *");
LOG.info("****************************************************************************");
LOG.info("");
}
Expand Down Expand Up @@ -148,8 +150,8 @@ public static void main(String... args) {
client = new Client(arguments, options);
client.init();
client.execute();
Analytics.track(Metric.METRIC_EXEC_TIME, (System.currentTimeMillis() - startTime) / 1000);
client.trackMetrics(phase);
Analytics.track(Metric.METRIC_EXEC_TIME, (System.currentTimeMillis() - startTime) / 1000, arguments.getCollectMetrics());
client.postMetrics(phase, arguments.getCollectMetrics());
LOG.warn("Zingg processing has completed");
}
catch(ZinggClientException e) {
Expand Down Expand Up @@ -218,20 +220,20 @@ public ClientOptions getOptions() {
public void setOptions(ClientOptions options) {
this.options = options;
}

private void trackMetrics(String phase) {
if (!getArguments().getCollectMetrics()) {
return;
}
Analytics.track(Metric.METRIC_FEATURE_COUNT, getArguments().getFieldDefinition().size());

private void postMetrics(String phase, boolean collectMetrics) {
Analytics.track(Metric.METRIC_FIELDS_COUNT, getArguments().getFieldDefinition().size(), collectMetrics);

Pipe[] dataPipes = getArguments().getData();
for (Pipe p : dataPipes) {
Analytics.track(Metric.METRIC_SOURCE_TYPE, p.getFormat().type());
}
Pipe[] outputPipes = getArguments().getOutput();
for (Pipe p : outputPipes) {
Analytics.track(Metric.METRIC_SINK_TYPE, p.getFormat().type());
}
Analytics.trackEvent(phase);
}
String inPipesStr = Arrays.stream(dataPipes).map(p -> p.getFormat().type()).collect(Collectors.toList())
.stream().reduce((p1, p2) -> p1 + "," + p2).map(Object::toString).orElse("");
Analytics.track(Metric.METRIC_DATA_FORMAT, inPipesStr, collectMetrics);

Pipe[] outputPipes = getArguments().getOutput();
String outPipesStr = Arrays.stream(outputPipes).map(p -> p.getFormat().type()).collect(Collectors.toList())
.stream().reduce((p1, p2) -> p1 + "," + p2).map(Object::toString).orElse("");
Analytics.track(Metric.METRIC_OUTPUT_FORMAT, outPipesStr, collectMetrics);

Analytics.postEvent(phase, collectMetrics);
}
}
19 changes: 12 additions & 7 deletions client/src/main/java/zingg/client/util/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ private static Map<String, String> getMetrics() {
return metrics;
}

public static void track(String metricName, String metricValue) {
getMetrics().put(metricName, metricValue);
public static void track(String metricName, String metricValue, boolean collectMetrics) {
if (collectMetrics) {
getMetrics().put(metricName, metricValue);
}
}

public static void track(String metricName, double metricValue) {
track(metricName, String.valueOf(metricValue));
public static void track(String metricName, double metricValue, boolean collectMetrics) {
track(metricName, String.valueOf(metricValue), collectMetrics);
}

public static void trackEvent(String phase) {
public static void postEvent(String phase, boolean collectMetrics) {
if (collectMetrics == false) {
return;
}
ObjectMapper mapper = new ObjectMapper();
ObjectNode rootNode = mapper.createObjectNode();

Expand Down Expand Up @@ -90,13 +95,13 @@ private static void sendEvents(String param) {
try {
URL url = uri.toURL();
String response = executePostRequest(url.toString(), param);
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
LOG.debug("Event tracked.");
}

public static String executePostRequest(String targetURL, String urlParameters) {
private static String executePostRequest(String targetURL, String urlParameters) {
HttpURLConnection connection = null;
try {
//Create connection
Expand Down
14 changes: 7 additions & 7 deletions client/src/main/java/zingg/client/util/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import org.apache.spark.sql.Row;

public class Metric {
public static final String METRIC_SOURCE_TYPE = "source_type";
public static final String METRIC_SINK_TYPE = "sink_type";
public static final String METRIC_FEATURE_COUNT = "no_features";
public static final String METRIC_EXEC_TIME = "exec_time";
public static final String METRIC_POSITIVE_COUNT = "positive_count";
public static final String METRIC_NEGATIVE_COUNT = "negative_count";
public static final String METRIC_TOTAL_COUNT = "total_count";
public static final String METRIC_DATA_FORMAT = "dataFormat";
public static final String METRIC_OUTPUT_FORMAT = "outputFormat";
public static final String METRIC_FIELDS_COUNT = "numFields";
public static final String METRIC_EXEC_TIME = "executionTime";
public static final String METRIC_TRAINING_POSITIVES = "trainingDataPositives";
public static final String METRIC_TRAINING_NEGATIVES = "trainingDataNegatives";
public static final String METRIC_DATA_COUNT = "dataCount";

public static final long timeout = 1200L;
public static final double confidence = 0.95; // default value
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/zingg/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ public void execute() throws ZinggClientException {
//testData = dropDuplicates(testData);
double count = testData.count();
LOG.info("Read " + count);
if (args.getCollectMetrics()) {
Analytics.track(Metric.METRIC_TOTAL_COUNT, count);
}
Analytics.track(Metric.METRIC_DATA_COUNT, count, args.getCollectMetrics());

Dataset<Row> blocked = getBlocked(testData);
LOG.info("Blocked ");
/*blocked = blocked.cache();
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/zingg/Trainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ public void execute() throws ZinggClientException {
Model model = ModelUtil.createModel(positives, negatives, new Model(this.featurers), spark);
model.save(args.getModel());
LOG.info("Learnt similarity rules and saved output at " + args.getZinggDir());
if (args.getCollectMetrics()) {
Analytics.track(Metric.METRIC_POSITIVE_COUNT, Metric.approxCount(positives));
Analytics.track(Metric.METRIC_NEGATIVE_COUNT, Metric.approxCount(negatives));
}
Analytics.track(Metric.METRIC_TRAINING_POSITIVES, Metric.approxCount(positives), args.getCollectMetrics());
Analytics.track(Metric.METRIC_TRAINING_NEGATIVES, Metric.approxCount(negatives), args.getCollectMetrics());
LOG.info("Finished Learning phase");
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit a900ba8

Please sign in to comment.