Пример #1
0
    def create(self, request):
        decoded_token = decode_token(request.META)
        token = get_token(request.META)
        serializer = EventSerializer(data=request.data)

        if serializer.is_valid():
            client = CalendarDBClient()

            event_id = client.add_event(token, serializer.data)

            st = SchedulingTask(task_type='schedule',
                                status='pending',
                                event_id=event_id,
                                start_time=None,
                                tentative_time=None,
                                initiator_id=decoded_token['user_id'],
                                result='needs_action')
            re = st.save()[0]
            re_s = EventSerializer(re.to_dict())

            return Response(re_s.data, status=status.HTTP_201_CREATED)
        return Response(
            {
                'data': serializer.errors,
                'message': 'Event could not be created with the given data.'
            },
            status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def read_data_sets_cf(user=None,
                      need_labels=False,
                      train_size=80,
                      validation_size=10,
                      test_size=10):
    """
    1. Get all the user ids
    2. For each user get their events
    3. Call the parser
    4. Return the results
    """
    assert (train_size + validation_size + test_size) == 100
    # Get the historical meetings of the user
    db_client = CalendarDBClient()
    events = db_client.list_all_events(user)
    # Get the training objects saved using the system
    training_objs = Training.objects.filter(user_id=user)
    # Obtain a representation of the data
    data, labels = parse_dataset_cf(events, training_objs)

    t_max = len(data) * train_size / 100
    v_max = len(data) * validation_size / 100

    data_sets = DataSets()
    data_sets.train = DataSet(data[:t_max],
                              labels[:t_max] if need_labels else [],
                              need_labels)
    data_sets.validation = DataSet(data[t_max:v_max],
                                   data[t_max:v_max] if need_labels else [],
                                   need_labels)
    data_sets.test = DataSet(data[v_max:],
                             labels[v_max:] if need_labels else [],
                             need_labels)
    # , Do the math to return the total number of items
    return data_sets, len(users)
Пример #3
0
def read_data_sets(user,
                   need_labels=False,
                   train_size=100,
                   validation_size=0,
                   test_size=0):
    assert (train_size + validation_size + test_size) == 100
    # Get the historical meetings of the user
    db_client = CalendarDBClient()
    events = db_client.list_all_events(user)
    # Get the training objects saved using the system
    training_objs = Training.objects.filter(user_id=user)
    # Obtain a representation of the data
    data, labels = parse_dataset(events, training_objs, need_labels)

    t_max = len(data) * train_size / 100
    v_max = len(data) * validation_size / 100

    data_sets = DataSets()
    data_sets.train = DataSet(data[:t_max],
                              labels[:t_max] if need_labels else [],
                              need_labels)
    data_sets.validation = DataSet(data[t_max:v_max],
                                   data[t_max:v_max] if need_labels else [],
                                   need_labels)
    data_sets.test = DataSet(data[v_max:],
                             labels[v_max:] if need_labels else [],
                             need_labels)
    return data_sets
Пример #4
0
    def is_available(self, from_date, to_date):
        if self.user_id is None:
            return True

        cl = CalendarDBClient()
        result = cl.available(self.user_id, from_date, to_date)

        return result
Пример #5
0
    def __init__(self, need_labels=True, rank=False, initial=False):
        # Bayesian Network config:
        #   need_labels = False, rank = False, initial = True
        # MLP config:
        #   need_labels = True, rank = False, initial = True
        # CF config:
        #   need_labels = True, rank = True, initial = True
        self.db_client = CalendarDBClient()
        self.x = np.array([[]])
        self.y = np.array([[]])
        self.need_labels = need_labels
        self.rank = rank
        self.initial = initial
        self.x = np.array([[]])
        self.y = np.array([[]])

        self._fetch()
Пример #6
0
    def _send_invitations(self, i, event):
        if self.tasks is not None:
            for attendee in event.participants:
                if attendee.user_id is not None:
                    e_id = None
                    if attendee.user_id == self.tasks[i].initiator_id:
                        e_id = self.tasks[i].event_id
                    new_invitation = Invitation(task=self.tasks[i],
                                                attendee=attendee.user_id,
                                                event_id=e_id,
                                                answered=False,
                                                decision=False)
                    new_invitation.save()

            client = CalendarDBClient()
            client.update_event(self.tasks[i].initiator_id,
                                self.tasks[i].event_id,
                                {"start": event.start_time.isoformat(),
                                 "end": event.end_time.isoformat(),
                                 "duration": event.duration})
Пример #7
0
class EventsDatasetGenerator(object):
    def __init__(self, need_labels=True, rank=False, initial=False):
        # Bayesian Network config:
        #   need_labels = False, rank = False, initial = True
        # MLP config:
        #   need_labels = True, rank = False, initial = True
        # CF config:
        #   need_labels = True, rank = True, initial = True
        self.db_client = CalendarDBClient()
        self.x = np.array([[]])
        self.y = np.array([[]])
        self.need_labels = need_labels
        self.rank = rank
        self.initial = initial
        self.x = np.array([[]])
        self.y = np.array([[]])

        self._fetch()

    def _to_item_id(self, event):
        ei = EventItem.objects.filter(event_type=event[1],
                                      duration=event[2],
                                      day=event[3],
                                      timeslot=event[4],
                                      location=event[5])
        if ei.exists():
            ei = ei[0]
        else:
            # Save the new element
            ei = EventItem(event_type=event[1],
                           duration=event[2],
                           day=event[3],
                           timeslot=event[4],
                           location=event[5])
            ei.save()

        return ei.id

    def _is_valid_for_training(self, event):
        # An event is valid when all the information is complete.
        if not event.get('deleted',
                         False) and event.get('start', '') != '' and event.get(
                             'end', '') != '' and event.get(
                                 'duration', -1) != -1 and event.get(
                                     'categories', '') != '':
            return True
        return False

    def _parse(self, events, training):
        """
        Returns a list of vectors:
            [[user_id, type, duration, day, timeslot, location, accepted], ...]
            OR
            [[user_id, type, duration, day, timeslot, location, rating], ...]
        We don't take into consideration location yet.
        """
        values = np.array([[]])
        labels = np.array([[]])

        for event in events:
            if self._is_valid_for_training(event):
                s = parse(event.get('start')).time().hour * SLOTS_PER_HOUR + \
                    parse(event.get('start')).time().minute / SLOT_SIZE

                item = [
                    event.get("user_id"),
                    TRAIN_EVENT_TYPES_DICT[event.get('categories')],
                    event.get('duration'),
                    parse(event.get('start')).weekday(), s / SLOTS_PER_DAY,
                    event.get('location', '')
                ]

                if self.rank:
                    # Each item should be like: [User, Item]
                    np.append(values, [[item[0], self._to_item_id(item)]])
                    if self.need_labels:
                        np.append(labels, [[4]])
                else:
                    np.append(values, [item])
                    if self.need_labels:
                        np.append(labels, [[1]])  # [0, 1]

        for to in training:
            s = to.start.time().hour * SLOTS_PER_HOUR + to.start.time(
            ).minute / SLOT_SIZE
            item = [
                to.user_id, TRAIN_EVENT_TYPES_DICT[to.event_type], to.duration,
                to.start.weekday(), s / SLOTS_PER_DAY, to.location
            ]

            if self.need_labels:
                np.append(labels, [[to.feedback]])

            if self.rank:
                # Each item should be like: [User, Item]
                np.append(values, [[item[0], self._to_item_id(item)]])
            else:
                np.append(values, [item])

            if self.need_labels:
                np.append(labels, [[to.feedback]])

        return values, labels

    def _fetch(self):
        events = []
        if self.initial:
            # Get the historical meetings of the user
            events = self.db_client.list_all_training_events()
        # Get the training objects saved using the system
        training_objs = RankingEvents.objects.all()
        # Obtain a representation of the data
        udata, ulabels = self._parse(events, training_objs)
        self.x = np.append(self.x, udata)
        self.y = np.append(self.y, ulabels)
Пример #8
0
 def get_event(self):
     client = CalendarDBClient()
     return client.get_event(self.event_id)