mirror of
https://github.com/LemmyNet/joinlemmy-site.git
synced 2024-11-21 20:01:16 +00:00
Change instance sort order to be more evenly distributed (#214)
* Changed 'popular' instance sorting to be more evenly distributed, and improve the weighting for user numbers * Ran prettier on prevoiusly comitted code * Ran prettier on prevoiusly comitted code * Modified averageFunc to be a class function instead of const var. Ensured prettier and yarn lint were successfully ran. * Updated variable names and reverted min_monthly_user const * Prettier ran, modified generate_translations.mjs only for formatting corrections
This commit is contained in:
parent
bbee0a29a9
commit
014c386348
2 changed files with 5816 additions and 3639 deletions
|
@ -9,12 +9,15 @@ export class Instances extends Component<any, any> {
|
|||
super(props, context);
|
||||
}
|
||||
|
||||
calculateInstanceSortValue(instance: any) {
|
||||
// Mostly sort by active users, but add a large random component to
|
||||
// randomize order of instances with about the same order of magnitude of
|
||||
// users.
|
||||
let active_users = instance.site_info.site_view.counts.users_active_month;
|
||||
return active_users + active_users * 3 * Math.random();
|
||||
biasedRandom(min, max, bias, i) {
|
||||
// Lets introduce a better bias to random suffle instances list
|
||||
var rnd = Math.random() * (max - min) + min;
|
||||
var mix = Math.random() * i;
|
||||
return rnd * (1 - mix) + bias * mix;
|
||||
}
|
||||
|
||||
averageFunc(values: any) {
|
||||
return values.reduce((a, b) => a + b) / values.length;
|
||||
}
|
||||
|
||||
render() {
|
||||
|
@ -27,22 +30,40 @@ export class Instances extends Component<any, any> {
|
|||
|
||||
var recommended = [];
|
||||
var remaining = [];
|
||||
var values = [];
|
||||
|
||||
for (var i of instance_stats.stats.instance_details) {
|
||||
if (recommended_instances.indexOf(i.domain) > -1) {
|
||||
recommended.push(i);
|
||||
} else {
|
||||
remaining.push(i);
|
||||
}
|
||||
|
||||
values.push(i.site_info.site_view.counts.users_active_month);
|
||||
}
|
||||
// shuffle recommended instances list into random order
|
||||
// https://stackoverflow.com/a/46545530
|
||||
|
||||
// Use these values for the shuffle
|
||||
const maxMonthlyUsers = Math.max(...values);
|
||||
const minMonthlyUsers = Math.min(...values);
|
||||
const avgMonthlyUsers = this.averageFunc(values);
|
||||
|
||||
let recommended2 = recommended
|
||||
.map(value => ({ value, sort: Math.random() }))
|
||||
.sort((a, b) => a.sort - b.sort)
|
||||
.map(({ value }) => value);
|
||||
|
||||
// BIASED sorting for instances, based on the min/max of users_active_month
|
||||
// weighted to 2/3 of all counts, but more even distribution
|
||||
let remaining2 = remaining
|
||||
.map(i => ({ instance: i, sort: this.calculateInstanceSortValue(i) }))
|
||||
.map(i => ({
|
||||
instance: i,
|
||||
sort: this.biasedRandom(
|
||||
minMonthlyUsers,
|
||||
maxMonthlyUsers,
|
||||
avgMonthlyUsers,
|
||||
0.75,
|
||||
),
|
||||
}))
|
||||
.sort((a, b) => b.sort - a.sort)
|
||||
.map(({ instance }) => instance);
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue