import products dinamically
This commit is contained in:
@@ -4191,6 +4191,7 @@ UserSchema.statics.getLastOnlineUsers = async function (idapp) {
|
||||
surname: 1,
|
||||
lasttimeonline: 1,
|
||||
date_reg: 1,
|
||||
verified_by_aportador: 1,
|
||||
'profile.img': 1,
|
||||
index: 1,
|
||||
}).sort({ lasttimeonline: -1 }).limit(lastn).then((arr) => {
|
||||
@@ -4217,6 +4218,7 @@ UserSchema.statics.getLastSharedLink = async function (idapp) {
|
||||
name: 1,
|
||||
surname: 1,
|
||||
lasttimeonline: 1,
|
||||
verified_by_aportador: 1,
|
||||
date_reg: 1,
|
||||
'profile.img': 1,
|
||||
index: 1,
|
||||
@@ -4370,6 +4372,47 @@ UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
|
||||
return query;
|
||||
};
|
||||
|
||||
UserSchema.statics.getUsersRegDailyAverage = function (idapp, nrec) {
|
||||
const query = [
|
||||
{
|
||||
$match: {
|
||||
idapp,
|
||||
date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) },
|
||||
$or: [
|
||||
{ deleted: { $exists: false } },
|
||||
{ deleted: { $exists: true, $eq: false } }
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
$dateToString: {
|
||||
format: '%Y-%m-%d',
|
||||
date: '$date_reg',
|
||||
timezone: 'Europe/Rome',
|
||||
},
|
||||
},
|
||||
count: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
total: { $sum: '$count' },
|
||||
days: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
dailyAverage: { $divide: ['$total', '$days'] },
|
||||
},
|
||||
},
|
||||
];
|
||||
return query;
|
||||
};
|
||||
|
||||
UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
|
||||
|
||||
const query = [
|
||||
@@ -4461,6 +4504,7 @@ UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
|
||||
idapp: 1,
|
||||
"profile.img": 1,
|
||||
'profile.mycircuits': 1,
|
||||
'profile.handshake': 1,
|
||||
},
|
||||
},
|
||||
];
|
||||
@@ -4668,6 +4712,19 @@ UserSchema.statics.getUsersRegWeekly = function (idapp, nrec) {
|
||||
count: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null, // Raggruppa tutti i risultati
|
||||
total: { $sum: '$count' }, // Calcola il numero totale di iscritti
|
||||
days: { $sum: 1 }, // Calcola il numero totale di giorni
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0, // Escludi il campo _id dal risultato finale
|
||||
dailyAverage: { $divide: ['$total', '$days'] }, // Calcola la media giornaliera
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: { _id: 1 },
|
||||
},
|
||||
@@ -4715,6 +4772,36 @@ UserSchema.statics.calcnumRegUntilDay = async function (idapp) {
|
||||
|
||||
};
|
||||
|
||||
function calculate30DayAverage(data) {
|
||||
const averages = [];
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const startDate = new Date(data[i]._id);
|
||||
let sum = data[i].count;
|
||||
let count = 1;
|
||||
|
||||
for (let j = i + 1; j < data.length; j++) {
|
||||
const currentDate = new Date(data[j]._id);
|
||||
const diffInTime = Math.abs(startDate.getTime() - currentDate.getTime());
|
||||
const diffInDays = Math.ceil(diffInTime / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffInDays <= 30) {
|
||||
sum += data[j].count;
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
averages.push({
|
||||
_id: data[i]._id,
|
||||
dailyAverage: sum / count
|
||||
});
|
||||
}
|
||||
|
||||
return averages;
|
||||
}
|
||||
|
||||
UserSchema.statics.calcRegDaily = async function (idapp) {
|
||||
const User = this;
|
||||
|
||||
@@ -4724,12 +4811,14 @@ UserSchema.statics.calcRegDaily = async function (idapp) {
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
UserSchema.statics.calcRegWeekly = async function (idapp) {
|
||||
const User = this;
|
||||
|
||||
return await User.aggregate(User.getUsersRegWeekly(idapp, 20 * 7)).then(ris => {
|
||||
return await User.aggregate(User.getUsersRegDaily(idapp, 120)).then(ris => {
|
||||
// console.table(ris);
|
||||
return ris.slice(0, -1);
|
||||
return calculate30DayAverage(ris);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user