Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Add enum cardinality option to Web UI (close #64)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Jun 22, 2015
1 parent e61b240 commit b11c708
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ object JsonSchemaMerger {
* @return the cumulative JsonSchema
*/
// TODO: handle case where _starting_ List is empty (see: reduceLeftOption above)
def mergeJsonSchemas(jsonSchemaList: List[JValue], accum: JValue = Nil, enumCardinality: Int = 0): JValue =
def mergeJsonSchemas(jsonSchemaList: List[JValue], accum: JValue = Nil, enumCardinality: Int = 0): JValue = {
jsonSchemaList match {
case x :: xs => {
val annotatedAcc = LevenshteinAnnotator.addPossibleDuplicates(x, accum)
mergeJsonSchemas(xs, formatSchemaForMerge(x).merge(annotatedAcc))
mergeJsonSchemas(xs, formatSchemaForMerge(x).merge(annotatedAcc), enumCardinality)
}
case Nil => reduceMergedSchema(accum, enumCardinality)
}
}


/**
* Transforms the type descriptor for every key
Expand Down
13 changes: 13 additions & 0 deletions webui/src/main/resources/web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ td {
background-color: #f7f7f7;
}

.options {
margin-bottom: 12px;
}

.options-value {
margin-right: 10px;
width: 30px;
}

.options-title {
font-size: 14px;
}

.schema {
float: right;
width: 48%;
Expand Down
3 changes: 2 additions & 1 deletion webui/src/main/resources/web/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ module.exports = Reflux.createActions([
"addErrors", // we received some errors
"addWarning", // we received warning
"viewShowDiff", // something wants to look at diff
"viewShowPlain" // something wants to look at plain schema
"viewShowPlain", // something wants to look at plain schema
"enumCardinalityChanged" // input with enum cardinality changed
]);
2 changes: 2 additions & 0 deletions webui/src/main/resources/web/js/components/InstancesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Dropzone = require('react-dropzone');
var GuruActions = require('../actions');
var InstanceList = require('./InstanceList');
var JsonInput = require('./JsonInput');
var Options = require('./Options');

/**
* Whole left side
Expand Down Expand Up @@ -62,6 +63,7 @@ module.exports = React.createClass({
{ this.state.dragging === true ? "I hope it is not a Bluray rip" : this.state.message }
</div>
</Dropzone>
<Options />

<JsonInput />
<InstanceList instances={this.props.instances} />
Expand Down
38 changes: 38 additions & 0 deletions webui/src/main/resources/web/js/components/Options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2012-2014 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0, and
* you may not use this file except in compliance with the Apache License
* Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Apache License Version 2.0 is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the Apache License Version 2.0 for the specific language
* governing permissions and limitations there under.
*/
'use strict';

var React = require('react');

var GuruActions = require('../actions');


module.exports = React.createClass({
update: function(ev) {
var value = Number(ev.target.value);
if (isNaN(value) || value < 0) value = 0;
GuruActions.enumCardinalityChanged(value);
},
render: function() {
return (
<div className="options">
<label>
<input className="options-value" onChange={this.update} defaultValue={0} />
<span className="options-title">Enum cardinality tolerance</span>
</label>
</div>
)
}
});
13 changes: 12 additions & 1 deletion webui/src/main/resources/web/js/stores/InstancesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = Reflux.createStore({
listenables: GuruActions,

textInstanceCounter: 0, // private auxiliary counter
enumCardinality: 0, // value of enum cardinality tolerance
instances: [], // list of File object with some additional attributes
// TODO: consider changing array to object
// TODO: find a place to maintain these mutable attributes
Expand All @@ -45,7 +46,8 @@ module.exports = Reflux.createStore({
postInstances: function(instances) {
var req = request.post('/upload');
instances.forEach(file => req.attach(file.name, file));
req.end(GuruActions.schemaReceived);
req.field('enumCardinality', this.enumCardinality)
.end(GuruActions.schemaReceived);
},

/**
Expand Down Expand Up @@ -123,5 +125,14 @@ module.exports = Reflux.createStore({
this.textInstanceCounter += 1;
var file = new File([value], "text_instance_" + this.textInstanceCounter + ".json");
GuruActions.instancesAdded([file])
},

/**
* Handle enumCardinalityChanged event
* Set InstanceStore's private var
* @param cardinality
*/
onEnumCardinalityChanged: function(cardinality) {
this.enumCardinality = cardinality;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2015 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.schemaguru

import spray.http.MultipartFormData

/**
* Trait responsible for extracting analogs of CLI options from HTTP-request
* or providing defaults
*/
// TODO: we need to add base trait which will add contract for extracting same values for CLI
trait HttpOptionsGetter {
def getCardinality(data: MultipartFormData): Int = {
try {
data.get("enumCardinality").get.entity.data.asString.toInt
}
catch {
case e: Exception => 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._

trait SchemaGuruRoutes extends HttpService with HttpJsonGetters {
trait SchemaGuruRoutes extends HttpService with HttpJsonGetters with HttpOptionsGetter {
/**
* Pipe route to ``convertsToJsonToSchema`` core function
* Accept POST request with JSON files
Expand All @@ -35,8 +35,8 @@ trait SchemaGuruRoutes extends HttpService with HttpJsonGetters {
detach() {
complete {
val jsons: ValidJsonList = getJsonFromRequest(formData)
// TODO: add enum cardinality here
val result = SchemaGuru.convertsJsonsToSchema(jsons)
val cardinality = getCardinality(formData)
val result = SchemaGuru.convertsJsonsToSchema(jsons, cardinality)
val errors = getErrorsAsJson(result.errors)
compact(
(("status", "processed"): JObject) ~
Expand Down

0 comments on commit b11c708

Please sign in to comment.