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

Commit

Permalink
Output duplicated keys in Web UI (closes #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Jun 24, 2015
1 parent 6beacad commit 140950f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ case class PossibleDuplicatesWarning(possibleDuplicates: List[List[String]]) ext
implicit def pairsToJArray(pairs: List[List[String]]): JArray =
JArray(pairs.map(p => JArray(p.map(s => JString(s)))))

def isEmpty =
if (possibleDuplicates.isEmpty || possibleDuplicates.head.isEmpty) true
else false

def jsonMessage =
("message", "Possibly duplicated keys found") ~ ("items", possibleDuplicates)
if (this.isEmpty) { JNothing }
else { ("message", "Possibly duplicated keys found") ~ ("items", possibleDuplicates) }

def consoleMessage = possibleDuplicates match {
case Nil => ""
case _ => "Possibly duplicated keys found:\n" + possibleDuplicates.map(_.mkString(": ")).mkString("\n")
}
def consoleMessage =
if (this.isEmpty) { "" }
else { "Possibly duplicated keys found:\n" + possibleDuplicates.map(_.mkString(": ")).mkString("\n") }
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
* 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.generators

// Scala
Expand Down Expand Up @@ -40,13 +52,13 @@ object LevenshteinAnnotator {
* @return JSON Array with unique pairs
*/
def pairsToJArray(pairs: KeyPairs): JArray = {
val setOfJArrays: Set[JArray] = pairs.map(pp => {
if (pp._1 <= pp._2) { // preserve order, so merge remain associative
List(JString(pp._1), JString(pp._2))
val setOfJArrays: Set[JArray] = pairs.map { case (first, second) => {
if (first <= second) { // preserve order, so merge remain associative
List(JString(first), JString(second))
} else {
List(JString(pp._2), JString(pp._1))
List(JString(second), JString(first))
}
}).map(JArray(_))
}} map(JArray(_))
JArray(setOfJArrays.toList)
}

Expand Down Expand Up @@ -77,7 +89,7 @@ object LevenshteinAnnotator {

/**
* Helper function for producing all possible pairs
* for Strings with length > 3
* for Strings with length > ``thresholdLength``
* Ex: (a, b),(d, e) = (a,d),(a,e)(b,d),(b,e)
*
* @param xs set of strings
Expand All @@ -99,10 +111,10 @@ object LevenshteinAnnotator {
* @return pairs which distance threshold is lower than specified
*/
def compareSets(schKeys: Set[String], accKeys: Set[String]): KeyPairs = {
crossProduct(schKeys, accKeys).flatMap(s => {
val distance = calculateDistance(s._1, s._2)
crossProduct(schKeys, accKeys).flatMap { case (first, second) => {
val distance = calculateDistance(first, second)
if (distance == 0 || distance > thresholdDistance) Set.empty[Pair[String, String]]
else Set((s._1, s._2))
})
else Set((first, second))
}}
}
}
12 changes: 12 additions & 0 deletions webui/src/main/resources/web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ button[disabled] {
cursor: default;
}

pre {
margin-bottom: 24px;
}

.warning-message {
margin-bottom: 6px;
}

.warning-items {
width: 100%;
}

footer {
border-top: 1px solid #cecece;
clear: both;
Expand Down
2 changes: 1 addition & 1 deletion webui/src/main/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div id="root"> </div>

<!-- <script src="/dist/vendors.js"></script> -->
<!--<script src="/dist/vendors.js"></script>-->
<script src="/dist/bundle.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions webui/src/main/resources/web/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = Reflux.createActions([
"instanceToggled", // we include/exclude some instance
"switchSchemaView", // we switched view from plain to diff
"addErrors", // we received some errors
"addWarning", // we received warning
"viewShowDiff", // something wants to look at diff
"viewShowPlain" // something wants to look at plain schema
]);
2 changes: 2 additions & 0 deletions webui/src/main/resources/web/js/components/SchemaBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
var React = require('react');

var SchemaViewSwitcher = require('./SchemaViewSwitcher');
var SchemaWarning = require('./SchemaWarning');

/**
* Component displaying schema
Expand All @@ -28,6 +29,7 @@ module.exports = React.createClass({
<h2>Your schema</h2>
<SchemaViewSwitcher currentView={this.props.schema.currentView} />
<pre dangerouslySetInnerHTML={{__html: this.props.schema.schemaText}} />
<SchemaWarning warning={this.props.schema.warning} />
</div>)
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2014 Snowplow Analytics Ltd. All rights reserved.
* 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
Expand Down
46 changes: 46 additions & 0 deletions webui/src/main/resources/web/js/components/SchemaWarning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/
'use strict';

var _ = require("lodash");
var React = require('react');

/**
* Component responsible for output duplicated keys warning
*/
module.exports = React.createClass({
render: function () {
if (_.isEmpty(this.props.warning)) {
return ( <div></div> )
}
else {
return (
<div className="warning">
<h2>Warning</h2>
<div className="warning-message">{this.props.warning.message}</div>
<table className="warning-items">
<tbody>
{ _.map(this.props.warning.items, i =>
(<tr key={i[0]+i[1]}>
<td>{i[0]}</td>
<td>{i[1]}</td>
</tr>)) }
</tbody>
</table>
</div>
)
}
}
});
2 changes: 1 addition & 1 deletion webui/src/main/resources/web/js/stores/InstancesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = Reflux.createStore({
*/
onAddErrors: function (errors) {
var self = this;
_.forEach(errors, (e) => {
_.forEach(errors, (e) => { // add error message to corresponding instance
_.map(self.instances, (i) => {
if (e.file === i.name) {
i.error = e.error;
Expand Down
22 changes: 21 additions & 1 deletion webui/src/main/resources/web/js/stores/SchemaStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = Reflux.createStore({
// public state accessible from components
state: {
currentView: 'diff', // current schema tab
schemaText: '{}' // textual representation of Schema (diff of plain text)
schemaText: '{}', // textual representation of Schema (diff of plain text)
warning: {} // warning about possible duplicates
},

// private auxiliary state
Expand Down Expand Up @@ -76,6 +77,16 @@ module.exports = Reflux.createStore({
}
},

/**
* Fire addWarning if warning found in response
* @param res full superagent's response object
*/
processWarning: function(res) {
if (typeof res.body.warning !== 'undefined') {
GuruActions.addWarning(res.body.warning);
}
},

/**
* Handle schemaReceived action
* @param err contains data on HTTP code != 200
Expand All @@ -84,6 +95,7 @@ module.exports = Reflux.createStore({
*/
onSchemaReceived: function(err, res) {
this.processErrors(res);
this.processWarning(res);
this.schema = res.body.schema;
if (_.isEqual(this.previous, this.schema)) { return }
this.computeDelta();
Expand All @@ -106,6 +118,14 @@ module.exports = Reflux.createStore({
this.state.currentView = 'plain';
this.state.schemaText = JSON.stringify(this.schema, null, " ");
this.trigger(this.state);
},

/**
* Handle addWarning action
*/
onAddWarning: function(warning) {
this.state.warning = warning;
this.trigger(this.state);
}
});

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ trait SchemaGuruRoutes extends HttpService with HttpJsonGetters {
val jsons: ValidJsonList = getJsonFromRequest(formData)
val result = SchemaGuru.convertsJsonsToSchema(jsons)
val errors = getErrorsAsJson(result.errors)
compact((("status", "processed"): JObject) ~ ("schema", result.schema) ~ ("errors", errors))
compact((
("status", "processed"): JObject) ~
("schema", result.schema) ~
("errors", errors) ~
("warning", result.warning.map(_.jsonMessage))
)
}
}
}
Expand Down

0 comments on commit 140950f

Please sign in to comment.