Skip to content

Commit

Permalink
Add time window in search requests
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Nov 13, 2023
1 parent b43050d commit 265ac3a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
54 changes: 50 additions & 4 deletions src/mixins/FetchFilesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
*/

import moment from '@nextcloud/moment'

import logger from '../services/logger.js'
import getPhotos from '../services/PhotoSearch.js'
import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js'
Expand All @@ -33,12 +35,19 @@ export default {
],

data() {
const dateTimeUpperBound = moment()
const dateTimeLowerBound = moment(dateTimeUpperBound).subtract(4, 'months')

return {
errorFetchingFiles: null,
loadingFiles: false,
doneFetchingFiles: false,
fetchSemaphore: new SemaphoreWithPriority(1),
fetchedFileIds: [],
dateTimeUpperBound,
dateTimeLowerBound,
timeWindowSteps: 4,
firstResultOffset: 0,
}
},

Expand Down Expand Up @@ -69,17 +78,51 @@ export default {

const numberOfImagesPerBatch = 200

logger.debug(`[FetchFilesMixin] Fetching file between ${this.dateTimeUpperBound?.format('L')} 'and' ${this.dateTimeLowerBound?.format('L')}`)

// Load next batch of images
const fetchedFiles = await getPhotos(path, {
firstResult: this.fetchedFileIds.length,
firstResult: this.firstResultOffset,
nbResults: numberOfImagesPerBatch,
dateTimeUpperBound: this.dateTimeUpperBound?.unix(),
dateTimeLowerBound: this.dateTimeLowerBound?.unix(),
...options,
signal: this.abortController.signal,
})

// If we get less files than requested that means we got to the end
if (fetchedFiles.length !== numberOfImagesPerBatch) {
this.doneFetchingFiles = true
if (fetchedFiles.length === numberOfImagesPerBatch) {
// If we have the same number of files that as requested
// then the time window probably contains more, so we simply bump the first result offset.
this.firstResultOffset += fetchedFiles.length
} else if (fetchedFiles.length === 0 && this.firstResultOffset === 0) {
// If we tried a new window and it is empty
if (this.dateTimeUpperBound === undefined) {
// if upper bound has been cleared, then we are done fetching files.
this.doneFetchingFiles = true
} else if (this.dateTimeLowerBound === undefined) {
// else if lower bound has been cleared, then we clear upper bound
// this will allow the server to return all files with either empty or above than now original date time
this.dateTimeUpperBound = undefined
} else if (this.timeWindowSteps === 64) {
// else if we reach 64 months, we clear the lower bound.
this.dateTimeUpperBound = this.dateTimeLowerBound
this.dateTimeLowerBound = undefined
} else {
// else we progressively increase the time window until we reach 64 months (3 requests)
this.timeWindowSteps *= 4
this.dateTimeUpperBound = this.dateTimeLowerBound
this.dateTimeLowerBound = moment(this.dateTimeLowerBound).subtract(this.timeWindowSteps, 'months')
}
} else if (fetchedFiles.length !== numberOfImagesPerBatch) {
// If we get less files than requested,
// we are at the end for the current time window, so we move to the next one.
this.timeWindowSteps = 4
this.dateTimeUpperBound = this.dateTimeLowerBound
if (this.dateTimeUpperBound !== undefined) {
this.dateTimeLowerBound = moment(this.dateTimeUpperBound).subtract(this.timeWindowSteps, 'months')
} else {
this.doneFetchingFiles = true
}
}

const fileIds = fetchedFiles
Expand Down Expand Up @@ -121,6 +164,9 @@ export default {
this.doneFetchingFiles = false
this.errorFetchingFiles = null
this.loadingFiles = false
this.timeWindowSteps = 4
this.dateTimeUpperBound = moment()
this.dateTimeLowerBound = moment(this.dateTimeUpperBound).subtract(this.timeWindowSteps, 'months')
this.fetchedFileIds = []
},
},
Expand Down
27 changes: 25 additions & 2 deletions src/services/PhotoSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ import moment from '@nextcloud/moment'
* @param {boolean} [options.full=false] get full data of the files
* @param {boolean} [options.onThisDay=false] get only items from this day of year
* @param {boolean} [options.onlyFavorites=false] get only favorite items
* @param {number} [options.dateTimeUpperBound] limit the search to photos taken before this lower bound
* @param {number} [options.dateTimeLowerBound] limit the search to photos taken after this lower bound
* @return {Promise<object[]>} the file list
*/
export default async function(path = '', options = {}) {
export default async function (path = '', options = {}) {

Check failure on line 45 in src/services/PhotoSearch.js

View workflow job for this annotation

GitHub Actions / eslint

Unexpected space before function parentheses
// default function options
options = {
firstResult: 0,
Expand All @@ -51,7 +53,7 @@ export default async function(path = '', options = {}) {
...options,
}

const prefixPath = `/files/${getCurrentUser().uid}`
const prefixPath = `/files/${getCurrentUser()?.uid}`

// generating the search or condition
// based on the allowed mimetypes
Expand Down Expand Up @@ -95,6 +97,26 @@ export default async function(path = '', options = {}) {
}).join('\n')}</d:or>`
: ''

let timeWindow = ''
if (options.dateTimeUpperBound !== undefined) {
timeWindow = `
<d:lt>
<d:prop>
<nc:metadata-photos-original_date_time/>
</d:prop>
<d:literal>${options.dateTimeUpperBound}</d:literal>
</d:lt>`
}
if (options.dateTimeLowerBound !== undefined) {
timeWindow += `
<d:gt>
<d:prop>
<nc:metadata-photos-original_date_time/>
</d:prop>
<d:literal>${options.dateTimeLowerBound}</d:literal>
</d:gt>`
}

options = Object.assign({
method: 'SEARCH',
headers: {
Expand Down Expand Up @@ -125,6 +147,7 @@ export default async function(path = '', options = {}) {
</d:or>
${eqFavorites}
${onThisDay}
${timeWindow}
</d:and>
</d:where>
<d:orderby>
Expand Down

0 comments on commit 265ac3a

Please sign in to comment.