github.com/greenplum-db/gpbackup@v0.0.0-20240517212602-89daab1885b3/ci/scripts/scale-test-slack-notify.py (about)

     1  #!/usr/bin/python3
     2  
     3  import os
     4  from time import sleep
     5  import psycopg2
     6  from slack_sdk.webhook import WebhookClient
     7  
     8  ## Constants for managing reporting
     9  RESULTS_DATABASE_HOST = os.environ.get('RESULTS_DATABASE_HOST')
    10  RESULTS_DATABASE_USER = os.environ.get('RESULTS_DATABASE_USER')
    11  RESULTS_DATABASE_NAME = os.environ.get('RESULTS_DATABASE_NAME')
    12  RESULTS_DATABASE_PASSWORD = os.environ.get('RESULTS_DATABASE_PASSWORD')
    13  SLACK_WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL')
    14  
    15  
    16  def get_unreported_failures():
    17      conn = psycopg2.connect(
    18          host=RESULTS_DATABASE_HOST, 
    19          user=RESULTS_DATABASE_USER, 
    20          dbname=RESULTS_DATABASE_NAME, 
    21          password=RESULTS_DATABASE_PASSWORD
    22      )
    23      cur = conn.cursor()
    24  
    25      select_string = """
    26          SELECT
    27              tr.test_id,
    28              tn.test_name,
    29              tr.run_timestamp,
    30              tr.test_runtime,
    31              tr.gpbackup_version,
    32              tr.gpdb_version,
    33              tr.was_reported
    34          FROM 
    35              prod.test_names tn
    36              left join prod.test_runs tr
    37                  on tn.test_id = tr.test_id
    38          WHERE
    39              tr.was_failed = true
    40              and tr.was_reported = false
    41          ;
    42      """
    43  
    44      cur.execute(select_string)
    45      unreported_failures = cur.fetchall()
    46      conn.commit()
    47      cur.close()
    48      conn.close()
    49      return unreported_failures
    50  
    51  def update_unreported_failures():
    52      conn = psycopg2.connect(
    53          host=RESULTS_DATABASE_HOST, 
    54          user=RESULTS_DATABASE_USER, 
    55          dbname=RESULTS_DATABASE_NAME, 
    56          password=RESULTS_DATABASE_PASSWORD
    57      )
    58      cur = conn.cursor()
    59  
    60      update_string = """
    61      UPDATE prod.test_runs
    62      SET
    63          was_reported = true
    64      WHERE
    65          was_failed = true
    66          and was_reported = false
    67      """
    68  
    69      cur.execute(update_string)
    70      conn.commit()
    71      cur.close()
    72      conn.close()
    73      return
    74  
    75  def send_slack_notification(unreported_failures):
    76      webhook = WebhookClient(SLACK_WEBHOOK_URL)
    77      webhook.send(
    78          text=f"""
    79          gpbackup/gprestore Scale perf regressions for the following test runs:
    80          {unreported_failures}
    81          """
    82      )
    83  
    84  def main():
    85      try:
    86          print("Examining runtime database to report test failures")
    87          unreported_failures = get_unreported_failures()
    88          if unreported_failures:
    89              print("Reporting test failures")
    90              send_slack_notification(unreported_failures)
    91              update_unreported_failures()
    92          else:
    93              print("No test failures found to report")
    94  
    95      except Exception as e:
    96          print("Python script errored: {}".format(e))
    97          return
    98  
    99  if __name__ == "__main__":
   100      main()