Skip to content

Commit

Permalink
health check approvement
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericdo committed Jan 13, 2025
1 parent 636baf1 commit 592fca5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
22 changes: 16 additions & 6 deletions src/cronjobs/system-health/health-check.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { Slack } from '@messenger/slack'
import { getAdsOfTodayByWebsite } from '@services/db/queries/get-ads-of-today-by-website'
import { getAdsOfTodayByWebsite, getAdsOfWeekByWebsite } from '@services/db/queries/get-ads-of-today-by-website'
import { FUNNIEST_WEBSITES, WEBSITE_LIST } from '@services/websites/website'

export class HealthCheck {
async call() {
const data: {
const adsOfToday: {
website: string, count: number
}[] = await getAdsOfTodayByWebsite()

const countByWebsite = data.reduce((prev, obj) => {
const adsOfWeek: {
website: string, count: number
}[] = await getAdsOfWeekByWebsite()

const websitesWithoutResultSinceOneWeek = WEBSITE_LIST.filter((website) => {
return !FUNNIEST_WEBSITES.includes(website) && !adsOfWeek.some((value) => value.website === website)
})

const countByWebsite = adsOfToday.reduce((prev, obj) => {
prev[obj.website] = obj.count
return prev
}, {})

let message = 'Voici un petit récap du nombre d\'annonces sauvergardées par site aujourd\'hui :\n'

const iterableWebsite = WEBSITE_LIST.filter((website) => {
return !FUNNIEST_WEBSITES.includes(website)
})
const iterableWebsite = Object.keys(countByWebsite)

iterableWebsite.forEach((website, index) => {
if (!Object.prototype.hasOwnProperty.call(countByWebsite, website)) {
Expand All @@ -31,6 +37,10 @@ export class HealthCheck {
}
})

if (websitesWithoutResultSinceOneWeek.length) {
message += `\n*${websitesWithoutResultSinceOneWeek.join(', ')} sont des sites qui semblent en panne.*`
}

new Slack().sendMessage('#health-check', message)
}
}
2 changes: 1 addition & 1 deletion src/services/db/queries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getClassicWebsiteFilter(): { website: { $nin: typeof FUNNIEST_WE
export function getWebsiteFilter(website?: string): { website: string | { $nin: typeof FUNNIEST_WEBSITES } } {
if (website) return { website }

return { website: { $nin: FUNNIEST_WEBSITES } }
return getClassicWebsiteFilter()
}

export function getMainCityFilter(mainCity: AvailableMainCities | 'all'): { city: { $in: AvailableCities[] } } | { city: { $nin: string[] } } | Record<string, never> {
Expand Down
40 changes: 39 additions & 1 deletion src/services/db/queries/get-ads-of-today-by-website.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Rent } from '@db/db'
import { getClassicWebsiteFilter, getDateRangeFilter } from '@services/db/queries/common'

export async function getAdsOfTodayByWebsite(): Promise<{
website: string, count: number
Expand All @@ -8,7 +9,44 @@ export async function getAdsOfTodayByWebsite(): Promise<{

const aggregation = [
{
$match: { createdAt: { $gte: minDate } },
$match: {
...getClassicWebsiteFilter(),
createdAt: { $gte: minDate },
},
},
{
$group: {
_id: { website: '$website' },
count: {
$sum: 1,
},
},
},
{
$project: {
_id: 0,
website: '$_id.website',
count: 1,
}
}
]

return (await Rent.aggregate(aggregation))
}

export async function getAdsOfWeekByWebsite(): Promise<{
website: string, count: number
}[]> {
const today = new Date()
const precedentDate = new Date()
precedentDate.setDate(precedentDate.getDate() - 7)

const aggregation = [
{
$match: {
...getClassicWebsiteFilter(),
...getDateRangeFilter([precedentDate.toISOString(), today.toISOString()]),
},
},
{
$group: {
Expand Down

0 comments on commit 592fca5

Please sign in to comment.