def create_dummy_subtheme(self) -> SubTheme:
     """
     Create SubTheme
     :return: SubTheme
     """
     subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
     if not subtheme:
         subtheme = SubTheme(self.theme.id, '_TEST_SUB_THEME_')
         subtheme.save()
         subtheme.commit()
         subtheme = SubTheme.get_by_name('_TEST_SUB_THEME_')
     return subtheme
def create_sub_theme(theme: Theme, name: str = 'Airquality') -> None:
    """
    Create a SubTheme
    :param theme: Parent Theme
    :param name: SubThemes name
    :raises ValueError: If the new SubTheme is not persisted to the dB
    """
    sub_theme = SubTheme.get_by_name(name)
    if not sub_theme:
        sub_theme = SubTheme(theme.id, name)
        sub_theme.save()
        sub_theme.commit()
    if not sub_theme:
        logger.critical('ValueError raised while creating SubTheme')
        raise ValueError
Exemplo n.º 3
0
    def post(self) -> (dict, HTTPStatus):
        """
        Get dummy data from CSV. Store dummy data in database
        :param file_name: File name to extract data from.
        :return: A Status Report detailing the dB Entries created and an HTTP
        Status code 200 on success otherwise, a JSON error message is returned
        with the appropriate HTTPStatus code
        """
        args = self.reqparser.parse_args()
        # Get size of tables before import
        self.loc_stats["before"] = db.session.query(func.count(
            LocationData.id)).scalar()
        self.tracker_stats["before"] = db.session.query(func.count(
            Tracker.id)).scalar()

        # Fetch Data from CSV
        try:
            df = pd.read_csv(args['file_name'])
        except IOError as ioe:
            logger.error("Unable to parse CSV data to dataframe",
                         ioe.with_traceback(ioe.__traceback__))
            return dict(error="Unable to parse CSV data to dataframe",
                        trackback=ioe.with_traceback(
                            ioe.__traceback__)), HTTPStatus.BAD_REQUEST

        moving_theme = Theme.get_by_name("Moving_Sensors")
        if moving_theme:
            moving_sensor_theme_id = moving_theme.id
        else:
            moving_sensor_theme = Theme("Moving_Sensors")
            moving_sensor_theme.save()
            moving_sensor_theme.commit()
            moving_sensor_theme_id = moving_sensor_theme.id

        moving_subtheme = SubTheme.get_by_name("Moving_Airquality")
        if moving_subtheme:
            moving_sensor_subtheme_id = moving_subtheme.id
        else:
            moving_sensor_subtheme = SubTheme(moving_sensor_theme_id,
                                              "Moving_Airquality")
            moving_sensor_subtheme.save()
            moving_sensor_subtheme.commit()
            moving_sensor_subtheme_id = moving_sensor_subtheme.id

        # Trackers must be unique, Fetch trackers and make dB entries for
        # each unique tracker
        unique_tracker_ids = df["tracker"].unique()

        for tracker_id in unique_tracker_ids:
            self.t_ids[self.create_trackers(str(tracker_id),
                                            moving_sensor_subtheme_id)] = 0

        # Define Location data Pandas DataFrame Column names
        loc_df = df[[
            'tracker', 'datetime', 'latitude', 'longitude', 'speed', 'heading',
            'elevation', 'charger', 'battery', 'signalquality', 'satcnt'
        ]]

        # Drop all entries that are incomplete have NaN or None/ Null values
        loc_df = loc_df.dropna()

        # Store Location Data in the dB
        for index, row in loc_df.iterrows():
            self.add_location_data(row['tracker'], row['datetime'],
                                   row['latitude'], row['longitude'],
                                   row['speed'], row['heading'],
                                   row['elevation'], row['charger'],
                                   row['battery'], row['signalquality'],
                                   row['satcnt'])

        self.loc_stats["after"] = db.session.query(func.count(
            LocationData.id)).scalar()
        self.tracker_stats["after"] = db.session.query(func.count(
            Tracker.id)).scalar()

        return self.status_report(), 200