Skip to content

Commit 59294ed

Browse files
committed
Add live data endpoint
1 parent 57c034b commit 59294ed

File tree

11 files changed

+78
-5
lines changed

11 files changed

+78
-5
lines changed

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import type {
88
import type { Field } from "payload/dist/fields/config/types";
99
import { extendWebpackConfig } from "./extendWebpackConfig";
1010
import getProvider from "./providers";
11+
1112
import getGlobalAggregateData from "./routes/getGlobalAggregateData";
1213
import getGlobalChartData from "./routes/getGlobalChartData";
1314
import getPageChartData from "./routes/getPageChartData";
1415
import getPageAggregateData from "./routes/getPageAggregateData";
16+
import getLiveData from "./routes/getLiveData";
17+
1518
import type { CollectionConfig } from "payload/dist/collections/config/types";
1619
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
1720
import { getAggregateDataWidget } from "./components/Aggregates/AggregateDataWidget";
@@ -65,6 +68,7 @@ const payloadDashboardAnalytics =
6568
getGlobalChartData(apiProvider),
6669
getPageChartData(apiProvider),
6770
getPageAggregateData(apiProvider),
71+
getLiveData(apiProvider),
6872
],
6973
...(collections && {
7074
collections: collections.map((collection) => {

src/providers/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import plausible from "./plausible";
22
import type { Provider } from "../types";
33
import type { ChartWidget, InfoWidget } from "../types/widgets";
4-
import type { ChartData, AggregateData } from "../types/data";
4+
import type { ChartData, AggregateData, LiveData } from "../types/data";
55

66
type BaseOptions = {
77
timeframe?: string;
88
};
99

10+
export interface LiveDataOptions {}
11+
1012
export interface GlobalAggregateOptions extends BaseOptions {
1113
metrics: InfoWidget["metrics"];
1214
}
@@ -32,6 +34,7 @@ export type ApiProvider = {
3234
options: PageAggregateOptions
3335
) => Promise<AggregateData>;
3436
getPageChartData: (options: PageChartOptions) => Promise<ChartData>;
37+
getLiveData: (options: LiveDataOptions) => Promise<LiveData>;
3538
};
3639

3740
const getProvider = (provider: Provider) => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { PlausibleProvider } from "../../types/providers";
2+
import type { LiveDataOptions } from "..";
3+
import type { LiveData } from "../../types/data";
4+
import client from "./client";
5+
6+
async function getLiveData(
7+
provider: PlausibleProvider,
8+
options: LiveDataOptions
9+
) {
10+
const plausibleClient = client(provider, {
11+
endpoint: "/stats/realtime/visitors",
12+
});
13+
14+
const data = await plausibleClient.fetch().then((response) => {
15+
return response.json();
16+
});
17+
18+
const processedData: LiveData = {
19+
visitors: data,
20+
};
21+
22+
return processedData;
23+
}
24+
25+
export default getLiveData;

src/providers/plausible/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import getGlobalAggregateData from "./getGlobalAggregateData";
33
import getGlobalChartData from "./getGlobalChartData";
44
import getPageAggregateData from "./getPageAggregateData";
55
import getPageChartData from "./getPageChartData";
6+
import getLiveData from "./getLiveData";
67
import type {
78
ApiProvider,
89
GlobalAggregateOptions,
910
GlobalChartOptions,
1011
PageChartOptions,
1112
PageAggregateOptions,
13+
LiveDataOptions,
1214
} from "..";
1315

1416
const plausible = (provider: PlausibleProvider): ApiProvider => {
@@ -21,6 +23,8 @@ const plausible = (provider: PlausibleProvider): ApiProvider => {
2123
await getPageChartData(provider, options),
2224
getPageAggregateData: async (options: PageAggregateOptions) =>
2325
await getPageAggregateData(provider, options),
26+
getLiveData: async (options: LiveDataOptions) =>
27+
await getLiveData(provider, options),
2428
};
2529
};
2630

src/routes/getGlobalAggregateData/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const handler = (provider: ApiProvider) => {
1313
res.status(200).send(data);
1414
} catch (error) {
1515
payload.logger.error(payload);
16-
res.status(500);
16+
res.sendStatus(500);
1717
}
1818
};
1919

src/routes/getGlobalChartData/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const handler = (provider: ApiProvider) => {
1414
res.status(200).send(data);
1515
} catch (error) {
1616
payload.logger.error(error);
17-
res.status(500);
17+
res.sendStatus(500);
1818
}
1919
};
2020

src/routes/getLiveData/handler.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Endpoint } from "payload/config";
2+
import { ApiProvider } from "../../providers";
3+
import payload from "payload";
4+
5+
const handler = (provider: ApiProvider) => {
6+
const handler: Endpoint["handler"] = async (req, res, next) => {
7+
try {
8+
const data = await provider.getLiveData({});
9+
10+
res.status(200).send(data);
11+
} catch (error) {
12+
payload.logger.error(error);
13+
res.sendStatus(500);
14+
}
15+
};
16+
17+
return handler;
18+
};
19+
20+
export default handler;

src/routes/getLiveData/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Endpoint } from "payload/config";
2+
import handler from "./handler";
3+
import { ApiProvider } from "../../providers";
4+
5+
const getLiveData = (provider: ApiProvider): Endpoint => {
6+
return {
7+
path: "/analytics/liveData",
8+
method: "post",
9+
handler: handler(provider),
10+
};
11+
};
12+
13+
export default getLiveData;

src/routes/getPageAggregateData/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const handler = (provider: ApiProvider) => {
1717
res.status(200).send(data);
1818
} catch (error) {
1919
payload.logger.error(error);
20-
res.status(500);
20+
res.sendStatus(500);
2121
}
2222
};
2323

src/routes/getPageChartData/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const handler = (provider: ApiProvider) => {
1717
res.status(200).send(data);
1818
} catch (error) {
1919
payload.logger.error(error);
20-
res.status(500);
20+
res.sendStatus(500);
2121
}
2222
};
2323

0 commit comments

Comments
 (0)