mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-22 08:47:10 +08:00
feat: improve admin panel design (#14464)
This commit is contained in:
@@ -56,7 +56,7 @@ defineModuleConfig('mailer', {
|
||||
env: 'MAILER_PASSWORD',
|
||||
},
|
||||
'SMTP.sender': {
|
||||
desc: 'Sender of all the emails (e.g. "AFFiNE Self Hosted <noreply@example.com>")',
|
||||
desc: 'Sender of all the emails (e.g. "AFFiNE Self Hosted <noreply@example.com>")',
|
||||
default: 'AFFiNE Self Hosted <noreply@example.com>',
|
||||
env: 'MAILER_SENDER',
|
||||
},
|
||||
@@ -92,7 +92,7 @@ defineModuleConfig('mailer', {
|
||||
default: '',
|
||||
},
|
||||
'fallbackSMTP.sender': {
|
||||
desc: 'Sender of all the emails (e.g. "AFFiNE Self Hosted <noreply@example.com>")',
|
||||
desc: 'Sender of all the emails (e.g. "AFFiNE Self Hosted <noreply@example.com>")',
|
||||
default: '',
|
||||
},
|
||||
'fallbackSMTP.ignoreTLS': {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import {
|
||||
Args,
|
||||
Field,
|
||||
Info,
|
||||
InputType,
|
||||
Int,
|
||||
Mutation,
|
||||
@@ -14,6 +15,12 @@ import {
|
||||
ResolveField,
|
||||
Resolver,
|
||||
} from '@nestjs/graphql';
|
||||
import {
|
||||
type FragmentDefinitionNode,
|
||||
type GraphQLResolveInfo,
|
||||
Kind,
|
||||
type SelectionNode,
|
||||
} from 'graphql';
|
||||
import { SafeIntResolver } from 'graphql-scalars';
|
||||
|
||||
import { PaginationInput, URLHelper } from '../../../base';
|
||||
@@ -53,6 +60,44 @@ registerEnumType(AdminSharedLinksOrder, {
|
||||
name: 'AdminSharedLinksOrder',
|
||||
});
|
||||
|
||||
function hasSelectedField(
|
||||
selections: readonly SelectionNode[],
|
||||
fieldName: string,
|
||||
fragments: Record<string, FragmentDefinitionNode>
|
||||
): boolean {
|
||||
for (const selection of selections) {
|
||||
if (selection.kind === Kind.FIELD) {
|
||||
if (selection.name.value === fieldName) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (selection.kind === Kind.INLINE_FRAGMENT) {
|
||||
if (
|
||||
hasSelectedField(
|
||||
selection.selectionSet.selections,
|
||||
fieldName,
|
||||
fragments
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const fragment = fragments[selection.name.value];
|
||||
if (
|
||||
fragment &&
|
||||
hasSelectedField(fragment.selectionSet.selections, fieldName, fragments)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@InputType()
|
||||
class ListWorkspaceInput {
|
||||
@Field(() => Int, { defaultValue: 20 })
|
||||
@@ -471,22 +516,40 @@ export class AdminWorkspaceResolver {
|
||||
})
|
||||
async adminDashboard(
|
||||
@Args('input', { nullable: true, type: () => AdminDashboardInput })
|
||||
input?: AdminDashboardInput
|
||||
input?: AdminDashboardInput,
|
||||
@Info() info?: GraphQLResolveInfo
|
||||
) {
|
||||
this.assertCloudOnly();
|
||||
const includeTopSharedLinks = Boolean(
|
||||
info?.fieldNodes.some(
|
||||
node =>
|
||||
node.selectionSet &&
|
||||
hasSelectedField(
|
||||
node.selectionSet.selections,
|
||||
'topSharedLinks',
|
||||
info.fragments
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
const dashboard = await this.models.workspaceAnalytics.adminGetDashboard({
|
||||
timezone: input?.timezone,
|
||||
storageHistoryDays: input?.storageHistoryDays,
|
||||
syncHistoryHours: input?.syncHistoryHours,
|
||||
sharedLinkWindowDays: input?.sharedLinkWindowDays,
|
||||
includeTopSharedLinks,
|
||||
});
|
||||
|
||||
return {
|
||||
...dashboard,
|
||||
topSharedLinks: dashboard.topSharedLinks.map(link => ({
|
||||
...link,
|
||||
shareUrl: this.url.link(`/workspace/${link.workspaceId}/${link.docId}`),
|
||||
})),
|
||||
topSharedLinks: includeTopSharedLinks
|
||||
? dashboard.topSharedLinks.map(link => ({
|
||||
...link,
|
||||
shareUrl: this.url.link(
|
||||
`/workspace/${link.workspaceId}/${link.docId}`
|
||||
),
|
||||
}))
|
||||
: [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
|
||||
import pkg from '../package.json' with { type: 'json' };
|
||||
|
||||
declare global {
|
||||
// oxlint-disable-next-line no-shadow-restricted-names
|
||||
namespace globalThis {
|
||||
// oxlint-disable-next-line no-var
|
||||
var env: Readonly<Env>;
|
||||
|
||||
@@ -51,6 +51,7 @@ export type AdminDashboardOptions = {
|
||||
storageHistoryDays?: number;
|
||||
syncHistoryHours?: number;
|
||||
sharedLinkWindowDays?: number;
|
||||
includeTopSharedLinks?: boolean;
|
||||
};
|
||||
|
||||
export type AdminAllSharedLinksOptions = {
|
||||
@@ -262,6 +263,7 @@ export class WorkspaceAnalyticsModel extends BaseModel {
|
||||
90,
|
||||
DEFAULT_SHARED_LINK_WINDOW_DAYS
|
||||
);
|
||||
const includeTopSharedLinks = options.includeTopSharedLinks ?? true;
|
||||
|
||||
const now = new Date();
|
||||
|
||||
@@ -274,6 +276,66 @@ export class WorkspaceAnalyticsModel extends BaseModel {
|
||||
const storageFrom = addUtcDays(currentDay, -(storageHistoryDays - 1));
|
||||
const sharedFrom = addUtcDays(currentDay, -(sharedLinkWindowDays - 1));
|
||||
|
||||
const topSharedLinksPromise = includeTopSharedLinks
|
||||
? this.db.$queryRaw<
|
||||
{
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
title: string | null;
|
||||
publishedAt: Date | null;
|
||||
docUpdatedAt: Date | null;
|
||||
workspaceOwnerId: string | null;
|
||||
lastUpdaterId: string | null;
|
||||
views: bigint | number;
|
||||
uniqueViews: bigint | number;
|
||||
guestViews: bigint | number;
|
||||
lastAccessedAt: Date | null;
|
||||
}[]
|
||||
>`
|
||||
WITH view_agg AS (
|
||||
SELECT
|
||||
workspace_id,
|
||||
doc_id,
|
||||
COALESCE(SUM(total_views), 0) AS views,
|
||||
COALESCE(SUM(unique_views), 0) AS unique_views,
|
||||
COALESCE(SUM(guest_views), 0) AS guest_views,
|
||||
MAX(last_accessed_at) AS last_accessed_at
|
||||
FROM workspace_doc_view_daily
|
||||
WHERE date BETWEEN ${sharedFrom}::date AND ${currentDay}::date
|
||||
GROUP BY workspace_id, doc_id
|
||||
)
|
||||
SELECT
|
||||
wp.workspace_id AS "workspaceId",
|
||||
wp.page_id AS "docId",
|
||||
wp.title AS title,
|
||||
wp.published_at AS "publishedAt",
|
||||
sn.updated_at AS "docUpdatedAt",
|
||||
owner.user_id AS "workspaceOwnerId",
|
||||
sn.updated_by AS "lastUpdaterId",
|
||||
COALESCE(v.views, 0) AS views,
|
||||
COALESCE(v.unique_views, 0) AS "uniqueViews",
|
||||
COALESCE(v.guest_views, 0) AS "guestViews",
|
||||
v.last_accessed_at AS "lastAccessedAt"
|
||||
FROM workspace_pages wp
|
||||
LEFT JOIN snapshots sn
|
||||
ON sn.workspace_id = wp.workspace_id AND sn.guid = wp.page_id
|
||||
LEFT JOIN view_agg v
|
||||
ON v.workspace_id = wp.workspace_id AND v.doc_id = wp.page_id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT user_id
|
||||
FROM workspace_user_permissions
|
||||
WHERE workspace_id = wp.workspace_id
|
||||
AND type = ${WorkspaceRole.Owner}
|
||||
AND status = 'Accepted'::"WorkspaceMemberStatus"
|
||||
ORDER BY created_at ASC
|
||||
LIMIT 1
|
||||
) owner ON TRUE
|
||||
WHERE wp.public = TRUE
|
||||
ORDER BY views DESC, "uniqueViews" DESC, "workspaceId" ASC, "docId" ASC
|
||||
LIMIT 10
|
||||
`
|
||||
: Promise.resolve([]);
|
||||
|
||||
const [
|
||||
syncCurrent,
|
||||
syncTimeline,
|
||||
@@ -350,63 +412,7 @@ export class WorkspaceAnalyticsModel extends BaseModel {
|
||||
AND created_at >= ${sharedFrom}
|
||||
AND created_at <= ${now}
|
||||
`,
|
||||
this.db.$queryRaw<
|
||||
{
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
title: string | null;
|
||||
publishedAt: Date | null;
|
||||
docUpdatedAt: Date | null;
|
||||
workspaceOwnerId: string | null;
|
||||
lastUpdaterId: string | null;
|
||||
views: bigint | number;
|
||||
uniqueViews: bigint | number;
|
||||
guestViews: bigint | number;
|
||||
lastAccessedAt: Date | null;
|
||||
}[]
|
||||
>`
|
||||
WITH view_agg AS (
|
||||
SELECT
|
||||
workspace_id,
|
||||
doc_id,
|
||||
COALESCE(SUM(total_views), 0) AS views,
|
||||
COALESCE(SUM(unique_views), 0) AS unique_views,
|
||||
COALESCE(SUM(guest_views), 0) AS guest_views,
|
||||
MAX(last_accessed_at) AS last_accessed_at
|
||||
FROM workspace_doc_view_daily
|
||||
WHERE date BETWEEN ${sharedFrom}::date AND ${currentDay}::date
|
||||
GROUP BY workspace_id, doc_id
|
||||
)
|
||||
SELECT
|
||||
wp.workspace_id AS "workspaceId",
|
||||
wp.page_id AS "docId",
|
||||
wp.title AS title,
|
||||
wp.published_at AS "publishedAt",
|
||||
sn.updated_at AS "docUpdatedAt",
|
||||
owner.user_id AS "workspaceOwnerId",
|
||||
sn.updated_by AS "lastUpdaterId",
|
||||
COALESCE(v.views, 0) AS views,
|
||||
COALESCE(v.unique_views, 0) AS "uniqueViews",
|
||||
COALESCE(v.guest_views, 0) AS "guestViews",
|
||||
v.last_accessed_at AS "lastAccessedAt"
|
||||
FROM workspace_pages wp
|
||||
LEFT JOIN snapshots sn
|
||||
ON sn.workspace_id = wp.workspace_id AND sn.guid = wp.page_id
|
||||
LEFT JOIN view_agg v
|
||||
ON v.workspace_id = wp.workspace_id AND v.doc_id = wp.page_id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT user_id
|
||||
FROM workspace_user_permissions
|
||||
WHERE workspace_id = wp.workspace_id
|
||||
AND type = ${WorkspaceRole.Owner}
|
||||
AND status = 'Accepted'::"WorkspaceMemberStatus"
|
||||
ORDER BY created_at ASC
|
||||
LIMIT 1
|
||||
) owner ON TRUE
|
||||
WHERE wp.public = TRUE
|
||||
ORDER BY views DESC, "uniqueViews" DESC, "workspaceId" ASC, "docId" ASC
|
||||
LIMIT 10
|
||||
`,
|
||||
topSharedLinksPromise,
|
||||
]);
|
||||
|
||||
const storageHistorySeries = storageHistory.map(row => ({
|
||||
|
||||
@@ -132,6 +132,10 @@ export class IndexerJob {
|
||||
indexed: true,
|
||||
});
|
||||
}
|
||||
if (!missingDocIds.length && !deletedDocIds.length) {
|
||||
this.logger.verbose(`workspace ${workspaceId} is already indexed`);
|
||||
return;
|
||||
}
|
||||
this.logger.log(
|
||||
`indexed workspace ${workspaceId} with ${missingDocIds.length} missing docs and ${deletedDocIds.length} deleted docs`
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user