Skip to content

Commit

Permalink
fix content file search (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova authored Jun 26, 2024
1 parent 8434ac2 commit ab4389f
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 117 deletions.
74 changes: 37 additions & 37 deletions frontends/api/src/generated/v1/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2249,72 +2249,72 @@ export interface LearningResourceSchool {
departments: Array<LearningResourceBaseDepartment>
}
/**
* SearchResponseSerializer with OpenAPI annotations for Learning Resources search
* Serializer for LearningResourceTopic model
* @export
* @interface LearningResourceSearchResponse
* @interface LearningResourceTopic
*/
export interface LearningResourceSearchResponse {
export interface LearningResourceTopic {
/**
*
* @type {number}
* @memberof LearningResourceSearchResponse
*/
count: number
/**
*
* @type {string}
* @memberof LearningResourceSearchResponse
* @memberof LearningResourceTopic
*/
next: string | null
id: number
/**
*
* @type {string}
* @memberof LearningResourceSearchResponse
* @memberof LearningResourceTopic
*/
previous: string | null
name: string
/**
*
* @type {Array<LearningResource>}
* @memberof LearningResourceSearchResponse
* @type {number}
* @memberof LearningResourceTopic
*/
results: Array<LearningResource>
parent?: number | null
/**
*
* @type {ContentFileSearchResponseMetadata}
* @memberof LearningResourceSearchResponse
* Get the channel url for the topic if it exists
* @type {string}
* @memberof LearningResourceTopic
*/
metadata: ContentFileSearchResponseMetadata
channel_url: string | null
}
/**
* Serializer for LearningResourceTopic model
* SearchResponseSerializer with OpenAPI annotations for Learning Resources search
* @export
* @interface LearningResourceTopic
* @interface LearningResourcesSearchResponse
*/
export interface LearningResourceTopic {
export interface LearningResourcesSearchResponse {
/**
*
* @type {number}
* @memberof LearningResourceTopic
* @memberof LearningResourcesSearchResponse
*/
id: number
count: number
/**
*
* @type {string}
* @memberof LearningResourceTopic
* @memberof LearningResourcesSearchResponse
*/
name: string
next: string | null
/**
*
* @type {number}
* @memberof LearningResourceTopic
* @type {string}
* @memberof LearningResourcesSearchResponse
*/
parent?: number | null
previous: string | null
/**
* Get the channel url for the topic if it exists
* @type {string}
* @memberof LearningResourceTopic
*
* @type {Array<LearningResource>}
* @memberof LearningResourcesSearchResponse
*/
channel_url: string | null
results: Array<LearningResource>
/**
*
* @type {ContentFileSearchResponseMetadata}
* @memberof LearningResourcesSearchResponse
*/
metadata: ContentFileSearchResponseMetadata
}
/**
* * `undergraduate` - Undergraduate * `graduate` - Graduate * `high_school` - High School * `noncredit` - Non-Credit * `advanced` - Advanced * `intermediate` - Intermediate * `introductory` - Introductory
Expand Down Expand Up @@ -3248,7 +3248,7 @@ export interface PatchedUserListRequest {
}

/**
*
* Serializer for PercolateQuery objects
* @export
* @interface PercolateQuery
*/
Expand Down Expand Up @@ -11567,7 +11567,7 @@ export const LearningResourcesSearchApiFp = function (
(
axios?: AxiosInstance,
basePath?: string,
) => AxiosPromise<LearningResourceSearchResponse>
) => AxiosPromise<LearningResourcesSearchResponse>
> {
const localVarAxiosArgs =
await localVarAxiosParamCreator.learningResourcesSearchRetrieve(
Expand Down Expand Up @@ -11629,7 +11629,7 @@ export const LearningResourcesSearchApiFactory = function (
learningResourcesSearchRetrieve(
requestParameters: LearningResourcesSearchApiLearningResourcesSearchRetrieveRequest = {},
options?: RawAxiosRequestConfig,
): AxiosPromise<LearningResourceSearchResponse> {
): AxiosPromise<LearningResourcesSearchResponse> {
return localVarFp
.learningResourcesSearchRetrieve(
requestParameters.aggregations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TabPanel,
styled,
} from "ol-components"
import { ResourceTypeEnum, LearningResourceSearchResponse } from "api"
import { ResourceTypeEnum, LearningResourcesSearchResponse } from "api"

const TabsList = styled(TabButtonList)({
".MuiTabScrollButton-root.Mui-disabled": {
Expand All @@ -25,7 +25,7 @@ type TabConfig = {
resource_type: ResourceTypeEnum[]
}

type Aggregations = LearningResourceSearchResponse["metadata"]["aggregations"]
type Aggregations = LearningResourcesSearchResponse["metadata"]["aggregations"]
const resourceTypeCounts = (aggregations?: Aggregations) => {
if (!aggregations) return null
const buckets = aggregations?.resource_type ?? []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from "react"
import { renderWithProviders, screen, waitFor, within } from "@/test-utils"
import type { LearningResourceSearchResponse } from "api"
import type { LearningResourcesSearchResponse } from "api"
import DepartmentListingPage from "./DepartmentListingPage"
import { factories, setMockResponse, urls } from "api/test-utils"
import invariant from "tiny-invariant"
import { faker } from "@faker-js/faker/locale/en"

const makeSearchResponse = (
aggregations: Record<string, number>,
): LearningResourceSearchResponse => {
): LearningResourcesSearchResponse => {
return {
metadata: {
suggestions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { MetaTags, pluralize } from "ol-utilities"
import type {
LearningResourceSchool,
LearningResourceSearchResponse,
LearningResourcesSearchResponse,
} from "api"
import {
useLearningResourcesSearch,
Expand Down Expand Up @@ -186,7 +186,7 @@ const SchoolList = styled(PlainList)(({ theme }) => ({
}))

const aggregateByDepartment = (
data: LearningResourceSearchResponse,
data: LearningResourcesSearchResponse,
): Record<string, number> => {
const buckets = data.metadata.aggregations["department"] ?? []
return Object.fromEntries(
Expand Down
4 changes: 2 additions & 2 deletions frontends/mit-open/src/pages/FieldPage/FieldPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertInstanceOf } from "ol-utilities"
import { urls, factories, makeRequest } from "api/test-utils"
import type { FieldChannel } from "api/v0"
import type { LearningResourceSearchResponse } from "api"
import type { LearningResourcesSearchResponse } from "api"
import {
renderTestApp,
screen,
Expand All @@ -22,7 +22,7 @@ const mockedFieldSearch = jest.mocked(FieldSearch)

const setupApis = (
fieldPatch?: Partial<FieldChannel>,
search?: Partial<LearningResourceSearchResponse>,
search?: Partial<LearningResourcesSearchResponse>,
{ isSubscribed = false, isAuthenticated = false } = {},
) => {
const field = factories.fields.field(fieldPatch)
Expand Down
4 changes: 2 additions & 2 deletions frontends/mit-open/src/pages/FieldPage/FieldSearch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { screen, within, waitFor, renderTestApp } from "@/test-utils"
import { setMockResponse, urls, factories, makeRequest } from "api/test-utils"
import type { LearningResourceSearchResponse } from "api"
import type { LearningResourcesSearchResponse } from "api"
import invariant from "tiny-invariant"
import { makeWidgetListResponse } from "ol-widgets/src/factories"
import type { FieldChannel } from "api/v0"
Expand All @@ -10,7 +10,7 @@ const setMockApiResponses = ({
search,
fieldPatch,
}: {
search?: Partial<LearningResourceSearchResponse>
search?: Partial<LearningResourcesSearchResponse>
fieldPatch?: Partial<FieldChannel>
}) => {
const field = factories.fields.field(fieldPatch)
Expand Down
4 changes: 2 additions & 2 deletions frontends/mit-open/src/pages/SearchPage/SearchPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import SearchPage from "./SearchPage"
import { setMockResponse, urls, factories, makeRequest } from "api/test-utils"
import type {
LearningResourceSearchResponse,
LearningResourcesSearchResponse,
PaginatedLearningResourceOfferorDetailList,
} from "api"
import invariant from "tiny-invariant"
Expand All @@ -19,7 +19,7 @@ const setMockApiResponses = ({
search,
offerors,
}: {
search?: Partial<LearningResourceSearchResponse>
search?: Partial<LearningResourcesSearchResponse>
offerors?: PaginatedLearningResourceOfferorDetailList
}) => {
setMockResponse.get(urls.userMe.get(), {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react"
import { renderWithProviders, screen, waitFor, within } from "@/test-utils"
import type { LearningResourceSearchResponse } from "api"
import type { LearningResourcesSearchResponse } from "api"
import TopicsListingPage from "./TopicsListingPage"
import { factories, setMockResponse, urls } from "api/test-utils"
import invariant from "tiny-invariant"

const makeSearchResponse = (
aggregations: Record<string, number>,
): LearningResourceSearchResponse => {
): LearningResourcesSearchResponse => {
return {
metadata: {
suggestions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
useLearningResourceTopics,
useLearningResourcesSearch,
} from "api/hooks/learningResources"
import { LearningResourceSearchResponse, LearningResourceTopic } from "api"
import { LearningResourcesSearchResponse, LearningResourceTopic } from "api"
import RootTopicIcon from "@/components/RootTopicIcon/RootTopicIcon"
import { HOME } from "@/common/urls"

Expand Down Expand Up @@ -181,7 +181,7 @@ const TopicBoxLoading = () => {
const Page = styled.div({})

const aggregateByTopic = (
data: LearningResourceSearchResponse,
data: LearningResourcesSearchResponse,
): Record<string, number> => {
const buckets = data.metadata.aggregations["topic"] ?? []
return Object.fromEntries(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from "react"
import { renderWithProviders, screen, waitFor, within } from "@/test-utils"
import type { LearningResourceSearchResponse } from "api"
import type { LearningResourcesSearchResponse } from "api"
import UnitsListingPage from "./UnitsListingPage"
import { factories, setMockResponse, urls } from "api/test-utils"

const makeSearchResponse = (
aggregations: Record<string, number>,
): LearningResourceSearchResponse => {
): LearningResourcesSearchResponse => {
return {
metadata: {
suggestions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RiBookOpenLine, RiSuitcaseLine } from "@remixicon/react"
import React from "react"
import {
LearningResourceOfferorDetail,
LearningResourceSearchResponse,
LearningResourcesSearchResponse,
OfferedByEnum,
} from "api"
import { MetaTags } from "ol-utilities"
Expand All @@ -26,7 +26,7 @@ const UNITS_BANNER_IMAGE = "/static/images/background_steps.jpeg"
const DESKTOP_WIDTH = "1056px"

const aggregateByUnits = (
data: LearningResourceSearchResponse,
data: LearningResourcesSearchResponse,
): Record<string, number> => {
const buckets = data.metadata.aggregations["offered_by"] ?? []
return Object.fromEntries(
Expand Down
52 changes: 27 additions & 25 deletions learning_resources_search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,29 @@ def get_previous(self, instance) -> str | None:
def get_count(self, instance) -> int:
return instance.get("hits", {}).get("total", {}).get("value")

def get_metadata(self, instance) -> SearchResponseMetadata:
return {
"aggregations": _transform_aggregations(instance.get("aggregations", {})),
"suggest": _transform_search_results_suggest(instance),
}


class PercolateQuerySerializer(serializers.ModelSerializer):
"""
Serializer for PercolateQuery objects
"""

class Meta:
model = PercolateQuery
exclude = (*COMMON_IGNORED_FIELDS, "users")


class LearningResourcesSearchResponseSerializer(SearchResponseSerializer):
"""
SearchResponseSerializer with OpenAPI annotations for Learning Resources
search
"""

def update_path_parents(self, hits):
"""Fill in learning_path_parents for path editors"""

Expand Down Expand Up @@ -505,6 +528,7 @@ def update_list_parents(self, hits, user):
).data
)

@extend_schema_field(LearningResourceSerializer(many=True))
def get_results(self, instance):
hits = instance.get("hits", {}).get("hits", [])
request = self.context.get("request")
Expand All @@ -519,38 +543,16 @@ def get_results(self, instance):
self.update_path_parents(hits)
return (hit.get("_source") for hit in hits)

def get_metadata(self, instance) -> SearchResponseMetadata:
return {
"aggregations": _transform_aggregations(instance.get("aggregations", {})),
"suggest": _transform_search_results_suggest(instance),
}


class PercolateQuerySerializer(serializers.ModelSerializer):
class Meta:
model = PercolateQuery
exclude = (*COMMON_IGNORED_FIELDS, "users")


class LearningResourceSearchResponseSerializer(SearchResponseSerializer):
"""
SearchResponseSerializer with OpenAPI annotations for Learning Resources
search
"""

@extend_schema_field(LearningResourceSerializer(many=True))
def get_results():
return super().get_results()


class ContentFileSearchResponseSerializer(SearchResponseSerializer):
"""
SearchResponseSerializer with OpenAPI annotations for Content Files search
"""

@extend_schema_field(ContentFileSerializer(many=True))
def get_results():
return super().get_results()
def get_results(self, instance):
hits = instance.get("hits", {}).get("hits", [])
return (hit.get("_source") for hit in hits)


class PercolateQuerySubscriptionRequestSerializer(
Expand Down
Loading

0 comments on commit ab4389f

Please sign in to comment.