Skip to content

Commit

Permalink
Merge pull request #1157 from XYOracleNetwork/feature/image-thumbnail…
Browse files Browse the repository at this point in the history
…-diviner-query-types

Image thumbnail diviner query types
  • Loading branch information
JoelBCarter authored Oct 5, 2023
2 parents bffdf5f + 2fcef59 commit 96d58d7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DivinerConfig } from '@xyo-network/diviner-model'

import { ImageThumbnailSchema } from '../Schema'
import { ImageThumbnailDivinerSchema } from './Schema'

export const ImageThumbnailDivinerConfigSchema = `${ImageThumbnailSchema}.diviner.config` as const
export const ImageThumbnailDivinerConfigSchema = `${ImageThumbnailDivinerSchema}.config` as const
export type ImageThumbnailDivinerConfigSchema = typeof ImageThumbnailDivinerConfigSchema

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PayloadDivinerPredicate } from '@xyo-network/diviner-payload-model'
import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'

import { ImageThumbnailDivinerSchema } from './Schema'

export type ImageThumbnailDivinerQuerySchema = `${ImageThumbnailDivinerSchema}.query`
export const ImageThumbnailDivinerQuerySchema: ImageThumbnailDivinerQuerySchema = `${ImageThumbnailDivinerSchema}.query`

export type ImageThumbnailDivinerQuery = Pick<PayloadDivinerPredicate, 'limit' | 'offset' | 'order'> &
Payload<{ status?: boolean; url: string }, ImageThumbnailDivinerQuerySchema>

export const isImageThumbnailDivinerQuery = isPayloadOfSchemaType<ImageThumbnailDivinerQuery>(ImageThumbnailDivinerQuerySchema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ImageThumbnailSchema } from '../Schema'

export const ImageThumbnailDivinerSchema = `${ImageThumbnailSchema}.diviner` as const
export type ImageThumbnailDivinerSchema = typeof ImageThumbnailDivinerSchema
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './Config'
export * from './Params'
export * from './Query'
export * from './Schema'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isBoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
import { asDivinerInstance, DivinerConfigSchema } from '@xyo-network/diviner-model'
import { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema, SortDirection } from '@xyo-network/diviner-payload-model'
import { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
import {
ImageThumbnail,
Expand All @@ -18,12 +18,12 @@ import {
ImageThumbnailResultIndexSchema,
ImageThumbnailSchema,
isImageThumbnail,
isImageThumbnailDivinerQuery,
isImageThumbnailResult,
} from '@xyo-network/image-thumbnail-payload-plugin'
import { isModuleState, ModuleState, ModuleStateSchema, StateDictionary } from '@xyo-network/module-model'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { Payload } from '@xyo-network/payload-model'
import { isUrlPayload, UrlPayload } from '@xyo-network/url-payload-plugin'
import { isTimestamp, TimeStamp, TimestampSchema } from '@xyo-network/witness-timestamp'

export type ImageThumbnailDivinerState = StateDictionary & {
Expand Down Expand Up @@ -163,19 +163,12 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
}

protected override async divineHandler(payloads: Payload[] = []): Promise<ImageThumbnailResult[]> {
const urls = payloads.filter(isUrlPayload)
const urls = payloads.filter(isImageThumbnailDivinerQuery)
const diviner = await this.getPayloadDivinerForStore('indexStore')
const results = (
await Promise.all(
urls.map(async (payload) => {
const {
limit: payloadLimit,
offset: payloadOffset,
order: payloadOrder,
status: payloadStatus,
url,
} = payload as UrlPayload & { limit: number; offset: number; order: SortDirection; status: boolean }
// TODO: Expose status, limit (and possibly offset) to caller. Currently only exposing URL
const { limit: payloadLimit, offset: payloadOffset, order: payloadOrder, status: payloadStatus, url } = payload
const limit = payloadLimit ?? 1
const order = payloadOrder ?? 'desc'
const offset = payloadOffset ?? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { PayloadHasher } from '@xyo-network/core'
import { MemoryBoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-memory'
import { MemoryPayloadDiviner } from '@xyo-network/diviner-payload-memory'
import { SearchableStorage } from '@xyo-network/image-thumbnail-payload-plugin'
import { ImageThumbnailDivinerQuery, ImageThumbnailDivinerQuerySchema, SearchableStorage } from '@xyo-network/image-thumbnail-payload-plugin'
import { MemoryArchivist } from '@xyo-network/memory-archivist'
import { MemoryNode } from '@xyo-network/node-memory'
import { UrlPayload, UrlSchema } from '@xyo-network/url-payload-plugin'
import { TimeStamp, TimestampSchema } from '@xyo-network/witness-timestamp'

import { ImageThumbnailDiviner } from '../Diviner'
Expand Down Expand Up @@ -153,14 +152,14 @@ describe('ImageThumbnailDiviner', () => {
})
describe('with no thumbnail for the provided URL', () => {
it('returns nothing', async () => {
const payload: UrlPayload = { schema: UrlSchema, url: 'https://does.not.exist.io' }
const payload: ImageThumbnailDivinerQuery = { schema: ImageThumbnailDivinerQuerySchema, url: 'https://does.not.exist.io' }
const result = await sut.divine([payload])
expect(result).toBeArrayOfSize(0)
})
})
describe('with successful thumbnail for the provided URL', () => {
it('returns the most recent success', async () => {
const payload: UrlPayload = { schema: UrlSchema, url: successfulThumbnail.sourceUrl }
const payload: ImageThumbnailDivinerQuery = { schema: ImageThumbnailDivinerQuerySchema, url: successfulThumbnail.sourceUrl }
const result = await sut.divine([payload])
expect(result).toBeArrayOfSize(1)
const expected = await PayloadHasher.hashAsync(successfulThumbnail)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Payload } from './Payload'

export const isPayloadOfSchemaType = <T extends Payload>(schema: string) => {
return (x: Payload): x is T => x.schema === schema
return (x?: Payload | null): x is T => x?.schema === schema
}

0 comments on commit 96d58d7

Please sign in to comment.