Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Metrics : add metrics listener and call its methods in appropriate places #316

Merged
merged 7 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Changes to Matrix Android SDK in 0.9.7 (2018-XX-XX)
=======================================================

Features:
-
- Add MetricsListener to measure some startup and stats metrics

Improvements:
- MXCrypto: Encrypt the messages for invited members according to the history visibility (if the option is enabled in MXCryptoConfig).
Expand Down
58 changes: 36 additions & 22 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.data.RoomSummary;
import org.matrix.androidsdk.data.metrics.MetricsListener;
import org.matrix.androidsdk.data.store.IMXStore;
import org.matrix.androidsdk.data.store.MXMemoryStore;
import org.matrix.androidsdk.db.MXMediasCache;
Expand Down Expand Up @@ -120,6 +121,8 @@ public interface RequestNetworkErrorListener {
private MXCallsManager mCallsManager;
private MXMediasCache mMediasCache;

private MetricsListener mMetricsListener;

private ProfileRestClient mProfileRestClient;
private PresenceRestClient mPresenceRestClient;
private ThirdPidRestClient mThirdPidRestClient;
Expand Down Expand Up @@ -195,6 +198,15 @@ public void setRequestNetworkErrorListener(RequestNetworkErrorListener requestNe
mRequestNetworkErrorListener = requestNetworkErrorListener;
}

/**
* Update the metrics listener
*
* @param metricsListener the metrics listener
*/
public void setMetricsListener(MetricsListener metricsListener) {
mMetricsListener = metricsListener;
}

/**
* @return the credentials
*/
Expand Down Expand Up @@ -1324,11 +1336,13 @@ private void manageResponse(final SyncResponse syncResponse, final String fromTo
// sanity check
if (null != syncResponse.rooms) {
// joined rooms events

if ((null != syncResponse.rooms.join) && (syncResponse.rooms.join.size() > 0)) {
Log.d(LOG_TAG, "Received " + syncResponse.rooms.join.size() + " joined rooms");

if (mMetricsListener != null) {
mMetricsListener.onRoomsLoaded(syncResponse.rooms.join.size());
}
Set<String> roomIds = syncResponse.rooms.join.keySet();

// Handle first joined rooms
for (String roomId : roomIds) {
try {
Expand Down Expand Up @@ -1419,27 +1433,27 @@ private void manageResponse(final SyncResponse syncResponse, final String fromTo
// Update account data to add new direct chat room(s)
mAccountDataRestClient.setAccountData(mCredentials.userId, AccountDataRestClient.ACCOUNT_DATA_TYPE_DIRECT_MESSAGES,
updatedDirectChatRoomsDict, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "## manageResponse() : succeeds");
}
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "## manageResponse() : succeeds");
}

@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
// TODO: we should try again.
}
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
// TODO: we should try again.
}

@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
}

@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
}
});
@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## manageResponse() : update account data failed " + e.getMessage());
}
});
}
}

Expand Down Expand Up @@ -1731,7 +1745,7 @@ public void onUnexpectedError(Exception e) {
}
}

/*
/*
* Handle a 'toDevice' event
* @param event the event
*/
Expand Down Expand Up @@ -2661,7 +2675,7 @@ public void run() {
* @return the direct chat room ids list
*/
public List<String> getDirectChatRoomIdsList() {
if (null != mLocalDirectChatRoomIdsList) return mLocalDirectChatRoomIdsList;
if (null != mLocalDirectChatRoomIdsList) return mLocalDirectChatRoomIdsList;

IMXStore store = getStore();
List<String> directChatRoomIdsList = new ArrayList<>();
Expand Down
27 changes: 20 additions & 7 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.matrix.androidsdk.data.RoomTag;
import org.matrix.androidsdk.data.cryptostore.IMXCryptoStore;
import org.matrix.androidsdk.data.cryptostore.MXFileCryptoStore;
import org.matrix.androidsdk.data.metrics.MetricsListener;
import org.matrix.androidsdk.data.store.IMXStore;
import org.matrix.androidsdk.data.store.MXStoreListener;
import org.matrix.androidsdk.db.MXLatestChatMessageCache;
Expand Down Expand Up @@ -139,6 +140,8 @@ public class MXSession {

public MXCallsManager mCallsManager;

private MetricsListener mMetricsListener;

private Context mAppContent;
private NetworkConnectivityReceiver mNetworkConnectivityReceiver;
private UnsentEventsManager mUnsentEventsManager;
Expand Down Expand Up @@ -430,6 +433,15 @@ public MXDataHandler getDataHandler() {
return mDataHandler;
}

/**
* Update the metrics listener
*
* @param metricsListener the metrics listener
*/
public void setMetricsListener(MetricsListener metricsListener) {
mMetricsListener = metricsListener;
}

/**
* Get the user credentials.
*
Expand Down Expand Up @@ -877,6 +889,7 @@ public void startEventStream(final EventsThreadListener anEventsListener,
final EventsThreadListener fEventsListener = (null == anEventsListener) ? new DefaultEventsThreadListener(mDataHandler) : anEventsListener;

mEventsThread = new EventsThread(mAppContent, mEventsRestClient, fEventsListener, initialToken);
mEventsThread.setMetricsListener(mMetricsListener);
mEventsThread.setNetworkConnectivityReceiver(networkConnectivityReceiver);
mEventsThread.setIsOnline(mIsOnline);
mEventsThread.setServerLongPollTimeout(mSyncTimeout);
Expand Down Expand Up @@ -1167,13 +1180,13 @@ public void createRoom(String name, String topic, String alias, final ApiCallbac
/**
* Create a new room with given properties. Needs the data handler.
*
* @param name the room name
* @param topic the room topic
* @param visibility the room visibility
* @param alias the room alias
* @param guestAccess the guest access rule (see {@link RoomState#GUEST_ACCESS_CAN_JOIN} or {@link RoomState#GUEST_ACCESS_FORBIDDEN})
* @param algorithm the crypto algorithm (null to create an unencrypted room)
* @param callback the async callback once the room is ready
* @param name the room name
* @param topic the room topic
* @param visibility the room visibility
* @param alias the room alias
* @param guestAccess the guest access rule (see {@link RoomState#GUEST_ACCESS_CAN_JOIN} or {@link RoomState#GUEST_ACCESS_FORBIDDEN})
* @param algorithm the crypto algorithm (null to create an unencrypted room)
* @param callback the async callback once the room is ready
*/
public void createRoom(String name,
String topic,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.androidsdk.data.metrics;

/**
* This interface defines methods for collecting metrics data associated with startup times and stats
* Those callbacks can be called from any threads
*/
public interface MetricsListener {

/**
* Called when the initial sync is finished
*
* @param duration of the sync
*/
void onInitialSyncFinished(long duration);

/**
* Called when the incremental sync is finished
*
* @param duration of the sync
*/
void onIncrementalSyncFinished(long duration);

/**
* Called when a store is preloaded
*
* @param duration of the preload
*/
void onStorePreloaded(long duration);

/**
* Called when a sync is complete
*
* @param nbOfRooms loaded in the @SyncResponse
*/
void onRoomsLoaded(int nbOfRooms);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomAccountData;
import org.matrix.androidsdk.data.RoomSummary;
import org.matrix.androidsdk.data.metrics.MetricsListener;
import org.matrix.androidsdk.rest.callback.SimpleApiCallback;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.ReceiptData;
Expand Down Expand Up @@ -619,4 +620,11 @@ public interface IMXStore {
*/
@Nullable
String getAntivirusServerPublicKey();

/**
* Update the metrics listener
*
* @param metricsListener the metrics listener
*/
void setMetricsListener(MetricsListener metricsListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.matrix.androidsdk.data.RoomAccountData;
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.data.RoomSummary;
import org.matrix.androidsdk.data.metrics.MetricsListener;
import org.matrix.androidsdk.rest.callback.SimpleApiCallback;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.ReceiptData;
Expand Down Expand Up @@ -521,6 +522,9 @@ public void run() {
// extract the room states
mRoomReceiptsToLoad.addAll(listFiles(mStoreRoomsMessagesReceiptsFolderFile.list()));
mPreloadTime = System.currentTimeMillis() - fLoadTimeT0;
if (mMetricsListener != null) {
mMetricsListener.onStorePreloaded(mPreloadTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will be be called on another thread, maybe we should specify it on the Javadoc

}

Log.e(LOG_TAG, "The store is opened.");
dispatchOnStoreReady(mCredentials.userId);
Expand Down Expand Up @@ -562,6 +566,9 @@ public void run() {
}
dispatchOnStoreReady(mCredentials.userId);
mPreloadTime = System.currentTimeMillis() - fLoadTimeT0;
if (mMetricsListener != null) {
mMetricsListener.onStorePreloaded(mPreloadTime);
}
}
}
});
Expand All @@ -571,6 +578,7 @@ public void run() {
Thread t = new Thread(r);
t.start();
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomAccountData;
import org.matrix.androidsdk.data.RoomSummary;
import org.matrix.androidsdk.data.metrics.MetricsListener;
import org.matrix.androidsdk.rest.callback.SimpleApiCallback;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.ReceiptData;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class MXMemoryStore implements IMXStore {

//
private final Map<String, Event> mTemporaryEventsList = new HashMap<>();
protected MetricsListener mMetricsListener;

protected Credentials mCredentials;

Expand Down Expand Up @@ -1619,4 +1621,13 @@ public void setAntivirusServerPublicKey(@Nullable String key) {
public String getAntivirusServerPublicKey() {
return mMetadata.mAntivirusServerPublicKey;
}

/**
* Update the metrics listener
*
* @param metricsListener the metrics listener
*/
public void setMetricsListener(MetricsListener metricsListener) {
mMetricsListener = metricsListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public void syncFromToken(final String token,
setConnectionTimeout(RestClient.CONNECTION_TIMEOUT_MS * ((null == token) ? 2 : 1));

final String description = "syncFromToken";

// Disable retry because it interferes with clientTimeout
// Let the client manage retries on events streams
mApi.sync(params)
Expand Down
Loading