Mejora en velocidad de busqueda

This commit is contained in:
Juan Pablo Vial
2024-02-28 21:44:37 -03:00
parent d9b5bc6507
commit 48bfe5d8ab
7 changed files with 269 additions and 38 deletions

View File

@ -96,6 +96,10 @@
id: '',
data: [],
table: null,
queues: {
unidades: [],
ventas: []
},
get: function() {
return {
results: () => {
@ -122,34 +126,33 @@
}
const progress = this.draw().progress(data.results.length)
const promises = []
data.results.forEach(row => {
if (row.tipo === 'venta') {
return promises.push(this.get().venta(row.id).then(json => {
if (json.venta === null) {
console.debug(json)
return
}
const venta = json.venta
this.queues.ventas = data.results.filter(row => row.tipo === 'venta').map(row => row.id)
this.queues.unidades = data.results.filter(row => row.tipo !== 'venta').map(row => row.id)
promises.push(this.get().ventas().then(arrays => {
arrays.forEach(json => {
if (json.ventas.length === 0) {
console.debug(json)
return
}
json.ventas.forEach(venta => {
progress.progress('increment')
const r = new Row({unidad: venta.propiedad.unidades[0], proyecto: venta.proyecto})
r.venta = venta
this.data.push(r)
}).catch(error => {
})
})
}))
promises.push(this.get().unidades().then(arrays => {
arrays.forEach(json => {
if (json.unidades.length === 0) {
return
}
json.unidades.forEach(unidad => {
progress.progress('increment')
console.error(row)
console.error(error)
}))
}
promises.push(this.get().unidad(row.id).then(json => {
const unidad = json.unidad
progress.progress('increment')
this.data.push(new Row({unidad: unidad, proyecto: unidad.proyecto_tipo_unidad.proyecto}))
}).catch(error => {
progress.progress('increment')
console.error(row)
console.error(error)
}))
})
this.data.push(new Row({unidad: unidad, proyecto: unidad.proyecto_tipo_unidad.proyecto}))
})
})
}))
Promise.all(promises).then(() => {
this.sort()
this.draw().clear()
@ -157,22 +160,43 @@
})
})
},
unidad: id => {
const url = '{{$urls->api}}/ventas/unidad/' + id
return fetchAPI(url).then(response => {
if (response.ok) {
unidades: () => {
const url = '{{$urls->api}}/search/ventas/unidades'
const chunks = []
for (let i = 0; i < this.queues.unidades.length; i += 100) {
chunks.push(this.queues.unidades.slice(i, i + 100))
}
const promises = []
chunks.forEach(ids => {
const body = new FormData()
body.set('unidades', ids)
promises.push(fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json()
}
}))
})
return Promise.all(promises)
},
venta: id => {
const url = '{{$urls->api}}/venta/' + id
return fetchAPI(url).then(response => {
if (!response) {
return
}
return response.json()
ventas: () => {
const url = '{{$urls->api}}/search/ventas'
const chunks = []
for (let i = 0; i < this.queues.ventas.length; i += 100) {
chunks.push(this.queues.ventas.slice(i, i + 100))
}
const promises = []
chunks.forEach(ids => {
const body = new FormData()
body.set('ventas', ids)
promises.push(fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json()
}))
})
return Promise.all(promises)
}
}
},