Skip to content
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
22 changes: 0 additions & 22 deletions src/api/catalog-search/__snapshots__/util.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ Array [
Object {
"name": "cdk-time-bomb",
},
Object {
"name": "cdk-time-bomb",
},
Object {
"name": "cdk-tweet-queue",
},
Expand All @@ -266,9 +263,6 @@ Array [
Object {
"name": "cdk8s-aws-alb-ingress-controller",
},
Object {
"name": "cdk8s-aws-alb-ingress-controller",
},
Object {
"name": "cdk8s-op",
},
Expand Down Expand Up @@ -639,10 +633,6 @@ Array [
"date": "Tue, 21 Jul 2020 14:12:38 GMT",
"name": "cdk-time-bomb",
},
Object {
"date": "Thu, 14 May 2020 15:21:58 GMT",
"name": "cdk-time-bomb",
},
Object {
"date": "Wed, 15 Sep 2021 00:19:48 GMT",
"name": "cdk-tweet-queue",
Expand All @@ -659,10 +649,6 @@ Array [
"date": "Mon, 23 Nov 2020 09:47:27 GMT",
"name": "cdk8s-aws-alb-ingress-controller",
},
Object {
"date": "Mon, 23 Nov 2020 08:29:12 GMT",
"name": "cdk8s-aws-alb-ingress-controller",
},
Object {
"date": "Wed, 18 Nov 2020 05:29:35 GMT",
"name": "cdk8s-op",
Expand Down Expand Up @@ -772,10 +758,6 @@ Array [
"date": "Fri, 17 Apr 2020 18:59:40 GMT",
"name": "cdk-sqs-monitored",
},
Object {
"date": "Thu, 14 May 2020 15:21:58 GMT",
"name": "cdk-time-bomb",
},
Object {
"date": "Thu, 04 Jun 2020 19:50:33 GMT",
"name": "aws-lambda-golang",
Expand Down Expand Up @@ -856,10 +838,6 @@ Array [
"date": "Wed, 18 Nov 2020 05:29:35 GMT",
"name": "cdk8s-op",
},
Object {
"date": "Mon, 23 Nov 2020 08:29:12 GMT",
"name": "cdk8s-aws-alb-ingress-controller",
},
Object {
"date": "Mon, 23 Nov 2020 09:47:27 GMT",
"name": "cdk8s-aws-alb-ingress-controller",
Expand Down
34 changes: 4 additions & 30 deletions src/api/catalog-search/catalog-search.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import catalogFixture from "../../__fixtures__/catalog.json";
import statsFixture from "../../__fixtures__/stats.json";
import { CDKType } from "../../constants/constructs";
import { Language } from "../../constants/languages";
import { CatalogPackage } from "../package/packages";
import { PackageStats } from "../stats";
import { CatalogSearchAPI } from "./catalog-search";
import { CatalogSearchSort } from "./constants";
import * as util from "./util";

describe("CatalogSearchAPI", () => {
const instance = new CatalogSearchAPI(
Expand All @@ -27,13 +25,10 @@ describe("CatalogSearchAPI", () => {
}

it("exposes a property which returns detected cdk frameworks", () => {
const cdkFrameWorkCount = catalogFixture.packages.reduce((sum, i) => {
if (i.metadata.constructFramework?.name) {
return sum + 1;
}

return sum;
}, 0);
const cdkFrameWorkCount = [...instance.search().values()].reduce(
(sum, { constructFrameworks }) => sum + constructFrameworks.size,
0
);

const detectedCount = Object.values(instance.constructFrameworks).reduce(
(sum, i) => sum + i.pkgCount,
Expand Down Expand Up @@ -103,27 +98,6 @@ describe("CatalogSearchAPI", () => {
expect([...dynamodbResults.values()]).toEqual([extendedP1, extendedP2]);
});

it("Ignores cdkMajor filter if no cdkType is passed", () => {
const cdkMajorFilterSpy = jest.spyOn(util.FILTER_FUNCTIONS, "cdkMajor");

instance.search({
filters: {
cdkType: CDKType.awscdk,
cdkMajor: 2,
},
});

expect(cdkMajorFilterSpy).toHaveBeenCalledWith(2);

instance.search({
filters: {
cdkMajor: 3,
},
});

expect(cdkMajorFilterSpy).toHaveBeenCalledWith(undefined);
});

it("Returns results ordered by Sort", () => {
const publishDateAsc = [
...instance.search({ sort: CatalogSearchSort.PublishDateAsc }).values(),
Expand Down
57 changes: 38 additions & 19 deletions src/api/catalog-search/catalog-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { Language } from "../../constants/languages";
import { CatalogPackage } from "../package/packages";
import { PackageStats } from "../stats";
import { CatalogSearchSort } from "./constants";
import { FILTER_FUNCTIONS, renderAllKeywords, SORT_FUNCTIONS } from "./util";
import {
FILTER_FUNCTIONS,
mapConstructFrameworks,
renderAllKeywords,
SORT_FUNCTIONS,
} from "./util";

const INDEX_FIELDS = {
AUTHOR_EMAIL: {
Expand Down Expand Up @@ -40,6 +45,7 @@ const INDEX_FIELDS = {
export interface ExtendedCatalogPackage extends CatalogPackage {
authorEmail?: string;
authorName?: string;
constructFrameworks: Map<CDKType, number | null>;
downloads: number;
id: string;
packageName?: string;
Expand Down Expand Up @@ -139,6 +145,7 @@ export class CatalogSearchAPI {
...pkg,
authorName,
authorEmail,
constructFrameworks: mapConstructFrameworks(pkg.metadata),
keywords,
downloads,
id,
Expand All @@ -152,7 +159,7 @@ export class CatalogSearchAPI {

this.map = this.sort(catalogMap, CatalogSearchSort.PublishDateDesc);

this.constructFrameworks = this.detectConstructFrameworks();
this.constructFrameworks = this.detectAllConstructFrameworks();
this.keywords = this.detectKeywords();

this.index = lunr(function () {
Expand Down Expand Up @@ -232,14 +239,28 @@ export class CatalogSearchAPI {
/**
* Performs a Search against the catalog and returns an array of all packages matching the query.
*/
public findByName(query: string): ExtendedCatalogPackage[] {
public findByName(
query: string,
opts?: { dedup?: boolean }
): ExtendedCatalogPackage[] {
const results = [...this.map.values()].filter((pkg) => pkg.name === query);
const matches = new Array<ExtendedCatalogPackage>();
for (const pkg of results.values()) {
if (pkg.name === query) {
matches.push(pkg);
}
}

if (opts?.dedup) {
const map = new Map<string, ExtendedCatalogPackage>();

matches.forEach((pkg) => {
map.set(pkg.name, pkg);
});

return [...this.dedup(map).values()];
}

return matches;
}

Expand All @@ -255,8 +276,7 @@ export class CatalogSearchAPI {

const filterFunctions = [
FILTER_FUNCTIONS.cdkType(cdkType),
// Ignore major version filter if no CDK Type is defined
FILTER_FUNCTIONS.cdkMajor(cdkType ? cdkMajor : undefined),
FILTER_FUNCTIONS.cdkMajor({ cdkType, cdkMajor }),
FILTER_FUNCTIONS.keywords(keywords),
FILTER_FUNCTIONS.languages(languages),
FILTER_FUNCTIONS.tags(tags),
Expand Down Expand Up @@ -342,26 +362,25 @@ export class CatalogSearchAPI {
* They are indexed by the name of the construct framework and record the count
* of packages for that framework as well as a list of major versions.
*/
private detectConstructFrameworks() {
private detectAllConstructFrameworks() {
const results: CatalogConstructFrameworks = [...this.map.values()].reduce(
(frameworks: CatalogConstructFrameworks, pkg: ExtendedCatalogPackage) => {
const { metadata } = pkg;
const { constructFrameworks } = pkg;

const frameworkName = metadata?.constructFramework?.name;
const majorVersion = metadata?.constructFramework?.majorVersion;
[...constructFrameworks.entries()]?.forEach(([name, majorVersion]) => {
if (majorVersion !== undefined) {
const entry = frameworks[name];

if (frameworkName) {
const entry = frameworks[frameworkName];
if (
majorVersion !== null &&
!entry.majorVersions.includes(majorVersion)
) {
entry.majorVersions.push(majorVersion);
}

if (
majorVersion !== undefined &&
!entry.majorVersions.includes(majorVersion)
) {
entry.majorVersions.push(majorVersion);
entry.pkgCount += 1;
}

entry.pkgCount += 1;
}
});

return frameworks;
},
Expand Down
Loading