]> git.ipfire.org Git - pbs.git/commitdiff
events: Create a temporary view
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 17:08:51 +0000 (17:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 17:08:51 +0000 (17:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/events.py

index cdc42715e6aa9fbd9551ee36516fc5c8cbf1be13..7fdb82af7f8a14123d32a0f7689a0177d1615418 100644 (file)
@@ -49,412 +49,415 @@ log = logging.getLogger("pbs.events")
 # repository
 # points
 #
-EVENTS_VIEW = """
-       WITH events AS (
-               -- Build creation times
-               SELECT
-                       'build-created'::text AS type,
-                       builds.created_at AS t,
-                       4 AS priority,
-                       builds.id AS build,
-                       NULL::integer AS by_build,
-                       NULL::integer AS build_comment,
-                       NULL::integer AS build_group,
-                       NULL::integer AS job,
-                       NULL::integer AS user,
-                       builds.owner_id AS by_user,
-                       NULL::integer AS builder,
-                       NULL::integer AS repository,
-                       NULL::integer AS points
-               FROM
-                       builds
-
-               UNION ALL
-
-               -- Build finish/failed times
-               SELECT
-                       CASE
-                               WHEN builds.failed IS TRUE
-                               THEN 'build-failed'::text
-                               ELSE 'build-finished'::text
-                       END AS type,
-                       builds.finished_at AS t,
-                       CASE
-                               WHEN builds.failed IS TRUE
-                               THEN 8
-                               ELSE 4
-                       END AS priority,
-                       builds.id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       builds
-               WHERE
-                       builds.finished_at IS NOT NULL
-
-               UNION ALL
-
-               -- Deleted Builds
-               SELECT
-                       'build-deleted' AS type,
-                       builds.deleted_at AS t,
-                       4 AS priority,
-                       builds.id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       builds.deleted_by AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       builds
-               WHERE
-                       builds.deleted_at IS NOT NULL
-
-               UNION ALL
-
-               -- Deprecated Builds
-
-               SELECT
-                       'build-deprecated' AS type,
-                       builds.deprecated_at AS t,
-                       4 AS priority,
-                       builds.id AS build,
-                       builds.deprecating_build_id AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       builds.deprecated_by AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       builds
-               WHERE
-                       builds.deleted_at IS NULL
-               AND
-                       builds.deprecated_at IS NOT NULL
-
-               UNION ALL
-
-               -- Build Comments
-               SELECT
-                       'build-comment' AS type,
-                       build_comments.created_at AS t,
-                       5 AS priority,
-                       build_comments.build_id AS build,
-                       NULL AS by_build,
-                       build_comments.id AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       build_comments.user_id AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       build_comments
-               WHERE
-                       deleted IS FALSE
-
-               UNION ALL
-
-               -- Build Watchers added
-               SELECT
-                       'build-watcher-added' AS type,
-                       build_watchers.added_at AS t,
-                       1 AS priority,
-                       build_watchers.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       build_watchers.user_id AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       build_watchers
-
-               UNION ALL
-
-               -- Build Watchers removed
-               SELECT
-                       'build-watcher-removed' AS type,
-                       build_watchers.deleted_at AS t,
-                       1 AS priority,
-                       build_watchers.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       build_watchers.user_id AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       build_watchers
-               WHERE
-                       deleted_at IS NOT NULL
-
-               UNION ALL
-
-               -- Build added to repository
-               SELECT
-                       'repository-build-added' AS type,
-                       repository_builds.added_at AS t,
-                       5 AS priority,
-                       repository_builds.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       repository_builds.added_by AS by_user,
-                       NULL AS builder,
-                       repository_builds.repo_id AS repository,
-                       NULL AS points
-               FROM
-                       repository_builds
-
-               UNION ALL
-
-               -- Build removed from repository
-               SELECT
-                       'repository-build-removed' AS type,
-                       repository_builds.removed_at AS t,
-                       5 AS priority,
-                       repository_builds.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       repository_builds.removed_by AS by_user,
-                       NULL AS builder,
-                       repository_builds.repo_id AS repository,
-                       NULL AS points
-               FROM
-                       repository_builds
-               WHERE
-                       removed_at IS NOT NULL
-
-               UNION ALL
-
-               -- Build Points
-
-               SELECT
-                       'build-points' AS type,
-                       build_points.created_at AS t,
-                       1 AS priority,
-                       build_points.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       build_points.user_id AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       build_points.points AS points
-               FROM
-                       build_points
-
-               UNION ALL
-
-               -- Test Builds
-               SELECT
-                       CASE WHEN build_groups.failed IS TRUE THEN 'test-builds-failed'
-                               ELSE 'test-builds-succeeded' END AS type,
-                       build_groups.finished_at AS t,
-                       4 AS priority,
-                       builds.id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       build_groups.id AS build_group,
-                       NULL AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       builds
-               JOIN
-                       build_groups ON builds.test_group_id = build_groups.id
-               WHERE
-                       builds.deleted_at IS NULL
-               AND
-                       build_groups.deleted_at IS NULL
-               AND
-                       build_groups.finished_at IS NOT NULL
-
-               UNION ALL
-
-               -- Jobs Creations
-               SELECT
-                       'job-created' AS type,
-                       jobs.created_at AS t,
-                       1 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               WHERE
-                       jobs.deleted_at IS NULL
-
-               UNION ALL
-
-               -- Failed Jobs
-               SELECT
-                       'job-failed' AS type,
-                       jobs.finished_at AS t,
-                       5 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       jobs.builder_id AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               WHERE
-                       jobs.deleted_at IS NULL
-               AND
-                       jobs.finished_at IS NOT NULL
-               AND
-                       jobs.aborted IS FALSE
-               AND
-                       jobs.failed IS TRUE
-
-               UNION ALL
-
-               -- Finished Jobs
-               SELECT
-                       'job-finished' AS type,
-                       jobs.finished_at AS t,
-                       4 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       jobs.builder_id AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               WHERE
-                       jobs.deleted_at IS NULL
-               AND
-                       jobs.finished_at IS NOT NULL
-               AND
-                       jobs.aborted IS FALSE
-               AND
-                       jobs.failed IS FALSE
-
-               UNION ALL
-
-               -- Aborted Jobs
-               SELECT
-                       'job-aborted' AS type,
-                       jobs.finished_at AS t,
-                       4 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       jobs.aborted_by AS by_user,
-                       jobs.builder_id AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               WHERE
-                       jobs.deleted_at IS NULL
-               AND
-                       jobs.aborted IS TRUE
-
-               UNION ALL
-
-               -- Dispatched Jobs
-               SELECT
-                       'job-dispatched' AS type,
-                       jobs.started_at AS t,
-                       1 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       jobs.builder_id AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               WHERE
-                       jobs.deleted_at IS NULL
-               AND
-                       jobs.started_at IS NOT NULL
-
-               UNION ALL
-
-               -- Retried jobs
-               SELECT
-                       'job-retry' AS type,
-                       jobs.created_at AS t,
-                       4 AS priority,
-                       jobs.build_id AS build,
-                       NULL AS by_build,
-                       NULL AS build_comment,
-                       NULL AS build_group,
-                       jobs.id AS job,
-                       NULL AS user,
-                       NULL AS by_user,
-                       NULL AS builder,
-                       NULL AS repository,
-                       NULL AS points
-               FROM
-                       jobs
-               JOIN
-                       jobs superseeded_jobs ON superseeded_jobs.superseeded_by = jobs.id
-               WHERE
-                       jobs.deleted_at IS NULL
-       )
-"""
 
 class Events(base.Object):
+       def init(self):
+               # Create events view
+               self.db.execute("""
+                       CREATE TEMPORARY VIEW events AS (
+                               -- Build creation times
+                               SELECT
+                                       'build-created'::text AS type,
+                                       builds.created_at AS t,
+                                       4 AS priority,
+                                       builds.id AS build,
+                                       NULL::integer AS by_build,
+                                       NULL::integer AS build_comment,
+                                       NULL::integer AS build_group,
+                                       NULL::integer AS job,
+                                       NULL::integer AS user,
+                                       builds.owner_id AS by_user,
+                                       NULL::integer AS builder,
+                                       NULL::integer AS repository,
+                                       NULL::integer AS points
+                               FROM
+                                       builds
+
+                               UNION ALL
+
+                               -- Build finish/failed times
+                               SELECT
+                                       CASE
+                                               WHEN builds.failed IS TRUE
+                                               THEN 'build-failed'::text
+                                               ELSE 'build-finished'::text
+                                       END AS type,
+                                       builds.finished_at AS t,
+                                       CASE
+                                               WHEN builds.failed IS TRUE
+                                               THEN 8
+                                               ELSE 4
+                                       END AS priority,
+                                       builds.id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       builds
+                               WHERE
+                                       builds.finished_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Deleted Builds
+                               SELECT
+                                       'build-deleted' AS type,
+                                       builds.deleted_at AS t,
+                                       4 AS priority,
+                                       builds.id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       builds.deleted_by AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       builds
+                               WHERE
+                                       builds.deleted_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Deprecated Builds
+
+                               SELECT
+                                       'build-deprecated' AS type,
+                                       builds.deprecated_at AS t,
+                                       4 AS priority,
+                                       builds.id AS build,
+                                       builds.deprecating_build_id AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       builds.deprecated_by AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       builds
+                               WHERE
+                                       builds.deleted_at IS NULL
+                               AND
+                                       builds.deprecated_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Build Comments
+                               SELECT
+                                       'build-comment' AS type,
+                                       build_comments.created_at AS t,
+                                       5 AS priority,
+                                       build_comments.build_id AS build,
+                                       NULL AS by_build,
+                                       build_comments.id AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       build_comments.user_id AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       build_comments
+                               WHERE
+                                       deleted IS FALSE
+
+                               UNION ALL
+
+                               -- Build Watchers added
+                               SELECT
+                                       'build-watcher-added' AS type,
+                                       build_watchers.added_at AS t,
+                                       1 AS priority,
+                                       build_watchers.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       build_watchers.user_id AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       build_watchers
+
+                               UNION ALL
+
+                               -- Build Watchers removed
+                               SELECT
+                                       'build-watcher-removed' AS type,
+                                       build_watchers.deleted_at AS t,
+                                       1 AS priority,
+                                       build_watchers.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       build_watchers.user_id AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       build_watchers
+                               WHERE
+                                       deleted_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Build added to repository
+                               SELECT
+                                       'repository-build-added' AS type,
+                                       repository_builds.added_at AS t,
+                                       5 AS priority,
+                                       repository_builds.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       repository_builds.added_by AS by_user,
+                                       NULL AS builder,
+                                       repository_builds.repo_id AS repository,
+                                       NULL AS points
+                               FROM
+                                       repository_builds
+
+                               UNION ALL
+
+                               -- Build removed from repository
+                               SELECT
+                                       'repository-build-removed' AS type,
+                                       repository_builds.removed_at AS t,
+                                       5 AS priority,
+                                       repository_builds.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       repository_builds.removed_by AS by_user,
+                                       NULL AS builder,
+                                       repository_builds.repo_id AS repository,
+                                       NULL AS points
+                               FROM
+                                       repository_builds
+                               WHERE
+                                       removed_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Build Points
+
+                               SELECT
+                                       'build-points' AS type,
+                                       build_points.created_at AS t,
+                                       1 AS priority,
+                                       build_points.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       build_points.user_id AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       build_points.points AS points
+                               FROM
+                                       build_points
+
+                               UNION ALL
+
+                               -- Test Builds
+                               SELECT
+                                       CASE WHEN build_groups.failed IS TRUE THEN 'test-builds-failed'
+                                               ELSE 'test-builds-succeeded' END AS type,
+                                       build_groups.finished_at AS t,
+                                       4 AS priority,
+                                       builds.id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       build_groups.id AS build_group,
+                                       NULL AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       builds
+                               JOIN
+                                       build_groups ON builds.test_group_id = build_groups.id
+                               WHERE
+                                       builds.deleted_at IS NULL
+                               AND
+                                       build_groups.deleted_at IS NULL
+                               AND
+                                       build_groups.finished_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Jobs Creations
+                               SELECT
+                                       'job-created' AS type,
+                                       jobs.created_at AS t,
+                                       1 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               WHERE
+                                       jobs.deleted_at IS NULL
+
+                               UNION ALL
+
+                               -- Failed Jobs
+                               SELECT
+                                       'job-failed' AS type,
+                                       jobs.finished_at AS t,
+                                       5 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       jobs.builder_id AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               WHERE
+                                       jobs.deleted_at IS NULL
+                               AND
+                                       jobs.finished_at IS NOT NULL
+                               AND
+                                       jobs.aborted IS FALSE
+                               AND
+                                       jobs.failed IS TRUE
+
+                               UNION ALL
+
+                               -- Finished Jobs
+                               SELECT
+                                       'job-finished' AS type,
+                                       jobs.finished_at AS t,
+                                       4 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       jobs.builder_id AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               WHERE
+                                       jobs.deleted_at IS NULL
+                               AND
+                                       jobs.finished_at IS NOT NULL
+                               AND
+                                       jobs.aborted IS FALSE
+                               AND
+                                       jobs.failed IS FALSE
+
+                               UNION ALL
+
+                               -- Aborted Jobs
+                               SELECT
+                                       'job-aborted' AS type,
+                                       jobs.finished_at AS t,
+                                       4 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       jobs.aborted_by AS by_user,
+                                       jobs.builder_id AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               WHERE
+                                       jobs.deleted_at IS NULL
+                               AND
+                                       jobs.aborted IS TRUE
+
+                               UNION ALL
+
+                               -- Dispatched Jobs
+                               SELECT
+                                       'job-dispatched' AS type,
+                                       jobs.started_at AS t,
+                                       1 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       jobs.builder_id AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               WHERE
+                                       jobs.deleted_at IS NULL
+                               AND
+                                       jobs.started_at IS NOT NULL
+
+                               UNION ALL
+
+                               -- Retried jobs
+                               SELECT
+                                       'job-retry' AS type,
+                                       jobs.created_at AS t,
+                                       4 AS priority,
+                                       jobs.build_id AS build,
+                                       NULL AS by_build,
+                                       NULL AS build_comment,
+                                       NULL AS build_group,
+                                       jobs.id AS job,
+                                       NULL AS user,
+                                       NULL AS by_user,
+                                       NULL AS builder,
+                                       NULL AS repository,
+                                       NULL AS points
+                               FROM
+                                       jobs
+                               JOIN
+                                       jobs superseeded_jobs ON superseeded_jobs.superseeded_by = jobs.id
+                               WHERE
+                                       jobs.deleted_at IS NULL
+                       )
+               """)
+
        @lazy_property
        def map(self):
                return {
@@ -553,16 +556,13 @@ class Events(base.Object):
 
                # Fetch all events
                events = self.db.query("""
-                       -- Fetch all events
-                       %(EVENTS_VIEW)s
-
                        -- Filter out everything we want
                        SELECT
                                *
                        FROM
                                events
                        WHERE
-                               %(conditions)s
+                               %s
 
                        -- Sort everything in reverse order
                        ORDER BY
@@ -571,10 +571,8 @@ class Events(base.Object):
                                %%s
                        LIMIT
                                %%s
-                       """ % {
-                               "EVENTS_VIEW" : EVENTS_VIEW,
-                               "conditions"  : " AND ".join(conditions) or "TRUE",
-                       }, *values, offset, limit,
+                       """ % " AND ".join(conditions) or "TRUE",
+                       *values, offset, limit,
                )
 
                # Expand all events