Commit c1412a3a authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

fix(Quiz): Jean-Jacques Ruisseau's quiz is no longer stuck in loading

parent 73bd87fa
......@@ -154,7 +154,7 @@
}
],
"customQuestion": {
"questionLabel": "Quel mois avez-vous le plus consommé l'année dernière parmi les mois suivants (tous fluides confondus) :",
"questionLabel": "Quel mois avez-vous le plus consommé parmi les mois suivants (tous fluides confondus) :",
"type": 0,
"timeStep": 40,
"interval": 50,
......
......@@ -59,10 +59,8 @@ export default class ConsumptionDataManager {
compareTimePeriod
)
if (!InputisValid) return null
if (fluidTypes.length === 1 && !isHome) {
//TODO validating input data
//TODO applying buisness logic to the query arguments
// running the query
......
......@@ -13,9 +13,9 @@ export default class ProfileTypeEntityService {
}
/**
* Retrieve ProfileType from the PROFILETYPE_DOCTYPE
* When called with date parameter, fetch closest profileType to the date
* When called without parameters, fetch last profileType in doctype
* Retrieves ProfileType from the PROFILETYPE_DOCTYPE
* When called with date parameter, fetches closest profileType to the date
* When called without parameters, fetches last profileType in doctype
* @param {DateTime} date
* @returns {ProfileType}
*/
......@@ -75,10 +75,10 @@ export default class ProfileTypeEntityService {
}
/**
* Retrieve all ProfileTypes from the PROFILETYPE_DOCTYPE
* When called with period parameter, return all profileTypes found for this period or null
* When called without parameters, return all profileTypes or null if none exist
* @param {DateTime} date
* Retrieves all ProfileTypes from the PROFILETYPE_DOCTYPE
* When called with period parameter, returns all profileTypes found for this period or null
* When called without parameters, returns all profileTypes or null if none exist
* @param {TimePeriod} timePeriod
* @returns {ProfileType}
*/
public async getAllProfileTypes(
......@@ -89,7 +89,9 @@ export default class ProfileTypeEntityService {
const data: QueryResult<ProfileType[]> = await this._client.query(
query
.where({
updateDate: { $gte: timePeriod.startDate.toString() },
updateDate: {
$gte: timePeriod.startDate.toString(),
},
})
.sortBy([{ updateDate: 'asc' }])
.limitBy(100)
......@@ -122,6 +124,10 @@ export default class ProfileTypeEntityService {
return null
}
/**
* Saves profileType in database
* @returns {ProfileType}
*/
public async updateProfileType(
attributes: Partial<ProfileType>
): Promise<ProfileType | null> {
......@@ -142,7 +148,7 @@ export default class ProfileTypeEntityService {
}
/**
* Delete Array of ProfileTypes
* Deletes Array of ProfileTypes
* @returns {boolean}
*/
public async deleteProfileTypes(
......@@ -159,7 +165,7 @@ export default class ProfileTypeEntityService {
}
/**
* Retrieve Profile from the ProfileEntity
* Retrieves ProfileType from the ProfileTypeEntity
* @param {ProfileType} profileTypeEntity
* @returns {ProfileType}
*/
......
......@@ -23,6 +23,8 @@ import {
} from 'enum/userQuiz.enum'
import { shuffle } from 'lodash'
import { formatNumberValues } from 'utils/utils'
import log from 'utils/logger'
export default class QuizService {
private readonly _client: Client
......@@ -403,7 +405,9 @@ export default class QuizService {
}
/**
* Calcul the max load and return the value and the corresponding date
* Finds max load on given past time period
* when no max load is found looks x month/year back for a max value
* if nothing is found fall back on most recent data
* @param timeStep
* @param interval
* @param fluidType
......@@ -419,7 +423,7 @@ export default class QuizService {
})
let max = 0
const consumptionService = new ConsumptionDataManager(this._client)
const limit = { date: interval.startDate, reached: false }
let graphData = await consumptionService.getGraphData(
interval,
timeStep,
......@@ -429,8 +433,7 @@ export default class QuizService {
max = Math.max(...graphData.actualData.map(d => d.value))
if (max == -1) {
const newInterval: TimePeriod = interval
const newInterval: TimePeriod = { ...interval }
let objectTimeStep: object
switch (timeStep) {
case TimeStep.MONTH:
......@@ -442,23 +445,51 @@ export default class QuizService {
default:
objectTimeStep = { week: 1 }
}
do {
newInterval.startDate = newInterval.startDate.minus(objectTimeStep)
newInterval.endDate = newInterval.endDate.minus(objectTimeStep)
limit.date = newInterval.startDate
graphData = await consumptionService.getGraphData(
interval,
newInterval,
timeStep,
fluidType
)
if (limit.date < DateTime.now().minus({ year: 5 })) {
limit.reached = true
}
if (graphData && graphData.actualData) {
max = Math.max(...graphData.actualData.map(d => d.value))
}
} while (max == -1 && graphData && graphData.actualData)
} while (
max == -1 &&
graphData &&
graphData.actualData &&
!limit.reached
)
}
if (limit.reached) {
const newInterval: TimePeriod = {
startDate: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ year: 1 }),
endDate: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('month'),
}
graphData = await consumptionService.getGraphData(
newInterval,
timeStep,
fluidType
)
}
if (graphData && graphData.actualData) {
max = Math.max(...graphData.actualData.map(d => d.value))
graphData.actualData.forEach(d => {
if (d.value === max) dateMax = d.date
})
......@@ -484,6 +515,7 @@ export default class QuizService {
singleFluid: boolean
): Promise<number> {
const consumptionService = new ConsumptionDataManager(this._client)
log.info('GetAverageOnGivenPeriod is about to call getgraphdata')
const graphData = await consumptionService.getGraphData(
interval,
timeStep,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment