diff --git a/astrid/src/com/todoroo/astrid/service/AstridDependencyInjector.java b/astrid/src/com/todoroo/astrid/service/AstridDependencyInjector.java index db5b81def..5df150afd 100644 --- a/astrid/src/com/todoroo/astrid/service/AstridDependencyInjector.java +++ b/astrid/src/com/todoroo/astrid/service/AstridDependencyInjector.java @@ -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 diff --git a/astrid/src/com/todoroo/astrid/service/StartupService.java b/astrid/src/com/todoroo/astrid/service/StartupService.java index b0d48348b..ca3d00888 100644 --- a/astrid/src/com/todoroo/astrid/service/StartupService.java +++ b/astrid/src/com/todoroo/astrid/service/StartupService.java @@ -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(); diff --git a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java index 6e88c5f72..9d14263b7 100644 --- a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java +++ b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java @@ -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 unreported = abTestEventDao.query(Query.select(ABTestEvent.PROPERTIES) + final TodorooCursor 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 { diff --git a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestReporter.java b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java similarity index 97% rename from astrid/src/com/todoroo/astrid/service/abtesting/ABTestReporter.java rename to astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java index b4a1a8183..1b56e41b1 100644 --- a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestReporter.java +++ b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java @@ -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); }