async loadMachineData()

in source/web-ui/src/views/machines/MachineDetail.tsx [61:121]


    async loadMachineData(lookbackTimestamp: moment.Moment, incrementalRefresh: boolean) {
        this.setState({ isLoading: true });

        const currentChartDataChunks = this.state.chartDataChunks;
        const now = moment.utc().unix();

        const getRealTimeMachineDataParams: IGetRealTimeMachineDataReqParams = {
            id: this.state.machineId,
            startTimestamp: lookbackTimestamp.unix(),
            endTimestamp: now,
            incrementalRefresh
        };

        const resp = (await API.graphql({
            query: getRealTimeMachineData,
            variables: getRealTimeMachineDataParams
        }) as IGetRealTimeMachineDataResponse);

        for (const dataChunk of resp.data.getRealTimeMachineData.dataChunks) {
            // Check if the dataChunk is in the existing data set. If so, replace the existing with 
            // the new one. If not, add the new one
            const idx = currentChartDataChunks.findIndex(item => item.dataAsOfUTCUnixTimestamp === dataChunk.dataAsOfUTCUnixTimestamp);

            if (idx > -1) {
                currentChartDataChunks.splice(idx, 1);
            }

            currentChartDataChunks.push(dataChunk);
        }

        currentChartDataChunks.sort(this.sortByChunkTimestamp);

        this.setState({
            chartDataChunks: currentChartDataChunks
                .filter(item => item.dataAsOfUTCUnixTimestamp > moment.utc().subtract(this.DEFAULT_LOOKBACK_IN_HOURS, 'hours').unix())
        });
        this.buildChart();

        let totalProductionCount = 0;
        let currentMaxProductionCount = 0;
        this.state.chartDataChunks.forEach(chunk => {
            if (chunk.productionCountValue) {
                try {
                    const value = parseInt(chunk.productionCountValue, 10);
                    if (value > currentMaxProductionCount) {
                        currentMaxProductionCount = value;
                    } else if (currentMaxProductionCount > value) {
                        totalProductionCount += currentMaxProductionCount;
                        currentMaxProductionCount = value;
                    }
                } catch (err) {
                    console.log('Unable to calculate total production count');
                    totalProductionCount = 0;
                    currentMaxProductionCount = 0;
                }
            }
        });

        totalProductionCount += currentMaxProductionCount;
        this.setState({ isLoading: false, totalProductionCount });
    }