Skip to content

Commit

Permalink
Change status to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Oct 5, 2023
1 parent 7e65658 commit df4d014
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export type ImageThumbnailResultIndexSchema = typeof ImageThumbnailResultIndexSc

export interface ImageThumbnailResultInfo {
sources: string[]
// TODO: Something richer than HTTP status code that allows for info about failure modes
status: number
status: boolean
timestamp: number
url: string
}
Expand Down
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 } from '@xyo-network/diviner-payload-model'
import { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema, SortDirection } from '@xyo-network/diviner-payload-model'
import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
import {
ImageThumbnail,
Expand Down Expand Up @@ -62,6 +62,10 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
return this.config.pollFrequency ?? 10_000
}

/**
* Works in the background to populate index for the Diviner
* @returns
*/
protected backgroundDivine = async (): Promise<void> => {
// Load last state
const lastState = (await this.retrieveState()) ?? { offset: 0 }
Expand All @@ -76,6 +80,7 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
})
const batch = await boundWitnessDiviner.divine([query])
if (batch.length === 0) return
// Find all the indexable hashes in this batch
type IndexableHashes = Readonly<[boundWitnessHash: string, imageThumbnailHash: string, timestampHash: string]>
const indexableHashes: IndexableHashes[] = (
await Promise.all(
Expand All @@ -95,6 +100,7 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
.flat()
.filter(exists)
const archivist = await this.getArchivistForStore('thumbnailStore')
// Find all the indexable data associated with the indexable hashes
type IndexableData = Readonly<
[
boundWitnessHash: string,
Expand All @@ -118,12 +124,12 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
}),
)
).filter(exists)
// Build index results
// Build index results from the indexable data
const indexes: ImageThumbnailResult[] = indexableData.map(
([boundWitnessHash, thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {
const { sourceUrl: url } = thumbnailPayload
const { timestamp } = timestampPayload
const status = thumbnailPayload.http?.status ?? -1
const status = thumbnailPayload.http?.status ? true : false
const sources = [boundWitnessHash, thumbnailHash, timestampHash]
const result = new PayloadBuilder<ImageThumbnailResult>({ schema: ImageThumbnailResultIndexSchema })
.fields({ sources, status, timestamp, url })
Expand Down Expand Up @@ -162,11 +168,20 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
const results = (
await Promise.all(
urls.map(async (payload) => {
const { url, status: payloadStatus } = payload as UrlPayload & { status: number }
const status = payloadStatus ?? 200
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 ?? 1
const order = payloadOrder ?? 'desc'
const offset = payloadOffset ?? 0
const status = payloadStatus ?? true
const query = new PayloadBuilder<ImageThumbnailResultQuery>({ schema: PayloadDivinerQuerySchema })
// TODO: Expose status, limit (and possibly offset) to caller. Currently only exposing URL
.fields({ limit: 1, offset: 0, order: 'desc', status, url })
.fields({ limit, offset, order, status, url })
.build()
return await diviner.divine([query])
}),
Expand Down

0 comments on commit df4d014

Please sign in to comment.