Skip to content

Commit

Permalink
fix: expiresAt can be a numericFilter (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bodin committed Jul 15, 2021
1 parent 18fea1e commit e89fd14
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/@types/pkg.ts
Expand Up @@ -75,7 +75,7 @@ export interface RawPkg {
moduleTypes: ModuleType[];
lastCrawl: string;
_searchInternal: {
expiresAt: string;
expiresAt: number;
alternativeNames: string[];
};
}
Expand Down
14 changes: 7 additions & 7 deletions src/__tests__/__snapshots__/formatPkg.test.ts.snap
Expand Up @@ -8,7 +8,7 @@ Object {
"0.js",
"0js",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -721,7 +721,7 @@ Object {
"@atlaskit/inputjs",
"@atlaskit/input",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -921,7 +921,7 @@ Object {
"@atomic-package/tabjs",
"@atomic-package/tab",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1043,7 +1043,7 @@ Object {
"create-instantsearch-appjs",
"create-instantsearch-app",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {
"create-instantsearch-app": "src/cli/index.js",
Expand Down Expand Up @@ -1169,7 +1169,7 @@ Object {
"indexof.js",
"indexofjs",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1255,7 +1255,7 @@ Object {
"prismjs",
"prism",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1459,7 +1459,7 @@ Object {
"long-boyjs",
"long-boy",
],
"expiresAt": Any<String>,
"expiresAt": Any<Number>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/formatPkg.test.ts
Expand Up @@ -63,7 +63,7 @@ it('transforms correctly', () => {
{
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
},
},
formattedPackage.objectID
Expand Down Expand Up @@ -104,7 +104,7 @@ it('truncates long readmes', () => {
readme: expect.any(String),
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
},
});
});
Expand Down Expand Up @@ -692,7 +692,7 @@ describe('deprecated', () => {
isDeprecated: true,
deprecatedReason: 'Yes this is deprecated',
_searchInternal: {
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
},
});
});
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/saveDocs.test.ts
Expand Up @@ -207,7 +207,7 @@ it('should be similar batch vs one', async () => {
modified: expect.any(Number),
_searchInternal: expect.objectContaining({
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
}),
});

Expand All @@ -220,5 +220,8 @@ it('should be similar batch vs one', async () => {
expect(single).toMatchObject({
...batch,
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(Number),
},
});
});
1 change: 0 additions & 1 deletion src/config.ts
Expand Up @@ -20,7 +20,6 @@ const indexSettings: Settings = {
'types.ts',
'moduleTypes',
'popular',
'_searchInternal.expiresAt',
],
customRanking: [
'desc(_searchInternal.downloadsMagnitude)',
Expand Down
4 changes: 1 addition & 3 deletions src/formatPkg.ts
Expand Up @@ -177,9 +177,7 @@ 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],
expiresAt: new Date(Date.now() + config.expiresAt).getTime(),
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/jsDelivr/__test__/index.test.ts
Expand Up @@ -11,6 +11,8 @@ jest.mock('../../utils/log', () => {
};
});

jest.setTimeout(10000);

beforeEach(() => {
jest.resetAllMocks();
});
Expand Down
7 changes: 5 additions & 2 deletions src/npm/__tests__/index.test.ts
@@ -1,5 +1,7 @@
import * as api from '../index';

jest.setTimeout(10000);

describe('findAll()', () => {
it('contains the correct keys', async () => {
const all = await api.findAll({ limit: 2, startkey: '0' });
Expand Down Expand Up @@ -152,7 +154,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: 'jest',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
},
}),
expect.objectContaining({
Expand All @@ -163,7 +165,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: '@angular/core',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
expiresAt: expect.any(Number),
},
}),
expect.objectContaining({
Expand All @@ -173,6 +175,7 @@ describe('getDownloads()', () => {
popular: false,
_searchInternal: {
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(Number),
},
}),
]);
Expand Down
25 changes: 15 additions & 10 deletions src/npm/index.ts
Expand Up @@ -11,7 +11,7 @@ import type {
import nano from 'nano';
import numeral from 'numeral';

import type { RawPkg } from '../@types/pkg';
import type { FinalPkg, RawPkg } from '../@types/pkg';
import { config } from '../config';
import { datadog } from '../utils/datadog';
import { log } from '../utils/log';
Expand All @@ -25,11 +25,10 @@ type GetDownload = {
humanDownloadsLast30Days: string;
downloadsRatio: number;
popular: boolean;
_searchInternal: {
expiresAt?: string;
popularName?: string;
downloadsMagnitude: number;
};
_searchInternal: Pick<
FinalPkg['_searchInternal'],
'expiresAt' | 'popularName' | 'downloadsMagnitude'
>;
};
let cacheTotalDownloads: { total: number; date: number } | undefined;

Expand Down Expand Up @@ -311,22 +310,28 @@ function computeDownload(
? downloadsLast30Days.toString().length
: 0;

// Rand -48h to +48h, to spread refresh
const randHours = Math.floor(Math.random() * (-48 - 48 + 1)) + 48;
const expiresAt =
new Date(
Date.now() + (popular ? config.popularExpiresAt : config.expiresAt)
).getTime() +
randHours * 3600 * 1000;

return {
downloadsLast30Days,
humanDownloadsLast30Days: numeral(downloadsLast30Days).format('0.[0]a'),
downloadsRatio,
popular,
_searchInternal: {
expiresAt,
downloadsMagnitude,
// 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: pkg.name,
expiresAt: new Date(Date.now() + config.popularExpiresAt)
.toISOString()
.split('T')[0],
}),
downloadsMagnitude,
},
};
}
Expand Down

0 comments on commit e89fd14

Please sign in to comment.