Skip to content

Commit

Permalink
Update analytics range
Browse files Browse the repository at this point in the history
  • Loading branch information
NavidK0 committed Feb 17, 2021
1 parent f417d40 commit 39d2fe0
Showing 1 changed file with 72 additions and 56 deletions.
128 changes: 72 additions & 56 deletions server/scripts/stats-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,81 @@ class StatsExporter {
});
}

async countProfilesCreated(monthsMin: number = 0, monthsMax = 1) {
async countProfilesCreated(daysMin: number = 0, daysMax = 30) {
const queryResult = await this.pool.query(`select count(*)
from app.profiles
where profiles.created_on < current_date - interval '1 month' * $1
and profiles.created_on > current_date - interval '1 month' * $2`,
where profiles.created_on < current_date - interval '1 day' * $1
and profiles.created_on > current_date - interval '1 day' * $2`,
[
monthsMin,
monthsMax
daysMin,
daysMax
]);

return {
name: `profiles created (past ${monthsMin} to ${monthsMax} month(s))`,
range: `${monthsMax - monthsMin} months`,
name: `profiles created (past ${daysMin} to ${daysMax} day(s))`,
range: `${daysMax - daysMin} day(s)`,
count: queryResult.rows[0].count
};
}

async countProfilesWithPageVisits(monthsMin: number = 0, monthsMax = 1) {
async countProfilesWithPageVisits(daysMin: number = 0, daysMax = 30) {
const queryResult = await this.pool.query(`select count(*)
from app.profiles
where exists(select 1
from analytics.visits
where type = 'page'
and referral_id = app.profiles.user_id
and profiles.created_on < current_date - interval '1 month' * $1
and profiles.created_on > current_date - interval '1 month' * $2);`,
and profiles.created_on < current_date - interval '1 day' * $1
and profiles.created_on > current_date - interval '1 day' * $2);`,
[
monthsMin,
monthsMax
daysMin,
daysMax
]);

return {
name: `profiles with page visits (past ${monthsMin} to ${monthsMax} month(s))`,
range: `${monthsMax - monthsMin} months`,
name: `profiles with page visits (past ${daysMin} to ${daysMax} day(s))`,
range: `${daysMax - daysMin} day(s)`,
count: queryResult.rows[0].count
};
}

async countProfilesWithLinkVisits(monthsMin: number = 0, monthsMax = 1) {
async countProfilesWithLinkVisits(daysMin: number = 0, daysMax = 30) {
const queryResult = await this.pool.query(`select count(*)
from app.links
where exists(select 1
from analytics.visits
where type = 'link'
and referral_id = app.links.profile_id
and links.created_on < current_date - interval '1 month' * $1
and links.created_on > current_date - interval '1 month' * $2);`,
and links.created_on < current_date - interval '1 day' * $1
and links.created_on > current_date - interval '1 day' * $2);`,
[
monthsMin,
monthsMax
daysMin,
daysMax
]);

return {
name: `profiles with link clicks (past ${monthsMin} to ${monthsMax} month(s))`,
range: `${monthsMax - monthsMin} months`,
name: `profiles with link clicks (past ${daysMin} to ${daysMax} day(s))`,
range: `${daysMax - daysMin} day(s)`,
count: queryResult.rows[0].count
};
}

async countProfilesWithLinksCreated(monthsMin: number = 0, monthsMax = 1) {
async countProfilesWithLinksCreated(daysMin: number = 0, daysMax = 30) {
const queryResult = await this.pool.query(`select count(*)
from app.profiles
where exists(select 1
from app.links
where app.profiles.id = app.links.profile_id
and links.created_on < current_date - interval '1 month' * $1
and links.created_on > current_date - interval '1 month' * $2)`,
and links.created_on < current_date - interval '1 day' * $1
and links.created_on > current_date - interval '1 day' * $2)`,
[
monthsMin,
monthsMax
daysMin,
daysMax
]);

return {
name: `links created (past ${monthsMin} to ${monthsMax} month(s))`,
range: `${monthsMax - monthsMin} months`,
name: `links created (past ${daysMin} to ${daysMax} day(s))`,
range: `${daysMax - daysMin} day(s)`,
count: queryResult.rows[0].count
};
}
Expand All @@ -129,68 +129,84 @@ class StatsExporter {
};
}

async countUsersCreated(monthsMin: number = 0, monthsMax = 1) {
async countUsersCreated(daysMin: number = 0, daysMax = 30) {
const queryResult = await this.pool.query(`select count(*)
from app.users
where users.created_on < current_date - interval '1 month' * $1
and users.created_on > current_date - interval '1 month' * $2;`,
where users.created_on < current_date - interval '1 day' * $1
and users.created_on > current_date - interval '1 day' * $2;`,
[
monthsMin,
monthsMax
daysMin,
daysMax
]);

return {
name: `users created (past ${monthsMin} to ${monthsMax} month(s))`,
range: `${monthsMax - monthsMin} months`,
name: `users created (past ${daysMin} to ${daysMax} day(s))`,
range: `${daysMax - daysMin} day(s)`,
count: queryResult.rows[0].count
};
}

async averagePageVisitsPerProfile() {
const queryResult = await this.pool.query(`select avg((select count(*)
from analytics.visits
where type = 'page' and referral_id = app.profiles.id))
from app.profiles`);

return {
name: `average page visits per profile`,
range: `all`,
count: queryResult.rows[0].avg
};
}

async exportAll() {
let json: unknown[] = [];

json.push(await this.countUsersWithMultipleProfiles());
json.push(await this.averagePageVisitsPerProfile());

for (let i = 0; i < 3; i++) {
json.push(await this.countUsersCreated(i, i + 1));
}
const daysIncrement = 30;
const maxDays = 180;

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesCreated(i, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countUsersCreated(i, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithPageVisits(i, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesCreated(i, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithLinkVisits(i, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithPageVisits(i, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithLinksCreated(i, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithLinkVisits(i, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countUsersCreated(0, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithLinksCreated(i, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesCreated(0, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countUsersCreated(0, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithPageVisits(0, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesCreated(0, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithLinkVisits(0, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithPageVisits(0, i + daysIncrement));
}

for (let i = 0; i < 3; i++) {
json.push(await this.countProfilesWithLinksCreated(0, i + 1));
for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithLinkVisits(0, i + daysIncrement));
}

for (let i = 0; i < maxDays; i += daysIncrement) {
json.push(await this.countProfilesWithLinksCreated(0, i + daysIncrement));
}

return json;
}
Expand Down

0 comments on commit 39d2fe0

Please sign in to comment.