mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
trends: rewrite getTrendingTags to process the history in serial, split across several queries
This commit is contained in:
parent
1afd8a739a
commit
6ce8aae0d1
1 changed files with 31 additions and 33 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { NostrEvent, NStore } from '@nostrify/nostrify';
|
import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
|
|
@ -148,58 +148,56 @@ interface TrendingTag {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTrendingTags(store: NStore, tagName: string): Promise<TrendingTag[]> {
|
export async function getTrendingTags(store: NStore, tagName: string): Promise<TrendingTag[]> {
|
||||||
const filter = {
|
const [label] = await store.query([{
|
||||||
kinds: [1985],
|
kinds: [1985],
|
||||||
'#L': ['pub.ditto.trends'],
|
'#L': ['pub.ditto.trends'],
|
||||||
'#l': [`#${tagName}`],
|
'#l': [`#${tagName}`],
|
||||||
authors: [Conf.pubkey],
|
authors: [Conf.pubkey],
|
||||||
limit: 1,
|
limit: 1,
|
||||||
};
|
}]);
|
||||||
|
|
||||||
const [label] = await store.query([filter]);
|
if (!label) return [];
|
||||||
|
|
||||||
if (!label) {
|
const date = new Date(label.created_at * 1000);
|
||||||
return [];
|
const lastWeek = new Date(date.getTime() - Time.days(7));
|
||||||
}
|
const dates = generateDateRange(lastWeek, date).reverse();
|
||||||
|
|
||||||
const tags = label.tags.filter(([name]) => name === tagName);
|
const results: TrendingTag[] = [];
|
||||||
|
|
||||||
const labelDate = new Date(label.created_at * 1000);
|
for (const [name, value] of label.tags) {
|
||||||
const lastWeek = new Date(labelDate.getTime() - Time.days(7));
|
if (name !== tagName) continue;
|
||||||
const dates = generateDateRange(lastWeek, labelDate).reverse();
|
|
||||||
|
|
||||||
return Promise.all(tags.map(async ([_, value]) => {
|
const history: TrendingTag['history'] = [];
|
||||||
const filters = dates.map((date) => ({
|
|
||||||
...filter,
|
for (const date of dates) {
|
||||||
|
const [label] = await store.query([{
|
||||||
|
kinds: [1985],
|
||||||
|
'#L': ['pub.ditto.trends'],
|
||||||
|
'#l': [`#${tagName}`],
|
||||||
[`#${tagName}`]: [value],
|
[`#${tagName}`]: [value],
|
||||||
|
authors: [Conf.pubkey],
|
||||||
since: Math.floor(date.getTime() / 1000),
|
since: Math.floor(date.getTime() / 1000),
|
||||||
until: Math.floor((date.getTime() + Time.days(1)) / 1000),
|
until: Math.floor((date.getTime() + Time.days(1)) / 1000),
|
||||||
}));
|
limit: 1,
|
||||||
|
} as NostrFilter]);
|
||||||
|
|
||||||
const labels = await store.query(filters);
|
const [, , , accounts, uses] = label?.tags.find(([n, v]) => n === tagName && v === value) ?? [];
|
||||||
|
|
||||||
const history = dates.map((date) => {
|
history.push({
|
||||||
const label = labels.find((label) => {
|
|
||||||
const since = Math.floor(date.getTime() / 1000);
|
|
||||||
const until = Math.floor((date.getTime() + Time.days(1)) / 1000);
|
|
||||||
return label.created_at >= since && label.created_at < until;
|
|
||||||
});
|
|
||||||
|
|
||||||
const [, , , accounts, uses] = label?.tags.find((tag) => tag[0] === tagName && tag[1] === value) ?? [];
|
|
||||||
|
|
||||||
return {
|
|
||||||
day: Math.floor(date.getTime() / 1000),
|
day: Math.floor(date.getTime() / 1000),
|
||||||
authors: Number(accounts || 0),
|
authors: Number(accounts || 0),
|
||||||
uses: Number(uses || 0),
|
uses: Number(uses || 0),
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
results.push({
|
||||||
name: tagName,
|
name: tagName,
|
||||||
value,
|
value,
|
||||||
history,
|
history,
|
||||||
};
|
});
|
||||||
}));
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { trendingLinksController, trendingStatusesController, trendingTagsController };
|
export { trendingLinksController, trendingStatusesController, trendingTagsController };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue