Skip to content

Commit

Permalink
feat(scans): set as running when a probe is running
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjeneer committed Apr 15, 2023
1 parent 1cc4177 commit 85b9686
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/models/probe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ProbeStartData = {
export type Probe = {
id: string;
status: ProbeStatus;
scanId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/models/scan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProbeStartData } from "./probe";
import { Probe } from "./probe";

export type ScanStartData = {
id: string;
Expand All @@ -20,5 +20,5 @@ export type ScanRequestResponse = {
}

export type ScanWithProbes = ScanStartData & {
probes: ProbeStartData[];
probes: Probe[];
}
32 changes: 30 additions & 2 deletions src/services/reports/probeResultService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { ProbeDoesNotExist, ScanDoesNotExist } from "../../exceptions/exceptions"
import { ProbeStatus } from "../../models/probe"
import { Probe, ProbeStatus } from "../../models/probe"
import { ScanStatus, ScanWithProbes } from "../../models/scan"
import { deleteMessageFromQueue, deleteMessagesFromQueue, listenResultsQueue } from "../../storage/awsSqsQueue"
import { saveReport } from "../../storage/mongo/mongoReport.storage"
import { getProbe, updateProbe, createProbeResult } from "../../storage/probe.storage"
import { getProbe, updateProbe, createProbeResult, listenProbes } from "../../storage/probe.storage"
import { createReport } from "../../storage/report.storage"
import { getScan, updateScan } from "../../storage/scan.storage"
import supabaseClient from "../../storage/supabase"
import { buildReport } from "./reportService"

const onProbeStart = async (probe: Probe) => {
if (!probe) {
console.error('[REPORT][PROBE START] Probe is missing')
return
}

const scan = await getScan(probe.scanId)
if (!scan) {
console.error(`[REPORT][PROBE START] Scan ${probe.scanId} of probe ${probe.id} does not exist`)
return
}

await updateScan(scan.id, { status: ScanStatus.RUNNING })
}

const onProbeResult = async (probeId: string, resultId: string): Promise<boolean> => {
console.log(`[RESULT][${probeId}] Received result of probe ${probeId} with resultId ${resultId}`)
const probe = await getProbe(probeId);
Expand Down Expand Up @@ -58,3 +74,15 @@ export const initResponsesQueue = () => {
}
})
}

export const initProbeListening = () => {
console.log('[REPORT][PROBES] Listening to realtime changes')
listenProbes((change) => {
if (change.eventType === 'UPDATE' && change.new.status === ProbeStatus.RUNNING) {
// No 100% sure the probe is starting
onProbeStart(change.new)
}
})
}

initProbeListening()
15 changes: 11 additions & 4 deletions src/storage/probe.storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SupabaseProbeResult, ProbeStartData } from "../models/probe"
import { RealtimePostgresChangesPayload } from "@supabase/supabase-js"
import { SupabaseProbeResult, Probe } from "../models/probe"
import { ProbeUpdatePayload, ProbeResultPayload } from "./dto/probe.dto"
import supabaseClient from "./supabase"

Expand All @@ -10,8 +11,8 @@ export const createProbeResult = async (data: ProbeResultPayload): Promise<Supab
return (await supabaseClient.from('probes_results').insert(data).select().single()).data
}

export const getProbe = async (probeId: string): Promise<ProbeStartData> => {
return (await supabaseClient.from('probes').select('*').eq('id', probeId).single()).data as ProbeStartData
export const getProbe = async (probeId: string): Promise<Probe> => {
return (await supabaseClient.from('probes').select('*').eq('id', probeId).single()).data as Probe
}

export const getProbeResultsByScanId = async (scanId: string): Promise<SupabaseProbeResult[]> => {
Expand All @@ -20,4 +21,10 @@ export const getProbeResultsByScanId = async (scanId: string): Promise<SupabaseP
.select('*, probes!inner(*, scans!inner(*))')
.eq('probes.scans.id', scanId)
).data as SupabaseProbeResult[]
}
}

export const listenProbes = (onChange: (payload: RealtimePostgresChangesPayload<Probe>) => void) => {
supabaseClient.channel('probes')
.on('postgres_changes', { event: '*', schema: 'public', table: 'probes'}, onChange)
.subscribe()
}
4 changes: 2 additions & 2 deletions src/storage/scan.storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ProbeStartData } from '../models/probe'
import { Probe } from '../models/probe'
import { ScanStartData, ScanWithProbes } from '../models/scan'
import { ScanUpdatePayload } from './dto/scan.dto'
import supabaseClient from './supabase'


export const saveProbesStartData = async (probes: ProbeStartData[]) => {
export const saveProbesStartData = async (probes: Probe[]) => {
await supabaseClient.from('probes').insert(probes)
}

Expand Down

0 comments on commit 85b9686

Please sign in to comment.