@@ -9,7 +9,7 @@ const logging = require('@tryghost/logging');
9
9
* @property {string } [title] - Page title
10
10
*/
11
11
12
- class TopContentStatsService {
12
+ class ContentStatsService {
13
13
/**
14
14
* @param {object } deps
15
15
* @param {import('knex').Knex } deps.knex - Database client
@@ -38,25 +38,25 @@ class TopContentStatsService {
38
38
if ( ! this . tinybirdClient ) {
39
39
return { data : [ ] } ;
40
40
}
41
-
41
+
42
42
// Step 1: Get raw data from Tinybird
43
43
const rawData = await this . fetchRawTopContentData ( options ) ;
44
-
44
+
45
45
if ( ! rawData || ! rawData . length ) {
46
46
return { data : [ ] } ;
47
47
}
48
-
48
+
49
49
// Step 2: Enrich the data with titles
50
50
const enrichedData = await this . enrichTopContentData ( rawData ) ;
51
-
51
+
52
52
return { data : enrichedData } ;
53
53
} catch ( error ) {
54
54
logging . error ( 'Error fetching top content:' ) ;
55
55
logging . error ( error ) ;
56
56
return { data : [ ] } ;
57
57
}
58
58
}
59
-
59
+
60
60
/**
61
61
* Fetch raw top pages data from Tinybird
62
62
* @param {Object } options - Query options with snake_case keys
@@ -71,10 +71,10 @@ class TopContentStatsService {
71
71
memberStatus : options . member_status ,
72
72
tbVersion : options . tb_version
73
73
} ;
74
-
74
+
75
75
return await this . tinybirdClient . fetch ( 'api_top_pages' , tinybirdOptions ) ;
76
76
}
77
-
77
+
78
78
/**
79
79
* Extract post UUIDs from page data (internal method)
80
80
* @param {Array<TopContentDataItem> } data - Raw page data
@@ -88,7 +88,7 @@ class TopContentStatsService {
88
88
} )
89
89
. filter ( Boolean ) ;
90
90
}
91
-
91
+
92
92
/**
93
93
* Lookup post titles in the database
94
94
* @param {Array<string> } uuids - Post UUIDs to look up
@@ -98,11 +98,11 @@ class TopContentStatsService {
98
98
if ( ! uuids . length ) {
99
99
return { } ;
100
100
}
101
-
101
+
102
102
const posts = await this . knex . select ( 'uuid' , 'title' , 'id' )
103
103
. from ( 'posts' )
104
104
. whereIn ( 'uuid' , uuids ) ;
105
-
105
+
106
106
return posts . reduce ( ( map , post ) => {
107
107
map [ post . uuid ] = {
108
108
title : post . title ,
@@ -111,7 +111,7 @@ class TopContentStatsService {
111
111
return map ;
112
112
} , { } ) ;
113
113
}
114
-
114
+
115
115
/**
116
116
* Get resource title using UrlService
117
117
* @param {string } pathname - Path to look up
@@ -121,10 +121,10 @@ class TopContentStatsService {
121
121
if ( ! this . urlService ) {
122
122
return null ;
123
123
}
124
-
124
+
125
125
try {
126
126
const resource = this . urlService . getResource ( pathname ) ;
127
-
127
+
128
128
if ( resource && resource . data ) {
129
129
if ( resource . data . title ) {
130
130
return {
@@ -144,10 +144,10 @@ class TopContentStatsService {
144
144
logging . warn ( `Error looking up resource for ${ pathname } : ${ err . message } ` ) ;
145
145
}
146
146
}
147
-
147
+
148
148
return null ;
149
149
}
150
-
150
+
151
151
/**
152
152
* Enrich top pages data with titles
153
153
* @param {Array<TopContentDataItem> } data - Raw page data
@@ -157,11 +157,11 @@ class TopContentStatsService {
157
157
if ( ! data || ! data . length ) {
158
158
return [ ] ;
159
159
}
160
-
160
+
161
161
// Extract post UUIDs and lookup titles
162
162
const postUuids = this . extractPostUuids ( data ) ;
163
163
const titleMap = await this . lookupPostTitles ( postUuids ) ;
164
-
164
+
165
165
// Enrich the data with post titles or UrlService lookups
166
166
return Promise . all ( data . map ( async ( item ) => {
167
167
// Check if post_uuid is available directly
@@ -172,7 +172,7 @@ class TopContentStatsService {
172
172
post_id : titleMap [ item . post_uuid ] . id
173
173
} ;
174
174
}
175
-
175
+
176
176
// Use UrlService for pages without post_uuid
177
177
const resourceInfo = this . getResourceTitle ( item . pathname ) ;
178
178
if ( resourceInfo ) {
@@ -182,7 +182,7 @@ class TopContentStatsService {
182
182
resourceType : resourceInfo . resourceType
183
183
} ;
184
184
}
185
-
185
+
186
186
// Otherwise fallback to pathname (removing leading/trailing slashes)
187
187
const formattedPath = item . pathname . replace ( / ^ \/ | \/ $ / g, '' ) || 'Home' ;
188
188
return {
@@ -193,4 +193,4 @@ class TopContentStatsService {
193
193
}
194
194
}
195
195
196
- module . exports = TopContentStatsService ;
196
+ module . exports = ContentStatsService ;
0 commit comments