From d83193721736f8fef4dbc4370f311cc1940ea9b6 Mon Sep 17 00:00:00 2001 From: SMe12435 Date: Wed, 1 Nov 2023 11:24:01 +0530 Subject: [PATCH] added feature: daily visit --- .../migrations/20200211220920_constraints.ts | 2 ++ server/models/daily-visit.ts | 36 +++++++++++++++++++ server/models/index.ts | 1 + server/queries/daily-visit.ts | 28 +++++++++++++++ server/queries/index.ts | 4 ++- server/queues/visit.ts | 2 ++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 server/models/daily-visit.ts create mode 100644 server/queries/daily-visit.ts diff --git a/server/migrations/20200211220920_constraints.ts b/server/migrations/20200211220920_constraints.ts index a991cdb38..9917dd8d1 100644 --- a/server/migrations/20200211220920_constraints.ts +++ b/server/migrations/20200211220920_constraints.ts @@ -8,6 +8,8 @@ export async function up(knex: Knex): Promise { await models.createHostTable(knex); await models.createLinkTable(knex); await models.createVisitTable(knex); + await models.createDailyVisitTable(knex); + await Promise.all([ knex.raw(` diff --git a/server/models/daily-visit.ts b/server/models/daily-visit.ts new file mode 100644 index 000000000..d155df44e --- /dev/null +++ b/server/models/daily-visit.ts @@ -0,0 +1,36 @@ +import { Knex } from "knex"; + + +export async function createDailyVisitTable(knex: Knex) { + const hasTable = await knex.schema.hasTable("daily_visit"); + // console.log("TRYING TO CREATE A TABLE"); + if (!hasTable) { + await knex.schema.createTable("daily_visit", table => { + table.increments("id").primary(); // Primary key, autoincrement + table.integer("link_id").notNullable(); // Foreign key to link_id + table.date("date").notNullable().defaultTo(knex.fn.now()); // Current date + table.integer("visit_count").notNullable().defaultTo(0); // Visit count + table.timestamp("created_at").defaultTo(knex.fn.now()); // Timestamp for entry creation + table.timestamp("updated_at").defaultTo(knex.fn.now()); // Timestamp for entry update + + // Ensure the combination of link_id and date is unique + table.unique(["link_id", "date"]); + }); + + await knex.raw(` + CREATE OR REPLACE FUNCTION update_updated_at_column() + RETURNS TRIGGER AS $$ + BEGIN + NEW.updated_at = NOW(); + RETURN NEW; + END; + $$ language 'plpgsql'; + + CREATE TRIGGER update_daily_visit_updated_at + BEFORE UPDATE ON daily_visit + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); + ` + ); + } + } diff --git a/server/models/index.ts b/server/models/index.ts index ec4dd6d5f..80a339375 100644 --- a/server/models/index.ts +++ b/server/models/index.ts @@ -4,3 +4,4 @@ export * from "./ip"; export * from "./link"; export * from "./user"; export * from "./visit"; +export * from "./daily-visit"; diff --git a/server/queries/daily-visit.ts b/server/queries/daily-visit.ts new file mode 100644 index 000000000..148a7ae7d --- /dev/null +++ b/server/queries/daily-visit.ts @@ -0,0 +1,28 @@ +import knex from "../knex"; + +interface DailyVisit { + link_id: number; + date: Date; + visit_count: number; +} + +interface LinkData { + id: number; // Assuming id is of type number + // Add other properties from Link if needed +} + +export const insertDailyVisit = async (data: LinkData) => { + const currentDate = new Date(); + console.log("incrementing Daily Visit"); + const dailyVisit: DailyVisit = { + link_id: data.id, // Extracting link_id from the data object + date: currentDate, + visit_count: 1, + }; + + // Insert a new row into the 'dailyVisit' table with link_id, current date, and visit_count of 1 + await knex("daily_visit") + .insert(dailyVisit) + .onConflict(["link_id", "date"]) // Specify the unique constraint columns + .merge({ visit_count: knex.raw("daily_visit.visit_count + 1") }); // Increment visit_count on conflict +}; \ No newline at end of file diff --git a/server/queries/index.ts b/server/queries/index.ts index f98c4bd8c..d98e5a28a 100644 --- a/server/queries/index.ts +++ b/server/queries/index.ts @@ -4,6 +4,7 @@ import * as link from "./link"; import * as user from "./user"; import * as host from "./host"; import * as ip from "./ip"; +import * as dailyVisit from "./daily-visit"; const queries = { domain, @@ -11,7 +12,8 @@ const queries = { ip, link, user, - visit + visit, + dailyVisit }; export default queries; diff --git a/server/queues/visit.ts b/server/queues/visit.ts index 1aba9ea7a..cbc601210 100644 --- a/server/queues/visit.ts +++ b/server/queues/visit.ts @@ -16,7 +16,9 @@ export default function visit({ data }) { const tasks = []; tasks.push(query.link.incrementVisit({ id: data.link.id })); + tasks.push(query.dailyVisit.insertDailyVisit({ id: data.link.id })); + if (data.link.visit_count < getStatsLimit()) { const agent = useragent.parse(data.headers["user-agent"]); const [browser = "Other"] = browsersList.filter(filterInBrowser(agent));