Skip to content

Commit

Permalink
Merge remote-tracking branch 'day/feature_DAILY_VISIT' into kutt_dev5
Browse files Browse the repository at this point in the history
  • Loading branch information
IITII committed Jan 29, 2024
2 parents cac6b2a + d831937 commit 079d1aa
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server/migrations/20200211220920_constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export async function up(knex: Knex): Promise<any> {
await models.createHostTable(knex);
await models.createLinkTable(knex);
await models.createVisitTable(knex);
await models.createDailyVisitTable(knex);


await Promise.all([
knex.raw(`
Expand Down
36 changes: 36 additions & 0 deletions server/models/daily-visit.ts
Original file line number Diff line number Diff line change
@@ -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();
`
);
}
}
1 change: 1 addition & 0 deletions server/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./ip";
export * from "./link";
export * from "./user";
export * from "./visit";
export * from "./daily-visit";
28 changes: 28 additions & 0 deletions server/queries/daily-visit.ts
Original file line number Diff line number Diff line change
@@ -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<DailyVisit>("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
};
4 changes: 3 additions & 1 deletion server/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ 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,
host,
ip,
link,
user,
visit
visit,
dailyVisit
};

export default queries;
2 changes: 2 additions & 0 deletions server/queues/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 079d1aa

Please sign in to comment.