feat: Update referrers and countries in js
This commit is contained in:
parent
a3b7a5a350
commit
00afd4f5ab
@ -11,7 +11,7 @@ DEFAULT_DOMAIN=localhost:3000
|
|||||||
LINK_LENGTH=6
|
LINK_LENGTH=6
|
||||||
|
|
||||||
# Postgres database credential details
|
# Postgres database credential details
|
||||||
DB_CLIENT=postgresql
|
DB_CLIENT=pg
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_NAME=postgres
|
DB_NAME=postgres
|
||||||
|
@ -14,50 +14,54 @@ async function add(params) {
|
|||||||
const truncatedNow = new Date();
|
const truncatedNow = new Date();
|
||||||
truncatedNow.setMinutes(0, 0, 0);
|
truncatedNow.setMinutes(0, 0, 0);
|
||||||
|
|
||||||
// Create a subquery first that truncates the
|
return knex.transaction(async (trx) => {
|
||||||
const subquery = knex("visits")
|
// Create a subquery first that truncates the
|
||||||
.select("visits.*")
|
const subquery = trx("visits")
|
||||||
.select({
|
.select("visits.*")
|
||||||
created_at_hours: utils.knexUtils(knex).truncatedTimestamp("created_at", "hour")
|
.select({
|
||||||
})
|
created_at_hours: utils.knexUtils(trx).truncatedTimestamp("created_at", "hour")
|
||||||
.where({ link_id: params.id })
|
})
|
||||||
.as("subquery");
|
.where({ link_id: params.id })
|
||||||
|
.as("subquery");
|
||||||
|
|
||||||
const visit = await knex
|
const visit = await trx
|
||||||
.select("*")
|
.select("*")
|
||||||
.from(subquery)
|
.from(subquery)
|
||||||
.where("created_at_hours", "=", truncatedNow.toISOString())
|
.where("created_at_hours", "=", truncatedNow.toISOString())
|
||||||
.first();
|
.forUpdate()
|
||||||
|
.first();
|
||||||
|
|
||||||
if (visit) {
|
if (visit) {
|
||||||
await knex("visits")
|
await trx("visits")
|
||||||
.where({ id: visit.id })
|
.where({ id: visit.id })
|
||||||
.increment(`br_${data.browser}`, 1)
|
.increment(`br_${data.browser}`, 1)
|
||||||
.increment(`os_${data.os}`, 1)
|
.increment(`os_${data.os}`, 1)
|
||||||
.increment("total", 1)
|
.increment("total", 1)
|
||||||
.update({
|
.update({
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
countries: knex.raw(
|
countries: {
|
||||||
"jsonb_set(countries, '{??}', (COALESCE(countries->>?,'0')::int + 1)::text::jsonb)",
|
...visit.countries,
|
||||||
[data.country, data.country]
|
[data.country]: visit.countries[data.country] + 1
|
||||||
),
|
},
|
||||||
referrers: knex.raw(
|
referrers: {
|
||||||
"jsonb_set(referrers, '{??}', (COALESCE(referrers->>?,'0')::int + 1)::text::jsonb)",
|
...visit.referrers,
|
||||||
[data.referrer, data.referrer]
|
[data.referrer]: visit.referrers[data.referrer] + 1
|
||||||
)
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// This must also happen in the transaction to avoid concurrency
|
||||||
|
await trx("visits").insert({
|
||||||
|
[`br_${data.browser}`]: 1,
|
||||||
|
countries: { [data.country]: 1 },
|
||||||
|
referrers: { [data.referrer]: 1 },
|
||||||
|
[`os_${data.os}`]: 1,
|
||||||
|
total: 1,
|
||||||
|
link_id: data.id
|
||||||
});
|
});
|
||||||
} else {
|
}
|
||||||
await knex("visits").insert({
|
|
||||||
[`br_${data.browser}`]: 1,
|
|
||||||
countries: { [data.country]: 1 },
|
|
||||||
referrers: { [data.referrer]: 1 },
|
|
||||||
[`os_${data.os}`]: 1,
|
|
||||||
total: 1,
|
|
||||||
link_id: data.id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return visit;
|
return visit;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function find(match, total) {
|
async function find(match, total) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user