|
| 1 | +import Logger from '../src/logger'; |
| 2 | +import {Container} from 'typedi'; |
| 3 | +import databaseLoader from '../src/loaders/database'; |
| 4 | +import RideService from '../src/api/services/RideService'; |
| 5 | +import UserService from '../src/api/services/UserService'; |
| 6 | +import {User} from '../src/api/entities/User'; |
| 7 | +import BikeService from '../src/api/services/BikeService'; |
| 8 | +import {Station} from '../src/api/entities/Station'; |
| 9 | +import StationService from '../src/api/services/StationService'; |
| 10 | +import {Bike} from '../src/api/entities/Bike'; |
| 11 | +import {Ride} from '../src/api/entities/Ride'; |
| 12 | +import {addDays, addMinutes, differenceInCalendarDays} from 'date-fns'; |
| 13 | + |
| 14 | +const rideCount = 10; |
| 15 | +const start = new Date(); |
| 16 | +const end = new Date('2021-07-26'); |
| 17 | +const difference = differenceInCalendarDays(end, start); |
| 18 | + |
| 19 | +const run = async () => { |
| 20 | + Container.set('logger', Logger); |
| 21 | + const log = Logger.info; |
| 22 | + const connection = await databaseLoader(); |
| 23 | + log('Database connection loaded successfully!'); |
| 24 | + |
| 25 | + const rideService: RideService = Container.get(RideService); |
| 26 | + const userService: UserService = Container.get(UserService); |
| 27 | + const bikeService: BikeService = Container.get(BikeService); |
| 28 | + const stationService: StationService = Container.get(StationService); |
| 29 | + |
| 30 | + const userIds: { id: number }[] = await userService.getRepo().createQueryBuilder('user') |
| 31 | + .select('id') |
| 32 | + .getRawMany(); |
| 33 | + |
| 34 | + const stationIds: { id: number }[] = await stationService.getRepo().createQueryBuilder('station') |
| 35 | + .select('id') |
| 36 | + .getRawMany(); |
| 37 | + |
| 38 | + for (let i = 0; i < rideCount; i++) { |
| 39 | + const user: User = await userService.findOne(userIds[Math.floor(Math.random() * 10000 % userIds.length)].id); |
| 40 | + if (user.subscriptions?.length == 0) { |
| 41 | + log(`User ${user.id} don't have subscription, skipping...`); |
| 42 | + i--; |
| 43 | + continue; |
| 44 | + } |
| 45 | + const startDate = addDays(start, Math.floor(Math.random() * difference)); |
| 46 | + const rideDuration = 1 + Math.floor(Math.random() * 60); |
| 47 | + const endDate = addMinutes(startDate, rideDuration); |
| 48 | + |
| 49 | + const startStation: Station = await stationService.findOne(stationIds[Math.floor(Math.random() * 10000 % stationIds.length)].id); |
| 50 | + const endStation: Station = await stationService.getRepo().createQueryBuilder('station') |
| 51 | + .leftJoin('bike', 'b', 'b.stationId = station.id') |
| 52 | + .select() |
| 53 | + .addSelect('count(distinct b.id) as bikeCount') |
| 54 | + .groupBy('station.id, station.bikeCapacity') |
| 55 | + .having(`bikeCount < station.bikeCapacity`) |
| 56 | + .getOne(); |
| 57 | + |
| 58 | + const availableBikes: Bike[] = await bikeService.getAllFromStation(startStation.id, {limit: 100, offset: 0}); |
| 59 | + if (availableBikes.length == 0) { |
| 60 | + log('Start station don\'t have available bikes, skipping...'); |
| 61 | + i--; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + const ride: Ride = await rideService.startRide( |
| 66 | + availableBikes[Math.floor(Math.random() * 10000 % availableBikes.length)], |
| 67 | + startStation, |
| 68 | + user, |
| 69 | + startDate |
| 70 | + ); |
| 71 | + await rideService.endRide( |
| 72 | + user, |
| 73 | + ride, |
| 74 | + endStation, |
| 75 | + Math.floor(rideDuration * Math.random() * 20), |
| 76 | + endDate |
| 77 | + ); |
| 78 | + log(`Ride ${ride.id} inserted (${i + 1}/${rideCount})`); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +}; |
| 83 | + |
| 84 | + |
| 85 | +run().then(r => console.log('Rides Seeded')); |
0 commit comments