Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created November 9, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/a1c9498b93fec7f7e23de95821a0a679 to your computer and use it in GitHub Desktop.
Save parzibyte/a1c9498b93fec7f7e23de95821a0a679 to your computer and use it in GitHub Desktop.
/**
* https://parzibyte.me/blog
*/
const obtenerNumeroAleatorioEnRango = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const fechaAleatoria = () => {
const fecha = new Date(obtenerNumeroAleatorioEnRango(2020, 2021), obtenerNumeroAleatorioEnRango(0, 11), obtenerNumeroAleatorioEnRango(1, 28), obtenerNumeroAleatorioEnRango(0, 23), obtenerNumeroAleatorioEnRango(0, 59), obtenerNumeroAleatorioEnRango(0, 59));
return fecha;
}
const obtenerCadenaFecha = (fecha) => {
const mes = fecha.getMonth() + 1; // Ya que los meses los cuenta desde el 0
const dia = fecha.getDate();
const cadenaFecha = `${fecha.getFullYear()}-${agregarCeroSiEsNecesario(mes)}-${agregarCeroSiEsNecesario(dia)}`;
const cadenaHora = `${agregarCeroSiEsNecesario(fecha.getHours())}:${agregarCeroSiEsNecesario(fecha.getMinutes())}:${agregarCeroSiEsNecesario(fecha.getSeconds())}`;
return cadenaFecha + "T" + cadenaHora;
}
const agregarCeroSiEsNecesario = (valor) => {
if (valor < 10) {
return '0'.concat(valor);
}
return valor.toString();
}
let cadena = `INSERT INTO dispositivos_rentados
(id_dispositivo, inicio, fin, costo, costo_productos, es_tiempo_libre, cobrado, segundos_pausa, fecha_pausa)
VALUES `;
const cantidadRegistros = 10;
for (let x = 0; x < cantidadRegistros; x++) {
const idDispositivo = obtenerNumeroAleatorioEnRango(1, 5);
const inicio = fechaAleatoria();
const fin = new Date(inicio.getTime() + (obtenerNumeroAleatorioEnRango(1, 600) * 60 * 1000));
const inicioFormateado = obtenerCadenaFecha(inicio);
const finFormateado = obtenerCadenaFecha(fin);
const costo = obtenerNumeroAleatorioEnRango(1, 1000);
const costoProductos = obtenerNumeroAleatorioEnRango(1, 1000);
const esTiempoLibre = obtenerNumeroAleatorioEnRango(0, 1);
const cobrado = 1;
const segundosPausa = obtenerNumeroAleatorioEnRango(0, 100);
const fechaPausa = "";
cadena += `
("${idDispositivo}","${inicioFormateado}", "${finFormateado}", ${costo}, ${costoProductos}, ${esTiempoLibre}, ${cobrado}, ${segundosPausa},"${fechaPausa}")`;
if (x + 1 < cantidadRegistros) {
cadena += ",";
} else {
cadena += ";";
}
}
console.log(cadena);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment