def handle_put(cls, **kwargs):
     """Refresh cached counts for given inventory ID."""
     inventory_id = kwargs.get("inventory_id", None)
     cls._system_exists(inventory_id)
     DB.execute_sql("SELECT refresh_system_cached_counts(%s)",
                    (inventory_id, ))
     return ""
 def handle_delete(cls, **kwargs):
     pid = kwargs["pid"]
     try:
         LOGGER.info("Killing query with PID %s", pid)
         DB.execute_sql("SELECT pg_terminate_backend(%s)", (pid,))
     except DatabaseError as err:
         LOGGER.exception("Internal server error: %s", err)
         return "Error"
     return "Ok"
 def handle_put(cls, **kwargs):
     """Refresh cached counts for given account ID and CVE."""
     account = kwargs.get("account_id", None)
     cve = kwargs.get("cve_id", None)
     cls._account_exists(account)
     cls._cve_exists(cve)
     DB.execute_sql("SELECT refresh_cve_account_cached_counts(%s, %s)",
                    (cve, account))
     return ""
    def handle_get(cls, **kwargs):
        rh_account_id = kwargs["rh_account_id"]
        try:
            tables = DB.execute_sql("""
                        SELECT tableoid::pg_catalog.regclass
                        FROM system_vulnerabilities
                        WHERE rh_account_id = %s""", (rh_account_id,))

            if tables.rowcount < 1:
                return "No tables to vacuum"

            command = ["pg_repack",
                       "-p", str(CFG.db_port), "-d", CFG.db_name,
                       "-h", CFG.db_host, "-U", CFG.db_user, "-k"]

            for table in tables:
                command.append("-t")
                command.append(table[0])

            LOGGER.info("Repacking tables %s", tables)
            subprocess.Popen(command, env={"PGPASSWORD": CFG.db_pass})

        except DatabaseError as err:
            LOGGER.exception("Internal server error: %s", err)
            return "Error"
        return "Ok"
 def handle_delete(cls, **kwargs):  # pylint: disable=unused-argument
     """Delete systems from system_platform table missing in inventory."""
     DB.execute_sql("""
         UPDATE system_platform sp
            SET opt_out = true, stale = true, when_deleted = now()
         FROM (
             SELECT sp2.inventory_id
             FROM system_platform sp2 LEFT JOIN
                  inventory.hosts ih ON sp2.inventory_id = ih.id
             WHERE ih.id IS NULL
               AND sp2.when_deleted IS NULL
             ORDER BY sp2.inventory_id
             FOR UPDATE OF sp2
         ) up
         WHERE sp.inventory_id = up.inventory_id
     """)
     return ""
    def handle_get(cls, **kwargs):
        rh_account_id = kwargs["rh_account_id"]
        result = []
        try:
            tables = DB.execute_sql("""
                        SELECT DISTINCT(tableoid::pg_catalog.regclass)
                        FROM system_vulnerabilities
                        WHERE rh_account_id = %s""", (rh_account_id,))

            for table in tables:
                res = DB.execute_sql("""SELECT * FROM pgstattuple(%s)""", (table[0],)).fetchone()
                res = cls.format_bloat_result(res)
                res["table_name"] = table[0]
                result.append(res)
        except DatabaseError as err:
            LOGGER.exception("Internal server error: %s", err)
        return {"tables": result}
    def handle_get(cls, **kwargs):
        table = kwargs["table_name"]
        try:
            res = DB.execute_sql("""SELECT * FROM pgstattuple(%s)""", (table,)).fetchone()
        except DatabaseError as err:
            LOGGER.exception("Internal server error: %s", err)
            return {}

        return cls.format_bloat_result(res)
 def handle_get(cls, **kwargs):  # pylint: disable=unused-argument
     """Get count of systems in system_platform table but missing in inventory."""
     cursor = DB.execute_sql("""
         SELECT COUNT(sp.inventory_id)
         FROM system_platform sp LEFT JOIN
              inventory.hosts ih ON sp.inventory_id = ih.id
         WHERE ih.id IS NULL
           AND sp.when_deleted IS NULL
     """)
     return cursor.fetchone()[0]
    def handle_get(cls, **kwargs):
        threshold = kwargs.get("ms_threshold", 0)
        username = kwargs.get("username", None)
        wait_event_type = kwargs.get("wait_event_type", None)

        result = []

        try:
            cursor = DB.execute_sql(
                """
                        SELECT pid, datname, usename, application_name,
                               query_start, now() - query_start AS running_time,
                               wait_event_type, wait_event, query
                        FROM pg_stat_activity
                        WHERE EXTRACT(MILLISECONDS FROM now() - query_start) >= %s AND
                              (usename = %s OR %s IS NULL) AND
                              (wait_event_type = %s OR %s IS NULL) AND
                              state = 'active'
                        """, (threshold, username, username, wait_event_type,
                              wait_event_type))
        except DatabaseError as err:
            LOGGER.exception("Internal server error: %s", err)
            return {"queries": result}

        for query in cursor:
            result.append({
                "pid": query[0],
                "db_name": query[1],
                "username": query[2],
                "app_name": query[3],
                "query_start": str(query[4]),
                "running_time": str(query[5]),
                "wait_event_type": query[6],
                "wait_event": query[7],
                "query": query[8]
            })

        return {"queries": result}
 def handle_delete(cls, **kwargs):
     """Delete system"""
     inventory_id = kwargs['inventory_id']
     cls._system_exists_and_visible(inventory_id)
     DB.execute_sql("SELECT delete_system(%s)", (inventory_id, ))
     return ""
 def handle_put(cls, **kwargs):
     """Refresh cached counts for given CVE."""
     cve = kwargs.get("cve_id", None)
     cls._cve_exists(cve)
     DB.execute_sql("SELECT refresh_cve_cached_counts(%s)", (cve, ))
     return ""
 def handle_put(cls, **kwargs):
     """Refresh cached counts for given account ID."""
     account = kwargs.get("account_id", None)
     cls._account_exists(account)
     DB.execute_sql("SELECT refresh_account_cached_counts(%s)", (account, ))
     return ""