Skip to content

Commit

Permalink
populate guild usage from deployment metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinfuchs committed Feb 24, 2024
1 parent 98f56d1 commit 7b5686a
Show file tree
Hide file tree
Showing 25 changed files with 523 additions and 129 deletions.
21 changes: 17 additions & 4 deletions kite-service/internal/api/handler/deployment/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/merlinfuchs/dismod/distype"
"github.com/merlinfuchs/kite/kite-service/internal/api/helpers"
"github.com/merlinfuchs/kite/kite-service/pkg/wire"
)
Expand All @@ -13,8 +14,10 @@ func (h *DeploymentHandler) HandleDeploymentSummaryMetricGet(c *fiber.Ctx) error
startOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)

metric, err := h.deploymentMetrics.GetDeploymentsMetricsSummary(
c.Context(), c.Params("guildID"),
startOfMonth, now,
c.Context(),
distype.Snowflake(c.Params("guildID")),
startOfMonth,
now,
)
if err != nil {
return err
Expand Down Expand Up @@ -76,7 +79,12 @@ func (h *DeploymentHandler) HandleDeploymentsEventMetricsList(c *fiber.Ctx) erro
return err
}

metrics, err := h.deploymentMetrics.GetDeploymentsEventMetrics(c.Context(), c.Params("guildID"), startAt, groupBy)
metrics, err := h.deploymentMetrics.GetDeploymentsEventMetrics(
c.Context(),
distype.Snowflake(c.Params("guildID")),
startAt,
groupBy,
)
if err != nil {
return err
}
Expand All @@ -98,7 +106,12 @@ func (h *DeploymentHandler) HandleDeploymentsCallMetricsList(c *fiber.Ctx) error
return err
}

metrics, err := h.deploymentMetrics.GetDeploymentsCallMetrics(c.Context(), c.Params("guildID"), startAt, groupBy)
metrics, err := h.deploymentMetrics.GetDeploymentsCallMetrics(
c.Context(),
distype.Snowflake(c.Params("guildID")),
startAt,
groupBy,
)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion kite-service/internal/cmd/server/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ func migrateRun(store string, operation string, opts migrate.MigrateOpts) error

logging.SetupLogger(cfg.Log)

migrate.Migrate(cfg, store, operation, migrate.MigrateOpts{})
migrate.Migrate(cfg, store, operation, opts)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ CREATE TABLE IF NOT EXISTS guild_usage (
id BIGSERIAL PRIMARY KEY,
guild_id TEXT NOT NULL REFERENCES guilds(id) ON DELETE CASCADE,

total_event_count INT NOT NULL,
success_event_count INT NOT NULL,
total_event_execution_time BIGINT NOT NULL, -- in microseconds
avg_event_execution_time BIGINT NOT NULL, -- in microseconds
total_event_total_time BIGINT NOT NULL, -- in microseconds
avg_event_total_time BIGINT NOT NULL, -- in microseconds
total_call_count INT NOT NULL,
success_call_count INT NOT NULL,
total_call_total_time BIGINT NOT NULL, -- in microseconds
avg_call_total_time BIGINT NOT NULL, -- in microseconds

period_starts_at TIMESTAMP NOT NULL,
period_ends_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
period_ends_at TIMESTAMP NOT NULL
);
37 changes: 24 additions & 13 deletions kite-service/internal/db/postgres/pgmodel/deployment_metrics.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions kite-service/internal/db/postgres/pgmodel/guild_usage.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions kite-service/internal/db/postgres/pgmodel/guilds.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion kite-service/internal/db/postgres/pgmodel/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions kite-service/internal/db/postgres/queries/deployment_metrics.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ INSERT INTO deployment_metrics (

-- name: GetDeploymentsMetricsSummary :one
SELECT
COUNT(*) AS total_count,
SUM(
COUNT(*) FILTER (WHERE type = 'EVENT_HANDLED') AS total_event_count,
COALESCE(SUM(
CASE WHEN event_success = TRUE THEN
1
ELSE
0
END) AS success_count,
AVG(event_execution_time) FILTER (WHERE type = 'EVENT_HANDLED') AS avg_event_execution_time,
SUM(event_execution_time) FILTER (WHERE type = 'EVENT_HANDLED') AS total_event_execution_time,
AVG(event_total_time) FILTER (WHERE type = 'EVENT_HANDLED') AS avg_event_total_time,
SUM(event_total_time) FILTER (WHERE type = 'EVENT_HANDLED') AS total_event_total_time,
AVG(call_total_time) FILTER (WHERE type = 'CALL_EXECUTED') AS avg_call_total_time,
SUM(call_total_time) FILTER (WHERE type = 'CALL_EXECUTED') AS total_call_total_time
END) FILTER (WHERE type = 'EVENT_HANDLED'), 0)::bigint AS success_event_count,
COALESCE(AVG(event_execution_time) FILTER (WHERE type = 'EVENT_HANDLED'), 0)::double precision AS avg_event_execution_time,
COALESCE(SUM(event_execution_time) FILTER (WHERE type = 'EVENT_HANDLED'), 0)::bigint AS total_event_execution_time,
COALESCE(AVG(event_total_time) FILTER (WHERE type = 'EVENT_HANDLED'), 0)::double precision AS avg_event_total_time,
COALESCE(SUM(event_total_time) FILTER (WHERE type = 'EVENT_HANDLED'), 0::bigint)::bigint AS total_event_total_time,
COUNT(*) FILTER (WHERE type = 'CALL_EXECUTED') AS total_call_count,
COALESCE(SUM(
CASE WHEN call_success = TRUE THEN
1
ELSE
0
END) FILTER (WHERE type = 'CALL_EXECUTED'), 0)::bigint AS success_call_count,
COALESCE(AVG(call_total_time) FILTER (WHERE type = 'CALL_EXECUTED'), 0)::double precision AS avg_call_total_time,
COALESCE(SUM(call_total_time) FILTER (WHERE type = 'CALL_EXECUTED'), 0)::bigint AS total_call_total_time
FROM
deployment_metrics
LEFT JOIN
Expand Down Expand Up @@ -195,4 +202,4 @@ FROM (
AND TYPE = 'CALL_EXECUTED'
GROUP BY
trunc_timestamp) AS y
RIGHT JOIN generate_series(date_trunc(sqlc.arg (precision)::text, sqlc.arg (start_at)::timestamp), sqlc.arg (end_at)::timestamp, (sqlc.arg (series_step)::text)::interval) ON trunc_timestamp = generate_series;
RIGHT JOIN generate_series(date_trunc(sqlc.arg (precision)::text, sqlc.arg (start_at)::timestamp), sqlc.arg (end_at)::timestamp, (sqlc.arg (series_step)::text)::interval) ON trunc_timestamp = generate_series;
34 changes: 33 additions & 1 deletion kite-service/internal/db/postgres/queries/guild_usage.sql
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
-- name: UpdateGuildUsage :exec
-- name: CreateGuildUsageEntry :exec
INSERT INTO guild_usage (
guild_id,
total_event_count,
success_event_count,
total_event_execution_time,
avg_event_execution_time,
total_event_total_time,
avg_event_total_time,
total_call_count,
success_call_count,
total_call_total_time,
avg_call_total_time,
period_starts_at,
period_ends_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13
);

-- name: GetLastGuildUsageEntry :one
SELECT * FROM guild_usage WHERE guild_id = $1 ORDER BY period_ends_at DESC LIMIT 1;
5 changes: 4 additions & 1 deletion kite-service/internal/db/postgres/queries/guilds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ RETURNING *;
SELECT * FROM guilds ORDER BY name DESC;

-- name: GetGuild :one
SELECT * FROM guilds WHERE id = $1;
SELECT * FROM guilds WHERE id = $1;

-- name: GetDistinctGuildIDs :many
SELECT DISTINCT id FROM guilds;
Loading

0 comments on commit 7b5686a

Please sign in to comment.