From 893c7c2003f99d8574732533b0432ec6870e52a9 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 7 Dec 2023 08:33:43 -0500 Subject: [PATCH] revert --- .../client-statistics.service.ts | 58 ++++--------------- 1 file changed, 10 insertions(+), 48 deletions(-) diff --git a/src/ORM/client-statistics/client-statistics.service.ts b/src/ORM/client-statistics/client-statistics.service.ts index aeb0239..748f68d 100644 --- a/src/ORM/client-statistics/client-statistics.service.ts +++ b/src/ORM/client-statistics/client-statistics.service.ts @@ -17,56 +17,18 @@ export class ClientStatisticsService { } - // public async save(clientStatistic: Partial) { - // const res1 = await this.clientStatisticsRepository.createQueryBuilder() - // .update(ClientStatisticsEntity) - // .set({ - // shares: () => `"shares" + ${clientStatistic.shares}`, // Use the actual value of shares here - // acceptedCount: () => `"acceptedCount" + 1` - // }) - // .where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time }) - // .execute(); - - // if (res1.affected == 0) { - // await this.clientStatisticsRepository.insert(clientStatistic); - // } - // } public async save(clientStatistic: Partial) { - const queryRunner = this.clientStatisticsRepository.manager.connection.createQueryRunner(); + const res1 = await this.clientStatisticsRepository.createQueryBuilder() + .update(ClientStatisticsEntity) + .set({ + shares: () => `"shares" + ${clientStatistic.shares}`, // Use the actual value of shares here + acceptedCount: () => `"acceptedCount" + 1` + }) + .where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time }) + .execute(); - await queryRunner.connect(); - await queryRunner.startTransaction(); - - try { - // Acquire a lock on the selected rows - const selectedRows = await queryRunner.manager.getRepository(ClientStatisticsEntity) - .createQueryBuilder() - .setLock('pessimistic_write') // Acquire a write lock - .where('address = :address AND "clientName" = :clientName AND "sessionId" = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time }) - .getMany(); - - // Update the rows - if (selectedRows.length > 0) { - await queryRunner.manager.getRepository(ClientStatisticsEntity) - .createQueryBuilder() - .update(ClientStatisticsEntity) - .set({ - shares: () => `"shares" + ${clientStatistic.shares}`, - acceptedCount: () => `"acceptedCount" + 1` - }) - .where('address = :address AND "clientName" = :clientName AND "sessionId" = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time }) - .execute(); - } else { - // If no rows were found, insert a new record - await queryRunner.manager.getRepository(ClientStatisticsEntity).insert(clientStatistic); - } - - await queryRunner.commitTransaction(); - } catch (error) { - await queryRunner.rollbackTransaction(); - throw error; - } finally { - await queryRunner.release(); + if (res1.affected == 0) { + await this.clientStatisticsRepository.insert(clientStatistic); } }