account for omitted rows

This commit is contained in:
Martin McKeaveney 2020-10-05 10:51:58 +01:00
parent 5e86076530
commit b754fc3f07
2 changed files with 27 additions and 31 deletions

View File

@ -2,6 +2,7 @@
import { Heading, Body, Button, Select } from "@budibase/bbui" import { Heading, Body, Button, Select } from "@budibase/bbui"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { FIELDS } from "constants/backend" import { FIELDS } from "constants/backend"
import api from "builderStore/api"
const BYTES_IN_KB = 1000 const BYTES_IN_KB = 1000
const BYTES_IN_MB = 1000000 const BYTES_IN_MB = 1000000
@ -28,6 +29,9 @@
const modelSchema = {} const modelSchema = {}
for (let key in schema) { for (let key in schema) {
const type = schema[key].type const type = schema[key].type
if (type === "omit") continue
modelSchema[key] = { modelSchema[key] = {
name: key, name: key,
type, type,
@ -38,16 +42,9 @@
} }
async function validateCSV() { async function validateCSV() {
const response = await fetch("/api/models/csv/validate", { const response = await api.post("/api/models/csv/validate", {
method: "POST",
body: JSON.stringify({
file: files[0], file: files[0],
schema: schema || {}, schema: schema || {},
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
}) })
parseResult = await response.json() parseResult = await response.json()
@ -79,8 +76,9 @@
await validateCSV() await validateCSV()
} }
function omitColumn(columnName) { async function omitColumn(columnName) {
parsers[columnName] = PARSERS.omit schema[columnName].type = "omit"
await validateCSV()
} }
const handleTypeChange = column => evt => { const handleTypeChange = column => evt => {
@ -97,7 +95,7 @@
</div> </div>
<div class="schema-fields"> <div class="schema-fields">
{#if schema} {#if schema}
{#each Object.keys(schema) as columnName} {#each Object.keys(schema).filter(key => schema[key].type !== 'omit') as columnName}
<div class="field"> <div class="field">
<span>{columnName}</span> <span>{columnName}</span>
<Select <Select
@ -109,9 +107,7 @@
<option value={'number'}>Number</option> <option value={'number'}>Number</option>
<option value={'datetime'}>Date</option> <option value={'datetime'}>Date</option>
</Select> </Select>
<span <span class="field-status" class:error={!schema[columnName].success}>
class:success={schema[columnName].success}
class:error={!schema[columnName].success}>
{schema[columnName].success ? 'Success' : 'Failure'} {schema[columnName].success ? 'Success' : 'Failure'}
</span> </span>
<i <i
@ -132,7 +128,7 @@
transition: all 0.3s; transition: all 0.3s;
} }
.success { .field-status {
color: green; color: green;
} }
@ -151,7 +147,7 @@
box-sizing: border-box; box-sizing: border-box;
overflow: hidden; overflow: hidden;
border-radius: var(--border-radius-s); border-radius: var(--border-radius-s);
color: var(--white); color: var(--ink);
padding: var(--spacing-s) var(--spacing-l); padding: var(--spacing-s) var(--spacing-l);
transition: all 0.2s ease 0s; transition: all 0.2s ease 0s;
display: inline-flex; display: inline-flex;
@ -166,13 +162,9 @@
justify-content: center; justify-content: center;
margin-top: 10px; margin-top: 10px;
width: 100%; width: 100%;
border: solid 1.5px var(--ink); background-color: var(--grey-2);
background-color: var(--ink);
} }
/* .schema-fields {
} */
.omit-button { .omit-button {
font-size: 1.2em; font-size: 1.2em;
color: var(--grey-7); color: var(--grey-7);

View File

@ -7,13 +7,11 @@ const VALIDATORS = {
} }
const PARSERS = { const PARSERS = {
string: attribute => attribute.toString(),
number: attribute => Number(attribute),
datetime: attribute => new Date(attribute).toISOString(), datetime: attribute => new Date(attribute).toISOString(),
} }
function parse(path, parsers) { function parse(path, parsers) {
const result = csv().fromFile(path) const result = csv({ parsers }).fromFile(path)
const schema = {} const schema = {}
@ -30,10 +28,17 @@ function parse(path, parsers) {
// For each CSV row // For each CSV row
// parse all the columns that need parsed // parse all the columns that need parsed
for (let key in parsers) { for (let key in parsers) {
// if the schema has already borked for a parser, skip this column // if the parsing has already failed for a column
if (!schema[key] || !schema[key].success) continue // skip that column
if (
!schema[key] ||
!schema[key].success ||
schema[key].type === "omit"
) {
continue
}
// get the validator // get the validator for the column type
const validator = VALIDATORS[parsers[key].type] const validator = VALIDATORS[parsers[key].type]
try { try {
@ -54,12 +59,11 @@ function parse(path, parsers) {
}) })
} }
// TODO: significant refactor
async function transform({ schema, path }) { async function transform({ schema, path }) {
const colParser = {} const colParser = {}
for (let key in schema) { for (let key in schema) {
colParser[key] = PARSERS[schema[key].type] colParser[key] = PARSERS[schema[key].type] || schema[key].type
} }
try { try {