Search in sources :

Example 6 with StartSessionLog

use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class AnalyticsSerializerTest method someBatch.

@Test
public void someBatch() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    Device device = new Device();
    device.setSdkName("mobilecenter.android");
    device.setSdkVersion("1.2.3");
    device.setModel("S5");
    device.setOemName("HTC");
    device.setOsName("Android");
    device.setOsVersion("4.0.3");
    device.setOsBuild("LMY47X");
    device.setOsApiLevel(15);
    device.setLocale("en_US");
    device.setTimeZoneOffset(120);
    device.setScreenSize("800x600");
    device.setAppVersion("3.2.1");
    device.setAppBuild("42");
    List<Log> logs = new ArrayList<>();
    {
        logs.add(new StartSessionLog());
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("click");
        eventLog.setProperties(new HashMap<String, String>() {

            {
                put("x", "1");
                put("y", "2");
            }
        });
        logs.add(eventLog);
    }
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
        log.setDevice(device);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
    serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
    serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    android.util.Log.v(TAG, payload);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) HashMap(java.util.HashMap) Device(com.microsoft.azure.mobile.ingestion.models.Device) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) StartSessionLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 7 with StartSessionLog

use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class SessionTracker method sendStartSessionIfNeeded.

/**
     * Generate a new session identifier if the first time or
     * we went in background for more X seconds or
     * if enough time has elapsed since the last background usage of the API.
     *
     * Indeed the API can be used for events or crashes only for example, we need to renew
     * the session even when no pages are triggered but at the same time we want to keep using
     * the same session as long as the current activity is not paused (long video for example).
     */
private void sendStartSessionIfNeeded() {
    if (mSid == null || hasSessionTimedOut()) {
        /* New session: generate a new identifier. */
        mSid = UUIDUtils.randomUUID();
        /* Update session map. */
        mSessions.put(System.currentTimeMillis(), mSid);
        /* Remove oldest session if we reached maximum storage capacity. */
        if (mSessions.size() > STORAGE_MAX_SESSIONS)
            mSessions.pollFirstEntry();
        /* Persist sessions. */
        Set<String> sessionStorage = new HashSet<>();
        for (Map.Entry<Long, UUID> session : mSessions.entrySet()) sessionStorage.add(session.getKey() + STORAGE_KEY_VALUE_SEPARATOR + session.getValue());
        StorageHelper.PreferencesStorage.putStringSet(STORAGE_KEY, sessionStorage);
        /* Enqueue a start session log. */
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setSid(mSid);
        mChannel.enqueue(startSessionLog, mGroupName);
    }
}
Also used : StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) UUID(java.util.UUID) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with StartSessionLog

use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method startSessionAfterUserApproval.

@Test
public void startSessionAfterUserApproval() {
    /*
         * Disable analytics while in background to set up the initial condition
         * simulating the optin use case.
         */
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.setEnabled(false);
    /* App in foreground: no log yet, we are disabled. */
    analytics.onActivityResumed(new Activity());
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable: start session sent retroactively. */
    Analytics.setEnabled(true);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), eq(analytics.getGroupName()));
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), eq(analytics.getGroupName()));
    /* Go background. */
    analytics.onActivityPaused(new Activity());
    /* Disable/enable: nothing happens on background. */
    Analytics.setEnabled(false);
    Analytics.setEnabled(true);
    /* No additional log. */
    verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with StartSessionLog

use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class SessionTrackerTest method startSessionWithoutLogs.

@Test
public void startSessionWithoutLogs() {
    final AtomicReference<StartSessionLog> startSessionLog = new AtomicReference<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            startSessionLog.set((StartSessionLog) invocation.getArguments()[0]);
            return null;
        }
    }).when(mChannel).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    /* Go foreground, start session is sent. */
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    assertNotNull(startSessionLog.get());
    UUID sid = startSessionLog.get().getSid();
    assertNotNull(sid);
    /* Change screen after a long time, session reused. */
    spendTime(30000);
    mSessionTracker.onActivityPaused();
    spendTime(1);
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    /* Go background and come back after timeout, second session. */
    spendTime(1);
    mSessionTracker.onActivityPaused();
    spendTime(30000);
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(2)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    assertNotEquals(sid, startSessionLog.get().getSid());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)9 Test (org.junit.Test)8 UUID (java.util.UUID)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)6 Log (com.microsoft.azure.mobile.ingestion.models.Log)6 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)4 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)3 Context (android.content.Context)2 Channel (com.microsoft.azure.mobile.channel.Channel)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 EventLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory)1 Device (com.microsoft.azure.mobile.ingestion.models.Device)1 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)1 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)1 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)1 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)1 ArrayList (java.util.ArrayList)1