Source: components/restmodules/StatisticsRestModule.js

import buildUrl from 'build-url';
import { API } from '../../LocalConfiguration';
import { handleErrors, errors} from "./RestModuleAPI";
import isUUID from "validator/lib/isUUID";

const statisticsPath = API + '/stats/';
/**
 * Statistics Rest API
 * @module StatisticsRestModule
 */

/**
 *
 * @param since
 * @param until
 * @param id
 * @returns {Promise<{ok} | never>}
 */
export function fetchTransits({ since, until, id } = {}) {
    const res = "transits/vehiclePassed";
    if (!isUUID(id)) {
        throw new Error(errors.invalidUUI);
    }

    return fetch(buildUrl(statisticsPath + res, {
        queryParams: {
            area_id: id,
            since: since,
            until: until
        }
    })).then(response => handleErrors(response))
        .then(response => response.json());
}

/**
 *
 * @param since
 * @param until
 * @param id
 * @returns {Promise<{ok} | never>}
 */
export function fetchAreaStatistics({ since, until, id } = {}) {
	const res = "transits/vehiclePassed/agregated";
	if (!isUUID(id)) {
		throw new Error(errors.invalidUUI);
	}

	return fetch(buildUrl(statisticsPath + res, {
		queryParams: {
			area_id: id,
			since: since,
			until: until
		}
	})).then(response => handleErrors(response))
		.then(response => response.json());
}

/**
 *
 * @param since
 * @param until
 * @param id
 * @param step
 * @returns {Promise<{ok} | never>}
 */
export function fetchTransitTimeSeries({ since, until, id, step } = {}) {
  const res = "transits";
  if (!isUUID(id)) {
    throw new Error(errors.invalidUUI);
  }

  let queryParams = {
    area_id: id,
    since: since,
    until: until,
    step: step
  };

  return fetch(buildUrl(statisticsPath + res, {
    queryParams
  })).then(response => handleErrors(response))
  .then(response => response.json());
}