Skip to content

Commit

Permalink
feat: log entries hogql (#17808)
Browse files Browse the repository at this point in the history
* feat: log entries hogql

* idiot fix

* more like Java

* fix

* Update query snapshots

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pauldambra and github-actions[bot] authored Oct 5, 2023
1 parent ac6c0c3 commit 4ecfb63
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 1 deletion.
10 changes: 9 additions & 1 deletion posthog/hogql/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
FloatDatabaseField,
FunctionCallTable,
)
from posthog.hogql.database.schema.log_entries import (
LogEntriesTable,
ReplayConsoleLogsLogEntriesTable,
BatchExportLogEntriesTable,
)
from posthog.hogql.database.schema.cohort_people import CohortPeople, RawCohortPeople
from posthog.hogql.database.schema.events import EventsTable
from posthog.hogql.database.schema.groups import GroupsTable, RawGroupsTable
Expand Down Expand Up @@ -45,6 +50,9 @@ class Database(BaseModel):
session_replay_events: SessionReplayEventsTable = SessionReplayEventsTable()
cohort_people: CohortPeople = CohortPeople()
static_cohort_people: StaticCohortPeople = StaticCohortPeople()
log_entries: LogEntriesTable = LogEntriesTable()
console_logs_log_entries: ReplayConsoleLogsLogEntriesTable = ReplayConsoleLogsLogEntriesTable()
batch_export_log_entries: BatchExportLogEntriesTable = BatchExportLogEntriesTable()

raw_session_replay_events: RawSessionReplayEventsTable = RawSessionReplayEventsTable()
raw_person_distinct_ids: RawPersonDistinctIdsTable = RawPersonDistinctIdsTable()
Expand All @@ -63,10 +71,10 @@ class Database(BaseModel):
"person",
"person_distinct_id2",
"person_overrides",
"session_recording_events",
"session_replay_events",
"cohortpeople",
"person_static_cohort",
"log_entries",
]

_timezone: Optional[str]
Expand Down
77 changes: 77 additions & 0 deletions posthog/hogql/database/schema/log_entries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from typing import Dict, List

from posthog.hogql import ast
from posthog.hogql.database.models import (
Table,
IntegerDatabaseField,
StringDatabaseField,
DateTimeDatabaseField,
LazyTable,
FieldOrTable,
)

LOG_ENTRIES_FIELDS: Dict[str, FieldOrTable] = {
"team_id": IntegerDatabaseField(name="team_id"),
"log_source": StringDatabaseField(name="log_source"),
"log_source_id": StringDatabaseField(name="log_source_id"),
"instance_id": StringDatabaseField(name="instance_id"),
"timestamp": DateTimeDatabaseField(name="timestamp"),
"message": StringDatabaseField(name="message"),
"level": StringDatabaseField(name="level"),
}


class LogEntriesTable(Table):
fields: Dict[str, FieldOrTable] = LOG_ENTRIES_FIELDS

def to_printed_clickhouse(self, context):
return "log_entries"

def to_printed_hogql(self):
return "log_entries"


class ReplayConsoleLogsLogEntriesTable(LazyTable):
fields: Dict[str, FieldOrTable] = LOG_ENTRIES_FIELDS

def lazy_select(self, requested_fields: Dict[str, List[str]]):
fields: List[ast.Expr] = [ast.Field(chain=["log_entries"] + chain) for name, chain in requested_fields.items()]

return ast.SelectQuery(
select=fields,
select_from=ast.JoinExpr(table=ast.Field(chain=["log_entries"])),
where=ast.CompareOperation(
op=ast.CompareOperationOp.Eq,
left=ast.Field(chain=["log_entries", "log_source"]),
right=ast.Constant(value="session_replay"),
),
)

def to_printed_clickhouse(self, context):
return "console_logs_log_entries"

def to_printed_hogql(self):
return "console_logs_log_entries"


class BatchExportLogEntriesTable(LazyTable):
fields: Dict[str, FieldOrTable] = LOG_ENTRIES_FIELDS

def lazy_select(self, requested_fields: Dict[str, List[str]]):
fields: List[ast.Expr] = [ast.Field(chain=["log_entries"] + chain) for name, chain in requested_fields.items()]

return ast.SelectQuery(
select=fields,
select_from=ast.JoinExpr(table=ast.Field(chain=["log_entries"])),
where=ast.CompareOperation(
op=ast.CompareOperationOp.Eq,
left=ast.Field(chain=["log_entries", "log_source"]),
right=ast.Constant(value="batch_export"),
),
)

def to_printed_clickhouse(self, context):
return "batch_export_log_entries"

def to_printed_hogql(self):
return "batch_export_log_entries"
156 changes: 156 additions & 0 deletions posthog/hogql/database/test/__snapshots__/test_database.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,84 @@
]
}
],
"log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"console_logs_log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"batch_export_log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"raw_session_replay_events": [
{
"key": "session_id",
Expand Down Expand Up @@ -991,6 +1069,84 @@
]
}
],
"log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"console_logs_log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"batch_export_log_entries": [
{
"key": "log_source",
"type": "string"
},
{
"key": "log_source_id",
"type": "string"
},
{
"key": "instance_id",
"type": "string"
},
{
"key": "timestamp",
"type": "datetime"
},
{
"key": "message",
"type": "string"
},
{
"key": "level",
"type": "string"
}
],
"raw_session_replay_events": [
{
"key": "session_id",
Expand Down

0 comments on commit 4ecfb63

Please sign in to comment.