Page MenuHomeDevCentral

No OneTemporary

diff --git a/README.md b/README.md
index ce098b4..bb0c12f 100644
--- a/README.md
+++ b/README.md
@@ -1,32 +1,32 @@
## Internal reports
Reports about Nasqueron internal data.
This repository can host:
- SQL queries to get report data
- Tools to produce reports
### SQL queries
Queries are organized by cluster/server name, then by service:
- acquisitariat/ contains the queries for MySQL Docker container
used by dev & community services like DevCentral
### Tools
Tools and utilities to work with reports are located in the tools/ folder:
* **[nasqueron-reports](tools/nasqueron-reports/README.md)**:
- allows to run the MariaDB or MySQL query,
- and format the result as expected, e.g. as MediaWiki table
+ allows run the MariaDB or MySQL query and format
+ the result as expected, like as a MediaWiki table
### Contribute
This repository is intended to behave as a monorepo for reporting.
You can so add any project to generate or use a report at Nasqueron here,
regardless of the choice of technology stack.
-Software in tools/<name of the project> are intended to be built autonomously.
+Projects in tools/<name of the project> are intended to be built autonomously.
diff --git a/tools/nasqueron-reports/src/nasqueron_reports/connectors/db_mysql.py b/tools/nasqueron-reports/src/nasqueron_reports/connectors/db_mysql.py
index b321474..c4790d0 100644
--- a/tools/nasqueron-reports/src/nasqueron_reports/connectors/db_mysql.py
+++ b/tools/nasqueron-reports/src/nasqueron_reports/connectors/db_mysql.py
@@ -1,120 +1,119 @@
# -------------------------------------------------------------
# Nasqueron Reports :: Connectors :: MySQL
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Description: Connect to MySQL or MariaDB
# License: BSD-2-Clause
# -------------------------------------------------------------
import mysql.connector
import sqlparse
from nasqueron_reports.config import resolve_sql_path
from nasqueron_reports.errors import *
# -------------------------------------------------------------
# Statements parser
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def clean_statement(statement):
return sqlparse.format(statement, strip_comments=True).strip()
def split_statements(queries):
return [clean_statement(stmt) for stmt in sqlparse.split(queries)]
def extract_database(query):
if query.startswith("USE "):
tokens = query.split()
try:
return tokens[1].rstrip(";")
except IndexError:
raise NasqueronReportQueryError("Malformed USE statement", query)
raise NasqueronReportQueryError(
"When a report query contains two statements, the first statement is expected to be USE.",
query,
)
def parse_statements(query):
- """Parse SELECT or USE; SELECT; statements.
- as a main SELECT query and a database."""
+ """Parse SELECT or USE/SELECT statements as a main SELECT query and a database."""
statements = split_statements(query)
n = len(statements)
if n == 0:
raise NasqueronReportQueryError("Empty query", query)
if n > 2:
raise NasqueronReportQueryError("Too many statements in query", query)
if n == 1:
query = statements[0]
database = None
else:
query = statements[1]
database = extract_database(statements[0])
return query, database
# -------------------------------------------------------------
# Queries
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def run_query(query, db_config):
query, database = parse_statements(query)
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
if database:
conn.database = database
cursor.execute(query)
rows = cursor.fetchall()
col_names = [desc[0] for desc in cursor.description]
cursor.close()
conn.close()
return col_names, rows
# -------------------------------------------------------------
# Database configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_db_config(config):
args = config["service_options"]
return {
"host": args.get("hostname", "localhost"),
"user": args["credentials"].get("username", ""),
"password": args["credentials"].get("password", ""),
"database": args.get("database", ""),
}
# -------------------------------------------------------------
# Reports
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def fetch_report(config):
query_path = resolve_sql_path(config["path"])
with open(query_path, "r") as fd:
query = fd.read()
db_config = get_db_config(config)
try:
return run_query(query, db_config)
except mysql.connector.Error as e:
raise NasqueronReportDatabaseError(str(e))

File Metadata

Mime Type
text/x-diff
Expires
Wed, Mar 18, 11:52 (17 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3539575
Default Alt Text
(4 KB)

Event Timeline