Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
fix/issue #321 (#322)
Browse files Browse the repository at this point in the history
- reset incident status when new event is received
 - use 82905 pattern in criteria when querying incidents by name, project_id, and message to 'avoid issues with escaping both single and double quotes'
  • Loading branch information
ghesla-mumms authored Mar 26, 2024
1 parent 6b345e6 commit 7c14407
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions relay-worker/src/db/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
getHealthByValue,
VitalsEnum,
TraceoSpan,
TraceoMetric
TraceoMetric,
IncidentStatus
} from "@traceo/types";
import dayjs from "dayjs";
import format from "pg-format";
Expand Down Expand Up @@ -73,12 +74,13 @@ export class DatabaseService {
}

public async getProjectById(id: string, client: PoolClient = this.client): Promise<IProject | undefined> {
const result = await client.query<IProject>(`SELECT * FROM project WHERE id = '${id}'`);
const result = await client.query<IProject>(`SELECT * FROM project WHERE id = $$${id}$$`);
return result.rows[0];
}

public async getIncident({ name, message, projectId }: { name: string, message: string, projectId: string }, client: PoolClient = this.client): Promise<IIncident | undefined> {
let sqlQuery = `SELECT * FROM incident WHERE name = '${name}' AND project_id = '${projectId}'`;
// help to avoid issues with escaping both single and double quotes
let sqlQuery = `SELECT * FROM incident WHERE name = $$${name}$$ AND project_id = $$${projectId}$$`;

if (message) {
// $4 - help to avoid issues with escaping both single and double quotes
Expand Down Expand Up @@ -155,7 +157,7 @@ export class DatabaseService {
private async updateIncidentOnEvent({ incident_id, timestamp }: { incident_id: string, timestamp: number }): Promise<void> {
await this.client.query(`
UPDATE incident
SET events_count = COALESCE(events_count, 0) + 1, last_event_at = '${timestamp}'
SET events_count = COALESCE(events_count, 0) + 1, last_event_at = '${timestamp}', status = '${IncidentStatus.UNRESOLVED}'
WHERE id = '${incident_id}'
`);
}
Expand Down

0 comments on commit 7c14407

Please sign in to comment.