Skip to content

Commit

Permalink
feat: write output csv
Browse files Browse the repository at this point in the history
Related to #21

Co-authored-by: steveoh <sgourley@utah.gov>
  • Loading branch information
stdavis and steveoh committed Oct 4, 2021
1 parent 894ff23 commit b979bd8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"noreferrer",
"relocator",
"semibold",
"stringifier",
"tailwindcss",
"UGRC",
"vercel",
Expand Down
53 changes: 32 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@
"@electron-forge/maker-squirrel": "^6.0.0-beta.60",
"@electron-forge/maker-zip": "^6.0.0-beta.60",
"@electron-forge/plugin-webpack": "^6.0.0-beta.60",
"@tailwindcss/forms": "^0.3.3",
"@tailwindcss/forms": "^0.3.4",
"@vercel/webpack-asset-relocator-loader": "^1.7.0",
"autoprefixer": "^10.3.6",
"babel-loader": "^8.2.2",
"babel-preset-react-app": "^10.0.0",
"css-loader": "^6.3.0",
"electron": "^15.0.0",
"electron": "^15.1.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"node-loader": "^2.0.0",
Expand All @@ -103,6 +103,7 @@
"assert": "^2.0.0",
"crypto-browserify": "^3.12.0",
"csv-parse": "^4.16.3",
"csv-stringify": "^5.6.5",
"electron-squirrel-startup": "^1.0.0",
"electron-store": "^8.0.1",
"file-loader": "^6.2.0",
Expand Down
24 changes: 22 additions & 2 deletions src/services/csv.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { app, ipcMain } = require('electron');
const fs = require('fs');
const path = require('path');
const parse = require('csv-parse');
const got = require('got');
const { readPackageUpAsync } = require('read-pkg-up');
const { ipcMain } = require('electron');
const stringify = require('csv-stringify');

export const getFields = async (filePath) => {
const parser = fs.createReadStream(filePath).pipe(parse({ columns: true, skipEmptyLines: true }));
Expand Down Expand Up @@ -69,6 +71,10 @@ export const cancelGeocode = () => {
export const geocode = async (event, { filePath, fields, apiKey }) => {
cancelled = false;
const parser = fs.createReadStream(filePath).pipe(parse({ columns: true, skipEmptyLines: true }));
const columns = await getFields(filePath);
const writer = fs.createWriteStream(path.join(app.getPath('userData'), 'output.csv'));
const stringifier = stringify({ columns: [...columns, 'x', 'y', 'score', 'match_address'], header: true });
stringifier.pipe(writer);

const packageInfo = await readPackageUpAsync();
let totalRows = await getRecordCount(filePath);
Expand All @@ -80,6 +86,7 @@ export const geocode = async (event, { filePath, fields, apiKey }) => {
if (cancelled) {
return;
}
const newRecord = { ...record, x: null, y: null, score: 0, match_address: null };

const street = cleanseStreet(record[fields.street]);
const zone = cleanseZone(record[fields.zone]);
Expand Down Expand Up @@ -112,10 +119,21 @@ export const geocode = async (event, { filePath, fields, apiKey }) => {

if (response.status === 200) {
const result = response.result;
const { score } = result;
const {
score,
matchAddress,
location: { x, y },
} = result;

totalScore += score;
newRecord.score = score;
newRecord.x = x;
newRecord.y = y;
newRecord.match_address = matchAddress;
}

stringifier.write(newRecord);

rowsProcessed++;

event.reply('onGeocodingUpdate', {
Expand All @@ -127,6 +145,8 @@ export const geocode = async (event, { filePath, fields, apiKey }) => {

await coolYourJets();
}

stringifier.end();
};

ipcMain.handle('getFieldsFromFile', (_, content) => {
Expand Down

0 comments on commit b979bd8

Please sign in to comment.