Push unreported test data on startup

pull/14/head
Sam Bosley 14 years ago
parent 7f57679469
commit 1b2d5c2585

@ -26,7 +26,8 @@ import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
import com.todoroo.astrid.gtasks.sync.GtasksSyncService;
import com.todoroo.astrid.service.abtesting.ABChooser;
import com.todoroo.astrid.service.abtesting.ABTestReporter;
import com.todoroo.astrid.service.abtesting.ABTestEventReportingService;
import com.todoroo.astrid.service.abtesting.ABTestInvoker;
import com.todoroo.astrid.service.abtesting.ABTests;
import com.todoroo.astrid.service.abtesting.FeatureFlipper;
import com.todoroo.astrid.tags.TagService;
@ -103,7 +104,8 @@ public class AstridDependencyInjector extends AbstractDependencyInjector {
injectables.put("abChooser", ABChooser.class);
injectables.put("abTests", new ABTests());
injectables.put("abTestEventDao", new ABTestEventDao());
injectables.put("abTestReporter", ABTestReporter.class);
injectables.put("abTestEventReportingService", ABTestEventReportingService.class);
injectables.put("abTestInvoker", ABTestInvoker.class);
injectables.put("featureFlipper", FeatureFlipper.class);
// com.todoroo.astrid.tags

@ -42,6 +42,7 @@ import com.todoroo.astrid.opencrx.OpencrxCoreUtils;
import com.todoroo.astrid.producteev.ProducteevUtilities;
import com.todoroo.astrid.reminders.ReminderStartupReceiver;
import com.todoroo.astrid.service.abtesting.ABChooser;
import com.todoroo.astrid.service.abtesting.ABTestEventReportingService;
import com.todoroo.astrid.service.abtesting.FeatureFlipper;
import com.todoroo.astrid.utility.AstridPreferences;
import com.todoroo.astrid.utility.Constants;
@ -88,6 +89,8 @@ public class StartupService {
@Autowired ABChooser abChooser;
@Autowired ABTestEventReportingService abTestEventReportingService;
/**
* bit to prevent multiple initializations
*/
@ -212,7 +215,7 @@ public class StartupService {
}
}).start();
abChooser.makeChoicesForAllTests(latestSetVersion == 0, taskService.getUserActivationStatus());
initializeABTesting(latestSetVersion == 0);
AstridPreferences.setPreferenceDefaults();
trackABTestingData();
@ -224,6 +227,12 @@ public class StartupService {
hasStartedUp = true;
}
private void initializeABTesting(boolean newUser) {
abTestEventReportingService.initialize();
abTestEventReportingService.pushAllUnreportedABTestEvents();
abChooser.makeChoicesForAllTests(newUser, taskService.getUserActivationStatus());
}
private void trackABTestingData() {
long firstLaunchTime = Preferences.getLong(AstridPreferences.P_FIRST_LAUNCH, 0);
long now = DateUtilities.now();

@ -30,7 +30,7 @@ public final class ABTestEventReportingService {
private ABTestEventDao abTestEventDao;
@Autowired
private ABTestReporter abTestReporter;
private ABTestInvoker abTestInvoker;
public ABTestEventReportingService() {
DependencyInjectionService.getInstance().inject(this);
@ -53,7 +53,7 @@ public final class ABTestEventReportingService {
public void run() {
try {
JSONArray payload = new JSONArray().put(jsonFromABTestEvent(model));
abTestReporter.post(payload);
abTestInvoker.post(payload);
model.setValue(ABTestEvent.REPORTED, 1);
abTestEventDao.saveExisting(model);
} catch (JSONException e) {
@ -66,31 +66,34 @@ public final class ABTestEventReportingService {
}
public void pushAllUnreportedABTestEvents() {
TodorooCursor<ABTestEvent> unreported = abTestEventDao.query(Query.select(ABTestEvent.PROPERTIES)
final TodorooCursor<ABTestEvent> unreported = abTestEventDao.query(Query.select(ABTestEvent.PROPERTIES)
.where(ABTestEvent.REPORTED.eq(0)));
try {
if (unreported.getCount() > 0) {
JSONArray payload = jsonArrayFromABTestEvents(unreported);
abTestReporter.post(payload);
for (unreported.moveToFirst(); !unreported.isAfterLast(); unreported.moveToNext()) {
ABTestEvent model = new ABTestEvent(unreported);
model.setValue(ABTestEvent.REPORTED, 1);
abTestEventDao.saveExisting(model);
if (unreported.getCount() > 0) {
new Thread(new Runnable() {
@Override
public void run() {
try {
JSONArray payload = jsonArrayFromABTestEvents(unreported);
abTestInvoker.post(payload);
for (unreported.moveToFirst(); !unreported.isAfterLast(); unreported.moveToNext()) {
ABTestEvent model = new ABTestEvent(unreported);
model.setValue(ABTestEvent.REPORTED, 1);
abTestEventDao.saveExisting(model);
}
} catch (JSONException e) {
handleException(e);
} catch (IOException e) {
handleException(e);
} finally {
unreported.close();
}
}
}
} catch (JSONException e) {
handleException(e);
} catch (IOException e) {
handleException(e);
} finally {
unreported.close();
}).start();
}
}
private void handleException(Exception e) {
Log.e("analytics", "analytics-error", e);
// TODO: Schedule retry
}
private static JSONObject jsonFromABTestEvent(ABTestEvent model) throws JSONException {

@ -20,7 +20,7 @@ import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.RestClient;
@SuppressWarnings("nls")
public class ABTestReporter {
public class ABTestInvoker {
/** NOTE: these values are development values and will not work on production */
private static final String URL = "http://analytics.astrid.com/api/1/retention";
@ -29,7 +29,7 @@ public class ABTestReporter {
@Autowired private RestClient restClient;
public ABTestReporter() {
public ABTestInvoker() {
DependencyInjectionService.getInstance().inject(this);
}
Loading…
Cancel
Save