Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add expiresAt field #643

Merged
merged 5 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/@types/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface RawPkg {
moduleTypes: ModuleType[];
lastCrawl: string;
_searchInternal: {
expiresAt: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to use it for a filter, why isn't it a timestamp/number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same same, it's just easier to read as string no?

alternativeNames: string[];
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/__snapshots__/formatPkg.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Object {
"0.js",
"0js",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -721,6 +722,7 @@ Object {
"@atlaskit/inputjs",
"@atlaskit/input",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -921,6 +923,7 @@ Object {
"@atomic-package/tabjs",
"@atomic-package/tab",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1043,6 +1046,7 @@ Object {
"create-instantsearch-appjs",
"create-instantsearch-app",
],
"expiresAt": Any<String>,
},
"bin": Object {
"create-instantsearch-app": "src/cli/index.js",
Expand Down Expand Up @@ -1169,6 +1173,7 @@ Object {
"indexof.js",
"indexofjs",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1255,6 +1260,7 @@ Object {
"prismjs",
"prism",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1459,6 +1465,7 @@ Object {
"long-boyjs",
"long-boy",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/formatPkg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ it('transforms correctly', () => {
expect(formattedPackage).toMatchSnapshot(
{
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
},
},
formattedPackage.objectID
)
Expand Down Expand Up @@ -100,6 +103,9 @@ it('truncates long readmes', () => {
expect(formatted).toMatchSnapshot({
readme: expect.any(String),
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
},
});
});

Expand Down Expand Up @@ -720,6 +726,9 @@ describe('deprecated', () => {
deprecated: 'Yes this is deprecated',
isDeprecated: true,
deprecatedReason: 'Yes this is deprecated',
_searchInternal: {
expiresAt: expect.any(String),
},
});
});
});
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const indexSettings: Settings = {
'isDeprecated',
'types.ts',
'moduleTypes',
'popular',
'_searchInternal.expiresAt',
],
customRanking: [
'desc(_searchInternal.downloadsMagnitude)',
Expand Down Expand Up @@ -168,6 +170,8 @@ export const config = {
indexSettings,
indexSynonyms,
indexRules,
expiresAt: ms('30 days'),
popularExpiresAt: ms('7 days'),
};

export type Config = typeof config;
Expand Down
3 changes: 3 additions & 0 deletions src/formatPkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ export default function formatPkg(pkg: GetPackage): RawPkg | undefined {
lastCrawl: new Date().toISOString(),
_searchInternal: {
alternativeNames,
expiresAt: new Date(Date.now() + config.expiresAt)
.toISOString()
.split('T')[0],
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/npm/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: 'jest',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
},
}),
expect.objectContaining({
Expand All @@ -162,6 +163,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: '@angular/core',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
},
}),
expect.objectContaining({
Expand Down
8 changes: 7 additions & 1 deletion src/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ async function getDownloads(pkgs: Array<Pick<RawPkg, 'name'>>): Promise<
downloadsRatio: number;
popular: boolean;
_searchInternal: {
expiresAt?: string;
popularName?: string;
downloadsMagnitude: number;
};
Expand Down Expand Up @@ -289,7 +290,12 @@ async function getDownloads(pkgs: Array<Pick<RawPkg, 'name'>>): Promise<
// if the package is popular, we copy its name to a dedicated attribute
// which will make popular records' `name` matches to be ranked higher than other matches
// see the `searchableAttributes` index setting
...(popular && { popularName: name }),
...(popular && {
popularName: name,
expiresAt: new Date(Date.now() + config.popularExpiresAt)
.toISOString()
.split('T')[0],
}),
downloadsMagnitude,
},
};
Expand Down