예제 #1
0
def clean_db(days=2):
    """cleanup API for cleaning up all the ingestion jobs. 
    """
    message = None
    tables = []
    try:
        conn = pg_conn()
        with conn.cursor() as cursor:
            sql = """SELECT * FROM {}.{}
                    WHERE entrydate < DATE(NOW() - INTERVAL '{} DAY')
                    AND table_exist = TRUE
            """
            cursor.execute(sql.format(DB_SCHEMA, AUDIT_TABLE, days))
            rows = cursor.fetchall()
            if len(rows) > 0:
                for row in rows:
                    tables.append(row['tablename'])
                    sql_delete_table = """DROP TABLE {}.{}"""
                    cursor.execute(
                        sql_delete_table.format(DB_SCHEMA, row['tablename']))
                    sql_update_audit = """UPDATE {}.{} SET table_exist=FALSE WHERE tablename='{}'"""
                    sql_update_audit = sql_update_audit.format(
                        DB_SCHEMA, AUDIT_TABLE, row['tablename'])
                    cursor.execute(sql_update_audit)
                conn.commit()
        conn.close()
        message = "Tables that got deleted: " + ", ".join(tables)
    except Exception as err:
        logger.error(err)
        message = str(err)
    return jsonify(message=message), 200
예제 #2
0
def post_satellite_advertise():
    """
    Satellites will advertise themselves by pinging this interface.
    """
    try:
        body = request.get_json()
        # Should we accept a custom satellite host?
        default_host = request.headers.get('x-forwarded-for',
                                           request.remote_addr)
        satellite_host = body.get('satellite_host', default_host)
        satellite_port = body.get('satellite_port')
        satellite_url = f'http://{satellite_host}:{satellite_port}'
        data = body["data"]
        name = body["name"]
        ttl = data['ttl']
        info = data['info']
        with pg_conn() as conn:
            with conn.cursor() as cursor:
                cursor.execute(
                    '''
                INSERT INTO public.satellite(satellite_url, name, expires, info) VALUES (%s, %s, %s, %s)
                ON CONFLICT (satellite_url) DO UPDATE SET expires = EXCLUDED.expires, info = EXCLUDED.info
                ''', [
                        satellite_url, name,
                        datetime.datetime.utcnow() +
                        datetime.timedelta(seconds=ttl), info
                    ])
        return jsonify({"status": "ok"})
    except Exception as e:
        logger.error(str(e))
        return jsonify({"status": "error", "error": str(e)}), 400
예제 #3
0
def get_testinfo(testSystem, testName):
    """
    Get metadata for a given test.

    Parameters:
    testSystem: string - The name of the test system ('vrl')
    testName: string - the name of the test ('shadertest')

    Returns:
    JSON[] {
        id: integer - id of the test
        system: string - name of the test system
        name: string - name of the test.
        table_name: The name of the table to put results in for this test. Should probably be the same as name.
           Ex: A test with table name 'shadertest' and system 'vrl' would have a table at vrl.shadertest in Postgres
    }

    On Error:
    404, JSON {
        status: "error"
        error: string
    }
    """
    with pg_conn() as conn:
        rows = do_select(conn, 'SELECT id, system, name FROM test_meta WHERE system = %s AND name = %s', [testSystem, testName])
        if rows is None:
            return jsonify({"status": "error", "error": "Test not found"}), 404
        return jsonify(rows)
예제 #4
0
def query_testnames(system, name, limit=None):
    """
    Gets test results for a given test. if no limit is given, returns all results in history. Otherwise, limits
    to an integer number of results.

    If a query parameter ?csv=true is given, will return results as a CSV file instead of JSON. This is used
    to download test results as a CSV for further analysis.

    Parameters:
    system: string - the name of the test System ('vrl')
    name: string - the name of the test ('shadertest')
    limit: integer? - optional limit to number of results to get

    Returns:
    Json[] of objects, each representing a row in the results.
    OR
    a CSV file of the test results (with limit rows if limit is given).

    On Error:
    {"status": "error": "error": string}
    """
    with pg_conn() as conn:
        query = None
        if limit:
            query = sql.SQL('SELECT * FROM {}.{} LIMIT {};').format(sql.Identifier(system), sql.Identifier(name), sql.Literal(int(limit)))
        else:
            query = sql.SQL('SELECT * FROM {}.{};').format(sql.Identifier(system), sql.Identifier(name))
        rows = do_select(conn, query, [])
        if rows is None:
            return jsonify({"status": "error", "error": "Unknown error returning results"}), 404
        elif "csv" in request.args or not request.accept_mimetypes['application/json']:
            return make_csv_response(conn, system, name, rows)
        else:
            return jsonify(rows)
예제 #5
0
def scrape_results(nsec, where_clause, sql_args):
    """
    Scrape for new results, only selecting scrapers where where_clause matches. nsec is the number
    of seconds to look backwards in time for results.
    """
    with pg_conn() as conn:
        rows = None
        with conn.cursor() as cursor:
            sql = '''
            SELECT test_scrapers.id as scrapeid, test_meta.id, test_meta.system, test_meta.name,
                   test_scrapers.scrape_tag, test_scrapers.scrape_params
            FROM test_meta JOIN test_scrapers
            ON test_meta.id = test_scrapers.test_meta_id
            WHERE test_scrapers.enabled AND
            ''' + where_clause
            cursor.execute(sql, sql_args)
            rows = list(cursor)
        print('Running %d scrapers' % len(rows))
        for row in rows:
            try:
                print('Scraping with scraper ' + str(row['scrapeid']))
                scraper = make_scraper(conn, row['scrape_tag'], row['system'], row['name'], row['scrape_params'])
                scraper.scrape_latest(nsec)
            except Exception as e:
                traceback.print_exc()
예제 #6
0
def get_ops_details(team_name):
    """get the ops details for the given team on chub.
    :param team_name: name of the team for which ops details are requested
    :type team_name: string
    :return: list of dict objects with the ops info
    :rtype: list
    """
    try:
        rows = ()
        conn = pg_conn()
        with conn.cursor() as cursor:
            sql = '''
            select
            o.name as ops_name, o.wiki, og.name as grp_name, oo.name as owner_name, oo.email, t.name as team
            from ops o, ops_group og, ops_owner oo, teams t
            where o.ownerid = oo.id
            AND o.opsgroupid = og.id
            AND og.teamid = t.id
            AND t.name = %s
            '''
            cursor.execute(sql, [unquote(team_name)])
            rows = cursor.fetchall()
        conn.close()
        return jsonify(rows)
    except Exception as e:
        logger.error(e)
        return jsonify("ERROR"), 400
예제 #7
0
def get_visualization_route(system, name, view):
    """
    Polymorphic visulization route.

    Given test system, test name, and a "view" name, runs the select sql query associated with "view", on
    the table defined by "system" and "name", and returns the results. Parameters to the query can be provided
    as query paramaters (the string after a url that looks like "?x=123&y=456&some_option=false"). Available options
    to send can be retreieved with the 'get_visualization_params' route below. 

    Returns:
    JSON:  {"data": JSON[] of rows}

    Each row will have keys depending on the columns in the test table.
    """
    v = VisualiztionView.visualizations.get(view)
    if v is None:
        return jsonify({"status": "error", "error": "Unknown view " + view}), 400
    try:
        ctx = {}
        merge_qargs_for_jinja(ctx, v)
        ctx['system'] = system
        ctx['name'] = name
        ctx['test_table'] = system + '.' + name
        ctx['test_table_denormal'] = system + '.' + name + '_denormal'
        query, bind_params = jsql.prepare_query(v.template, ctx)
        with pg_conn(readOnly=True) as conn:
            with conn.cursor() as cursor:
                cursor.execute(query, list(bind_params))
                res = list(cursor)
                return jsonify({
                  "renderers": v.renderers,
                  "data": res
                })
    except Exception as e:
        return jsonify({"status": "error", "error": str(e)}), 400
예제 #8
0
def get_all_visualizations(system, name):
    """
    Get all available visualization configurations for a given test.

    Parameters:
    system: string - the name of the test system ('vrl')
    name: string - the name of the test ('shadertest')

    Returns:
    JSON[] of visualization objects with keys:
      - "id" : integer - id of row
      - "view": string - string tag that corresponds to a visualization view.
      - "json_params": JSON - reserved.

    On Error (400)
      {"status": "error", "error": string}
    """
    try:
      with pg_conn() as conn:
        sql = '''
        SELECT test_visualizations.id as id, query_name, query_params, renderer_name, renderer_params from test_visualizations
        JOIN test_meta ON test_meta.id = test_visualizations.test_meta_id
        WHERE test_meta.system = %s AND test_meta.name = %s;
        '''
        with conn.cursor() as cursor:
          cursor.execute(sql, [system, name])
          res = list(cursor)
          return jsonify(res)
    except Exception as e:
      return jsonify({"status": "error", "error": str(e)}), 400
예제 #9
0
    def __init__(self, test_system, test_name, conn=None):
        self.test_system = test_system
        self.test_name = test_name
        self.connection = conn or pg_conn()
        self._clear_memo()

        # Get the columns of the tests corresponding output table.
        with self.connection.cursor() as cursor:
            cursor.execute("""
            SELECT column_name, is_nullable FROM information_schema.columns
            WHERE table_schema = %s AND
                  table_name = %s
            ORDER BY ordinal_position;
            """, [self.test_system, self.test_name])
            raw_results = list(cursor)
            cols = []
            all_cols = map(lambda row: row["column_name"], raw_results)
            for col in all_cols:
                if col in ['id']:
                    continue
                cols.append(col)
            self.columns = cols
            self.optional_cols = {}
            for row in raw_results:
                # Yes, instead of a boolean, somehow the string 'NO' is returned.
                if row["is_nullable"] and row["is_nullable"] != 'NO':
                    self.optional_cols[row["column_name"]] = True
            # Generate denormalized columns
            self.denormalized_columns = denormalize_columns(cols)
            # Create SQL fragment for inserting columns
            self._columns_sql = '(' + (', ').join(['"%s"' % c for c in cols]) + ')'
            # Build final insert query
            self._insert_sql = f'INSERT INTO "{self.test_system}"."{self.test_name}" {self._columns_sql} VALUES %s ON CONFLICT DO NOTHING RETURNING id;'

        self.log('Created test writer with columns (%s)' % (', '.join(self.columns)))
예제 #10
0
def get_machineconfig_info(test_names):
    """Get machine config which includes gpu list, machine pool names and machine name list
    :param test_names: string of test names separated by "+"
    :type test_names: string
    :return: response of the api split into list
    :rtype: list
    """
    with pg_conn() as conn:
        with conn.cursor() as cursor:
            try:

                test_count = 0
                clause = ""
                for test_name in test_names.split('+'):
                    if test_name:
                        test_count += 1
                        if test_count == 1:
                            clause = test_name
                        else:
                            clause = clause + " AND public.tests.name = " + test_name

                query = """Select DISTINCT public.gpus.name as GPUName, public.machineconfig.name as MachineName, public.machine_pools.name MachinePoolName from public.gpus
                        Join public.machineconfig on public.machineconfig.gpu_id = public.gpus.id
                        Join public.test_machine_mapping on public.test_machine_mapping.machine_id = public.machineconfig.vrl_machine_id
                        Join public.tests on public.tests.id = public.test_machine_mapping.test_id
                        Join public.machine_pools on public.machine_pools.id = public.test_machine_mapping.machine_pool_id
                        Where public.tests.name = """
                query = query + clause
                cursor.execute(query)

                res = list(cursor)
                return jsonify(res)

            except Exception as e:
                return jsonify({"status": "error", "error": str(e)}), 404
예제 #11
0
def get_package_tests(package, test_system):
    """Get test name list for given package name and test system
    :param package: Any valid DVS package name
    :type package: string
    :param test_system: Test System (Valid values are "VRL", "cvcServer")
    :type test_system: string
    :return: response of the api split into list
    :rtype: list
    """
    with pg_conn() as conn:
        with conn.cursor() as cursor:
            try:

                # Get all the test name and filter them based on mapped package name and system id
                cursor.execute(
                    '''
                    Select public.tests.name from public.tests
                    Join dvs.test_package_mapping on dvs.test_package_mapping.test_id = public.tests.id
                    Join dvs.binarydrop on dvs.binarydrop.id = dvs.test_package_mapping.package_id
                    Join public.test_system_mapping on public.test_system_mapping.test_id = public.tests.id
                    Join public.test_systems on public.test_systems.id = public.test_system_mapping.system_id
                    where dvs.binarydrop.name = %s AND public.test_systems.name = %s
                    ''', [package, test_system])

                res = list(cursor)
                return jsonify(res)

            except Exception as e:
                return jsonify({"status": "error", "error": str(e)}), 404
예제 #12
0
def single_result_getter(system, name, key, value):
    """
    Helper to get a single result based on some key
    """
    with pg_conn() as conn:
        query = sql.SQL('SELECT * FROM {}.{} WHERE {} = %s LIMIT 1;').format(sql.Identifier(system), sql.Identifier(name), sql.Identifier(key))
        rows = do_select(conn, query, [value])
        if rows is None or not rows:
            return jsonify({"status": "error", "error": "test not found"}), 404
        return jsonify(rows[0])
예제 #13
0
def scrape_binary_drop_packages():
    """
    Scrape binary drop packages.
    """
    with pg_conn() as conn:
        print("Getting list of build types in binary drop...")
        builds = all_builds()
        names = builds.keys()
        tuples = map(extract_package_parts, names)
        print("Adding list of build types to database")
        with conn.cursor() as cursor:
            cursor.executemany(
                '''INSERT INTO dvs.binarydrop (branch, build, shortname, name, scrape_periodically)
                                  VALUES (%s, %s, %s, %s, false) ON CONFLICT (name) DO NOTHING;''',
                list(tuples))
        print("Getting which builds to scrape for specific changelists...")
        to_scrape = []
        with conn.cursor() as cursor:
            cursor.execute(
                'SELECT id, name FROM dvs.binarydrop WHERE scrape_periodically = true;',
                [])
            to_scrape = list(cursor)
        for scrape_dict in to_scrape:
            name = scrape_dict["name"]
            binarydrop_id = scrape_dict["id"]
            print("Get available changelists for binary drop buildtype " +
                  name + "...")
            cl2urls = binary_urls(name)
            args = [[cl, binarydrop_id, url] for cl, url in cl2urls.items()]
            with conn.cursor() as cursor:
                try:
                    print(
                        "Deleting old changelists for binary drop package type "
                        + name + " from database...")
                    cursor.execute(
                        'DELETE FROM dvs.binarydrop_changelists WHERE binarydrop_id = %s',
                        [binarydrop_id])
                    print(
                        "Adding available changelists for binary drop package type "
                        + name + " to database...")
                    cursor.executemany(
                        '''
                        INSERT INTO dvs.binarydrop_changelists (changelist, binarydrop_id, url)
                        VALUES (%s, %s, %s)
                        ON CONFLICT (changelist, binarydrop_id) DO NOTHING;
                        ''', args)
                    # Probably want to vacuum here, although IT might do it for us.
                    # We need this because we are creating a fair bit of row churn here by deleting and then adding.
                    # Since this is run infrequently, it probably will not be an issue.
                except Exception:
                    print('Failed to add changelists')
                    conn.rollback()
예제 #14
0
def orchestrate_ingestion():
    """
    Orchestration funtion for ingestion
    """
    status, filename = save_file(request)
    if status:
        conn = pg_conn()
        csv_file = os.path.join(UPLOAD_FOLDER, filename)
        ingest_status, message = copy_csv_to_staging_table(conn=conn,
                                                           csv_file=csv_file)
        clean_files(filename)
        return jsonify(ingest_status=ingest_status, message=message), 200
    else:
        return jsonify("Ingest Failed"), 200
예제 #15
0
def fetch_distinct_machine():
    """Fetch a list of distinct machines from the machine monitoring table
    """
    machines = []
    conn = pg_conn()
    with conn.cursor() as cursor:
        machine_sql = """SELECT distinct(name)
                         FROM machine_monitoring"""
        cursor.execute(machine_sql)
        rows = cursor.fetchall()
        for row in rows:
            machines.append(row['name'])
    conn.close()
    return machines
예제 #16
0
def binarydrop_changelists(drivername):
    with pg_conn() as conn:
        with conn.cursor() as cursor:
            try:
                cursor.execute(
                    '''
                    SELECT changelist, url FROM dvs.binarydrop_changelists
                    JOIN dvs.binarydrop ON dvs.binarydrop.id = dvs.binarydrop_changelists.binarydrop_id
                    WHERE dvs.binarydrop.name = %s
                    ORDER BY 1 DESC
                    ''', [drivername])
                res = list(cursor)
                return jsonify(res)
            except Exception as e:
                return jsonify({"status": "error", "error": str(e)}), 404
예제 #17
0
def get_satellite_available():
    """
    Get a list of available satellites.
    """
    try:
        with pg_conn() as conn:
            with conn.cursor() as cursor:
                cursor.execute(
                    "DELETE FROM public.satellite WHERE expires <= (NOW() at time zone 'utc')",
                    [])
                sql = '''SELECT * FROM public.satellite'''
                cursor.execute(sql, [])
                rows = cursor.fetchall()
                return jsonify(rows)
    except Exception as e:
        logger.info(str(e))
        return jsonify({"status": "error", "error": str(e)}), 400
예제 #18
0
def get_machine_details_from_postgres(days=7, filter_by="totaltime"):
    """
    api funtion used by the UI to get the machine details.
    Currently the limit is set for the last 10 days.
    In futute this 10 days limit will be controlled via the UI.
    :return: list of dict of machine details
    :rtype: list
    """
    filters = ['totaltime', 'jobcount']
    try:
        days = days + 2
        if filter_by not in filters:
            filter_by = filters[0]
        sql = """
                SELECT * 
                FROM machine_monitoring 
                WHERE jobdate >DATE(NOW() - INTERVAL '%s DAY')
                ORDER BY jobdate"""
        result = {}
        dates = []
        conn = pg_conn()
        with conn.cursor() as cursor:
            cursor.execute(sql, [days])
            rows = cursor.fetchall()
            for row in rows:
                job_date = str(row['jobdate'])
                machine = row['name']
                gpu = row['gpu']
                total_time = int(row[filter_by])
                if job_date not in dates:
                    dates.append(job_date)
                if gpu in result:
                    result_gpu = result[gpu]
                    if machine in result_gpu:
                        result[gpu][machine].append(total_time)
                    else:
                        result[gpu][machine] = [total_time]
                else:
                    temp = {}
                    temp[machine] = [total_time]
                    result[gpu] = temp
        return jsonify(results=result, dates=dates, filter_by=filter_by)
    except Exception as e:
        print(e)
        logger.error(e)
        return jsonify("ERROR"), 400
예제 #19
0
def get_visualization_params(system, name, view, only=None):
  """
  Get available parameters to a visualization route, as well as possible values for
  those options.
  """
  # Pass in only to only get available params for certain fields.
  only_names = []
  if only:
    only_names = only.split('+')
  v = VisualiztionView.visualizations.get(view)
  if v is None:
    return jsonify({"status": "error", "error": "Unknown view " + view}), 404
  params = []
  try:
    with pg_conn(readOnly=True) as conn:
      with conn.cursor() as cursor:
        for param_name, param_value in v.options.items():
          if only and (param_name not in only_names):
            continue
          if param_value['type'] == 'query':
            ctx = {}
            merge_qargs_for_jinja(ctx, v)
            ctx['system'] = system
            ctx['name'] = name
            ctx['test_table'] = system + '.' + name
            ctx['test_table_denormal'] = system + '.' + name + '_denormal'
            query, bind_params = jsql.prepare_query(param_value['payload'], ctx)
            cursor.execute(query, list(bind_params))
            res = [row['name'] for row in cursor]
            params.append({
              'name': param_name,
              'type': 'query',
              'options': res,
              'optional': param_value['optional'],
              'doc': param_value['doc'],
              'default': param_value['default']
            })
          else:
            params.append({
              'name': param_name,
              **param_value
            })
  except Exception as e:
    return jsonify({"status": "error", "error": str(e)}), 400
  return jsonify({"params": params})
예제 #20
0
def get_all_teams():
    """get all the teams name and id
    :return: a list of dict with team name and team id
    :rtype: list
    """
    try:
        conn = pg_conn()
        with conn.cursor() as cursor:
            sql = '''
                SELECT id, name
                FROM teams
            '''
            cursor.execute(sql)
            rows = cursor.fetchall()
        conn.close()
        return jsonify(rows), 200
    except Exception as e:
        logger.error(e)
        return jsonify("ERROR"), 400
예제 #21
0
def binarydrop_all_builds(filterstring=""):
    """
    Get a list available binary builds, like Release, Debug, Develop, etc.
    Takes a filter string that can be used to narrow this down,
    because as of Jan 2020, there are about 68,000 results here, about
    68,000 of which are useless to us.
    The filter string is just a series of search terms concatenated with +.
    The table dvs.binarydrop in postgres is where all of the (potentially)
    available builds are stored - this is updated by a daily scrape job.
    """
    with pg_conn() as conn:
        with conn.cursor() as cursor:
            try:
                sql = 'SELECT name, id FROM dvs.binarydrop WHERE ' + sql_filter_clause(
                    'name', filterstring) + ';'
                cursor.execute(sql)
                rows = list(cursor)
                return jsonify(rows)
            except Exception as e:
                return jsonify({"status": "error", "error": str(e)}), 404
예제 #22
0
def push_record(system, name):
    """
    Add a test result to the compiler hub.

    Parameters:
    system: string - The name of the test system ('vrl')
    name: string - the name of the test ('shadertest')

    Body:
    JSON object containing column names -> column values for the test table. Can also be an array of such obects.

    Returns:
    {"status": "ok", "id": id(s)}
    On Error:
    {"status": "error", "error": string}
    """
    with pg_conn() as conn:
        return_value = None
        scraper = None
        try:
            writer = TestWriter(system, name, conn)

            # Check if json sent.
            json = request.json
            if json is None:
                return jsonify({"status": "error", "error": "expected JSON"})

            if isinstance(json, dict):
                # single row
                return_value = writer.insert_dict(json, dropExtra=False)[0]
            else:
                # many rows
                return_value = writer.insert_dicts(json, dropExtra=False)

        except Exception as e:
            if scraper:
                scraper.log(str(e))
            return jsonify({"status": "error", "error": str(e)})
        return jsonify({"status": "ok", "id": return_value})
예제 #23
0
def post_satellite_unadvertise():
    """
    satellites will advertise themselves by pinging this interface.
    """
    try:
        body = request.get_json()
        # Should we accept a custom satellite host?
        default_host = request.headers.get('x-forwarded-for',
                                           request.remote_addr)
        satellite_host = body.get('satellite_host', default_host)
        satellite_port = body.get('satellite_port')
        satellite_url = f'http://{satellite_host}:{satellite_port}'
        with pg_conn() as conn:
            with conn.cursor() as cursor:
                cursor.execute(
                    '''
                DELETE FROM public.satellite WHERE satellite_url = %s
                ''', [satellite_url])
        return jsonify({"status": "ok"})
    except Exception as e:
        logger.error(str(e))
        return jsonify({"status": "error", "error": str(e)}), 400
예제 #24
0
def query_all_tests(testSystem):
    """
    Get all unique tests types in the system. This can be used to check which tests a user wants to view.
    testSystem is a string like "vrl" or "ccv", or can be the string "all" for all test systems.

    Parameters:
    testSystem: string - the name of test system ('vrl')

    Returns:
    JSON[] {
        id: integer - id of the test
        system: string - name of the test system
        name: string - name of the test.
    }
    """
    filter_str = '' if (testSystem == 'all') else ' WHERE system = %s'
    filter_args = [] if (testSystem == 'all') else [testSystem]
    with pg_conn() as conn:
        rows = do_select(conn, 'SELECT * FROM test_meta ' + filter_str + ' ORDER BY system, name;', filter_args)
        if rows is None:
            return jsonify({"status": "error"}), 404
        return jsonify(rows)
예제 #25
0
def insert_into_chub(vrl_data, missing_machines):
    """insert the machine monitoring data into CHUB database
    :param vrl_data: dict of machine monitoring data.
    :type vrl_data: dict
    :param missing_machines: list of machines that missed job execution
    :type missing_machines: list
    """
    jobdate = vrl_data[0]['jobdate']
    conn = pg_conn()
    with conn.cursor() as cursor:
        insert_sql = """INSERT INTO machine_monitoring(jobdate, name, gpu, jobcount, totaltime)
                        VALUES (%s, %s, %s, %s, %s) ON CONFLICT DO NOTHING"""
        for row in vrl_data:
            params = [
                str(row['jobdate']), row['name'], row['gpu'],
                str(row['jobcount']),
                str(row['totaltime'])
            ]
            print(params)
            cursor.execute(insert_sql, params)
            cursor.execute("COMMIT")

        for machine in missing_machines:
            sql = """
                    SELECT jobdate, gpu 
                    from machine_monitoring 
                    where name=%s 
                    ORDER BY 1
                    limit 1"""
            cursor.execute(sql, [machine])
            gpu = cursor.fetchall()
            gpu = gpu[0]['gpu']
            params = [str(jobdate), machine, gpu, "0", "0"]
            print(params)
            cursor.execute(insert_sql, params)
            conn.commit()
    conn.close()
예제 #26
0
def load_csv_to_staging_table(filename, staging_table):
    """
    Loads the data into the given the staging table and later truncates it.
    This function should be used only when a trigger or stored procedure acts
    on the staging table.
    """
    try:
        start_time = time.time()
        csv_file = os.path.join(UPLOAD_FOLDER, filename)
        conn = pg_conn()
        with conn.cursor() as cursor:
            copy_sql = """COPY %s FROM STDIN WITH CSV HEADER DELIMITER AS ',' """
            data = open(csv_file, 'r')
            cursor.copy_expert(sql=copy_sql % staging_table, file=data)
            data.close()
            # commit the changes
            truncate_sql = """truncate table {}"""
            cursor.execute(truncate_sql.format(staging_table))
        conn.commit()
        conn.close()
        elapsed_time = time.time() - start_time
        return True, elapsed_time
    except Exception as e:
        return str(e)
예제 #27
0
def get_all_available_queries(system, name):
    """
    Get all available queries that should work with a given test.

    Parameters:
    system: string - the name of the test system ('vrl')
    name: string - the name of the test ('shadertest')

    Returns:
    JSON[] of visualization objects with keys:
      - "query": string - string tag that corresponds to the query.

    On Error (400)
      {"status": "error", "error": string}
    """
    try:
      with pg_conn() as conn:
        # Use a test writer to get columns in table
        test_writer = TestWriter(system, name, conn=conn)
        res = [{'query': vis.name, 'renderers': vis.renderers}
               for vis in VisualiztionView.visualizations.values() if vis.matches(test_writer.columns)]
        return jsonify(res)
    except Exception as e:
      return jsonify({"status": "error", "error": str(e)}), 400
예제 #28
0
def get_all_ops():
    """get the ops information for all the teams on chub
    :return: list of dict containing ops information for all the teams
    :rtype: list
    """
    try:
        rows = ()
        conn = pg_conn()
        with conn.cursor() as cursor:
            sql = '''
            select
            o.name as ops_name, o.wiki, og.name as grp_name, oo.name as owner_name, oo.email, t.name as team
            from ops o, ops_group og, ops_owner oo, teams t
            where o.ownerid = oo.id
            AND o.opsgroupid = og.id
            AND og.teamid = t.id
            '''
            cursor.execute(sql)
            rows = cursor.fetchall()
        conn.close()
        return jsonify(rows)
    except Exception as e:
        logger.error(e)
        return jsonify("ERROR"), 400
예제 #29
0
 def _refresh_connection(self):
     "Make sure connection is fresh if upload takes to long"
     self.connection.commit()
     self.connection.close()
     self.connection = pg_conn()