Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/config/generate-query-docs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'
import { getTwoDaysAgo } from './swagger'
import { getQueryDataSchema, getQueryResponseExample } from './query-examples'

/**
* Generates OpenAPI documentation for SQL query endpoints
Expand All @@ -9,6 +10,13 @@ import { getTwoDaysAgo } from './swagger'

const QUERIES_DIR = path.join(__dirname, '../../db_schema/queries/v1')

type QueryDoc = {
name: string
category: string
description: string
path: string
}

// Category mappings based on query name patterns
const categorizeQuery = (queryName: string): string => {
if (queryName.includes('Spotify') || queryName.includes('spotify'))
Expand Down Expand Up @@ -45,8 +53,8 @@ const extractDescription = (sqlContent: string): string => {
return commentLines.join('<br>') || 'Analytics query endpoint'
}

export const generateQueryDocs = () => {
const queries: any[] = []
export const generateQueryDocs = (): QueryDoc[] => {
const queries: QueryDoc[] = []

if (!fs.existsSync(QUERIES_DIR)) {
console.warn(`Queries directory not found: ${QUERIES_DIR}`)
Expand Down Expand Up @@ -85,16 +93,23 @@ export const generateQueryDocs = () => {
// Generate OpenAPI paths object for all queries
export const generateQueryPaths = () => {
const queries = generateQueryDocs()
const paths: any = {}
const paths: Record<string, unknown> = {}

// Group by category for better organization
const byCategory = queries.reduce((acc, q) => {
if (!acc[q.category]) acc[q.category] = []
acc[q.category].push(q)
return acc
}, {} as Record<string, typeof queries>)
}, {} as Record<string, QueryDoc[]>)

for (const query of queries) {
const exampleDate = getTwoDaysAgo()
const responseExample = getQueryResponseExample(
query.name,
exampleDate,
exampleDate
)

paths[query.path] = {
get: {
summary: query.name,
Expand All @@ -115,7 +130,7 @@ export const generateQueryPaths = () => {
schema: {
type: 'string',
format: 'date',
example: getTwoDaysAgo(),
example: exampleDate,
},
description: 'Start date (YYYY-MM-DD)',
},
Expand All @@ -126,7 +141,7 @@ export const generateQueryPaths = () => {
schema: {
type: 'string',
format: 'date',
example: getTwoDaysAgo(),
example: exampleDate,
},
description: 'End date (YYYY-MM-DD)',
},
Expand All @@ -144,7 +159,9 @@ export const generateQueryPaths = () => {
type: 'object',
properties: {
query: { type: 'string' },
accountId: { type: 'string' },
podcastId: { type: 'string' },
date: { type: 'string' },
startDate: {
type: 'string',
format: 'date',
Expand All @@ -153,14 +170,15 @@ export const generateQueryPaths = () => {
type: 'string',
format: 'date',
},
result: { type: 'string' },
},
},
data: {
type: 'array',
items: { type: 'object' },
},
data: getQueryDataSchema(query.name),
},
},
...(responseExample
? { example: responseExample }
: {}),
},
},
},
Expand Down
Loading
Loading