π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
feat: connected clients
Nevo Davidbb5099a84aee
diff --git a/apps/frontend/src/components/admin/admin-stats.component.tsx b/apps/frontend/src/components/admin/admin-stats.component.tsxindex 2673eaa3b..79f66b690 100644--- a/apps/frontend/src/components/admin/admin-stats.component.tsx+++ b/apps/frontend/src/components/admin/admin-stats.component.tsx@@ -28,6 +28,7 @@ interface StatsResponse {publishingChannels?: StatsBlock;scheduledChannels?: StatsBlock;activeOrgsBySource?: StatsBlock;+ connectedClients?: StatsBlock;}const isoDaysAgo = (days: number) => {@@ -289,6 +290,14 @@ export const AdminStatsComponent: FC = () => {/></div>)}+ {data.connectedClients && (+ <div className="flex-1 min-w-[220px] shrink-0">+ <SummaryCard+ label="Connected clients (all clients combined)"+ value={data.connectedClients.total}+ />+ </div>+ )}</div><div className="flex gap-[12px] items-start">@@ -354,6 +363,14 @@ export const AdminStatsComponent: FC = () => {/></div>)}+ {data.connectedClients && (+ <div className="flex-1 min-w-[220px] shrink-0">+ <PerSocialTable+ title="Connected clients per name"+ block={data.connectedClients}+ />+ </div>+ )}</div></div>)}diff --git a/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts b/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.tsindex fded269d2..e898f2888 100644--- a/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts+++ b/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts@@ -55,6 +55,7 @@ export interface StatsResponse {publishingChannels: { total: number; perSocial: PerSocial[] };scheduledChannels: { total: number; perSocial: PerSocial[] };activeOrgsBySource: { total: number; perSocial: PerSocial[] };+ connectedClients: { total: number; perSocial: PerSocial[] };}const sortDesc = (list: PerSocial[]) =>@@ -65,7 +66,9 @@ export class AdminStatsRepository {constructor(private _post: PrismaRepository<'post'>,private _integration: PrismaRepository<'integration'>,- private _errors: PrismaRepository<'errors'>+ private _errors: PrismaRepository<'errors'>,+ private _oauthApp: PrismaRepository<'oAuthApp'>,+ private _oauthAuth: PrismaRepository<'oAuthAuthorization'>) {}private async errorStats(params: StatsParams) {@@ -312,6 +315,61 @@ export class AdminStatsRepository {};}+ // Active OAuth authorizations first created in the range (MCP clients like+ // Claude, Cursor or ChatGPT connecting to Postiz), per client name. A+ // re-authorization upserts the same row, so it keeps its original createdAt+ // and is not counted again. Dynamic registration creates a new OAuthApp row+ // for every client install, so the counts are folded by the registered+ // client_name rather than by app id.+ private async clientStats(params: StatsParams) {+ const where: Prisma.OAuthAuthorizationWhereInput = {+ accessToken: { not: null },+ ...(params.includeDeleted ? {} : { revokedAt: null }),+ createdAt: { gte: params.from, lte: params.to },+ ...(params.organizationId+ ? { organizationId: params.organizationId }+ : {}),+ };++ const [total, grouped] = await Promise.all([+ this._oauthAuth.model.oAuthAuthorization.count({ where }),+ this._oauthAuth.model.oAuthAuthorization.groupBy({+ by: ['oauthAppId'],+ where,+ _count: { _all: true },+ }),+ ]);++ // groupBy can't reach into the oauthApp relation, so resolve the client+ // name for the apps we saw and fold the counts.+ const appIds = grouped.map((g) => g.oauthAppId);+ const apps = appIds.length+ ? await this._oauthApp.model.oAuthApp.findMany({+ where: { id: { in: appIds } },+ select: { id: true, name: true },+ })+ : [];+ const nameById = new Map(+ apps.map((a) => [a.id, a.name.trim().toLowerCase()])+ );++ const byName = new Map<string, number>();+ for (const g of grouped) {+ const name = nameById.get(g.oauthAppId) || 'unknown';+ byName.set(name, (byName.get(name) || 0) + g._count._all);+ }++ return {+ total,+ perSocial: sortDesc(+ [...byName.entries()].map(([provider, count]) => ({+ provider,+ count,+ }))+ ),+ };+ }+private async postStateStats(params: OrgActivityParams) {const grouped = await this._post.model.post.groupBy({by: ['state'],@@ -405,14 +463,21 @@ export class AdminStatsRepository {}async getStats(params: StatsParams): Promise<StatsResponse> {- const [errors, posts, accounts, connected, activeOrgsBySource] =- await Promise.all([- this.errorStats(params),- this.postStats(params),- this.accountStats(params),- this.connectedStats(params),- this.sourceStats(params),- ]);+ const [+ errors,+ posts,+ accounts,+ connected,+ activeOrgsBySource,+ connectedClients,+ ] = await Promise.all([+ this.errorStats(params),+ this.postStats(params),+ this.accountStats(params),+ this.connectedStats(params),+ this.sourceStats(params),+ this.clientStats(params),+ ]);return {from: params.from.toISOString(),@@ -425,6 +490,7 @@ export class AdminStatsRepository {publishingChannels: accounts.publishingChannels,scheduledChannels: accounts.scheduledChannels,activeOrgsBySource,+ connectedClients,};}}