Exemple #1
0
    def _filter_out_dates_with_no_readings(self, first_isodate, last_isodate):
        """This internal method checks the mongo db to see if the dates in the
        first_isodate and last_isodates range have power readings.

        :param first_isodate: first date in isodate format that should
        be checked until the last_isodate.
        :param last_isodate: the last date between the first date in
        isodate format.
        :return: lists of dates in isodate format that have power readings.
        """
        isodates_list = []
        try:
            # First get the string isodates into date types.
            start_date = datetime.fromisoformat(first_isodate).date()
            end_date = datetime.fromisoformat(last_isodate).date()
            # Create a general expresiion to enumerate through all the
            # possible dates between the start and end dates.
            gen_expr = (start_date + timedelta(n)
                        for n in range(int((end_date - start_date).days) + 1))
            # Go through each date and see if there are readings available.
            # If readings are available for the date, append the date to
            # the isodate list in isodate format.

            for dt in gen_expr:
                if self._there_is_a_reading(dt):
                    # Is there at least one reading for this date?
                    isodates_list.append(dt.isoformat())
        except Exception as e:
            handle_exception(e)
        return isodates_list
Exemple #2
0
    def get_DataFrame_for_date(self, date_str=ALL_READINGS):
        """Return the active Power readings for a
        specific date or for all dates.

        :param date: isodate formatted date or "*".
            Defaults to "*"
        :return: A pandas DataFrame with a dateindex and a
            column named 'pA' of active power readings.
        """

        df = self._get_df_no_datetimeindex(date_str)
        if df.empty:
            handle_exception(
                f'There are no readings in the database for {date_str}')
        return self._set_datetimeindex(df)
Exemple #3
0
    def _check_good_isodate(self, date_str):
        """internal method that returns a Date datatype
        by converting the isodate date_str into a Date.
        An exception is raised if the date_str is not
        isodate formatted.

        :param date_str: isodate formatted date string.

        """
        dt = None
        try:
            dt = date.fromisoformat(date_str)
        except ValueError as e:
            handle_exception(e)
        return dt
Exemple #4
0
    def _get_first_and_last_isodate(self):
        """Internal method that finds the first date and last date in which
        there are readings within the mongo db.

        Dates are in isodate format.
        """
        try:
            first_record = self.collection.find_one()
            last_record = list(self.collection.find().sort([('_id', -1)
                                                            ]).limit(1))[0]
            isodate_first = self._id_to_isodate(first_record['_id'])
            isodate_last = self._id_to_isodate(last_record['_id'])
        except Exception as e:
            handle_exception(e)
        return isodate_first, isodate_last
Exemple #5
0
    def _connect_to_collection(self):
        """Internal method that connects to the mongo database using The path,
        database name, and collection name the instance of Analyze was
        initialized with.

        This method doesn't do anything if there is already a connection
        to the database.
        """
        if self.collection is None:
            # Create a connection and attempt to access mongod
            client = MongoClient(self.mongodb_path)
            try:
                client.server_info()  # Exception thrown if can't connect.
            except ConfigurationError as e:
                handle_exception(e)
            db = client[self.db_str]
            self.collection = db[self.collection_name]
Exemple #6
0
    def _get_df_no_datetimeindex(self, date_str):
        """An internal method that returns a DataFrame that includes mongo db's
        object id.  The object id has not been coverted to a datetimeindex.

        :param date_str: Either a date in isodate format to use readings in the
            calculation for that date or '*' to use all available dates for
            the calculation.  Defaults to '*'
        :raises a: Exception if the records can't be retrieved or the date_str
            isn't of isodate format (or '*').
        :return: [description]
        :rtype: [type]
        """
        df = pd.DataFrame()
        self._connect_to_collection()
        if date_str not in ALL_READINGS:  # Get eadings for a specific isodate.
            # Check to make sure the date_str is a valid isodate.
            try:
                # Will raise a ValueError if cannot
                # convert from date_str to date datetype.
                dt = self._check_good_isodate(date_str)
                day_id = self._make_objectid(dt)
                dt_next = dt + timedelta(days=1)
                next_day_id = self._make_objectid(dt_next)
                try:
                    df = pd.DataFrame.from_records(
                        self.collection.find(
                            {"_id": {
                                '$gt': day_id,
                                '$lt': next_day_id
                            }}, {
                                '_id': 1,
                                'Pa': 1
                            }))
                except Exception as e:
                    handle_exception(e)
            except ValueError as e:
                handle_exception(e)
        else:
            try:
                df = pd.DataFrame.from_records(
                    self.collection.find(projection={
                        '_id': 1,
                        'Pa': 1
                    }))
            except Exception as e:
                handle_exception(e)
        return df