Guest User

Untitled

a guest
Mar 24th, 2019
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express')
  2. const fs = require('fs')
  3. const router = express.Router()
  4.  
  5. const MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
  6.  
  7. router.get('/populate-data', async (req, res) => {
  8. // Read offence data from file
  9. const data = fs.readFileSync('src/data/lga_reported_offenders_number.csv', { encoding: 'utf8' }).split('\r\n')
  10.  
  11. // Parse out relivant data
  12. const headers = data[0].split(',').map(h => h.replace(/"/g, ''))
  13. const offences = headers.slice(4)
  14. const rows = data.slice(1).map(row => row.split(','))
  15. const areas = Array.from(new Set(rows.map(row => row[0]))).filter(i => i.length)
  16. const ages = Array.from(new Set(rows.map(row => row[2]))).filter(i => !!i)
  17. const genders = Array.from(new Set(rows.map(row => row[3]))).filter(i => !!i)
  18.  
  19. // Purge old data
  20. await req.db('offences').delete().catch(error => res.status(500).json(error))
  21. await req.db('areas').delete().catch(error => res.status(500).json(error))
  22. await req.db('ages').delete().catch(error => res.status(500).json(error))
  23. await req.db('genders').delete().catch(error => res.status(500).json(error))
  24. await req.db('statistics').delete().catch(error => res.status(500).json(error))
  25.  
  26. // Populate initial data
  27. await req.db('offences').insert(offences.map(i => ({ offence: i }))).catch(error => res.status(500).json(error))
  28. await req.db('areas').insert(areas.map(i => ({ area: i }))).catch(error => res.status(500).json(error))
  29. await req.db('ages').insert(ages.map(i => ({ age: i }))).catch(error => res.status(500).json(error))
  30. await req.db('genders').insert(genders.map(i => ({ gender: i }))).catch(error => res.status(500).json(error))
  31.  
  32. // Query initial data
  33. const dbOffences = await req.db('offences').catch(error => res.status(500).json(error))
  34. const dbAreas = await req.db('areas').catch(error => res.status(500).json(error))
  35. const dbAges = await req.db('ages').catch(error => res.status(500).json(error))
  36. const dbGenders = await req.db('genders').catch(error => res.status(500).json(error))
  37.  
  38. // Build statistic data
  39. const statistics = rows.filter(i => i.length>1).map(row => {
  40. return {
  41. offence: null,
  42. area: row[0],
  43. age: row[2],
  44. gender: row[3],
  45. year: `20${row[1].substring(3, 5)}`,
  46. month: MONTHS.indexOf(row[1].substring(0, 3)) + 1,
  47. value: null,
  48. }
  49. })
  50.  
  51. res.json({ success: true, data: statistics[0], dbAges })
  52. })
  53.  
  54. module.exports = router
RAW Paste Data