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:
Security-Chief-Odo 2023-07-12 08:21:54 -05:00 committed by GitHub
parent bbee0a29a9
commit 014c386348
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5816 additions and 3639 deletions

View file

@ -9,12 +9,15 @@ export class Instances extends Component<any, any> {
super(props, context); super(props, context);
} }
calculateInstanceSortValue(instance: any) { biasedRandom(min, max, bias, i) {
// Mostly sort by active users, but add a large random component to // Lets introduce a better bias to random suffle instances list
// randomize order of instances with about the same order of magnitude of var rnd = Math.random() * (max - min) + min;
// users. var mix = Math.random() * i;
let active_users = instance.site_info.site_view.counts.users_active_month; return rnd * (1 - mix) + bias * mix;
return active_users + active_users * 3 * Math.random(); }
averageFunc(values: any) {
return values.reduce((a, b) => a + b) / values.length;
} }
render() { render() {
@ -27,22 +30,40 @@ export class Instances extends Component<any, any> {
var recommended = []; var recommended = [];
var remaining = []; var remaining = [];
var values = [];
for (var i of instance_stats.stats.instance_details) { for (var i of instance_stats.stats.instance_details) {
if (recommended_instances.indexOf(i.domain) > -1) { if (recommended_instances.indexOf(i.domain) > -1) {
recommended.push(i); recommended.push(i);
} else { } else {
remaining.push(i); 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 let recommended2 = recommended
.map(value => ({ value, sort: Math.random() })) .map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort) .sort((a, b) => a.sort - b.sort)
.map(({ value }) => value); .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 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) .sort((a, b) => b.sort - a.sort)
.map(({ instance }) => instance); .map(({ instance }) => instance);

File diff suppressed because one or more lines are too long