Browse Source

skip duplicate timestamps on ServerDataEntry

Christian Kahlau 11 months ago
parent
commit
30e2d3f2e5
1 changed files with 18 additions and 10 deletions
  1. 18 10
      server/src/ctrl/mariadb-database.class.ts

+ 18 - 10
server/src/ctrl/mariadb-database.class.ts

@@ -1,5 +1,5 @@
 import moment from 'moment';
-import { Pool } from 'mysql';
+import { MysqlError, Pool } from 'mysql';
 
 import defaults from '../../../common/defaults.module';
 import { ServiceConfig, validateParamType } from '../../../common/interfaces/service-config.interface';
@@ -75,15 +75,23 @@ export class MariaDBDatabase implements DataProvider, HealthCheckDataProvider {
     await this.db.beginTransaction();
     try {
       for (const entry of data) {
-        const result = await this.db.query(
-          `INSERT INTO \`ServerDataEntry\`(\`ServerID\`, \`Timestamp\`) VALUES(?, ?);
-          SELECT LAST_INSERT_ID() as 'ID';`,
-          [serverID, entry.time]
-        );
-
-        if (!result || result.length < 2) throw new DatabaseException('Unexpected result during insertServerData');
-
-        let entryID = (result[1] as any[])[0]['ID'];
+        let entryID = 0;
+        try {
+          const result = await this.db.query(
+            `INSERT INTO \`ServerDataEntry\`(\`ServerID\`, \`Timestamp\`) VALUES(?, ?);
+            SELECT LAST_INSERT_ID() as 'ID';`,
+            [serverID, entry.time]
+          );
+
+          if (!result || result.length < 2) throw new DatabaseException('Unexpected result during insertServerData');
+
+          entryID = (result[1] as any[])[0]['ID'];
+        } catch (err: any) {
+          if (err.code === 'ER_DUP_ENTRY' && (err as MysqlError).sqlMessage?.includes('UQ_ServerDataEntry_1')) {
+            Logger.warn('Skipping', err.sqlMessage);
+            continue;
+          }
+        }
 
         for (const type of Object.keys(entry).filter(t => !['time', 'hdd'].includes(t))) {
           for (const key of Object.keys((entry as any)[type])) {