Merge pull request #1587 from crazyserver/MOBILE-2698

MOBILE-2698 calendar: Reduce core_group_course_user_groups requests
main
Juan Leyva 2018-11-09 13:09:34 +01:00 committed by GitHub
commit a5688bd1db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 56 deletions

View File

@ -291,7 +291,7 @@ export class AddonCalendarListPage implements OnDestroy {
refreshEvents(refresher: any): void { refreshEvents(refresher: any): void {
const promises = []; const promises = [];
promises.push(this.calendarProvider.invalidateEventsList(this.courses)); promises.push(this.calendarProvider.invalidateEventsList());
if (this.categoriesRetrieved) { if (this.categoriesRetrieved) {
promises.push(this.coursesProvider.invalidateCategories(0, true)); promises.push(this.coursesProvider.invalidateCategories(0, true));

View File

@ -297,46 +297,51 @@ export class AddonCalendarProvider {
: Promise<any[]> { : Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => { return this.sitesProvider.getSite(siteId).then((site) => {
siteId = site.getId(); siteId = site.getId();
const promises = [];
let courses, groups;
return this.coursesProvider.getUserCourses(false, siteId).then((courses) => { promises.push(this.coursesProvider.getUserCourses(false, siteId).then((data) => {
courses = data;
courses.push({ id: site.getSiteHomeId() }); // Add front page. courses.push({ id: site.getSiteHomeId() }); // Add front page.
}));
promises.push(this.groupsProvider.getAllUserGroups(siteId).then((data) => {
groups = data;
}));
return this.groupsProvider.getUserGroups(courses, siteId).then((groups) => { return Promise.all(promises).then(() => {
const now = this.timeUtils.timestamp(), const now = this.timeUtils.timestamp(),
start = now + (CoreConstants.SECONDS_DAY * daysToStart), start = now + (CoreConstants.SECONDS_DAY * daysToStart),
end = start + (CoreConstants.SECONDS_DAY * daysInterval), end = start + (CoreConstants.SECONDS_DAY * daysInterval),
data = { data = {
options: { options: {
userevents: 1, userevents: 1,
siteevents: 1, siteevents: 1,
timestart: start, timestart: start,
timeend: end timeend: end
}, },
events: { events: {
courseids: [], courseids: [],
groupids: [] groupids: []
} }
};
courses.forEach((course, index) => {
data.events.courseids[index] = course.id;
});
groups.forEach((group, index) => {
data.events.groupids[index] = group.id;
});
// We need to retrieve cached data using cache key because we have timestamp in the params.
const preSets = {
cacheKey: this.getEventsListCacheKey(daysToStart, daysInterval),
getCacheUsingCacheKey: true
}; };
return site.read('core_calendar_get_calendar_events', data, preSets).then((response) => { data.events.courseids = courses.map((course) => {
this.storeEventsInLocalDB(response.events, siteId); return course.id;
});
data.events.groupids = groups.map((group) => {
return group.id;
});
return response.events; // We need to retrieve cached data using cache key because we have timestamp in the params.
}); const preSets = {
cacheKey: this.getEventsListCacheKey(daysToStart, daysInterval),
getCacheUsingCacheKey: true
};
return site.read('core_calendar_get_calendar_events', data, preSets).then((response) => {
this.storeEventsInLocalDB(response.events, siteId);
return response.events;
}); });
}); });
}); });
@ -365,18 +370,17 @@ export class AddonCalendarProvider {
/** /**
* Invalidates events list and all the single events and related info. * Invalidates events list and all the single events and related info.
* *
* @param {any[]} courses List of courses or course ids.
* @param {string} [siteId] Site Id. If not defined, use current site. * @param {string} [siteId] Site Id. If not defined, use current site.
* @return {Promise<any[]>} Promise resolved when the list is invalidated. * @return {Promise<any[]>} Promise resolved when the list is invalidated.
*/ */
invalidateEventsList(courses: any[], siteId?: string): Promise<any[]> { invalidateEventsList(siteId?: string): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => { return this.sitesProvider.getSite(siteId).then((site) => {
siteId = site.getId(); siteId = site.getId();
const promises = []; const promises = [];
promises.push(this.coursesProvider.invalidateUserCourses(siteId)); promises.push(this.coursesProvider.invalidateUserCourses(siteId));
promises.push(this.groupsProvider.invalidateUserGroups(courses, siteId)); promises.push(this.groupsProvider.invalidateAllUserGroups(siteId));
promises.push(site.invalidateWsCacheForKeyStartingWith(this.getEventsListPrefixCacheKey())); promises.push(site.invalidateWsCacheForKeyStartingWith(this.getEventsListPrefixCacheKey()));
return Promise.all(promises); return Promise.all(promises);

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CoreSitesProvider } from './sites'; import { CoreSitesProvider } from './sites';
import { CoreCoursesProvider } from '@core/courses/providers/courses';
/** /**
* Group info for an activity. * Group info for an activity.
@ -50,7 +51,8 @@ export class CoreGroupsProvider {
static VISIBLEGROUPS = 2; static VISIBLEGROUPS = 2;
protected ROOT_CACHE_KEY = 'mmGroups:'; protected ROOT_CACHE_KEY = 'mmGroups:';
constructor(private sitesProvider: CoreSitesProvider, private translate: TranslateService) { } constructor(private sitesProvider: CoreSitesProvider, private translate: TranslateService,
private coursesProvider: CoreCoursesProvider) { }
/** /**
* Check if group mode of an activity is enabled. * Check if group mode of an activity is enabled.
@ -204,6 +206,28 @@ export class CoreGroupsProvider {
return this.ROOT_CACHE_KEY + 'groupmode:' + cmId; return this.ROOT_CACHE_KEY + 'groupmode:' + cmId;
} }
/**
* Get user groups in all the user enrolled courses.
*
* @param {string} [siteId] Site to get the groups from. If not defined, use current site.
* @return {Promise<any[]>} Promise resolved when the groups are retrieved.
*/
getAllUserGroups(siteId?: string): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => {
siteId = siteId || site.getId();
if (site.isVersionGreaterEqualThan('3.6')) {
return this.getUserGroupsInCourse(0, siteId);
}
return this.coursesProvider.getUserCourses(false, siteId).then((courses) => {
courses.push({ id: site.getSiteHomeId() }); // Add front page.
return this.getUserGroups(courses, siteId);
});
});
}
/** /**
* Get user groups in all the supplied courses. * Get user groups in all the supplied courses.
* *
@ -213,33 +237,31 @@ export class CoreGroupsProvider {
* @return {Promise<any[]>} Promise resolved when the groups are retrieved. * @return {Promise<any[]>} Promise resolved when the groups are retrieved.
*/ */
getUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any[]> { getUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any[]> {
const promises = []; // Get all courses one by one.
let groups = []; const promises = courses.map((course) => {
courses.forEach((course) => {
const courseId = typeof course == 'object' ? course.id : course; const courseId = typeof course == 'object' ? course.id : course;
promises.push(this.getUserGroupsInCourse(courseId, siteId, userId).then((courseGroups) => {
groups = groups.concat(courseGroups); return this.getUserGroupsInCourse(courseId, siteId, userId);
}));
}); });
return Promise.all(promises).then(() => { return Promise.all(promises).then((courseGroups) => {
return groups; return [].concat(...courseGroups);
}); });
} }
/** /**
* Get user groups in a course. * Get user groups in a course.
* *
* @param {number} courseId ID of the course. * @param {number} courseId ID of the course. 0 to get all enrolled courses groups (Moodle version > 3.6).
* @param {string} [siteId] Site to get the groups from. If not defined, use current site. * @param {string} [siteId] Site to get the groups from. If not defined, use current site.
* @param {number} [userId] ID of the user. If not defined, use ID related to siteid. * @param {number} [userId] ID of the user. If not defined, use ID related to siteid.
* @return {Promise<any[]>} Promise resolved when the groups are retrieved. * @return {Promise<any[]>} Promise resolved when the groups are retrieved.
*/ */
getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any[]> { getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => { return this.sitesProvider.getSite(siteId).then((site) => {
userId = userId || site.getUserId();
const data = { const data = {
userid: userId || site.getUserId(), userid: userId,
courseid: courseId courseid: courseId
}, },
preSets = { preSets = {
@ -256,6 +278,15 @@ export class CoreGroupsProvider {
}); });
} }
/**
* Get prefix cache key for user groups in course WS calls.
*
* @return {string} Prefix Cache key.
*/
protected getUserGroupsInCoursePrefixCacheKey(): string {
return this.ROOT_CACHE_KEY + 'courseGroups:';
}
/** /**
* Get cache key for user groups in course WS calls. * Get cache key for user groups in course WS calls.
* *
@ -264,7 +295,7 @@ export class CoreGroupsProvider {
* @return {string} Cache key. * @return {string} Cache key.
*/ */
protected getUserGroupsInCourseCacheKey(courseId: number, userId: number): string { protected getUserGroupsInCourseCacheKey(courseId: number, userId: number): string {
return this.ROOT_CACHE_KEY + 'courseGroups:' + courseId + ':' + userId; return this.getUserGroupsInCoursePrefixCacheKey() + courseId + ':' + userId;
} }
/** /**
@ -312,6 +343,22 @@ export class CoreGroupsProvider {
return Promise.all(promises); return Promise.all(promises);
} }
/**
* Invalidates user groups in all user enrolled courses.
*
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved when the data is invalidated.
*/
invalidateAllUserGroups(siteId?: string): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => {
if (site.isVersionGreaterEqualThan('3.6')) {
return this.invalidateUserGroupsInCourse(0, siteId);
}
return site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey());
});
}
/** /**
* Invalidates user groups in courses. * Invalidates user groups in courses.
* *
@ -322,13 +369,12 @@ export class CoreGroupsProvider {
*/ */
invalidateUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any> { invalidateUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => { return this.sitesProvider.getSite(siteId).then((site) => {
const promises = [];
userId = userId || site.getUserId(); userId = userId || site.getUserId();
courses.forEach((course) => { const promises = courses.map((course) => {
const courseId = typeof course == 'object' ? course.id : course; const courseId = typeof course == 'object' ? course.id : course;
promises.push(this.invalidateUserGroupsInCourse(courseId, site.id, userId));
return this.invalidateUserGroupsInCourse(courseId, site.id, userId);
}); });
return Promise.all(promises); return Promise.all(promises);
@ -338,7 +384,7 @@ export class CoreGroupsProvider {
/** /**
* Invalidates user groups in course. * Invalidates user groups in course.
* *
* @param {number} courseId Course ID. * @param {number} courseId ID of the course. 0 to get all enrolled courses groups (Moodle version > 3.6).
* @param {string} [siteId] Site ID. If not defined, current site. * @param {string} [siteId] Site ID. If not defined, current site.
* @param {number} [userId] User ID. If not defined, use current user. * @param {number} [userId] User ID. If not defined, use current user.
* @return {Promise<any>} Promise resolved when the data is invalidated. * @return {Promise<any>} Promise resolved when the data is invalidated.