Skip to content

Commit

Permalink
feat: increase batch size in updatePoints API and improve query effic…
Browse files Browse the repository at this point in the history
…iency
  • Loading branch information
pranshu05 committed Dec 7, 2024
1 parent 9bd1f7a commit 3e97b90
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/pages/api/updatePoints/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,53 @@
import { db } from "@/lib/firebaseConfig";
import { collection, getDocs, query, limit, startAfter, orderBy } from "firebase/firestore";
import { collection, getDocs, query, limit, startAfter, orderBy, updateDoc, doc } from "firebase/firestore";
import { getAccessToken } from "@/lib/getAccessToken";
import axios from "axios";
import pLimit from "p-limit";

const BATCH_SIZE = 10;
const BATCH_SIZE = 50;
const CONCURRENT_REQUESTS = 10;

export default async function handler(req, res) {
try {
const usersCollection = collection(db, "users");
let lastVisible = null;
let totalProcessed = 0;

const limitConcurrency = pLimit(CONCURRENT_REQUESTS);

do {
const usersQuery = lastVisible
? query(usersCollection, orderBy("id"), startAfter(lastVisible), limit(BATCH_SIZE))
: query(usersCollection, orderBy("id"), limit(BATCH_SIZE));
? query(usersCollection, orderBy("__name__"), startAfter(lastVisible), limit(BATCH_SIZE))
: query(usersCollection, orderBy("__name__"), limit(BATCH_SIZE));

const usersSnapshot = await getDocs(usersQuery);

console.log(`Batch size fetched: ${usersSnapshot.size}`);

if (usersSnapshot.empty) break;

const users = usersSnapshot.docs.map((doc) => ({
const users = usersSnapshot.docs.map(doc => ({
id: doc.id,
...doc.data(),
}));

const updatePromises = users.map((user) =>
const updatePromises = users.map(user =>
limitConcurrency(async () => {
const { id: userId, lastUpdated, points = 0 } = user;
const currentTimestamp = Date.now();

if (lastUpdated && currentTimestamp - lastUpdated < 60 * 60 * 1000) return;
if (lastUpdated && currentTimestamp - lastUpdated < 60 * 60 * 1000) {
return;
}

const token = await getAccessToken(userId);
if (!token) {
console.warn(`Unable to fetch access token for user ${userId}`);
return;
}

const oneHourAgo = currentTimestamp - 1 * 60 * 60 * 1000;
const response = await axios.get(
`https://api.spotify.com/v1/me/player/recently-played?after=${oneHourAgo}&limit=50`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
const response = await axios.get(`https://api.spotify.com/v1/me/player/recently-played?after=${oneHourAgo}&limit=50`, { headers: { Authorization: `Bearer ${token}` }, });

const tracksPlayed = response.data.items.length;

const userDocRef = doc(db, "users", userId);
await updateDoc(userDocRef, {
points: points + tracksPlayed,
Expand All @@ -71,7 +67,6 @@ export default async function handler(req, res) {
totalProcessed,
});
} catch (error) {
console.error("Error updating points:", error);
res.status(500).json({ error: "Error updating points" });
}
}

0 comments on commit 3e97b90

Please sign in to comment.