Esempio n. 1
0
    def listing_all_gps_location_daywise(self, user_id: str,
                                         all_days: List[str]):
        """
        Produce and save the gps location of participant's in day basis

        :param str user_id: UUID of the stream owner
        :param List(str) all_days: All days of the user in the format 'YYYYMMDD'
        """

        self.CC.logging.log('%s started processing for user_id %s' %
                            (self.__class__.__name__, str(user_id)))
        gps_data = []
        stream_ids = self.CC.get_stream_id(
            user_id, GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM)
        for stream_id in stream_ids:

            for day in all_days:
                location_data_stream = \
                    self.CC.get_stream(stream_id["identifier"], user_id, day, localtime=False)

                for data in set(location_data_stream.data):

                    if data.start_time.date() != data.end_time.date():
                        temp = DataPoint(data.start_time, data.end_time,
                                         data.offset, data.sample)
                        start_day = data.start_time.date()
                        end_time = datetime.combine(start_day, time.max)
                        end_time = end_time.replace(
                            tzinfo=data.start_time.tzinfo)
                        temp.end_time = end_time
                        gps_data.append(temp)

                        end_day = data.end_time.date()
                        start_day += timedelta(days=1)
                        while start_day != end_day:
                            temp = DataPoint(data.start_time, data.end_time,
                                             data.offset, data.sample)
                            start_time = datetime.combine(start_day, time.min)
                            start_time = start_time.replace(
                                tzinfo=data.start_time.tzinfo)
                            temp.start_time = start_time
                            end_time = datetime.combine(start_day, time.max)
                            end_time = end_time.replace(
                                tzinfo=data.start_time.tzinfo)
                            temp.end_time = end_time
                            gps_data.append(temp)
                            start_day += timedelta(days=1)
                        temp = DataPoint(data.start_time, data.end_time,
                                         data.offset, data.sample)
                        start_time = datetime.combine(start_day, time.min)
                        start_time = start_time.replace(
                            tzinfo=data.start_time.tzinfo)
                        temp.start_time = start_time
                        gps_data.append(temp)
                    else:
                        gps_data.append(data)

        try:
            if len(gps_data):
                streams = self.CC.get_user_streams(user_id)
                for stream_name, stream_metadata in streams.items():
                    if stream_name == GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM:
                        self.store_stream(filepath="gps_location_daywise.json",
                                          input_streams=[stream_metadata],
                                          user_id=user_id,
                                          data=gps_data)
                        break
        except Exception as e:
            self.CC.logging.log("Exception:", str(e))
            self.CC.logging.log(traceback.format_exc())

        self.CC.logging.log(
            '%s finished processing for user_id %s saved %d '
            'data points' %
            (self.__class__.__name__, str(user_id), len(gps_data)))
Esempio n. 2
0
    def listing_all_work_days(self, user_id: str, all_days: List[str]):
        """
        Produce and save the list of work_days Works-days are generated by the
        gps location of participant's which is labeled as 'Work' the first time
        of a day is marked as start time and the last time marked as end time
        and sample is saved as 'Office'

        :param str user_id: UUID of the stream owner
        :param List(str) all_days: All days of the user in the format 'YYYYMMDD'
        :return:
        """

        self.CC.logging.log('%s started processing for user_id %s' %
                            (self.__class__.__name__, str(user_id)))
        work_data = []
        work_data_ems = []
        location_data = []
        stream_ids = self.CC.get_stream_id(
            user_id, GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM)
        current_day = None  # in beginning current day is null
        for stream_id in stream_ids:
            for day in all_days:
                location_data_stream = \
                    self.CC.get_stream(stream_id["identifier"], user_id, day, localtime=True)
                location_data += location_data_stream.data
        location_data = list(set(location_data))
        location_data.sort(key=lambda x: x.start_time)
        for data in location_data:
            print(data)
            if data.sample.lower() != "work":
                # only the data marked as Work are needed
                continue

            d = DataPoint(data.start_time, data.end_time, data.offset,
                          data.sample)

            if d.start_time.date() != current_day:
                '''
                when the day in d.start_time.date() is not equal
                current_day that means its a new day.
                '''
                if current_day:
                    temp = DataPoint(data.start_time, data.end_time,
                                     data.offset, data.sample)
                    temp.start_time = work_start_time
                    temp.end_time = work_end_time
                    temp.sample = 'Office'
                    work_data.append(temp)
                    work_data_ems.append(
                        DataPoint(temp.start_time, temp.end_time, temp.offset,
                                  1))
                work_start_time = d.start_time

                # save the new day as current day
                current_day = d.start_time.date()

            work_end_time = d.end_time
        if current_day:
            temp = DataPoint(data.start_time, data.end_time, data.offset,
                             data.sample)
            temp.start_time = work_start_time
            temp.end_time = work_end_time
            temp.sample = 'Office'
            work_data.append(temp)
            work_data_ems.append(
                DataPoint(temp.start_time, temp.end_time, temp.offset, 1))

        try:
            if len(work_data):
                streams = self.CC.get_user_streams(user_id)
                for stream_name, stream_metadata in streams.items():
                    if stream_name == GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM:
                        self.store_stream(filepath="working_days.json",
                                          input_streams=[stream_metadata],
                                          user_id=user_id,
                                          data=work_data,
                                          localtime=True)
                        self.store_stream(filepath="working_days_ems.json",
                                          input_streams=[stream_metadata],
                                          user_id=user_id,
                                          data=work_data_ems,
                                          localtime=True)
                        break
        except Exception as e:
            print("Exception:", str(e))
            print(traceback.format_exc())
        self.CC.logging.log(
            '%s finished processing for user_id %s saved %d '
            'data points' %
            (self.__class__.__name__, str(user_id), len(work_data)))
    def listing_all_work_days(self, user_id, all_days):
        """
        Produce and save the list of work_days Works-days are generated by the
        gps location of participant's which is labeled as 'Work' the first time
        of a day is marked as start time and the last time marked as end time
        and sample is saved as 'Office' """

        self.CC.logging.log('%s started processing for user_id %s' %
                            (self.__class__.__name__, str(user_id)))
        work_data = []
        stream_ids = self.CC.get_stream_id(
            user_id, GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM)
        for stream_id in stream_ids:
            current_day = None  # in beginning current day is null
            for day in all_days:
                location_data_stream = \
                    self.CC.get_stream(stream_id["identifier"], user_id, day)

                for data in location_data_stream.data:
                    if data.sample[0].lower() != "work":
                        # only the data marked as Work are needed
                        continue

                    d = DataPoint(data.start_time, data.end_time, data.offset,
                                  data.sample)
                    #                     if d.offset:
                    #                         d.start_time += timedelta(milliseconds=d.offset)
                    #                         if d.end_time:
                    #                             d.end_time += timedelta(milliseconds=d.offset)
                    #                         else:
                    #                             continue

                    if d.start_time.date() != current_day:
                        '''
                        when the day in d.start_time.date() is not equal
                        current_day that means its a new day.
                        '''
                        if current_day:
                            temp = DataPoint(data.start_time, data.end_time,
                                             data.offset, data.sample)
                            temp.start_time = work_start_time
                            temp.end_time = work_end_time
                            temp.sample = 'Office'
                            work_data.append(temp)
                        work_start_time = d.start_time

                        # save the new day as current day
                        current_day = d.start_time.date()

                    work_end_time = d.end_time
        # print(work_data)
        try:
            if len(work_data):
                streams = self.CC.get_user_streams(user_id)
                for stream_name, stream_metadata in streams.items():
                    if stream_name == GPS_EPISODES_AND_SEMANTIC_lOCATION_STREAM:
                        # print(stream_metadata)
                        print("Going to pickle the file: ", work_data)

                        self.store_stream(filepath="working_days.json",
                                          input_streams=[stream_metadata],
                                          user_id=user_id,
                                          data=work_data)
                        break
        except Exception as e:
            print("Exception:", str(e))
            print(traceback.format_exc())
        self.CC.logging.log(
            '%s finished processing for user_id %s saved %d '
            'data points' %
            (self.__class__.__name__, str(user_id), len(work_data)))