MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / run

Function run

apps/web/src/scripts/posthog/audit-microdollar-usage.ts:204–323  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

202// --- Main Logic ---
203
204export async function run(): Promise<void> {
205 console.log('🔍 Auditing PostHog for whole-word references to "microdollar_usage"...\n');
206
207 const errorLog: ErrorLog[] = [];
208
209 try {
210 // 1. Fetch all Data Warehouse Saved Queries (aka views) via OpenAPI path
211 // Spec: /api/environments/{project_id}/warehouse_saved_queries/
212 const viewsUrl = `${POSTHOG_API_BASE}/api/environments/${PROJECT_ID}/warehouse_saved_queries/`;
213 const views = await fetchAllPages<DataWarehouseSavedQuery>(viewsUrl);
214 console.log(`\n✅ Retrieved ${views.length} data warehouse saved queries from: ${viewsUrl}\n`);
215 const flaggedViews = views.filter(view => {
216 if (view.query?.query) {
217 return SEARCH_REGEX.test(view.query.query);
218 }
219 return false;
220 });
221
222 if (flaggedViews.length > 0) {
223 console.log(
224 `🔴 Found ${flaggedViews.length} Data Warehouse Saved Query(ies) with references:`
225 );
226 for (const view of flaggedViews) {
227 const id = hasId(view) ? String(view.id) : 'unknown';
228 console.log(` - Saved Query: "${view.name}" (ID: ${id})`);
229 console.log(' Full object:');
230 console.log(JSON.stringify(view, null, 2));
231 console.log(`Patching view...`);
232
233 try {
234 await patchView(view);
235 } catch (error) {
236 const errorMessage = error instanceof Error ? error.message : String(error);
237 console.error(`❌ Failed to patch view "${view.name}":`, errorMessage);
238 errorLog.push({
239 type: 'view',
240 name: view.name,
241 id: view.id,
242 error: errorMessage,
243 oldQuery: view.query?.query,
244 newQuery: view.query?.query.replace(SEARCH_REGEX, REPLACEMENT_TERM),
245 timestamp: new Date().toISOString(),
246 });
247 console.log(`⏭️ Skipping to next view...\n`);
248 }
249 }
250 } else {
251 console.log(`🟢 No Data Warehouse Saved Queries found with references.`);
252 }
253
254 // 2. Fetch all Insights via OpenAPI path
255 // Spec: /api/environments/{project_id}/insights/
256 const insightsUrl = `${POSTHOG_API_BASE}/api/environments/${PROJECT_ID}/insights/`;
257 const insights: Insight[] = await fetchAllPages<Insight>(insightsUrl);
258 console.log(`\n✅ Retrieved ${insights.length} insights from: ${insightsUrl}\n`);
259
260 // 4. Audit Insights - extract HogQL and check for search term
261 const flaggedInsights = insights.filter(insight => {

Callers 1

Calls 11

hasIdFunction · 0.85
patchViewFunction · 0.85
extractHogQLStringsFunction · 0.85
patchInsightFunction · 0.85
replaceInQueryFunction · 0.85
pushMethod · 0.80
joinMethod · 0.80
logMethod · 0.65
errorMethod · 0.65
replaceMethod · 0.65
writeFileFunction · 0.50

Tested by

no test coverage detected