You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: db/schema.sql
+7-1Lines changed: 7 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ CREATE FUNCTION api_umbrella.analytics_cache_extract_unique_user_ids() RETURNS t
67
67
AS $$
68
68
BEGIN
69
69
IF (jsonb_typeof(NEW.data->'aggregations'->'unique_user_ids'->'buckets') ='array') THEN
70
-
NEW.unique_user_ids := (SELECT array_agg(DISTINCT bucket->>'key')::uuid[] FROM jsonb_array_elements(NEW.data->'aggregations'->'unique_user_ids'->'buckets') AS bucket);
70
+
NEW.unique_user_ids := (SELECT array_agg(DISTINCT bucket->'key'->>'user_id')::uuid[] FROM jsonb_array_elements(NEW.data->'aggregations'->'unique_user_ids'->'buckets') AS bucket);
'average', ROUND(SUM(CASE WHEN interval_totals.response_time_average IS NOT NULL AND interval_totals.hit_count IS NOT NULL THEN interval_totals.response_time_average * interval_totals.hit_count END) / SUM(CASE WHEN interval_totals.response_time_average IS NOT NULL AND interval_totals.hit_count IS NOT NULL THEN interval_totals.hit_count END))
60
104
)
61
105
) AS response
62
-
FROM (
63
-
SELECT
64
-
interval_date,
65
-
hit_count,
66
-
response_time_average,
67
-
(
68
-
SELECT array_agg(DISTINCT user_id)
69
-
FROM unnest(interval_agg.user_ids) AS user_id
70
-
LEFT JOIN api_users ON user_id = api_users.id
71
-
WHERE user_id IS NOT NULL AND api_users.disabled_at IS NULL
72
-
) AS unique_user_ids
73
-
FROM (
74
-
SELECT
75
-
substring(data->'aggregations'->'hits_over_time'->'buckets'->0->>'key_as_string' from 1 for :date_key_length) AS interval_date,
76
-
SUM((data->'aggregations'->'hits_over_time'->'buckets'->0->>'doc_count')::bigint) AS hit_count,
77
-
array_accum(unique_user_ids) AS user_ids,
78
-
SUM(ROUND((data->'aggregations'->'response_time_average'->>'value')::numeric)) AS response_time_average
79
-
FROM analytics_cache
80
-
WHERE id IN :ids
81
-
GROUP BY interval_date
82
-
ORDER BY interval_date
83
-
) AS interval_agg
84
-
) AS interval_totals
106
+
FROM interval_totals
85
107
]]
86
108
87
109
-- Expire the monthly data in 3 months. While the historical data shouldn't
@@ -97,7 +119,7 @@ local function generate_organization_summary(start_time, end_time, recent_start_
97
119
date_key_length=7,
98
120
}, {
99
121
fatal=true,
100
-
statement_timeout=5*60*1000, -- 5 minutes
122
+
statement_timeout=db_statement_timeout_ms,
101
123
})[1]["response"]
102
124
103
125
search:set_start_time(recent_start_time)
@@ -112,13 +134,26 @@ local function generate_organization_summary(start_time, end_time, recent_start_
iflast_month[1] ~=expected_last_monthorlast_day[1] ~=expected_last_dayor#analytics_cache_ids~=#response["hits"]["monthly"] or#recent_analytics_cache_ids~=#response["hits"]["recent"]["daily"] then
154
+
returnnil, "incomplete data"
155
+
end
156
+
122
157
localresponse_json=json_encode(response)
123
158
expires_at=ngx.now() +60*60*24*2-- 2 days
124
159
Cache:upsert(cache_id, response_json, expires_at)
@@ -127,6 +162,8 @@ local function generate_organization_summary(start_time, end_time, recent_start_
0 commit comments