import isUUID from 'validator/lib/isUUID';
import { errors, handleErrors } from './RestModuleAPI';
import buildUrl from 'build-url';
import { API } from '../../LocalConfiguration';
const movementsPath = API + '/movements/';
/**
* Movements Rest API
* @module MovementsRestModule
*/
/**
* how many points can browser handle before we have to aggregate data
* @type {number}
*/
const aggregationThreshold = 50000;
/**
* Returns minimum and maximum pixel difference for speed
* @param since
* @param until
* @param id
* @returns {Promise<void | never>}
*/
export function fetchMovementsMinMax({ since, until, id } = {}) {
const res = "maybe_aggregated/velocity_range";
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
max_points: aggregationThreshold
}
})).then(response => handleErrors(response))
.then(response => response.json());
}
/**
* Returns Count of movements(points)
* @param since
* @param until
* @param id
* @param min
* @param max
* @returns {Promise<void | never>}
*/
export function fetchMovementsCount({ since, until, id } = {}, min, max) {
const res = "count";
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
min_velocity: min,
max_velocity: max
}
})).then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param since
* @param until
* @param id
* @param min
* @param max
* @returns {Promise<void | never>}
*/
export function fetchMovements({ since, until, id } = {}, min, max) {
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
const res = "groups";
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
min_velocity: min,
max_velocity: max
}
})).then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param since
* @param until
* @param id
* @param min
* @param max
* @returns {Promise<void | never>}
*/
export function fetchAggregatedMovements({ since, until, id } = {}, min, max) {
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
const res = "aggregated";
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
min_velocity: min,
max_velocity: max
}
})).then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param since
* @param until
* @param id
* @param min
* @param max
* @returns {Promise<void | never>}
*/
export function fetchAggregatedTrajectories({ since, until, id } = {}, min, max) {
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
const res = "aggregated/trajectories";
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
min_velocity: min,
max_velocity: max
}
})).then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param since
* @param until
* @param id
* @param min
* @param max
* @returns {Promise<void | never>}
*/
export function fetchAggregatedMovementsCount({ since, until, id } = {}, min, max) {
const res = "aggregated/count";
if (!isUUID(id)) {
throw new Error(errors.invalidUUID);
}
return fetch(buildUrl(movementsPath + res, {
queryParams: {
area_id: id,
since: since,
until: until,
min_velocity: min,
max_velocity: max
}
})).then(response => handleErrors(response))
.then(response => response.json());
}