Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor cron

Steven Tey6da3c4077eb0
diff --git a/components/app/links/link-filters/index.tsx b/components/app/links/link-filters/index.tsx
index 1bffb040dc..5d384a984b 100644
--- a/components/app/links/link-filters/index.tsx
+++ b/components/app/links/link-filters/index.tsx
@@ -31,7 +31,7 @@ export default function LinkFilters() {
<AnimatePresence>
{showFilters && (
<motion.div
- className="border-t border-gray-200 w-full flex flex-col space-y-2 py-5"
+ className="border-t border-gray-200 w-full flex flex-col space-y-2 py-5 sm:hidden"
{...AnimationSettings}
>
<LinkSort />
diff --git a/components/app/links/links-container.tsx b/components/app/links/links-container.tsx
index e07a809016..a5aa90a2b0 100644
--- a/components/app/links/links-container.tsx
+++ b/components/app/links/links-container.tsx
@@ -33,7 +33,7 @@ export default function LinksContainer({
);
return (
- <MaxWidthWrapper>
+ <MaxWidthWrapper className="pb-10">
<LinkFilters />
<ul className="grid grid-cols-1 gap-3">
{links ? (
diff --git a/lib/cron/usage.tsx b/lib/cron/usage.tsx
index 2564bccfee..712d7d0e25 100644
--- a/lib/cron/usage.tsx
+++ b/lib/cron/usage.tsx
@@ -1,7 +1,8 @@
import sendMail from "emails";
import UsageExceeded from "emails/UsageExceeded";
import prisma from "@/lib/prisma";
-import { getUsage } from "@/lib/upstash";
+import { redis } from "@/lib/upstash";
+import { getFirstAndLastDay } from "@/lib/utils";
import { log } from "./utils";
export const updateUsage = async () => {
@@ -129,6 +130,44 @@ export const updateUsage = async () => {
return response;
};
+/**
+ * Get the usage for a project
+ **/
+const getUsage = async (
+ domain: string,
+ billingCycleStart: number,
+): Promise<number> => {
+ const { firstDay, lastDay } = getFirstAndLastDay(billingCycleStart);
+
+ const links = await prisma.link.findMany({
+ where: {
+ domain,
+ // only for dub.sh, pull data for owner's usage only
+ ...(domain === "dub.sh" && {
+ userId: process.env.DUB_OWNER_ID,
+ }),
+ },
+ select: {
+ key: true,
+ },
+ });
+ let results: number[] = [];
+
+ if (links.length > 0) {
+ const pipeline = redis.pipeline();
+ links.forEach(({ key }) => {
+ pipeline.zcount(
+ `${domain}:clicks:${key}`,
+ firstDay.getTime(),
+ lastDay.getTime(),
+ );
+ });
+ results = await pipeline.exec();
+ }
+ const usage = results.reduce((acc, curr) => acc + curr, 0);
+ return usage;
+};
+
const sendUsageLimitEmail = async (
email: string,
usage: number,
diff --git a/lib/upstash.ts b/lib/upstash.ts
index 8edb4074d6..b9d66bdada 100644
--- a/lib/upstash.ts
+++ b/lib/upstash.ts
@@ -1,9 +1,9 @@
import { NextRequest, userAgent } from "next/server";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
-import { LOCALHOST_GEO_DATA, RESERVED_KEYS } from "@/lib/constants";
-import { LinkProps, ProjectProps } from "@/lib/types";
-import { getFirstAndLastDay, nanoid } from "@/lib/utils";
+import { LOCALHOST_GEO_DATA } from "@/lib/constants";
+import { LinkProps } from "@/lib/types";
+import { nanoid } from "@/lib/utils";
// Initiate Redis instance
export const redis = new Redis({
@@ -123,33 +123,6 @@ export async function getLinkClicksCount(domain: string, key: string) {
);
}
-/**
- * Get the usage for a project
- **/
-export async function getUsage(
- domain: string,
- billingCycleStart: number,
-): Promise<number> {
- const { firstDay, lastDay } = getFirstAndLastDay(billingCycleStart);
-
- const links = await redis.zrange(`${domain}:links:timestamps`, 0, -1);
- let results: number[] = [];
-
- if (links.length > 0) {
- const pipeline = redis.pipeline();
- links.forEach((link) => {
- pipeline.zcount(
- `${domain}:clicks:${link}`,
- firstDay.getTime(),
- lastDay.getTime(),
- );
- });
- results = await pipeline.exec();
- }
- const usage = results.reduce((acc, curr) => acc + curr, 0);
- return usage;
-}
-
export async function changeDomain(domain: string, newHostname: string) {
const keys = await redis.zrange<string[]>(
`${domain}:links:timestamps`,
diff --git a/pages/api/cron/links.ts b/pages/api/cron/links.ts
deleted file mode 100644
index 4ad5f5671a..0000000000
--- a/pages/api/cron/links.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-import { verifySignature } from "@upstash/qstash/nextjs";
-import { log } from "@/lib/cron/utils";
-import { redis } from "@/lib/upstash";
-
-/**
- * Cron to delete generic links (links created on dub.sh) that are older than 30 days.
- * Runs once every day.
- **/
-
-async function handler(_req: NextApiRequest, res: NextApiResponse) {
- try {
- const aWeekAgo = Date.now() - 1000 * 60 * 60 * 24 * 7;
- const links = await redis.zrange<string[]>(
- "dub.sh:links:timestamps:generic",
- 0,
- aWeekAgo,
- { byScore: true },
- );
- const pipeline = redis.pipeline();
- for (const link of links) {
- pipeline.zrem("dub.sh:links:timestamps:generic", link);
- pipeline.hdel("dub.sh:links", link);
- pipeline.del(`dub.sh:clicks:${link}`);
- }
- const [results, _] = await Promise.all([
- pipeline.exec(),
- log(`Successfully deleted *${links.length}* generic links.`),
- ]);
- res.status(200).json(results);
- } catch (error) {
- res.status(500).json({ error: error.message });
- }
-}
-
-/**
- * verifySignature will try to load `QSTASH_CURRENT_SIGNING_KEY` and `QSTASH_NEXT_SIGNING_KEY` from the environment.
-
- * To test out the endpoint manually (wihtout using QStash), you can do `export default handler` instead and
- * hit this endpoint via http://localhost:3000/api/cron/links
- */
-export default verifySignature(handler);
-
-export const config = {
- api: {
- bodyParser: false,
- },
-};