-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8df389
commit 2264516
Showing
12 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class AddLastCall1733506046286 implements MigrationInterface { | ||
name = 'AddLastCall1733506046286' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "monitor" ADD "lastCall" TIMESTAMP`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "monitor" DROP COLUMN "lastCall"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
interface genericEvent { | ||
id: number; | ||
|
||
title: string; | ||
|
||
kind: eventKindType; | ||
|
||
createdAt: string; | ||
} | ||
|
||
const EVENT_KINDS = ['up', 'down'] as const; | ||
|
||
type eventKindType = (typeof EVENT_KINDS)[number]; | ||
|
||
export { EVENT_KINDS }; | ||
export type { eventKindType, genericEvent }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Monitor } from './Monitor.entity'; | ||
import { buildMonitorController } from './monitor.controller'; | ||
import { buildMonitorService } from './monitor.service'; | ||
|
||
export { Monitor, buildMonitorController }; | ||
export { Monitor, buildMonitorController, buildMonitorService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Monitor } from '../monitor'; | ||
import { EVENT_KINDS, eventKindType, genericEvent } from '../genericEvent/types'; | ||
|
||
@Entity() | ||
export class MonitorEvent implements genericEvent { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@Column('enum', { enum: EVENT_KINDS }) | ||
kind: eventKindType; | ||
|
||
@ManyToOne(() => Monitor, { onDelete: 'CASCADE', nullable: false }) | ||
monitor: Monitor; | ||
|
||
@CreateDateColumn({ type: 'timestamp' }) | ||
createdAt: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { MonitorEvent } from './MonitorEvent.entity'; | ||
import { buildMonitorEventService } from './monitorEvent.service'; | ||
export { MonitorEvent, buildMonitorEventService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { dataSource } from '../../dataSource'; | ||
import { Monitor } from '../monitor'; | ||
import { MonitorEvent } from './MonitorEvent.entity'; | ||
|
||
export { buildMonitorEventService }; | ||
|
||
function buildMonitorEventService() { | ||
const monitorMonitorEventRepository = dataSource.getRepository(MonitorEvent); | ||
|
||
const monitorMonitorEventService = { | ||
createMonitorEvent, | ||
getLastMonitorEvent, | ||
}; | ||
|
||
return monitorMonitorEventService; | ||
|
||
async function getLastMonitorEvent(monitorId: Monitor['id']) { | ||
return monitorMonitorEventRepository.findOne({ | ||
where: { monitor: { id: monitorId } }, | ||
order: { createdAt: 'DESC' }, | ||
relations: ['monitor'], | ||
}); | ||
} | ||
|
||
async function createMonitorEvent( | ||
monitorId: Monitor['id'], | ||
params: { title: MonitorEvent['title']; kind: MonitorEvent['kind'] }, | ||
) { | ||
await monitorMonitorEventRepository.insert({ | ||
monitor: { id: monitorId }, | ||
title: params.title, | ||
kind: params.kind, | ||
}); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { dataSource } from '../dataSource'; | ||
import { buildMonitorService } from '../modules/monitor'; | ||
|
||
async function pingMonitors() { | ||
const monitorService = buildMonitorService(); | ||
console.log('Initializing database...'); | ||
await dataSource.initialize(); | ||
console.log('Database initialized!'); | ||
console.log('Getting monitors...'); | ||
const monitors = await monitorService.getMonitors(); | ||
console.log(`${monitors.length} monitors found!`); | ||
console.log('Pinging monitors...'); | ||
await Promise.all( | ||
monitors.map(async (monitor) => { | ||
const shouldPingMonitor = monitorService.computeShouldPingMonitor(monitor, new Date()); | ||
if (!shouldPingMonitor) { | ||
return; | ||
} | ||
return monitorService.pingMonitor(monitor); | ||
}), | ||
); | ||
|
||
console.log('Done!'); | ||
} | ||
|
||
pingMonitors(); |