Search in sources :

Example 1 with BaseStatsModel

use of org.wordpress.android.ui.stats.models.BaseStatsModel in project WordPress-Android by wordpress-mobile.

the class StatsService method startTasks.

private void startTasks(final long blogId, final StatsTimeframe timeframe, final String date, final StatsEndpointsEnum sectionToUpdate, final int maxResultsRequested, final int pageRequested) {
    EventBus.getDefault().post(new StatsEvents.UpdateStatusChanged(true));
    String cachedStats = getCachedStats(blogId, timeframe, date, sectionToUpdate, maxResultsRequested, pageRequested);
    if (cachedStats != null) {
        BaseStatsModel mResponseObjectModel;
        try {
            JSONObject response = new JSONObject(cachedStats);
            mResponseObjectModel = StatsUtils.parseResponse(sectionToUpdate, blogId, response);
            EventBus.getDefault().post(sectionToUpdate.getEndpointUpdateEvent(blogId, timeframe, date, maxResultsRequested, pageRequested, mResponseObjectModel));
            updateWidgetsUI(blogId, sectionToUpdate, timeframe, date, pageRequested, mResponseObjectModel);
            checkAllRequestsFinished(null);
            return;
        } catch (JSONException e) {
            AppLog.e(AppLog.T.STATS, e);
        }
    }
    final RestClientUtils restClientUtils = WordPress.getRestClientUtilsV1_1();
    String period = timeframe.getLabelForRestCall();
    RestListener vListener = new RestListener(sectionToUpdate, blogId, timeframe, date, maxResultsRequested, pageRequested);
    final String periodDateMaxPlaceholder = "?period=%s&date=%s&max=%s";
    String path = String.format(Locale.US, "/sites/%s/stats/" + sectionToUpdate.getRestEndpointPath(), blogId);
    synchronized (mStatsNetworkRequests) {
        switch(sectionToUpdate) {
            case VISITS:
                path = String.format(Locale.US, path + "?unit=%s&quantity=15&date=%s", period, date);
                break;
            case TOP_POSTS:
            case REFERRERS:
            case CLICKS:
            case GEO_VIEWS:
            case AUTHORS:
            case VIDEO_PLAYS:
            case SEARCH_TERMS:
                path = String.format(Locale.US, path + periodDateMaxPlaceholder, period, date, maxResultsRequested);
                break;
            case TAGS_AND_CATEGORIES:
            case PUBLICIZE:
                path = String.format(Locale.US, path + "?max=%s", maxResultsRequested);
                break;
            case COMMENTS:
                // No parameters
                break;
            case FOLLOWERS_WPCOM:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "&max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "&period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case FOLLOWERS_EMAIL:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "&max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "&period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case COMMENT_FOLLOWERS:
                if (pageRequested < 1) {
                    path = String.format(Locale.US, path + "?max=%s", maxResultsRequested);
                } else {
                    path = String.format(Locale.US, path + "?period=%s&date=%s&max=%s&page=%s", period, date, maxResultsRequested, pageRequested);
                }
                break;
            case INSIGHTS_ALL_TIME:
            case INSIGHTS_POPULAR:
                break;
            case INSIGHTS_TODAY:
                path = String.format(Locale.US, path + "?period=day&date=%s", date);
                break;
            case INSIGHTS_LATEST_POST_SUMMARY:
                // This is an edge cases since  we're not loading stats but posts
                path = String.format(Locale.US, "/sites/%s/%s", blogId, sectionToUpdate.getRestEndpointPath() + "?order_by=date&number=1&type=post&fields=ID,title,URL,discussion,like_count,date");
                break;
            case INSIGHTS_LATEST_POST_VIEWS:
                // This is a kind of edge case, since we used the pageRequested parameter to request a single postID
                path = String.format(Locale.US, path + "/%s?fields=views", pageRequested);
                break;
            default:
                AppLog.i(T.STATS, "Called an update of Stats of unknown section!?? " + sectionToUpdate.name());
                return;
        }
        // We need to check if we already have the same request in the queue
        if (checkIfRequestShouldBeEnqueued(restClientUtils, path)) {
            AppLog.d(AppLog.T.STATS, "Enqueuing the following Stats request " + path);
            Request<JSONObject> currentRequest = restClientUtils.get(path, vListener, vListener);
            vListener.currentRequest = currentRequest;
            currentRequest.setTag("StatsCall");
            mStatsNetworkRequests.add(currentRequest);
        } else {
            AppLog.d(AppLog.T.STATS, "Stats request is already in the queue:" + path);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) StatsEvents(org.wordpress.android.ui.stats.StatsEvents) RestClientUtils(org.wordpress.android.networking.RestClientUtils) JSONException(org.json.JSONException) BaseStatsModel(org.wordpress.android.ui.stats.models.BaseStatsModel)

Aggregations

JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 RestClientUtils (org.wordpress.android.networking.RestClientUtils)1 StatsEvents (org.wordpress.android.ui.stats.StatsEvents)1 BaseStatsModel (org.wordpress.android.ui.stats.models.BaseStatsModel)1