Esempio n. 1
0
def insert_rows(items):
    """Insert timeseries data.

    Args:
        items: List of IDXTimestampValue objects

    Returns:
        result: DataPoint.checksum value

    """
    # Initialize key variables
    _rows = []
    last_timestamps = {}
    polling_intervals = {}

    # Fail safe checks
    if bool(items) is False:
        return

    # Update the data
    for item in sorted(items, key=attrgetter('timestamp')):
        # Insert data
        value = round(item.value, 10)
        _rows.append(
            Data(idx_datapoint=item.idx_datapoint,
                 timestamp=item.timestamp,
                 value=value))

        # Get the most recent timestamp for each idx_datapoint
        if item.idx_datapoint in last_timestamps:
            last_timestamps[item.idx_datapoint] = max(
                item.timestamp, last_timestamps[item.idx_datapoint])
        else:
            last_timestamps[item.idx_datapoint] = item.timestamp
        polling_intervals[item.idx_datapoint] = item.polling_interval

    # Update the last_timestamp
    for idx_datapoint, timestamp in last_timestamps.items():
        with db.db_modify(20047, die=False) as session:
            session.query(DataPoint).filter(
                and_(DataPoint.idx_datapoint == idx_datapoint,
                     DataPoint.enabled == 1)).update({
                         'last_timestamp':
                         timestamp,
                         'polling_interval':
                         int(polling_intervals[idx_datapoint])
                     })

    # Update after updating the last timestamp. Helps to prevent
    # 'Duplicate entry' errors in the event you need to re-run the ingester
    # after a previous crash.
    if bool(_rows) is True:
        with db.db_modify(20012, die=True) as session:
            session.add_all(_rows)
Esempio n. 2
0
def insert_rows(idx_datapoint, _idx_pairs):
    """Create db Pair table entries.

    Args:
        idx_datapoint: DataPoint.idx_datapoint
        _idx_pairs: List of Pair.idx_pair values

    Returns:
        None

    """
    # Initialize key variables
    rows = []

    # Create a list for processing if not available
    if isinstance(_idx_pairs, list) is False:
        _idx_pairs = [_idx_pairs]

    # Iterate over _idx_pairs
    for idx_pair in _idx_pairs:
        pair_exists = glue_exists(idx_datapoint, idx_pair)
        if bool(pair_exists) is False:
            # Insert and get the new idx_datasource value
            row = Glue(idx_pair=idx_pair, idx_datapoint=idx_datapoint)
            rows.append(row)

    if bool(rows) is True:
        with db.db_modify(20002, die=True) as session:
            session.add_all(rows)
Esempio n. 3
0
def insert_row(row):
    """Create a User table entry.

    Args:
        row: DbRowUser object

    Returns:
        None

    """
    # Verify values
    if bool(row) is False or isinstance(row, DbRowUser) is False:
        log_message = 'Invalid user type being inserted'
        log.log2die(20070, log_message)

    # Lowercase the name
    username = row.username.strip()[:MAX_KEYPAIR_LENGTH]
    password = row.password[:MAX_KEYPAIR_LENGTH]
    first_name = row.first_name.strip()[:MAX_KEYPAIR_LENGTH]
    last_name = row.last_name.strip()[:MAX_KEYPAIR_LENGTH]
    role = int(row.role)
    password_expired = int(row.password_expired)
    enabled = int(bool(row.enabled))

    # Insert
    row = _User(username=username.encode(),
                password=crypt.crypt(password).encode(),
                first_name=first_name.encode(),
                last_name=last_name.encode(),
                role=role,
                password_expired=password_expired,
                enabled=enabled)

    with db.db_modify(20054, die=True) as session:
        session.add(row)
Esempio n. 4
0
    def mutate(cls, _, info_, Input):

        data = _input_to_dictionary(Input)
        user = UserModel(**data)
        token_username = get_jwt_identity()

        # Getting current user making request to resource
        current_user = UserTable(token_username)

        # Accessing user data
        person = UserTable(user.username.decode())

        # Checking that the user creating the new user is an admin
        if current_user.role != 0:
            raise GraphQLError('Only admins can create a new user!')

        # Checking that a given username is not taken
        if person.exists:
            raise GraphQLError('Username already exists!')

        # Creating new user entry into User table
        with db.db_modify(20150, close=False) as session:
            session.add(user)

        return CreateUser(user=user)
Esempio n. 5
0
def insert_row(code, name=''):
    """Create a Language table entry.

    Args:
        code: Language code
        name: Language code name

    Returns:
        None

    """
    # Verify values
    if bool(name) is False or isinstance(name, str) is False:
        name = 'Change me. Language name not provided.'
    if bool(code) is False or isinstance(code, str) is False:
        log_message = 'Language code "{}" is invalid'.format(code)
        log.log2die(20033, log_message)

    # Lowercase the code
    code = code.strip().lower()[:MAX_KEYPAIR_LENGTH]
    name = name.strip()[:MAX_KEYPAIR_LENGTH]

    # Insert
    row = Language(code=code.encode(), name=name.encode())
    with db.db_modify(20032, die=True) as session:
        session.add(row)
Esempio n. 6
0
def update_row(key, translation, units, idx_language, idx_pair_xlate_group):
    """Update a database PairXlate.agent row.

    Args:
        key: PairXlate key
        translation: PairXlate translation
        units: PairXlate units of measure
        idx_language: Language table index
        idx_pair_xlate_group: PairXlateGroup table index

    Returns:
        None

    """
    # Insert and get the new agent value
    with db.db_modify(20080, die=False) as session:
        session.query(PairXlate).filter(
            and_(PairXlate.key == key.encode(),
                 PairXlate.idx_language == idx_language,
                 PairXlate.idx_pair_xlate_group ==
                 idx_pair_xlate_group)).update({
                     'translation':
                     translation.strip().encode(),
                     'units':
                     units.strip().encode()
                 })
Esempio n. 7
0
    def mutate(self, info_, Input):
        data = _input_to_dictionary(Input)

        user = UserModel(**data)
        with db.db_modify(20150, close=False) as session:
            session.add(user)

        return CreateUser(user=user)
Esempio n. 8
0
    def mutate(cls, _, info_, Input):
        data = _input_to_dictionary(Input)

        chart_datapoint = ChartDataPointModel(**data)
        with db.db_modify(20147, close=False) as session:
            session.add(chart_datapoint)

        return CreateChartDataPoint(chart_datapoint=chart_datapoint)
Esempio n. 9
0
    def mutate(self, info_, Input):
        data = _input_to_dictionary(Input)

        chart = ChartModel(**data)
        with db.db_modify(20142, close=False) as session:
            session.add(chart)

        return CreateChart(chart=chart)
Esempio n. 10
0
    def mutate(self, info_, Input):
        data = _create(Input)

        favorite = FavoriteModel(**data)
        with db.db_modify(20149, close=False) as session:
            session.add(favorite)

        return CreateFavorite(favorite=favorite)
Esempio n. 11
0
    def mutate(self, info_, Input):
        data = _input_to_dictionary(Input)
        user = UserModel(**data)

        # Create user only if they don't already exist
        person = UserTable(user.username.decode())
        if person.exists is False:
            with db.db_modify(20150, close=False) as session:
                session.add(user)
            return CreateUser(user=user)

        # Return nothing otherwise
        return None
Esempio n. 12
0
    def mutate(cls, _, info_, Input):
        data = _input_to_dictionary(Input)

        # Update database
        with db.db_modify(20145) as session:
            session.query(ChartDataPointModel).filter_by(
                idx_chart_datapoint=data['idx_chart_datapoint']).update(data)

        # Get code from database
        with db.db_query(20146, close=False) as session:
            chart_datapoint = session.query(ChartDataPointModel).filter_by(
                idx_chart_datapoint=data['idx_chart_datapoint']).first()

        return UpdateChartDataPoint(chart_datapoint=chart_datapoint)
Esempio n. 13
0
    def mutate(self, info_, Input):
        data = _input_to_dictionary(Input)

        # Update database
        with db.db_modify(20143) as session:
            session.query(ChartModel).filter_by(
                idx_chart=data['idx_chart']).update(data)

        # Get code from database
        with db.db_query(20144, close=False) as session:
            chart = session.query(ChartModel).filter_by(
                idx_chart=data['idx_chart']).first()

        return UpdateChart(chart=chart)
Esempio n. 14
0
    def mutate(self, info_, Input):
        data = _input_to_dictionary(Input)

        # Update database
        with db.db_modify(20151) as session:
            session.query(UserModel).filter_by(
                idx_user=data['idx_user']).update(data)

        # Get code from database
        with db.db_query(20163, close=False) as session:
            user = session.query(UserModel).filter_by(
                idx_user=data['idx_user']).first()

        return UpdateUser(user=user)
Esempio n. 15
0
    def mutate(self, info_, Input):
        data = _update(Input)

        # Update database
        with db.db_modify(20153) as session:
            session.query(FavoriteModel).filter_by(
                idx_favorite=data['idx_favorite']).update(data)

        # Get code from database
        with db.db_query(20154, close=False) as session:
            favorite = session.query(FavoriteModel).filter_by(
                idx_favorite=data['idx_favorite']).first()

        return UpdateFavorite(favorite=favorite)
Esempio n. 16
0
    def password_expired(self, value):
        """Set the password_expired flag.

        Args:
            value: New value to apply

        Returns:
            None

        """
        # Update
        with db.db_modify(20160, die=True) as session:
            session.query(_User).filter(
                _User.username == self._username.encode()).update(
                    {'password_expired': int(bool(value))})
Esempio n. 17
0
    def enabled(self, value):
        """Modify enabled status.

        Args:
            value: New value to apply

        Returns:
            None

        """
        # Update
        with db.db_modify(20161, die=True) as session:
            session.query(_User).filter(
                _User.username == self._username.encode()).update(
                    {'enabled': int(bool(value))})
Esempio n. 18
0
def insert_row(name):
    """Create the database PairXlateGroup row.

    Args:
        name: PairXlateGroup name

    Returns:
        None

    """
    # Filter invalid data
    if isinstance(name, str) is True:
        # Insert and get the new pair_xlate value
        with db.db_modify(20063, die=True) as session:
            session.add(PairXlateGroup(name=name.strip().encode()))
Esempio n. 19
0
def assign(_idx_agent, _idx_pair_xlate_group):
    """Assign an agent to an agent group.

    Args:
        idx_agent: Agent index
        _idx_pair_xlate_group: idx_pair_xlate_group for the agent

    Returns:
        None

    """
    # Update
    with db.db_modify(20059, die=False) as session:
        session.query(Agent).filter(Agent.idx_agent == _idx_agent).update(
            {'idx_pair_xlate_group': _idx_pair_xlate_group})
Esempio n. 20
0
def assign(_idx_agent, idx_agent_group):
    """Assign an agent to an agent group.

    Args:
        idx_agent: Agent index
        idx_agent_group: Agent group index

    Returns:
        None

    """
    # Update
    with db.db_modify(20059, die=False) as session:
        session.query(Agent).filter(Agent.idx_agent == _idx_agent).update(
            {'idx_agent_group': idx_agent_group})
Esempio n. 21
0
    def password(self, value):
        """Modify.

        Args:
            value: New value to apply

        Returns:
            None

        """
        # Initialize key variables
        _value = crypt.crypt(value).encode()
        with db.db_modify(20158, die=True) as session:
            session.query(_User).filter(
                _User.username == self._username.encode()).update(
                    {'password': _value})
Esempio n. 22
0
    def remove_test_entry(self, name, target_table):
        """Removes a given test entry from a target table

        Args:
            name: Corresponding name of test entry in target_table
            target_table: Table to look for entry to be removed

        Return:
            None

        """
        with db.db_modify(31003) as session:
            query = session.query(target_table)
            entry = query.filter_by(name = name.encode()).first()
            session.delete(entry)
            session.commit()
Esempio n. 23
0
    def role(self, value):
        """Modify.

        Args:
            value: New value to apply

        Returns:
            None

        """
        # Update
        _value = int(value)
        with db.db_modify(20159, die=True) as session:
            session.query(_User).filter(
                _User.username == self._username.encode()).update(
                    {'role': _value})
Esempio n. 24
0
def update_name(code, name):
    """Upadate a Language table entry.

    Args:
        code: Language code
        name: Language code name

    Returns:
        None

    """
    # Update
    code = code.strip().lower()
    with db.db_modify(20048, die=False) as session:
        session.query(Language).filter(Language.code == code.encode()).update(
            {'name': name.strip().encode()})
Esempio n. 25
0
    def first_name(self, value):
        """Modify first name.

        Args:
            value: New value to apply

        Returns:
            None

        """
        # Update
        _value = value.strip()[:MAX_KEYPAIR_LENGTH].encode()
        with db.db_modify(20156, die=True) as session:
            session.query(_User).filter(
                _User.username == self._username.encode()).update(
                    {'first_name': _value})
Esempio n. 26
0
def update_name(idx, name):
    """Upadate a PairXlateGroup table entry.

    Args:
        idx: PairXlateGroup idx_pair_xlate_group
        name: PairXlateGroup idx_pair_xlate_group name

    Returns:
        None

    """
    # Filter invalid data
    if isinstance(name, str) is True:
        with db.db_modify(20065, die=False) as session:
            session.query(PairXlateGroup).filter(
                PairXlateGroup.idx_pair_xlate_group == int(idx)).update(
                    {'name': name.strip().encode()})
Esempio n. 27
0
def update_row(key, translation, idx_language):
    """Update a database AgentXlate.agent row.

    Args:
        key: AgentXlate key
        translation: AgentXlate translation
        idx_language: Language table index

    Returns:
        None

    """
    # Insert and get the new agent value
    with db.db_modify(20131, die=False) as session:
        session.query(AgentXlate).filter(and_(
            AgentXlate.agent_program == key.encode(),
            AgentXlate.idx_language == idx_language)).update(
                {'translation': translation.strip().encode()}
            )
Esempio n. 28
0
def insert_rows(items):
    """Create db Pair table entries.

    Args:
        items: List of lists, or list of key-value pairs

    Returns:
        None

    """
    # Initialize key variables
    _rows = []
    uniques = {}
    all_kvs = []

    # Make list if not so
    if isinstance(items, list) is False:
        items = [items]

    # Create a single list of key-value pairs.
    # Add them to a dict to make the pairs unique.
    for item in items:
        if isinstance(item, list):
            all_kvs.extend(item)
        else:
            all_kvs.append(item)
    for _kv in all_kvs:
        uniques[_kv] = None

    # Insert the key-value pairs into the database
    for (key, value), _ in uniques.items():
        # Skip pre-existing pairs
        if bool(pair_exists(key, value)) is True:
            continue

        # Add values to list for future insertion
        _row = Pair(key=key.encode(), value=value.encode())
        _rows.append(_row)

    if bool(_rows) is True:
        with db.db_modify(20007, die=True) as session:
            session.add_all(_rows)
Esempio n. 29
0
def insert_row(agent_id, agent_target, agent_program):
    """Create the database Agent.agent value.

    Args:
        agent_id: Agent ID value (pattoo_agent_id)
        agent_target: Agent target (pattoo_agent_polled_target)
        agent_program: Agent program (pattoo_agent_program)

    Returns:
        None

    """
    # Filter invalid data
    if isinstance(agent_id, str) is True:
        # Insert and get the new agent value
        with db.db_modify(20036, die=True) as session:
            session.add(
                Agent(agent_id=agent_id.encode(),
                      agent_polled_target=agent_target.encode(),
                      agent_program=agent_program.encode()))
Esempio n. 30
0
def insert_row(key, translation, idx_language):
    """Create a database AgentXlate.agent row.

    Args:
        key: AgentXlate key
        translation: AgentXlate translation
        idx_language: Language table index

    Returns:
        None

    """
    # Insert and get the new agent value
    with db.db_modify(20130, die=True) as session:
        session.add(
            AgentXlate(
                agent_program=str(key).strip().encode(),
                translation=str(translation).strip().encode(),
                idx_language=idx_language
            )
        )