diff --git a/packages/generator/src/bin.ts b/packages/generator/src/bin.ts index 831fe18..24ef4cb 100644 --- a/packages/generator/src/bin.ts +++ b/packages/generator/src/bin.ts @@ -82,7 +82,7 @@ function posthogPropertyTypeToGeneric( void fetchFromPosthog("event_definitions").then(async (data) => { const events: AnalyticsEvent[] = []; - for await (const event of data.results.slice(0, 2)) { + for await (const event of data.results.slice(0, 20)) { const definitions = await fetchFromPosthog("property_definitions", { queryParams: { event_names: [event.name], diff --git a/packages/generator/src/core/generate.ts b/packages/generator/src/core/generate.ts index a4d0ba3..d54d873 100644 --- a/packages/generator/src/core/generate.ts +++ b/packages/generator/src/core/generate.ts @@ -9,16 +9,150 @@ export async function generateTypeScriptFile(events: AnalyticsEvent[]) { }, {} as Record); const typeScriptFileContent = ` const events = ${JSON.stringify(eventMap, null, 2)} as const; - type AnalyticsEvents = typeof events; -type InsightOptions = { - filters: []; - breakdownBy: T[number]["name"]; + + +type AnalyticsEvents = typeof events; +type AnalyticsEventNames = keyof AnalyticsEvents; + +type AnalyticsEvent = AnalyticsEvents[keyof AnalyticsEvents]; + +type SamplingOptions = + | "TotalCount" + | "CountPerUser" + | "UniqueUsers" + | "WeeklyActiveUsers" + | "MonthlyActiveUsers" + | "UniqueSessions"; + +type Filter = { + name: AnalyticsEvents[T]["properties"][number]["name"]; + compare: + | "equals" + | "does_not_equal" + | "contains" + | "does_not_contain" + | "matches_regex" + | "does_not_match_regex" + | "greater_than" + | "less_than" + | "is_set" + | "is_not_set"; + value: string | number | boolean; +}; + +type SeriesEntry = { + name: T; + sampling?: SamplingOptions; + where?: Filter | Filter[]; +}; + +type InsightOptions = { + events: SeriesEntry | SeriesEntry[]; + filters?: []; + breakdownBy?: AnalyticsEvents[T]["properties"][number]["name"]; +}; + +type ExecuteOptions = { + groupBy?: "day" | "hour" | "week" | "month"; + filterMatch: "all" | "any"; + type: + | "line" + | "bar" + | "area" + | "cumulative-line" + | "number" + | "pie" + | "bar-total" + | "table" + | "world"; + breakdownBy?: AnalyticsEvents[T]["properties"][number]["name"]; }; -function fetchInsight( - eventName: T -) {} +function addFilter( + series: SeriesEntry, + existingFilters?: Filter | Filter[] +) { + const existing = Array.isArray(existingFilters) + ? existingFilters + : existingFilters + ? [existingFilters] + : []; + return { + addFilter: (filter: Filter) => addFilter(series, [...existing, filter]), + execute: (opts: ExecuteOptions) => execute([...existing, series], opts), + }; +} + +function execute( + series: SeriesEntry[], + opts: ExecuteOptions +) { + console.log("series", series); + return [ + { + hello: "world", + }, + ]; +} + +function addSeries< + Current extends AnalyticsEventNames, + Existing extends AnalyticsEventNames, + Next extends AnalyticsEventNames, + ExecOpts extends ExecuteOptions, + FilterOpts extends Filter +>( + series: SeriesEntry, + existingSeries?: SeriesEntry | SeriesEntry[] +) { + const existing = Array.isArray(existingSeries) + ? existingSeries + : existingSeries + ? [existingSeries] + : []; + return { + addSeries: (series: SeriesEntry) => + addSeries(series, [...existing, series]), + addFilter: (opts: FilterOpts) => addFilter(series, opts), + execute: (opts: ExecOpts) => execute([...existing, series], opts), + }; +} + +function buildQuery() { + return { + addSeries, + }; +} + +const data = buildQuery() + .addSeries({ + name: "User Grant Consent", + sampling: "TotalCount", + where: { + name: "Answer Overflow Account Id", + compare: "equals", + value: 123, + }, + }) + .addSeries({ + name: "$autocapture", + where: { + name: "Answer Overflow Account Id", + compare: "equals", + value: 123, + }, + }).addFilter({ +name: "Server" + }) + .execute({ + breakdownBy: "Answer Overflow Account Id", + type: "line", + filterMatch: "all", + }); + +console.log(data); + `; // write file to dist/events.ts