Added last update, cache strategy, telegram fail notifications, teachers photos

This commit is contained in:
VityaSchel
2023-10-02 18:54:26 +04:00
parent f6daee6201
commit 755654cf9d
19 changed files with 579 additions and 124 deletions

View File

@@ -4,22 +4,27 @@ import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next'
import { getSchedule } from '@/app/agregator/schedule'
import { NextSerialized, nextDeserializer, nextSerialized } from '@/app/utils/date-serializer'
import { NavBar } from '@/widgets/navbar'
import { LastUpdateAt } from '@/entities/last-update-at'
type PageProps = NextSerialized<{
schedule: Day[]
parsedAt: Date
}>
export default function HomePage(props: PageProps) {
const { schedule } = nextDeserializer(props)
const { schedule, parsedAt } = nextDeserializer(props)
return (
<>
<NavBar />
<LastUpdateAt date={parsedAt} />
<Schedule days={schedule} />
</>
)
}
const cachedSchedules = new Map<string, { lastFetched: Date, results: Day[] }>()
const maxCacheDurationInMS = 1000 * 60 * 60
export async function getServerSideProps(context: GetServerSidePropsContext<{ group: string }>): Promise<GetServerSidePropsResult<PageProps>> {
const groups: { [group: string]: [number, string] } = {
ps7: [146, 'ПС-7'],
@@ -27,11 +32,24 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
}
const group = context.params?.group
if (group && Object.hasOwn(groups, group) && group in groups) {
const schedule = await getSchedule(...groups[group])
let schedule
let parsedAt
const cachedSchedule = cachedSchedules.get(group)
if (cachedSchedule?.lastFetched && Date.now() - cachedSchedule.lastFetched.getTime() < maxCacheDurationInMS) {
schedule = cachedSchedule.results
parsedAt = cachedSchedule.lastFetched
} else {
schedule = await getSchedule(...groups[group])
parsedAt = new Date()
cachedSchedules.set(group, { lastFetched: new Date(), results: schedule })
}
return {
props: {
schedule: nextSerialized(schedule)
}
props: nextSerialized({
schedule: schedule,
parsedAt: parsedAt
})
}
} else {
return {