Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent csv delimiter #179

Merged
merged 5 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
presets: ['@vue/cli-plugin-babel/preset'],
presets: ['@vue/cli-plugin-babel/preset', '@babel/preset-typescript'],
}
7 changes: 4 additions & 3 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/*.spec.ts'],
}
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"webfontloader": "^1.6.28"
},
"devDependencies": {
"@babel/preset-typescript": "^7.22.5",
"@types/crypto-js": "^4.1.1",
"@types/file-saver": "^2.0.5",
"@types/google.maps": "^3.49.2",
Expand Down
17 changes: 14 additions & 3 deletions frontend/src/lib/csv/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import {
} from './schema-parser'
import { MapJsonProperties } from './types'

type Column = string | number | boolean

const quote = (s: string) => `"${s}"`
const constructDelimetedRow = (delimeter: string) => (row: Column[]) =>
row.map((c) => (typeof c === 'string' ? quote(c) : c)).join(delimeter)
const addNewlines = (s: Column[]) => s.join('\n')

export const objectToCsv =
<T>(
headers: string[],
fnFlatten: (obj: T) => (string | number | boolean)[][],
joinStr: string = ',',
fnFlatten: (obj: T) => Column[][],
delimeter: string = ',',
) =>
(obj: T) => {
const csvData = fnFlatten(obj)
Expand All @@ -21,7 +28,11 @@ export const objectToCsv =
)}, see incorrect rows: ${JSON.stringify(errorRows)}`,
)
}
return [headers, ...csvData].map((row) => row.join(joinStr)).join('\n')
const rowsWithHeaders = [headers, ...csvData]
const delimetedRows = rowsWithHeaders.map(
constructDelimetedRow(delimeter),
)
return addNewlines(delimetedRows)
}

/**
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/lib/csv/schema-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ describe('csv', () => {
]),
)(obj)
expect(csv).toEqual(
'id,fruit,cultivar,inSeason\n' +
'1,apple,gala,false\n' +
'1,apple,red delicious,true\n' +
'1,pear,bosc,false\n' +
'1,pear,bartlett,true',
'"id","fruit","cultivar","inSeason"\n' +
'1,"apple","gala",false\n' +
'1,"apple","red delicious",true\n' +
'1,"pear","bosc",false\n' +
'1,"pear","bartlett",true',
)
})

Expand Down
79 changes: 38 additions & 41 deletions frontend/src/lib/store/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export const useImageStore = defineStore<
const zname = `${bname}.zip`
const folder = zip.folder(bname)

generateSummaryFiles(this.summary).map(([fileName, data]) =>
generateSummaryFiles(this.summary).map(({fileName, data}) =>
folder!.file(fileName, data),
)

Expand Down Expand Up @@ -371,47 +371,44 @@ export const useImageStore = defineStore<
},
})

const generateSummaryFiles = (summary: Summary) => {
const files: [string, string][] = []
// summary json
files.push(['summary.json', JSON.stringify(summary)])
// summary totals csv
const sTotalsHeaders = ['total_detections', 'unique_detections']
const summaryTotalsToCsv = objectToCsv(sTotalsHeaders, (o: Summary) => [
[o.total_detections, o.unique_detections],
])

// summary totals csv
const sTotalsHeaders = ['total_detections', 'unique_detections']
const summaryTotalsCsv = objectToCsv(sTotalsHeaders, (o: Summary) => [
[o.total_detections, o.unique_detections],
])(summary)
files.push(['summary_totals.csv', summaryTotalsCsv])
// summary detected objects csv
const detectedHeaders = getCsvHeadersFromJsonSchema(
summarySchema.properties.detected_objects as JSONSchema7,
)
const detectedToCsv = objectToCsv(detectedHeaders, (obj: Summary) =>
obj.detected_objects.map((v) => [v.name, v.count, v.hashes.join(',')]),
)

// summary detected objects csv
const detectedHeaders = getCsvHeadersFromJsonSchema(
summarySchema.properties.detected_objects as JSONSchema7,
)
const detectedCsv = objectToCsv(
detectedHeaders,
(obj: Summary) =>
obj.detected_objects.map((v) => [
v.name,
v.count,
v.hashes.join(','),
]),
';',
)(summary)
files.push(['summary_detected.csv', detectedCsv])
// summary gps objects csv
const gpsHeaders = getCsvHeadersFromJsonSchema(
summarySchema.properties.gps.properties.list.items as JSONSchema7,
)
const gpsToCsv = objectToCsv(gpsHeaders, (obj: Summary) =>
obj.gps.list.map((v) => [
v?.coordinate?.lat ?? '',
v?.coordinate?.lng ?? '',
v.hash,
]),
)

// summary gps objects csv
if (summary.gps.list.length > 0) {
const gpsHeaders = getCsvHeadersFromJsonSchema(
summarySchema.properties.gps.properties.list.items as JSONSchema7,
)
const gpsCsv = objectToCsv(gpsHeaders, (obj: Summary) =>
obj.gps.list.map((v) => [
v?.coordinate?.lat ?? '',
v?.coordinate?.lng ?? '',
v.hash,
]),
)(summary)
files.push(['summary_gps.csv', gpsCsv])
}
return files
const generateSummaryFiles = (summary: Summary) => {
const filesToGenerate = [
{ fileName: 'summary.json', dataFn: JSON.stringify },
{ fileName: 'summary_totals.csv', dataFn: summaryTotalsToCsv },
{ fileName: 'summary_detected.csv', dataFn: detectedToCsv },
...(summary.gps.list.length > 0
? [{ fileName: 'summary_gps.csv', dataFn: gpsToCsv }]
: []),
]

return filesToGenerate.map(({ fileName, dataFn }) => ({
fileName,
data: dataFn(summary),
}))
}
23 changes: 22 additions & 1 deletion frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"

"@babel/plugin-syntax-typescript@^7.7.2":
"@babel/plugin-syntax-typescript@^7.22.5", "@babel/plugin-syntax-typescript@^7.7.2":
version "7.22.5"
resolved "https://registry.npmjs.org/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.22.5.tgz"
integrity sha512-1mS2o03i7t1c6VzH6fdQ3OA8tcEIxwG18zIPRp+UY1Ihv6W+XZzBCVxExF9upussPXJ0xE9XRHwMoNs1ep/nRQ==
Expand Down Expand Up @@ -950,6 +950,16 @@
dependencies:
"@babel/helper-plugin-utils" "^7.22.5"

"@babel/plugin-transform-typescript@^7.22.5":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.22.5.tgz#5c0f7adfc1b5f38c4dbc8f79b1f0f8074134bd7d"
integrity sha512-SMubA9S7Cb5sGSFFUlqxyClTA9zWJ8qGQrppNUm05LtFuN1ELRFNndkix4zUJrC9F+YivWwa1dHMSyo0e0N9dA==
dependencies:
"@babel/helper-annotate-as-pure" "^7.22.5"
"@babel/helper-create-class-features-plugin" "^7.22.5"
"@babel/helper-plugin-utils" "^7.22.5"
"@babel/plugin-syntax-typescript" "^7.22.5"

"@babel/plugin-transform-unicode-escapes@^7.22.5":
version "7.22.5"
resolved "https://registry.npmjs.org/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.22.5.tgz"
Expand Down Expand Up @@ -1078,6 +1088,17 @@
"@babel/types" "^7.4.4"
esutils "^2.0.2"

"@babel/preset-typescript@^7.22.5":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.22.5.tgz#16367d8b01d640e9a507577ed4ee54e0101e51c8"
integrity sha512-YbPaal9LxztSGhmndR46FmAbkJ/1fAsw293tSU+I5E5h+cnJ3d4GTwyUgGYmOXJYdGA+uNePle4qbaRzj2NISQ==
dependencies:
"@babel/helper-plugin-utils" "^7.22.5"
"@babel/helper-validator-option" "^7.22.5"
"@babel/plugin-syntax-jsx" "^7.22.5"
"@babel/plugin-transform-modules-commonjs" "^7.22.5"
"@babel/plugin-transform-typescript" "^7.22.5"

"@babel/regjsgen@^0.8.0":
version "0.8.0"
resolved "https://registry.npmjs.org/@babel/regjsgen/-/regjsgen-0.8.0.tgz"
Expand Down