Skip to content

Commit 040fe2f

Browse files
committed
Renamed TopContentStatsService to ContentStatsService
1 parent bf64591 commit 040fe2f

File tree

3 files changed

+84
-84
lines changed

3 files changed

+84
-84
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

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const MRRService = require('./MrrStatsService');
22
const MembersService = require('./MembersStatsService');
33
const SubscriptionStatsService = require('./SubscriptionStatsService');
44
const ReferrersStatsService = require('./ReferrersStatsService');
5-
const TopContentStatsService = require('./TopContentStatsService');
5+
const ContentStatsService = require('./ContentStatsService');
66
const tinybird = require('./utils/tinybird');
77

88
class StatsService {
@@ -12,14 +12,14 @@ class StatsService {
1212
* @param {MembersService} deps.members
1313
* @param {SubscriptionStatsService} deps.subscriptions
1414
* @param {ReferrersStatsService} deps.referrers
15-
* @param {TopContentStatsService} deps.topContent
15+
* @param {ContentStatsService} deps.content
1616
**/
1717
constructor(deps) {
1818
this.mrr = deps.mrr;
1919
this.members = deps.members;
2020
this.subscriptions = deps.subscriptions;
2121
this.referrers = deps.referrers;
22-
this.topContent = deps.topContent;
22+
this.content = deps.content;
2323
}
2424

2525
async getMRRHistory() {
@@ -38,7 +38,7 @@ class StatsService {
3838
startDate: options.dateFrom
3939
};
4040
delete mappedOptions.dateFrom;
41-
41+
4242
return this.members.getCountHistory(mappedOptions);
4343
}
4444

@@ -59,12 +59,12 @@ class StatsService {
5959
meta: {}
6060
};
6161
}
62-
62+
6363
/**
6464
* @param {Object} options
6565
*/
6666
async getTopContent(options = {}) {
67-
return await this.topContent.getTopContent(options);
67+
return await this.content.getTopContent(options);
6868
}
6969

7070
/**
@@ -77,15 +77,15 @@ class StatsService {
7777
let tinybirdClient = null;
7878
const config = deps.config || require('../../../shared/config');
7979
const request = deps.request || require('../../lib/request-external');
80-
80+
8181
// Only create the client if Tinybird is configured
8282
if (config.get('tinybird') && config.get('tinybird:stats')) {
8383
tinybirdClient = tinybird.create({
84-
config,
84+
config,
8585
request
8686
});
8787
}
88-
88+
8989
// Add the Tinybird client to the dependencies
9090
const depsWithTinybird = {
9191
...deps,
@@ -97,9 +97,9 @@ class StatsService {
9797
members: new MembersService(deps),
9898
subscriptions: new SubscriptionStatsService(deps),
9999
referrers: new ReferrersStatsService(deps),
100-
topContent: new TopContentStatsService(depsWithTinybird)
100+
content: new ContentStatsService(depsWithTinybird)
101101
});
102102
}
103103
}
104104

105-
module.exports = StatsService;
105+
module.exports = StatsService;

0 commit comments

Comments
 (0)