Skip to content

Commit 3ac5c5f

Browse files
authored
Renamed TopContentStatsService to ContentStatsService (#23148)
no refs Renaming this service and its test files to align with the pattern in the other stats services. There are likely to be other content endpoints that aren't necessarily `top-*`, which can be added to this service and given their own endpoints.
1 parent c7fa60f commit 3ac5c5f

File tree

3 files changed

+86
-86
lines changed

3 files changed

+86
-86
lines changed

ghost/core/core/server/services/stats/TopContentStatsService.js renamed to ghost/core/core/server/services/stats/ContentStatsService.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const logging = require('@tryghost/logging');
99
* @property {string} [title] - Page title
1010
*/
1111

12-
class TopContentStatsService {
12+
class ContentStatsService {
1313
/**
1414
* @param {object} deps
1515
* @param {import('knex').Knex} deps.knex - Database client
@@ -38,25 +38,25 @@ class TopContentStatsService {
3838
if (!this.tinybirdClient) {
3939
return {data: []};
4040
}
41-
41+
4242
// Step 1: Get raw data from Tinybird
4343
const rawData = await this.fetchRawTopContentData(options);
44-
44+
4545
if (!rawData || !rawData.length) {
4646
return {data: []};
4747
}
48-
48+
4949
// Step 2: Enrich the data with titles
5050
const enrichedData = await this.enrichTopContentData(rawData);
51-
51+
5252
return {data: enrichedData};
5353
} catch (error) {
5454
logging.error('Error fetching top content:');
5555
logging.error(error);
5656
return {data: []};
5757
}
5858
}
59-
59+
6060
/**
6161
* Fetch raw top pages data from Tinybird
6262
* @param {Object} options - Query options with snake_case keys
@@ -71,10 +71,10 @@ class TopContentStatsService {
7171
memberStatus: options.member_status,
7272
tbVersion: options.tb_version
7373
};
74-
74+
7575
return await this.tinybirdClient.fetch('api_top_pages', tinybirdOptions);
7676
}
77-
77+
7878
/**
7979
* Extract post UUIDs from page data (internal method)
8080
* @param {Array<TopContentDataItem>} data - Raw page data
@@ -88,7 +88,7 @@ class TopContentStatsService {
8888
})
8989
.filter(Boolean);
9090
}
91-
91+
9292
/**
9393
* Lookup post titles in the database
9494
* @param {Array<string>} uuids - Post UUIDs to look up
@@ -98,11 +98,11 @@ class TopContentStatsService {
9898
if (!uuids.length) {
9999
return {};
100100
}
101-
101+
102102
const posts = await this.knex.select('uuid', 'title', 'id')
103103
.from('posts')
104104
.whereIn('uuid', uuids);
105-
105+
106106
return posts.reduce((map, post) => {
107107
map[post.uuid] = {
108108
title: post.title,
@@ -111,7 +111,7 @@ class TopContentStatsService {
111111
return map;
112112
}, {});
113113
}
114-
114+
115115
/**
116116
* Get resource title using UrlService
117117
* @param {string} pathname - Path to look up
@@ -121,10 +121,10 @@ class TopContentStatsService {
121121
if (!this.urlService) {
122122
return null;
123123
}
124-
124+
125125
try {
126126
const resource = this.urlService.getResource(pathname);
127-
127+
128128
if (resource && resource.data) {
129129
if (resource.data.title) {
130130
return {
@@ -144,10 +144,10 @@ class TopContentStatsService {
144144
logging.warn(`Error looking up resource for ${pathname}: ${err.message}`);
145145
}
146146
}
147-
147+
148148
return null;
149149
}
150-
150+
151151
/**
152152
* Enrich top pages data with titles
153153
* @param {Array<TopContentDataItem>} data - Raw page data
@@ -157,11 +157,11 @@ class TopContentStatsService {
157157
if (!data || !data.length) {
158158
return [];
159159
}
160-
160+
161161
// Extract post UUIDs and lookup titles
162162
const postUuids = this.extractPostUuids(data);
163163
const titleMap = await this.lookupPostTitles(postUuids);
164-
164+
165165
// Enrich the data with post titles or UrlService lookups
166166
return Promise.all(data.map(async (item) => {
167167
// Check if post_uuid is available directly
@@ -172,7 +172,7 @@ class TopContentStatsService {
172172
post_id: titleMap[item.post_uuid].id
173173
};
174174
}
175-
175+
176176
// Use UrlService for pages without post_uuid
177177
const resourceInfo = this.getResourceTitle(item.pathname);
178178
if (resourceInfo) {
@@ -182,7 +182,7 @@ class TopContentStatsService {
182182
resourceType: resourceInfo.resourceType
183183
};
184184
}
185-
185+
186186
// Otherwise fallback to pathname (removing leading/trailing slashes)
187187
const formattedPath = item.pathname.replace(/^\/|\/$/g, '') || 'Home';
188188
return {
@@ -193,4 +193,4 @@ class TopContentStatsService {
193193
}
194194
}
195195

196-
module.exports = TopContentStatsService;
196+
module.exports = ContentStatsService;

ghost/core/core/server/services/stats/StatsService.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const MRRService = require('./MrrStatsService');
22
const MembersService = require('./MembersStatsService');
33
const SubscriptionStatsService = require('./SubscriptionStatsService');
44
const ReferrersStatsService = require('./ReferrersStatsService');
5-
const TopContentStatsService = require('./TopContentStatsService');
65
const PostsStatsService = require('./PostsStatsService');
6+
const ContentStatsService = require('./ContentStatsService');
77
const tinybird = require('./utils/tinybird');
88

99
class StatsService {
@@ -13,16 +13,16 @@ class StatsService {
1313
* @param {MembersService} deps.members
1414
* @param {SubscriptionStatsService} deps.subscriptions
1515
* @param {ReferrersStatsService} deps.referrers
16-
* @param {TopContentStatsService} deps.topContent
1716
* @param {PostsStatsService} deps.posts
17+
* @param {ContentStatsService} deps.content
1818
**/
1919
constructor(deps) {
2020
this.mrr = deps.mrr;
2121
this.members = deps.members;
2222
this.subscriptions = deps.subscriptions;
2323
this.referrers = deps.referrers;
24-
this.topContent = deps.topContent;
25-
this.topPosts = deps.posts;
24+
this.posts = deps.posts;
25+
this.content = deps.content;
2626
}
2727

2828
async getMRRHistory() {
@@ -41,7 +41,7 @@ class StatsService {
4141
startDate: options.dateFrom
4242
};
4343
delete mappedOptions.dateFrom;
44-
44+
4545
return this.members.getCountHistory(mappedOptions);
4646
}
4747

@@ -67,7 +67,7 @@ class StatsService {
6767
* @param {Object} options
6868
*/
6969
async getTopContent(options = {}) {
70-
return await this.topContent.getTopContent(options);
70+
return await this.content.getTopContent(options);
7171
}
7272

7373
/**
@@ -77,7 +77,7 @@ class StatsService {
7777
*/
7878
async getTopPosts(options = {}) {
7979
// Return the original { data: results } structure
80-
const result = await this.topPosts.getTopPosts(options);
80+
const result = await this.posts.getTopPosts(options);
8181
return result;
8282
}
8383

@@ -111,8 +111,8 @@ class StatsService {
111111
members: new MembersService(deps),
112112
subscriptions: new SubscriptionStatsService(deps),
113113
referrers: new ReferrersStatsService(deps),
114-
topContent: new TopContentStatsService(depsWithTinybird),
115-
posts: new PostsStatsService(deps)
114+
posts: new PostsStatsService(deps),
115+
content: new ContentStatsService(depsWithTinybird)
116116
});
117117
}
118118
}

0 commit comments

Comments
 (0)