/// import ClickHouse from '@posthog/clickhouse'; import { Meta, PluginAttachment, PluginConfigSchema, PluginEvent, Properties } from '@posthog/plugin-scaffold'; import { Pool as GenericPool } from 'generic-pool'; import { StatsD } from 'hot-shots'; import { Redis } from 'ioredis'; import { Kafka } from 'kafkajs'; import { DateTime } from 'luxon'; import { JobQueueManager } from 'main/job-queues/job-queue-manager'; import { Job } from 'node-schedule'; import { Pool } from 'pg'; import { VM } from 'vm2'; import { DB } from './utils/db/db'; import { KafkaProducerWrapper } from './utils/db/kafka-producer-wrapper'; import { InternalMetrics } from './utils/internal-metrics'; import { PluginMetricsManager } from './utils/plugin-metrics'; import { UUID } from './utils/utils'; import { ActionManager } from './worker/ingestion/action-manager'; import { ActionMatcher } from './worker/ingestion/action-matcher'; import { HookCommander } from './worker/ingestion/hooks'; import { OrganizationManager } from './worker/ingestion/organization-manager'; import { EventsProcessor } from './worker/ingestion/process-event'; import { TeamManager } from './worker/ingestion/team-manager'; import { PluginsApiKeyManager } from './worker/vm/extensions/helpers/api-key-manager'; import { LazyPluginVM } from './worker/vm/lazy'; export declare enum LogLevel { None = "none", Debug = "debug", Info = "info", Log = "log", Warn = "warn", Error = "error" } export declare const logLevelToNumber: Record; export interface PluginsServerConfig extends Record { WORKER_CONCURRENCY: number; TASKS_PER_WORKER: number; TASK_TIMEOUT: number; CELERY_DEFAULT_QUEUE: string; DATABASE_URL: string | null; POSTHOG_DB_NAME: string | null; POSTHOG_DB_USER: string; POSTHOG_DB_PASSWORD: string; POSTHOG_POSTGRES_HOST: string; POSTHOG_POSTGRES_PORT: number; CLICKHOUSE_HOST: string; CLICKHOUSE_DATABASE: string; CLICKHOUSE_USER: string; CLICKHOUSE_PASSWORD: string | null; CLICKHOUSE_CA: string | null; CLICKHOUSE_SECURE: boolean; KAFKA_ENABLED: boolean; KAFKA_HOSTS: string | null; KAFKA_CLIENT_CERT_B64: string | null; KAFKA_CLIENT_CERT_KEY_B64: string | null; KAFKA_TRUSTED_CERT_B64: string | null; KAFKA_CONSUMPTION_TOPIC: string | null; KAFKA_PRODUCER_MAX_QUEUE_SIZE: number; KAFKA_MAX_MESSAGE_BATCH_SIZE: number; KAFKA_FLUSH_FREQUENCY_MS: number; PLUGINS_CELERY_QUEUE: string; REDIS_URL: string; POSTHOG_REDIS_PASSWORD: string; POSTHOG_REDIS_HOST: string; POSTHOG_REDIS_PORT: number; BASE_DIR: string; PLUGINS_RELOAD_PUBSUB_CHANNEL: string; LOG_LEVEL: LogLevel; SENTRY_DSN: string | null; STATSD_HOST: string | null; STATSD_PORT: number; STATSD_PREFIX: string; SCHEDULE_LOCK_TTL: number; REDIS_POOL_MIN_SIZE: number; REDIS_POOL_MAX_SIZE: number; DISABLE_MMDB: boolean; DISTINCT_ID_LRU_SIZE: number; INTERNAL_MMDB_SERVER_PORT: number; PLUGIN_SERVER_IDLE: boolean; JOB_QUEUES: string; JOB_QUEUE_GRAPHILE_URL: string; JOB_QUEUE_GRAPHILE_SCHEMA: string; JOB_QUEUE_GRAPHILE_PREPARED_STATEMENTS: boolean; JOB_QUEUE_S3_AWS_ACCESS_KEY: string; JOB_QUEUE_S3_AWS_SECRET_ACCESS_KEY: string; JOB_QUEUE_S3_AWS_REGION: string; JOB_QUEUE_S3_BUCKET_NAME: string; JOB_QUEUE_S3_PREFIX: string; CRASH_IF_NO_PERSISTENT_JOB_QUEUE: boolean; STALENESS_RESTART_SECONDS: number; CAPTURE_INTERNAL_METRICS: boolean; PISCINA_USE_ATOMICS: boolean; PISCINA_ATOMICS_TIMEOUT: number; SITE_URL: string | null; NEW_PERSON_PROPERTIES_UPDATE_ENABLED_TEAMS: string; } export interface Hub extends PluginsServerConfig { instanceId: UUID; db: DB; postgres: Pool; redisPool: GenericPool; clickhouse?: ClickHouse; kafka?: Kafka; kafkaProducer?: KafkaProducerWrapper; statsd?: StatsD; internalMetrics?: InternalMetrics; pluginMetricsManager: PluginMetricsManager; pluginMetricsJob: Job | undefined; plugins: Map; pluginConfigs: Map; pluginConfigsPerTeam: Map; pluginSchedule: Record | null; pluginSchedulePromises: Record | null>>; pluginConfigSecrets: Map; pluginConfigSecretLookup: Map; teamManager: TeamManager; organizationManager: OrganizationManager; pluginsApiKeyManager: PluginsApiKeyManager; actionManager: ActionManager; actionMatcher: ActionMatcher; hookCannon: HookCommander; eventsProcessor: EventsProcessor; jobQueueManager: JobQueueManager; lastActivity: number; lastActivityType: string; } export interface Pausable { pause: () => Promise | void; resume: () => Promise | void; isPaused: () => boolean; } export interface Queue extends Pausable { start: () => Promise | void; stop: () => Promise | void; } export declare type OnJobCallback = (queue: EnqueuedJob[]) => Promise | void; export interface EnqueuedJob { type: string; payload: Record; timestamp: number; pluginConfigId: number; pluginConfigTeam: number; } export interface JobQueue { startConsumer: (onJob: OnJobCallback) => Promise | void; stopConsumer: () => Promise | void; pauseConsumer: () => Promise | void; resumeConsumer: () => Promise | void; isConsumerPaused: () => boolean; connectProducer: () => Promise | void; enqueue: (job: EnqueuedJob) => Promise | void; disconnectProducer: () => Promise | void; } export declare enum JobQueueType { FS = "fs", Graphile = "graphile", S3 = "s3" } export declare enum JobQueuePersistence { Local = "local", Concurrent = "concurrent", Redlocked = "redlocked" } export declare type JobQueueExport = { type: JobQueueType; persistence: JobQueuePersistence; getQueue: (serverConfig: PluginsServerConfig) => JobQueue; }; export declare type PluginId = Plugin['id']; export declare type PluginConfigId = PluginConfig['id']; export declare type TeamId = Team['id']; export declare enum MetricMathOperations { Increment = "increment", Max = "max", Min = "min" } export declare type StoredMetricMathOperations = 'max' | 'min' | 'sum'; export declare type StoredPluginMetrics = Record | null; export declare type PluginMetricsVmResponse = Record | null; export interface Plugin { id: number; organization_id: string; name: string; plugin_type: 'local' | 'respository' | 'custom' | 'source'; description?: string; is_global: boolean; is_preinstalled: boolean; url?: string; config_schema: Record | PluginConfigSchema[]; tag?: string; archive: Buffer | null; source?: string; error?: PluginError; from_json?: boolean; from_web?: boolean; created_at: string; updated_at: string; capabilities?: PluginCapabilities; metrics?: StoredPluginMetrics; } export interface PluginCapabilities { jobs?: string[]; scheduled_tasks?: string[]; methods?: string[]; } export interface PluginConfig { id: number; team_id: TeamId; plugin?: Plugin; plugin_id: PluginId; enabled: boolean; order: number; config: Record; error?: PluginError; attachments?: Record; vm?: LazyPluginVM | null; created_at: string; updated_at: string; } export interface PluginJsonConfig { name?: string; description?: string; url?: string; main?: string; lib?: string; config?: Record | PluginConfigSchema[]; } export interface PluginError { message: string; time: string; name?: string; stack?: string; event?: PluginEvent | null; } export interface PluginAttachmentDB { id: number; team_id: TeamId | null; plugin_config_id: PluginConfigId | null; key: string; content_type: string; file_size: number | null; file_name: string; contents: Buffer | null; } export declare enum PluginLogEntrySource { System = "SYSTEM", Plugin = "PLUGIN", Console = "CONSOLE" } export declare enum PluginLogEntryType { Debug = "DEBUG", Log = "LOG", Info = "INFO", Warn = "WARN", Error = "ERROR" } export interface PluginLogEntry { id: string; team_id: number; plugin_id: number; plugin_config_id: number; timestamp: string; source: PluginLogEntrySource; type: PluginLogEntryType; message: string; instance_id: string; } export declare enum PluginTaskType { Job = "job", Schedule = "schedule" } export interface PluginTask { name: string; type: PluginTaskType; exec: (payload?: Record) => Promise; } export declare type WorkerMethods = { onEvent: (event: PluginEvent) => Promise; onSnapshot: (event: PluginEvent) => Promise; processEvent: (event: PluginEvent) => Promise; ingestEvent: (event: PluginEvent) => Promise; }; export declare type VMMethods = { setupPlugin?: () => Promise; teardownPlugin?: () => Promise; onEvent?: (event: PluginEvent) => Promise; onSnapshot?: (event: PluginEvent) => Promise; exportEvents?: (events: PluginEvent[]) => Promise; processEvent?: (event: PluginEvent) => Promise; }; export interface PluginConfigVMResponse { vm: VM; methods: VMMethods; tasks: Record>; } export interface PluginConfigVMInternalResponse { methods: VMMethods; tasks: Record>; meta: M; } export interface EventUsage { event: string; usage_count: number | null; volume: number | null; } export interface PropertyUsage { key: string; usage_count: number | null; volume: number | null; } export interface BaseEventMessage { distinct_id: string; ip: string; site_url: string; team_id: number; uuid: string; } export interface RawEventMessage extends BaseEventMessage { data: string; now: string; sent_at: string; kafka_offset: string; } export interface EventMessage extends BaseEventMessage { data: PluginEvent; now: DateTime; sent_at: DateTime | null; } export interface RawOrganization { id: string; name: string; created_at: string; updated_at: string; available_features: string[]; } export interface Team { id: number; uuid: string; organization_id: string; name: string; anonymize_ips: boolean; api_token: string; app_urls: string[]; completed_snippet_onboarding: boolean; opt_out_capture: boolean; slack_incoming_webhook: string; session_recording_opt_in: boolean; ingested_event: boolean; } export interface Element { text?: string; tag_name?: string; href?: string; attr_id?: string; attr_class?: string[]; nth_child?: number; nth_of_type?: number; attributes?: Record; event_id?: number; order?: number; group_id?: number; } export interface ElementGroup { id: number; hash: string; team_id: number; } export interface Event { id: number; event?: string; properties: Record; elements?: Element[]; timestamp: string; team_id: number; distinct_id: string; elements_hash: string; created_at: string; } export interface ClickHouseEvent extends Omit { uuid: string; elements_chain: string; } export interface DeadLetterQueueEvent { id: string; event_uuid: string; event: string; properties: string; distinct_id: string; team_id: number; elements_chain: string; created_at: string; ip: string; site_url: string; now: string; raw_payload: string; error_timestamp: string; error_location: string; error: string; _timestamp: string; _offset: number; } export interface BasePerson { id: number; team_id: number; properties: Properties; is_user_id: number; is_identified: boolean; uuid: string; properties_last_updated_at: Record; properties_last_operation: Record | null; } export interface RawPerson extends BasePerson { created_at: string; version: string | null; } export interface Person extends BasePerson { created_at: DateTime; version: number; } export interface ClickHousePerson { id: string; created_at: string; team_id: number; properties: string; is_identified: number; is_deleted: number; timestamp: string; } export interface ClickhouseGroup { group_type_index: number; group_key: string; created_at: string; team_id: number; group_properties: string; } export interface PersonDistinctId { id: number; team_id: number; person_id: number; distinct_id: string; } export interface ClickHousePersonDistinctId { team_id: number; person_id: string; distinct_id: string; is_deleted: 0 | 1; } export interface Cohort { id: number; name: string; deleted: boolean; groups: any[]; team_id: Team['id']; created_at: string; created_by_id: number; is_calculating: boolean; last_calculation: string; errors_calculating: number; is_static: boolean; } export interface CohortPeople { id: number; cohort_id: number; person_id: number; } export interface Hook { id: string; team_id: number; user_id: number; resource_id: number | null; event: string; target: string; created: string; updated: string; } export declare enum PropertyOperator { Exact = "exact", IsNot = "is_not", IContains = "icontains", NotIContains = "not_icontains", Regex = "regex", NotRegex = "not_regex", GreaterThan = "gt", LessThan = "lt", IsSet = "is_set", IsNotSet = "is_not_set" } interface PropertyFilterBase { key: string; value?: string | number | Array | null; label?: string; } export interface PropertyFilterWithOperator extends PropertyFilterBase { operator?: PropertyOperator; } export interface EventPropertyFilter extends PropertyFilterWithOperator { type: 'event'; } export interface PersonPropertyFilter extends PropertyFilterWithOperator { type: 'person'; } export interface ElementPropertyFilter extends PropertyFilterWithOperator { type: 'element'; key: 'tag_name' | 'text' | 'href' | 'selector'; value: string | string[]; } export interface CohortPropertyFilter extends PropertyFilterBase { type: 'cohort'; key: 'id'; value: number | string; } export declare type PropertyFilter = EventPropertyFilter | PersonPropertyFilter | ElementPropertyFilter | CohortPropertyFilter; export declare enum ActionStepUrlMatching { Contains = "contains", Regex = "regex", Exact = "exact" } export interface ActionStep { id: number; action_id: number; tag_name: string | null; text: string | null; href: string | null; selector: string | null; url: string | null; url_matching: ActionStepUrlMatching | null; name: string | null; event: string | null; properties: PropertyFilter[] | null; } export interface RawAction { id: number; team_id: TeamId; name: string | null; created_at: string; created_by_id: number | null; deleted: boolean; post_to_slack: boolean; slack_message_format: string; is_calculating: boolean; updated_at: string; last_calculated_at: string; } export interface Action extends RawAction { steps: ActionStep[]; } export interface ActionEventPair { id: number; action_id: Action['id']; event_id: Event['id']; } export interface SessionRecordingEvent { uuid: string; timestamp: string; team_id: number; distinct_id: string; session_id: string; window_id: string; snapshot_data: string; created_at: string; } export interface PostgresSessionRecordingEvent extends Omit { id: string; } export declare enum TimestampFormat { ClickHouseSecondPrecision = "clickhouse-second-precision", ClickHouse = "clickhouse", ISO = "iso" } export declare enum Database { ClickHouse = "clickhouse", Postgres = "postgres" } export interface ScheduleControl { stopSchedule: () => Promise; reloadSchedule: () => Promise; } export interface JobQueueConsumerControl { stop: () => Promise; resume: () => Promise | void; } export declare type IngestEventResponse = { success?: boolean; error?: string; }; export interface EventDefinitionType { id: string; name: string; volume_30_day: number | null; query_usage_30_day: number | null; team_id: number; } export interface PropertyDefinitionType { id: string; name: string; is_numerical: boolean; volume_30_day: number | null; query_usage_30_day: number | null; team_id: number; } export declare type PluginFunction = 'onEvent' | 'processEvent' | 'onSnapshot' | 'pluginTask'; export declare enum CeleryTriggeredJobOperation { Start = "start" } export declare type GroupTypeToColumnIndex = Record; export declare enum PersonPropertyUpdateOperation { Set = "set", SetOnce = "set_once" } export {};