<commit_before>import maxBy from 'lodash/maxBy';
import df from 'date-fns';
import notificationsResource from '../../apiResources/notifications';
import { summarizedNotifications } from './getters';

export default {
  namespaced: true,
  state: {
    currentClassroomId: null,
    notifications: [],
    lastPolled: null,
    poller: null,
  },
  mutations: {
    SET_NOTIFICATIONS(state, notifications) {
      state.notifications = [...notifications];
    },
    APPEND_NOTIFICATIONS(state, notifications) {
      state.notifications = [...notifications, ...state.notifications];
    },
    SET_POLLER(state, poller) {
      state.poller = poller;
    },
    SET_CURRENT_CLASSROOM_ID(state, classroomId) {
      state.currentClassroomId = classroomId;
    },
    TEST_MOVE_LAST(state) {
      const len = state.notifications.length;
      const max = df.max.apply(null, state.notifications.map(n => n.timestamp));
      if (len > 1) {
        const modded = {
          ...state.notifications[len - 1],
          timestamp: df.addSeconds(max, 10),
        };
        state.notifications = [modded, ...state.notifications.slice(0, len - 1)];
      }
    },
  },
  getters: {
    summarizedNotifications,
    notificationsByUserId(state) {
      return function byUserId(userId) {
        return state.notifications.filter(notification => notification.user_id === userId);
      };
    },
    notificationsByLearnerGroupId(state, getters, rootState, rootGetters) {
      return function byLearnerGroupId(groupId) {
        const groupMembers =
          rootGetters['classSummary/notificationModuleData'].learnerGroups[groupId].member_ids;
        return state.notifications.filter(notification =>
          groupMembers.includes(notification.user_id)
        );
      };
    },
    maxNotificationIndex(state) {
      const match = maxBy(state.notifications, 'id');
      return match ? match.id : 0;
    },
  },
  actions: {
    fetchNotificationsForClass(store, classroomId) {
      if (!store.state.currentClassroomId) {
        store.commit('SET_CURRENT_CLASSROOM_ID', classroomId);
      }
      return notificationsResource
        .fetchCollection({
          getParams: {
            collection_id: classroomId,
          },
        })
        .then(data => {
          store.commit('SET_NOTIFICATIONS', data.results);
          if (!store.state.poller) {
            store.dispatch('startPolling');
          }
        });
    },
    updateNotificationsForClass(store, { classroomId, after }) {
      return notificationsResource
        .fetchCollection({
          getParams: {
            collection_id: classroomId,
            after,
          },
          force: true,
        })
        .then(data => {
          if (data.count > 0) {
            store.commit('APPEND_NOTIFICATIONS', data.results);
          }
        });
    },
    startPolling(store) {
      const poller = setInterval(() => {
        store.dispatch('updateNotificationsForClass', {
          classroomId: store.state.currentClassroomId,
          after: store.getters.maxNotificationIndex,
        });
      }, 5000);
      store.commit('SET_POLLER', poller);
    },
    stopPollingAndClear(store) {
      clearInterval(store.state.poller);
      store.commit('SET_NOTIFICATIONS', null);
      store.commit('SET_POLLER', null);
    },
  },
};
<commit_msg>Vary notification polling  time based on coaches_polling value in response<commit_after>import maxBy from 'lodash/maxBy';
import df from 'date-fns';
import notificationsResource from '../../apiResources/notifications';
import { summarizedNotifications } from './getters';

export default {
  namespaced: true,
  state: {
    currentClassroomId: null,
    notifications: [],
    lastPolled: null,
    poller: null,
  },
  mutations: {
    SET_NOTIFICATIONS(state, notifications) {
      state.notifications = [...notifications];
    },
    APPEND_NOTIFICATIONS(state, notifications) {
      state.notifications = [...notifications, ...state.notifications];
    },
    SET_POLLER(state, poller) {
      state.poller = poller;
    },
    SET_CURRENT_CLASSROOM_ID(state, classroomId) {
      state.currentClassroomId = classroomId;
    },
    TEST_MOVE_LAST(state) {
      const len = state.notifications.length;
      const max = df.max.apply(null, state.notifications.map(n => n.timestamp));
      if (len > 1) {
        const modded = {
          ...state.notifications[len - 1],
          timestamp: df.addSeconds(max, 10),
        };
        state.notifications = [modded, ...state.notifications.slice(0, len - 1)];
      }
    },
  },
  getters: {
    summarizedNotifications,
    notificationsByUserId(state) {
      return function byUserId(userId) {
        return state.notifications.filter(notification => notification.user_id === userId);
      };
    },
    notificationsByLearnerGroupId(state, getters, rootState, rootGetters) {
      return function byLearnerGroupId(groupId) {
        const groupMembers =
          rootGetters['classSummary/notificationModuleData'].learnerGroups[groupId].member_ids;
        return state.notifications.filter(notification =>
          groupMembers.includes(notification.user_id)
        );
      };
    },
    maxNotificationIndex(state) {
      const match = maxBy(state.notifications, 'id');
      return match ? match.id : 0;
    },
  },
  actions: {
    fetchNotificationsForClass(store, classroomId) {
      if (!store.state.currentClassroomId) {
        store.commit('SET_CURRENT_CLASSROOM_ID', classroomId);
      }
      return notificationsResource
        .fetchCollection({
          getParams: {
            collection_id: classroomId,
          },
        })
        .then(data => {
          store.commit('SET_NOTIFICATIONS', data.results);
          if (!store.state.poller) {
            store.dispatch('startingPolling', { coachesPolling: data.coaches_polling });
          }
        });
    },
    updateNotificationsForClass(store, { classroomId, after }) {
      return notificationsResource
        .fetchCollection({
          getParams: {
            collection_id: classroomId,
            after,
          },
          force: true,
        })
        .then(data => {
          if (data.count > 0) {
            store.commit('APPEND_NOTIFICATIONS', data.results);
          }
          store.dispatch('startingPolling', { coachesPolling: data.coaches_polling });
        });
    },
    startingPolling(store, { coachesPolling }) {
      const timeout = 2000 * Math.min(Math.max(coachesPolling, 1), 10);
      setTimeout(() => {
        store.dispatch('updateNotificationsForClass', {
          classroomId: store.state.currentClassroomId,
          after: store.getters.maxNotificationIndex,
        });
      }, timeout);
    },
    stopPollingAndClear(store) {
      clearInterval(store.state.poller);
      store.commit('SET_NOTIFICATIONS', null);
      store.commit('SET_POLLER', null);
    },
  },
};

{'fraction_non_alphanumeric': 0.12281462216442711, 'fraction_numerical': 0.0039011703511053317, 'mean_word_length': 2.4818913480885314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7151328', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1996, 'n_words': 331}