Skip to content

Instantly share code, notes, and snippets.

@niks551
Created February 12, 2013 20:32
Show Gist options
  • Save niks551/4773121 to your computer and use it in GitHub Desktop.
Save niks551/4773121 to your computer and use it in GitHub Desktop.
package com.convert.admin.service;
import static com.convert.common.config.ConvertConfig.getProperty;
import java.net.UnknownHostException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import com.convert.analytics.campaign.CampaignActivityAnalytics;
import com.convert.analytics.campaign.DefaultCampaignActivityAnalytics;
import com.convert.analytics.campaign.messaging.DefaultMessagingCampaignAnalytics;
import com.convert.analytics.campaign.messaging.MessagingCampaignAnalytics;
import com.convert.analytics.campaign.online.DefaultOnlineCampaignAnalytics;
import com.convert.analytics.campaign.online.OnlineCampaignAnalytics;
import com.convert.analytics.site.DefaultSiteActivityAnalytics;
import com.convert.analytics.site.SiteActivityAnalytics;
import com.convert.analytics.uniques.Requests;
import com.convert.noodles.client.NoodlesClient;
import com.convert.rice.client.RiceClient;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
public class AnalyticsFactory {
private static Mongo mongo;
private static final RiceClient riceClient;
private static final DB analyticsDB;
private static final String erCollection;
private static final NoodlesClient noodlesClient;
private static final AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
private static final AsyncHttpClientConfig config = builder
.setAllowPoolingConnection(true)
.setMaximumConnectionsPerHost(1000)
.setMaximumConnectionsTotal(1000)
.setIdleConnectionInPoolTimeoutInMs(100000)
.build();
private static final AsyncHttpClient httpClient = new AsyncHttpClient(config);
private static final Requests requests;
public static ListenableFuture<List<Void>> shutDown() throws Exception {
List<ListenableFuture<Void>> shutdownListeners = Lists.newArrayListWithCapacity(2);
shutdownListeners.add(noodlesClient.close());
shutdownListeners.add(riceClient.close());
return Futures.allAsList(shutdownListeners);
}
static {
requests = new Requests(httpClient, getProperty("gobably.url", "http://localhost:1234/g"));
String hosts = getProperty("analytics.mongo.host", "127.0.0.1");
MongoOptions mongoOptions = new MongoOptions();
mongoOptions.connectionsPerHost = Integer
.parseInt(getProperty("analytics.mongo.connectionPerHost", "400"));
List<ServerAddress> serverAddresses = Lists.newArrayList();
try {
for (String host : StringUtils.split(hosts, ",")) {
serverAddresses.add(new ServerAddress(host));
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
mongo = new Mongo(serverAddresses, mongoOptions);
riceClient = new RiceClient(getProperty("analytics.rice.host", "localhost"),
new Integer(getProperty("analytics.rice.port", "5077")),
new GenericObjectPool.Config());
analyticsDB = mongo.getDB(getProperty("analytics.mongo.db", "analytics"));
erCollection = getProperty("analytics.mongo.erCollection", "c");
String noodlesServer = getProperty("noodles.server", "localhost");
int noodlesPort = new Integer(getProperty("noodles.port", "6077"));
noodlesClient = new NoodlesClient(noodlesServer, noodlesPort, new Config());
}
static public class SiteAnalyticsFactory implements Supplier<SiteActivityAnalytics> {
public SiteActivityAnalytics get() {
return new DefaultSiteActivityAnalytics(riceClient, requests);
}
}
static public class CampaignAnalyticsFactory implements Supplier<CampaignActivityAnalytics> {
public CampaignActivityAnalytics get() {
return new DefaultCampaignActivityAnalytics(riceClient, requests);
}
}
static public class OnlineCampaignAnalyticsFactory implements Supplier<OnlineCampaignAnalytics> {
public OnlineCampaignAnalytics get() {
return new DefaultOnlineCampaignAnalytics(riceClient, requests);
}
}
static public class C2E2MAnalyticsFactory implements Supplier<MessagingCampaignAnalytics> {
public MessagingCampaignAnalytics get() {
return new DefaultMessagingCampaignAnalytics(riceClient, requests, new DefaultCampaignActivityAnalytics(
riceClient, requests));
}
}
static public class NoodlesClientSupplier implements Supplier<NoodlesClient> {
public NoodlesClient get() {
return noodlesClient;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment