Skip to content

Commit

Permalink
Merge pull request #1175 from serlo/simplify-time-interface
Browse files Browse the repository at this point in the history
refactor(time interface): remove duplicate properties
  • Loading branch information
kulla authored Dec 4, 2023
2 parents bfea11e + 0632663 commit 50017c3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
4 changes: 2 additions & 2 deletions __tests__/examples/data-sources/how-to-create-a-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ describe('How to create a query in a data source: Fetching the content of an art
enableSwr: true,

// After one hour, a cached value shall be considered to be stale
staleAfter: { hour: 1 },
staleAfter: { hours: 1 },

// After one day, no cached value shall be used
maxAge: { day: 1 },
maxAge: { days: 1 },

getKey({ id }) {
return `article/${id}`
Expand Down
20 changes: 1 addition & 19 deletions packages/server/src/internals/swr-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,37 +299,19 @@ async function shouldProcessJob({
}

export interface Time {
day?: number
days?: number
hour?: number
hours?: number
minute?: number
minutes?: number
second?: number
seconds?: number
}

export function timeToSeconds({
day = 0,
days = 0,
hour = 0,
hours = 0,
minute = 0,
minutes = 0,
second = 0,
seconds = 0,
}: Time) {
const SECOND = 1
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR

return (
(day + days) * DAY +
(hour + hours) * HOUR +
(minute + minutes) * MINUTE +
(second + seconds) * SECOND
)
return ((days * 24 + hours) * 60 + minutes) * 60 + seconds
}

export function timeToMilliseconds(time: Time) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/model/google-spreadsheet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function createGoogleSpreadsheetApiModel({
return specifyErrorLocation(E.left({ error: E.toError(error) }))
}
},
staleAfter: { hour: 1 },
staleAfter: { hours: 1 },
getKey: (args) => {
const { spreadsheetId, range } = args
const majorDimension = args.majorDimension ?? MajorDimension.Rows
Expand Down
22 changes: 11 additions & 11 deletions packages/server/src/model/serlo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createSerloModel({
const uuid = await DatabaseLayer.makeRequest('UuidQuery', payload)
return isSupportedUuid(uuid) ? uuid : null
},
staleAfter: { day: 1 },
staleAfter: { days: 1 },
getKey: ({ id }) => {
return `de.serlo.org/api/uuid/${id}`
},
Expand Down Expand Up @@ -89,7 +89,7 @@ export function createSerloModel({
getCurrentValue() {
return DatabaseLayer.makeRequest('ActiveAuthorsQuery', undefined)
},
staleAfter: { hour: 1 },
staleAfter: { hours: 1 },
getKey: () => {
return 'de.serlo.org/api/user/active-authors'
},
Expand All @@ -110,7 +110,7 @@ export function createSerloModel({
getCurrentValue() {
return DatabaseLayer.makeRequest('ActiveReviewersQuery', undefined)
},
staleAfter: { hour: 1 },
staleAfter: { hours: 1 },
getKey: () => {
return 'de.serlo.org/api/user/active-reviewers'
},
Expand Down Expand Up @@ -236,7 +236,7 @@ export function createSerloModel({
})
},
enableSwr: true,
staleAfter: { day: 1 },
staleAfter: { days: 1 },
getKey: ({ path, instance }) => {
const cleanPath = encodePath(decodePath(path))
return `${instance}.serlo.org/api/alias${cleanPath}`
Expand All @@ -261,7 +261,7 @@ export function createSerloModel({
return DatabaseLayer.makeRequest('SubjectsQuery', {})
},
enableSwr: true,
staleAfter: { day: 1 },
staleAfter: { days: 1 },
getKey: () => 'serlo.org/subjects',
getPayload: (key) => {
return key === 'serlo.org/subjects' ? O.some(undefined) : O.none
Expand All @@ -280,7 +280,7 @@ export function createSerloModel({
},
enableSwr: true,
staleAfter: { minutes: 2 },
maxAge: { hour: 1 },
maxAge: { hours: 1 },
getKey: () => 'serlo.org/unrevised',
getPayload: (key) => {
return key === 'serlo.org/unrevised' ? O.some(undefined) : O.none
Expand Down Expand Up @@ -322,7 +322,7 @@ export function createSerloModel({
return isSupportedNotificationEvent(event) ? event : null
},
enableSwr: true,
staleAfter: { day: 1 },
staleAfter: { days: 1 },
getKey: ({ id }) => {
return `de.serlo.org/api/event/${id}`
},
Expand Down Expand Up @@ -372,8 +372,8 @@ export function createSerloModel({
}
},
enableSwr: true,
staleAfter: { minute: 2 },
maxAge: { hour: 1 },
staleAfter: { minutes: 2 },
maxAge: { hours: 1 },
examplePayload: { first: 5 },
},
environment,
Expand Down Expand Up @@ -436,7 +436,7 @@ export function createSerloModel({
) => {
return DatabaseLayer.makeRequest('SubscriptionsQuery', payload)
},
staleAfter: { hour: 1 },
staleAfter: { hours: 1 },
getKey: ({ userId }) => {
return `de.serlo.org/api/subscriptions/${userId}`
},
Expand Down Expand Up @@ -500,7 +500,7 @@ export function createSerloModel({
return DatabaseLayer.makeRequest('ThreadsQuery', payload)
},
enableSwr: true,
staleAfter: { day: 1 },
staleAfter: { days: 1 },
getKey: ({ id }) => {
return `de.serlo.org/api/threads/${id}`
},
Expand Down

0 comments on commit 50017c3

Please sign in to comment.