From 0b368db9fd9151228ee9c6bc0057a050e9e8cc40 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Fri, 10 Oct 2025 22:24:14 +0300 Subject: [PATCH 1/6] perf(chartData): added indexes for dailyEvents collection --- ...9-add-businessOperations-currency copy.js} | 0 .../2025101022010-add-daily-events-indexes.js | 107 ++++++++++++++++++ package.json | 3 +- src/resolvers/project.js | 15 ++- 4 files changed, 123 insertions(+), 2 deletions(-) rename migrations/{20241110161019-add-businessOperations-currency.js => 20241110161019-add-businessOperations-currency copy.js} (100%) create mode 100644 migrations/2025101022010-add-daily-events-indexes.js diff --git a/migrations/20241110161019-add-businessOperations-currency.js b/migrations/20241110161019-add-businessOperations-currency copy.js similarity index 100% rename from migrations/20241110161019-add-businessOperations-currency.js rename to migrations/20241110161019-add-businessOperations-currency copy.js diff --git a/migrations/2025101022010-add-daily-events-indexes.js b/migrations/2025101022010-add-daily-events-indexes.js new file mode 100644 index 00000000..6c77060a --- /dev/null +++ b/migrations/2025101022010-add-daily-events-indexes.js @@ -0,0 +1,107 @@ +const groupingTimestampIndexName = 'groupingTimestamp'; +const groupingTimestampAndGroupHashIndexName = 'groupingTimestampAndGroupHash'; + +module.exports = { + async up(db) { + const collections = await db.listCollections({}, { + authorizedCollections: true, + nameOnly: true, + }).toArray(); + + const targetCollections = []; + + collections.forEach((collection) => { + if (/dailyEvents/.test(collection.name)) { + targetCollections.push(collection.name); + } + }); + + console.log(`${targetCollections.length} collections will be updated.`); + + let currentCollectionNumber = 1; + + for (const collectionName of targetCollections) { + console.log(`${collectionName} in process.`); + console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`); + try { + const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName); + + if (!hasGroupingTimestampIndexAlready) { + await db.collection(collectionName).createIndex({ + groupingTimestamp: 1, + }, { + name: groupingTimestampIndexName, + sparse: true, + background: true, + }); + console.log(`Index ${groupingTimestampIndexName} created for ${collectionName}`); + } else { + console.log(`Index ${groupingTimestampIndexName} already exists for ${collectionName}`); + } + + const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName); + + if (!hasGroupingTimestampAndGroupHashIndexAlready) { + await db.collection(collectionName).createIndex({ + groupingTimestamp: 1, + groupHash: 1, + }, { + name: groupingTimestampAndGroupHashIndexName, + sparse: true, + background: true, + }); + console.log(`Index ${groupingTimestampAndGroupHashIndexName} created for ${collectionName}`); + } else { + console.log(`Index ${groupingTimestampAndGroupHashIndexName} already exists for ${collectionName}`); + } + } catch (error) { + console.error(`Error adding indexes to ${collectionName}:`, error); + } + currentCollectionNumber++; + } + }, + async down(db) { + const collections = await db.listCollections({}, { + authorizedCollections: true, + nameOnly: true, + }).toArray(); + + const targetCollections = []; + + collections.forEach((collection) => { + if (/dailyEvents/.test(collection.name)) { + targetCollections.push(collection.name); + } + }); + + console.log(`${targetCollections.length} collections will be updated.`); + + let currentCollectionNumber = 1; + + for (const collectionName of targetCollections) { + console.log(`${collectionName} in process.`); + console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`); + + try { + const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName); + if (hasGroupingTimestampIndexAlready) { + await db.collection(collectionName).dropIndex(groupingTimestampIndexName); + console.log(`Index ${groupingTimestampIndexName} dropped for ${collectionName}`); + } else { + console.log(`Index ${groupingTimestampIndexName} does not exist for ${collectionName}, skipping drop.`); + } + + const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName); + if (hasGroupingTimestampAndGroupHashIndexAlready) { + await db.collection(collectionName).dropIndex(groupingTimestampAndGroupHashIndexName); + console.log(`Index ${groupingTimestampAndGroupHashIndexName} dropped for ${collectionName}`); + } else { + console.log(`Index ${groupingTimestampAndGroupHashIndexName} does not exist for ${collectionName}, skipping drop.`); + } + } catch (error) { + console.error(`Error dropping indexes from ${collectionName}:`, error); + } + currentCollectionNumber++; + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index f4b8a889..b882adab 100644 --- a/package.json +++ b/package.json @@ -84,5 +84,6 @@ "safe-regex": "^2.1.0", "ts-node-dev": "^2.0.0", "uuid": "^8.3.2" - } + }, + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } diff --git a/src/resolvers/project.js b/src/resolvers/project.js index bfe2ef31..ef8b9d69 100644 --- a/src/resolvers/project.js +++ b/src/resolvers/project.js @@ -99,7 +99,20 @@ module.exports = { const projectRepetitionsEventsCollection = await mongo.databases.events.createCollection('repetitions:' + project._id); - await mongo.databases.events.createCollection('dailyEvents:' + project._id); + const projectDailyEventsCollection = await mongo.databases.events.createCollection('dailyEvents:' + project._id); + + await projectDailyEventsCollection.createIndex({ + groupingTimestamp: 1, + }, { + name: 'groupingTimestamp', + }); + + await projectDailyEventsCollection.createIndex({ + groupingTimestamp: 1, + groupHash: 1, + }, { + name: 'groupingTimestampAndGroupHash', + }); await projectEventsCollection.createIndex({ groupHash: 1, From 482b868db363419d620b82fd1c7901e15e42c410 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 10 Oct 2025 19:30:07 +0000 Subject: [PATCH 2/6] Bump version up to 1.1.43 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b882adab..956de11d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.1.42", + "version": "1.1.43", "main": "index.ts", "license": "BUSL-1.1", "scripts": { From aebdb8fc8bcae029ef10888721492bf872719da2 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Fri, 10 Oct 2025 22:30:43 +0300 Subject: [PATCH 3/6] revert package.json changes --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index b882adab..f4b8a889 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,5 @@ "safe-regex": "^2.1.0", "ts-node-dev": "^2.0.0", "uuid": "^8.3.2" - }, - "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" + } } From 89efa9ea81f34e600dff8fea94c09c11a0a477d1 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Fri, 10 Oct 2025 22:33:24 +0300 Subject: [PATCH 4/6] fix name --- ... copy.js => 20241110161019-add-businessOperations-currency.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename migrations/{20241110161019-add-businessOperations-currency copy.js => 20241110161019-add-businessOperations-currency.js} (100%) diff --git a/migrations/20241110161019-add-businessOperations-currency copy.js b/migrations/20241110161019-add-businessOperations-currency.js similarity index 100% rename from migrations/20241110161019-add-businessOperations-currency copy.js rename to migrations/20241110161019-add-businessOperations-currency.js From 47b3ef3201aa2e1cdaf807a558703275378a6e78 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Wed, 5 Nov 2025 02:09:33 +0300 Subject: [PATCH 5/6] rm migration --- .../2025101022010-add-daily-events-indexes.js | 107 ------------------ 1 file changed, 107 deletions(-) delete mode 100644 migrations/2025101022010-add-daily-events-indexes.js diff --git a/migrations/2025101022010-add-daily-events-indexes.js b/migrations/2025101022010-add-daily-events-indexes.js deleted file mode 100644 index 6c77060a..00000000 --- a/migrations/2025101022010-add-daily-events-indexes.js +++ /dev/null @@ -1,107 +0,0 @@ -const groupingTimestampIndexName = 'groupingTimestamp'; -const groupingTimestampAndGroupHashIndexName = 'groupingTimestampAndGroupHash'; - -module.exports = { - async up(db) { - const collections = await db.listCollections({}, { - authorizedCollections: true, - nameOnly: true, - }).toArray(); - - const targetCollections = []; - - collections.forEach((collection) => { - if (/dailyEvents/.test(collection.name)) { - targetCollections.push(collection.name); - } - }); - - console.log(`${targetCollections.length} collections will be updated.`); - - let currentCollectionNumber = 1; - - for (const collectionName of targetCollections) { - console.log(`${collectionName} in process.`); - console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`); - try { - const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName); - - if (!hasGroupingTimestampIndexAlready) { - await db.collection(collectionName).createIndex({ - groupingTimestamp: 1, - }, { - name: groupingTimestampIndexName, - sparse: true, - background: true, - }); - console.log(`Index ${groupingTimestampIndexName} created for ${collectionName}`); - } else { - console.log(`Index ${groupingTimestampIndexName} already exists for ${collectionName}`); - } - - const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName); - - if (!hasGroupingTimestampAndGroupHashIndexAlready) { - await db.collection(collectionName).createIndex({ - groupingTimestamp: 1, - groupHash: 1, - }, { - name: groupingTimestampAndGroupHashIndexName, - sparse: true, - background: true, - }); - console.log(`Index ${groupingTimestampAndGroupHashIndexName} created for ${collectionName}`); - } else { - console.log(`Index ${groupingTimestampAndGroupHashIndexName} already exists for ${collectionName}`); - } - } catch (error) { - console.error(`Error adding indexes to ${collectionName}:`, error); - } - currentCollectionNumber++; - } - }, - async down(db) { - const collections = await db.listCollections({}, { - authorizedCollections: true, - nameOnly: true, - }).toArray(); - - const targetCollections = []; - - collections.forEach((collection) => { - if (/dailyEvents/.test(collection.name)) { - targetCollections.push(collection.name); - } - }); - - console.log(`${targetCollections.length} collections will be updated.`); - - let currentCollectionNumber = 1; - - for (const collectionName of targetCollections) { - console.log(`${collectionName} in process.`); - console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`); - - try { - const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName); - if (hasGroupingTimestampIndexAlready) { - await db.collection(collectionName).dropIndex(groupingTimestampIndexName); - console.log(`Index ${groupingTimestampIndexName} dropped for ${collectionName}`); - } else { - console.log(`Index ${groupingTimestampIndexName} does not exist for ${collectionName}, skipping drop.`); - } - - const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName); - if (hasGroupingTimestampAndGroupHashIndexAlready) { - await db.collection(collectionName).dropIndex(groupingTimestampAndGroupHashIndexName); - console.log(`Index ${groupingTimestampAndGroupHashIndexName} dropped for ${collectionName}`); - } else { - console.log(`Index ${groupingTimestampAndGroupHashIndexName} does not exist for ${collectionName}, skipping drop.`); - } - } catch (error) { - console.error(`Error dropping indexes from ${collectionName}:`, error); - } - currentCollectionNumber++; - } - } -} \ No newline at end of file From f00b7e7d6e3f50181cd7d6466df64cd8e46eea43 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Wed, 5 Nov 2025 02:20:14 +0300 Subject: [PATCH 6/6] add constants --- src/resolvers/project.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/resolvers/project.js b/src/resolvers/project.js index 07c53cf7..9513e497 100644 --- a/src/resolvers/project.js +++ b/src/resolvers/project.js @@ -14,6 +14,8 @@ const EVENTS_GROUP_HASH_INDEX_NAME = 'groupHashUnique'; const REPETITIONS_GROUP_HASH_INDEX_NAME = 'groupHash_hashed'; const REPETITIONS_USER_ID_INDEX_NAME = 'userId'; const EVENTS_TIMESTAMP_INDEX_NAME = 'timestamp'; +const GROUPING_TIMESTAMP_INDEX_NAME = 'groupingTimestamp'; +const GROUPING_TIMESTAMP_AND_GROUP_HASH_INDEX_NAME = 'groupingTimestampAndGroupHash'; const MAX_SEARCH_QUERY_LENGTH = 50; /** @@ -106,14 +108,14 @@ module.exports = { await projectDailyEventsCollection.createIndex({ groupingTimestamp: 1, }, { - name: 'groupingTimestamp', + name: GROUPING_TIMESTAMP_INDEX_NAME, }); await projectDailyEventsCollection.createIndex({ groupingTimestamp: 1, groupHash: 1, }, { - name: 'groupingTimestampAndGroupHash', + name: GROUPING_TIMESTAMP_AND_GROUP_HASH_INDEX_NAME, }); await projectEventsCollection.createIndex({