Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stories view counts #2336

Merged
merged 9 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docker/compose/postgres/model/07-clean-up-blog-objects.psql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
SET search_path TO EVENTS;

-- ensure an object for the '/stories' uri exists for all /blog uris

INSERT INTO objects (uri)
(SELECT DISTINCT REPLACE(uri, '/blog/', '/stories/')
FROM objects) ON CONFLICT DO NOTHING;

-- on actions/history linked to /blog/ objects, update the link to be to the /stories/ object

UPDATE actions
SET object_id=good_id
FROM
(SELECT bad.id bad_id,
rwd marked this conversation as resolved.
Show resolved Hide resolved
good.id good_id
FROM objects bad
INNER JOIN objects good ON good.uri=REPLACE(bad.uri, '/blog/', '/stories/')
WHERE bad.uri like '%/blog/%'
AND good.uri like '%/stories/%') bad_good
WHERE object_id=bad_id;

UPDATE history
SET object_id=good_id
FROM
(SELECT bad.id bad_id,
good.id good_id
FROM objects bad
INNER JOIN objects good ON good.uri=REPLACE(bad.uri, '/blog/', '/stories/')
WHERE bad.uri like '%/blog/%'
AND good.uri like '%/stories/%') bad_good
WHERE object_id=bad_id;

-- delete the /blog/ objects

DELETE
FROM objects
WHERE uri like '/blog/';
8 changes: 6 additions & 2 deletions packages/portal/src/server-middleware/api/events/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default (config = {}) => {
// Ignore any search query or hash
const uri = `${url.origin}${url.pathname}`;

// TODO: update old objects, then stop using legacy uris
// @/docker/compose/postgres/model/07-clean-up-blog-objects.psql
const legacyUri = url.pathname.startsWith('/stories') ? `${url.origin}${url.pathname.replace('/stories', '/blog')}` : uri;

const result = await pg.query(`
SELECT SUM(views) AS views
FROM
Expand All @@ -33,9 +37,9 @@ export default (config = {}) => {
LEFT JOIN events.action_types AT ON a.action_type_id=at.id
GROUP BY at.name,
o.uri) actions_and_history
WHERE action_type_name='view' AND (uri=$1 OR uri LIKE $2)
WHERE action_type_name='view' AND (uri=$1 OR uri=$2)
`,
[uri, `${uri}?%`]
[uri, legacyUri]
);

const viewCount = Number(result.rows[0]?.views);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('@/server-middleware/api/events/views', () => {

expect(pgPoolQuery.calledWith(
sinon.match((sql) => sql.trim().startsWith('SELECT ')),
['https://example.com/example', 'https://example.com/example?%']
['https://example.com/example', 'https://example.com/example']
)).toBe(true);
});

Expand Down
Loading