Skip to content

Commit

Permalink
[MIM-2141] Mim 2141 rss news 90 days (#3066)
Browse files Browse the repository at this point in the history
* Added days to getNews

* Refactoring and removed loglines

* Refactoring Code
  • Loading branch information
ssb-cgn authored Dec 16, 2024
1 parent 093735f commit 309e158
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
40 changes: 17 additions & 23 deletions src/main/resources/lib/ssb/rss/news.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { query, type Content } from '/lib/xp/content'
import { StatisticInListing, VariantInListing } from '/lib/ssb/dashboard/statreg/types'
import { getTimeZoneIso } from '/lib/ssb/utils/dateUtils'
import { subDays, isSameDay, format, parseISO } from '/lib/vendor/dateFns'
import { fetchStatisticsWithReleaseToday } from '/lib/ssb/statreg/statistics'
import { subDays, format, parseISO } from '/lib/vendor/dateFns'
import { fetchStatisticsWithReleaseToday, fetchStatisticsDaysBack } from '/lib/ssb/statreg/statistics'
import { getMainSubjects } from '/lib/ssb/utils/subjectUtils'
import { nextReleasedPassed } from '/lib/ssb/utils/variantUtils'
// @ts-ignore
import { xmlEscape } from '/lib/text-encoding'
import { type SubjectItem } from '/lib/types/subject'
Expand All @@ -14,8 +15,8 @@ const dummyReq: Partial<XP.Request> = {
branch: 'master',
}

export function getRssItemsNews(): string | null {
const news: NewsItem[] = getNews()
export function getRssItemsNews(days: number = 1): string | null {
const news: NewsItem[] = getNews(days)
const xml = `<?xml version="1.0" encoding="utf-8"?>
<rssitems count="${news.length}">
${news
Expand All @@ -37,15 +38,15 @@ export function getRssItemsNews(): string | null {
return xml
}

export function getNews(): NewsItem[] {
export function getNews(days: number = 1): NewsItem[] {
const mainSubjects: SubjectItem[] = getMainSubjects(dummyReq as XP.Request)
const articles: NewsItem[] = getArticles(mainSubjects)
const statistics: NewsItem[] = getStatistics(mainSubjects)
const articles: NewsItem[] = getArticles(mainSubjects, days)
const statistics: NewsItem[] = getStatistics(mainSubjects, days)
return articles.concat(statistics)
}

function getArticles(mainSubjects: SubjectItem[]): NewsItem[] {
const from: string = subDays(new Date(), 1).toISOString()
function getArticles(mainSubjects: SubjectItem[], days: number): NewsItem[] {
const from: string = subDays(new Date(), days).toISOString()
const to: string = new Date().toISOString()
const serverOffsetInMilliSeconds: number = parseInt(app.config?.['serverOffsetInMs']) || 0
const timeZoneIso: string = getTimeZoneIso(serverOffsetInMilliSeconds)
Expand Down Expand Up @@ -93,8 +94,9 @@ function getArticles(mainSubjects: SubjectItem[]): NewsItem[] {
return news
}

function getStatistics(mainSubjects: SubjectItem[]): NewsItem[] {
const statregStatistics: Array<StatisticInListing> = fetchStatisticsWithReleaseToday()
function getStatistics(mainSubjects: SubjectItem[], days: number): NewsItem[] {
const statregStatistics: Array<StatisticInListing> =
days > 1 ? fetchStatisticsDaysBack(days) : fetchStatisticsWithReleaseToday()
const serverOffsetInMS: number = parseInt(app.config?.['serverOffsetInMs']) || 0
const timeZoneIso: string = getTimeZoneIso(serverOffsetInMS)

Expand All @@ -114,6 +116,7 @@ function getStatistics(mainSubjects: SubjectItem[]): NewsItem[] {
)
const variant: VariantInListing | undefined = statreg?.variants?.[0] || undefined
const pubDate: string | undefined = variant ? getPubDateStatistic(variant, timeZoneIso) : undefined

const link = getLinkByPath(statistic._path)
if (pubDate) {
statisticsNews.push({
Expand All @@ -136,18 +139,9 @@ function getStatistics(mainSubjects: SubjectItem[]): NewsItem[] {
}

function getPubDateStatistic(variant: VariantInListing, timeZoneIso: string): string | undefined {
const previousReleaseSameDayNow: boolean = variant.previousRelease
? isSameDay(new Date(variant.previousRelease), new Date())
: false
const nextReleaseSameDayNow: boolean = variant.nextRelease
? isSameDay(new Date(variant.nextRelease), new Date())
: false
if (previousReleaseSameDayNow) {
return variant.previousRelease ? formatPubDateStatistic(variant.previousRelease, timeZoneIso) : undefined
} else if (nextReleaseSameDayNow) {
return variant.nextRelease ? formatPubDateStatistic(variant.nextRelease, timeZoneIso) : undefined
}
return undefined
const nextReleaseDatePassed: boolean = variant.nextRelease ? nextReleasedPassed(variant) : false
const pubDate: string | undefined = nextReleaseDatePassed ? variant.nextRelease : variant.previousRelease
return pubDate ? formatPubDateStatistic(pubDate, timeZoneIso) : undefined
}

function formatPubDateArticle(date: string, serverOffsetInMS: number, timeZoneIso: string): string {
Expand Down
22 changes: 21 additions & 1 deletion src/main/resources/lib/ssb/statreg/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ReleaseDatesVariant,
} from '/lib/ssb/dashboard/statreg/types'
import { HttpResponse } from '/lib/http-client'
import { format, isSameDay, isAfter } from '/lib/vendor/dateFns'
import { format, isSameDay, isAfter, subDays, isBefore } from '/lib/vendor/dateFns'
import { isDateBetween } from '/lib/ssb/utils/dateUtils'

import { ensureArray } from '/lib/ssb/utils/arrayUtils'
Expand Down Expand Up @@ -114,6 +114,26 @@ export function fetchStatisticsWithReleaseToday(): Array<StatisticInListing> {
}, [])
}

export function fetchStatisticsDaysBack(days: number): Array<StatisticInListing> {
const statistics: Array<StatisticInListing> = getAllStatisticsFromRepo()
const serverOffsetInMs: number = app.config?.['serverOffsetInMs'] ? parseInt(app.config['serverOffsetInMs']) : 0
const now: Date = new Date(new Date().getTime() + serverOffsetInMs)
const from = subDays(new Date(), days)
return statistics.reduce((statsWithRelease: Array<StatisticInListing>, stat) => {
const variants: Array<VariantInListing> = ensureArray<VariantInListing>(stat.variants).filter(
(variant) =>
(isAfter(new Date(variant.nextRelease), from) && isBefore(new Date(variant.nextRelease), now)) ||
isAfter(new Date(variant.previousRelease), from)
)

if (variants.length > 0) {
stat.variants = variants
statsWithRelease.push(stat)
}
return statsWithRelease
}, [])
}

//TODO: Remove possibly unused code
export function fetchStatisticsWithPreviousReleaseBetween(from: Date, to: Date): Array<StatisticInListing> {
const statistics: Array<StatisticInListing> = getAllStatisticsFromRepo()
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/services/rssNews/rssNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { getNews, getRssItemsNews } from '/lib/ssb/rss/news'

export function get(req: XP.Request): XP.Response {
const format: string = req.params.format ?? 'json'
const days: string = req.params.days ?? '90'
const isXml = format === 'xml'
return {
body: isXml ? getRssItemsNews() : getNews(),
body: isXml ? getRssItemsNews(Number(days)) : getNews(Number(days)),
contentType: isXml ? 'text/xml' : 'application/json',
}
}

0 comments on commit 309e158

Please sign in to comment.