File tree Expand file tree Collapse file tree 11 files changed +78
-5
lines changed
Expand file tree Collapse file tree 11 files changed +78
-5
lines changed Original file line number Diff line number Diff line change @@ -8,10 +8,13 @@ import type {
88import type { Field } from "payload/dist/fields/config/types" ;
99import { extendWebpackConfig } from "./extendWebpackConfig" ;
1010import getProvider from "./providers" ;
11+
1112import getGlobalAggregateData from "./routes/getGlobalAggregateData" ;
1213import getGlobalChartData from "./routes/getGlobalChartData" ;
1314import getPageChartData from "./routes/getPageChartData" ;
1415import getPageAggregateData from "./routes/getPageAggregateData" ;
16+ import getLiveData from "./routes/getLiveData" ;
17+
1518import type { CollectionConfig } from "payload/dist/collections/config/types" ;
1619import { getPageViewsChart } from "./components/Charts/PageViewsChart" ;
1720import { 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 ) => {
Original file line number Diff line number Diff line change 11import plausible from "./plausible" ;
22import type { Provider } from "../types" ;
33import type { ChartWidget , InfoWidget } from "../types/widgets" ;
4- import type { ChartData , AggregateData } from "../types/data" ;
4+ import type { ChartData , AggregateData , LiveData } from "../types/data" ;
55
66type BaseOptions = {
77 timeframe ?: string ;
88} ;
99
10+ export interface LiveDataOptions { }
11+
1012export 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
3740const getProvider = ( provider : Provider ) => {
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -3,12 +3,14 @@ import getGlobalAggregateData from "./getGlobalAggregateData";
33import getGlobalChartData from "./getGlobalChartData" ;
44import getPageAggregateData from "./getPageAggregateData" ;
55import getPageChartData from "./getPageChartData" ;
6+ import getLiveData from "./getLiveData" ;
67import type {
78 ApiProvider ,
89 GlobalAggregateOptions ,
910 GlobalChartOptions ,
1011 PageChartOptions ,
1112 PageAggregateOptions ,
13+ LiveDataOptions ,
1214} from ".." ;
1315
1416const 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments