gateway/scripts/_archive/check_orphan_featureinstance.py
2026-04-29 21:27:08 +02:00

97 lines
3.3 KiB
Python

"""Quick-Check: existiert FeatureInstance-Row 6019e7d0-b23d-41ec-b9f7-3dd1293078f2
in poweron_app, und welche Mandate/Instances stehen mit dem RedmineTicketMirror in Verbindung?
Aufruf: python gateway/scripts/check_orphan_featureinstance.py
"""
from __future__ import annotations
import sys
from pathlib import Path
_GATEWAY = Path(__file__).resolve().parents[1]
if str(_GATEWAY) not in sys.path:
sys.path.insert(0, str(_GATEWAY))
import psycopg2
import psycopg2.extras
from modules.shared.configuration import APP_CONFIG
_TARGET_FI = "6019e7d0-b23d-41ec-b9f7-3dd1293078f2"
_TARGET_MANDATE = "674b1bc0-1d01-4696-a094-3374c450f6e2"
def _connect(dbName: str):
return psycopg2.connect(
host=APP_CONFIG.get("DB_HOST", "localhost"),
user=APP_CONFIG.get("DB_USER"),
password=APP_CONFIG.get("DB_PASSWORD_SECRET"),
port=int(APP_CONFIG.get("DB_PORT", 5432)),
dbname=dbName,
)
def main() -> int:
print(f"Checking FeatureInstance {_TARGET_FI} ...\n")
with _connect("poweron_app") as appConn:
with appConn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
'SELECT id, "mandateId", "featureCode", label, enabled, '
'"sysCreatedAt", "sysModifiedAt" '
'FROM "FeatureInstance" WHERE id = %s',
(_TARGET_FI,),
)
fi = cur.fetchone()
print(f"FeatureInstance row in poweron_app: {fi}\n")
cur.execute(
'SELECT id, "mandateId", "featureCode", label, enabled '
'FROM "FeatureInstance" '
'WHERE "mandateId" = %s AND "featureCode" = %s',
(_TARGET_MANDATE, "redmine"),
)
sameMandateRedmine = cur.fetchall()
print(
f"All redmine FeatureInstances on mandate {_TARGET_MANDATE} "
f"({len(sameMandateRedmine)}):"
)
for r in sameMandateRedmine:
print(f" {r}")
print()
cur.execute(
'SELECT id, name, label, enabled, "deletedAt", '
'"sysCreatedAt", "sysModifiedAt" '
'FROM "Mandate" WHERE id = %s',
(_TARGET_MANDATE,),
)
mandate = cur.fetchone()
print(f"Mandate row: {mandate}\n")
with _connect("poweron_redmine") as rmConn:
with rmConn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
'SELECT COUNT(*) AS n '
'FROM "RedmineTicketMirror" WHERE "featureInstanceId" = %s',
(_TARGET_FI,),
)
n = cur.fetchone()["n"]
print(f"RedmineTicketMirror rows with featureInstanceId={_TARGET_FI}: {n}")
cur.execute(
'SELECT DISTINCT "featureInstanceId", "mandateId", COUNT(*) AS n '
'FROM "RedmineTicketMirror" '
'GROUP BY "featureInstanceId", "mandateId" ORDER BY n DESC LIMIT 20'
)
distribution = cur.fetchall()
print(f"\nRedmineTicketMirror distribution (top 20):")
for r in distribution:
print(f" fi={r['featureInstanceId']} mandate={r['mandateId']} count={r['n']}")
return 0
if __name__ == "__main__":
sys.exit(main())