0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00
posthog/ee/clickhouse/sql/plugin_log_entries.py

74 lines
2.1 KiB
Python
Raw Normal View History

Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
from ee.kafka_client.topics import KAFKA_PLUGIN_LOG_ENTRIES
from posthog.settings import CLICKHOUSE_CLUSTER, CLICKHOUSE_DATABASE
from posthog.tasks.delete_old_plugin_logs import TTL_WEEKS
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
from .clickhouse import KAFKA_COLUMNS, REPLACING_MERGE_TREE, kafka_engine, table_engine, ttl_period
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
PLUGIN_LOG_ENTRIES_TABLE = "plugin_log_entries"
PLUGIN_LOG_ENTRIES_TABLE_BASE_SQL = """
CREATE TABLE IF NOT EXISTS {table_name} ON CLUSTER {cluster}
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
(
id UUID,
team_id Int64,
plugin_id Int64,
plugin_config_id Int64,
timestamp DateTime64(6, 'UTC'),
source VARCHAR,
type VARCHAR,
message VARCHAR,
instance_id UUID
{extra_fields}
) ENGINE = {engine}
"""
PLUGIN_LOG_ENTRIES_TABLE_SQL = lambda: (
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
PLUGIN_LOG_ENTRIES_TABLE_BASE_SQL
+ """PARTITION BY plugin_id ORDER BY (team_id, id)
{ttl_period}
SETTINGS index_granularity=512
"""
).format(
table_name=PLUGIN_LOG_ENTRIES_TABLE,
cluster=CLICKHOUSE_CLUSTER,
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
extra_fields=KAFKA_COLUMNS,
engine=table_engine(PLUGIN_LOG_ENTRIES_TABLE, "_timestamp", REPLACING_MERGE_TREE),
ttl_period=ttl_period("timestamp", TTL_WEEKS),
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
)
KAFKA_PLUGIN_LOG_ENTRIES_TABLE_SQL = lambda: PLUGIN_LOG_ENTRIES_TABLE_BASE_SQL.format(
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
table_name="kafka_" + PLUGIN_LOG_ENTRIES_TABLE,
cluster=CLICKHOUSE_CLUSTER,
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
engine=kafka_engine(topic=KAFKA_PLUGIN_LOG_ENTRIES),
extra_fields="",
)
PLUGIN_LOG_ENTRIES_TABLE_MV_SQL = """
CREATE MATERIALIZED VIEW {table_name}_mv ON CLUSTER {cluster}
TO {database}.{table_name}
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
AS SELECT
id,
team_id,
plugin_id,
plugin_config_id,
timestamp,
source,
type,
message,
instance_id,
_timestamp,
_offset
FROM {database}.kafka_{table_name}
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
""".format(
table_name=PLUGIN_LOG_ENTRIES_TABLE, cluster=CLICKHOUSE_CLUSTER, database=CLICKHOUSE_DATABASE,
Plugin log entries (#3482) * Add Postgres model PluginLogEntry * Add equivalent PluginLogEntry to Kafka+ClickHouse * Add migration * Add PluginLogEntry.Type.LOG & make PluginLogEntry.message a TextField * Update 0130_pluginlogentry.py * Add PluginLogEntry.instance_id * Update migration * Update migration * Add plugin log entries API * Test plugin log entries DB fetching * Add PluginLogs component prototype * Fix API * Improve PluginLogs component * Remove almost unused plugin Feedback button * Update migration * Fixed typing * Fix org permission error test asserts * Fix plugin log entry tests * Fix CH plugin log entry timestamp string * Update CH test_plugin_log_entry.py * Fix plugin log entry tests across PG/CH * Satisfy mypy * Add search and limit to plugin log entry API * Send team_id in plugin config API * Rework plugin logs UI * Add plugin config team ID in tests * Add plugin config team ID in tests actually * Fix code quality * Make logs plugin config-based * Fix CH queries * Fix typing * Improve UX and fix things * Polish plugin logs logic * Update migration * Add Celery task to delete old plugin logs * Fix UX bug with loading more plugin logs * Fix missing import * Remove OrganizationMemberPermissions message change * Make mypy happy * Add PluginLogEntry.is_system * Optimize CH plugin_log_entires PARTITION/ORDER * Increment migration * Adjust plugin logs drawer display * Fix plugin_log_factory_ch * Fix plugin_log_factory_ch fix * Replace PluginLogEntry.is_system with source * Adjust PluginLogEntrySerializer * Update CH fetch_plugin_log_entries * Make kea-typegen happy
2021-05-06 09:54:32 +02:00
)
INSERT_PLUGIN_LOG_ENTRY_SQL = """
INSERT INTO plugin_log_entries SELECT %(id)s, %(team_id)s, %(plugin_id)s, %(plugin_config_id)s, %(timestamp)s, %(source)s, %(type)s, %(message)s, %(instance_id)s, now(), 0
"""
TRUNCATE_PLUGIN_LOG_ENTRIES_TABLE_SQL = (
f"TRUNCATE TABLE IF EXISTS {PLUGIN_LOG_ENTRIES_TABLE} ON CLUSTER {CLICKHOUSE_CLUSTER}"
)