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
806 changes: 806 additions & 0 deletions specs/031-copilot-metrics-no-data-investigation/analysis.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1,158 changes: 1,158 additions & 0 deletions specs/031-copilot-metrics-no-data-investigation/migration-plan.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/actions/copilot-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,50 @@ async function getActiveConnection() {
});
}

/**
* Days behind today (UTC) before metrics are considered stale.
*
* GitHub finalizes Copilot usage data within ~3 days; we add a 3-day buffer to
* stay quiet during normal latency, surfacing only persistent freshness issues.
*/
const FRESHNESS_STALENESS_THRESHOLD_DAYS = 6;

export interface CopilotFreshness {
/** Latest metric date in `copilot_usage_metrics`, or null if no rows. */
latestDate: string | null;
/** Whole UTC days between today and `latestDate`. Null when latestDate null. */
daysBehind: number | null;
/** True when `daysBehind` exceeds the staleness threshold. */
stale: boolean;
}

export async function getCopilotMetricsFreshness(): Promise<CopilotFreshness> {
const connection = await getActiveConnection();
if (!connection) {
return { latestDate: null, daysBehind: null, stale: false };
}

const [row] = await db
.select({ latest: sql<string>`MAX(${copilotUsageMetrics.date})` })
.from(copilotUsageMetrics)
.where(eq(copilotUsageMetrics.connectionId, connection.id));

if (!row?.latest) {
return { latestDate: null, daysBehind: null, stale: false };
}

const today = new Date();
const latest = new Date(row.latest);
const msPerDay = 24 * 60 * 60 * 1000;
const daysBehind = Math.floor((today.getTime() - latest.getTime()) / msPerDay);

return {
latestDate: row.latest,
daysBehind,
stale: daysBehind > FRESHNESS_STALENESS_THRESHOLD_DAYS,
};
}

function derivePlanType(tierName: string | null): "business" | "enterprise" {
return (tierName ?? "").toLowerCase().includes("enterprise")
? "enterprise"
Expand Down
29 changes: 20 additions & 9 deletions src/app/copilot/analytics/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { getCopilotAnalytics } from "@/actions/copilot-data";
import {
getCopilotAnalytics,
getCopilotMetricsFreshness,
} from "@/actions/copilot-data";
import { LanguageChart } from "@/components/copilot/language-chart";
import { EditorChart } from "@/components/copilot/editor-chart";
import { ActivityDistribution } from "@/components/copilot/activity-distribution";
import { MetricsFreshnessCard } from "@/components/copilot/metrics-freshness-card";
import { Card, CardContent } from "@/components/ui/card";
import { BarChart3 } from "lucide-react";
import Link from "next/link";

export default async function CopilotAnalyticsPage() {
const result = await getCopilotAnalytics();
const [result, freshness] = await Promise.all([
getCopilotAnalytics(),
getCopilotMetricsFreshness(),
]);

if (!result.success) {
return (
Expand All @@ -20,18 +27,22 @@ export default async function CopilotAnalyticsPage() {

if (!hasData) {
return (
<Card>
<CardContent className="py-12 text-center space-y-3">
<BarChart3 className="size-12 mx-auto text-muted-foreground" />
<h3 className="text-lg font-medium">No analytics data yet</h3>
<p className="text-sm text-muted-foreground">Enable Copilot sync in <Link href="/settings/integrations" className="underline text-primary">Settings</Link> to start collecting usage analytics.</p>
</CardContent>
</Card>
<div className="space-y-6">
<MetricsFreshnessCard freshness={freshness} />
<Card>
<CardContent className="py-12 text-center space-y-3">
<BarChart3 className="size-12 mx-auto text-muted-foreground" />
<h3 className="text-lg font-medium">No analytics data yet</h3>
<p className="text-sm text-muted-foreground">Enable Copilot sync in <Link href="/settings/integrations" className="underline text-primary">Settings</Link> to start collecting usage analytics.</p>
</CardContent>
</Card>
</div>
);
}

return (
<div className="space-y-6">
<MetricsFreshnessCard freshness={freshness} />
<div className="grid gap-6 lg:grid-cols-2">
<LanguageChart data={data.byLanguage} />
<EditorChart data={data.byEditor} />
Expand Down
13 changes: 12 additions & 1 deletion src/app/settings/integrations/github-integration-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export function GitHubIntegrationClient({ initialConnection }: Props) {
aria-describedby="token-help"
/>
<p id="token-help" className="text-xs text-muted-foreground">
Requires <code>read:org</code> and <code>read:user</code> scopes.
Requires <code>read:org</code>, <code>read:user</code>, and{" "}
<code>manage_billing:copilot</code> scopes (the last for Copilot
billing and usage metrics).
</p>
</div>

Expand Down Expand Up @@ -291,7 +293,16 @@ export function GitHubIntegrationClient({ initialConnection }: Props) {
placeholder="ghp_..."
value={updateToken}
onChange={(e) => setUpdateTokenValue(e.target.value)}
aria-describedby="update-token-help"
/>
<p
id="update-token-help"
className="text-xs text-muted-foreground"
>
Requires <code>read:org</code>, <code>read:user</code>, and{" "}
<code>manage_billing:copilot</code> scopes (the last for Copilot
billing and usage metrics).
</p>
<div className="flex gap-2">
<Button
size="sm"
Expand Down
244 changes: 0 additions & 244 deletions src/components/copilot/copilot-sync-section.tsx

This file was deleted.

Loading
Loading