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

modified handsontable, replace thrift client with new datahub client #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Empty file added HandsOnTable/.index.html.swo
Empty file.
Empty file added HandsOnTable/.index.html.swp
Empty file.
1,016 changes: 1,016 additions & 0 deletions HandsOnTable/HandsOnTable.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions HandsOnTable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Temporary application for handsontable integration to DataHub
=================
35 changes: 35 additions & 0 deletions HandsOnTable/SampleClient.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<html>
<head>
<title>Sample DataHub HTML</title>
<script type="text/javascript" src="http://datahub.csail.mit.edu/static/lib/jquery/jquery.min.js"></script>
<script type="text/javascript" src="http://datahub.csail.mit.edu/static/lib/datahub/datahub_types.js"></script>
<script type="text/javascript" src="http://datahub.csail.mit.edu/static/lib/datahub/DataHub.js"></script>
</head>
<body>
<table id="results">
</table>
</body>
</html>


<script type="text/javascript">
/**
@author: anant bhardwaj
@date: Oct 11, 2013

Sample javascript client for DataHub
*/

transport = new Thrift.Transport("http://datahub.csail.mit.edu/service/json"),
protocol = new Thrift.Protocol(transport),
client = new DataHubClient(protocol),
con_params = new ConnectionParams({'user': 'anantb', 'password': 'anant'}),
con = client.open_connection(con_params),
res = client.execute_sql(con, 'select * from anantb.test.demo');

$('#results').append('<tr><th>' + res.field_names.join('</th><th>') + '</th></tr>');

$.each(res.tuples, function(tuple_idx, tuple) {
$('#results').append('<tr><td>' + tuple.cells.join('</td><td>') + '</td></tr>');
});
</script>
49 changes: 49 additions & 0 deletions HandsOnTable/aggregate.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
body {
padding: 15px;
}

.container-fluid {
padding: 0;
}

.navbar-collapse {
padding: 0;
}

.aggregateSection {
border: 5px;
}

.aggregateHeaderTable {
padding-bottom: 45px;
}

.navbar-oauth {
list-style-type: none;
margin-top: 10px;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}

.navbar-oauth > li {
float: right;
}

.navbar-oauth > li a {
display: block;
color: #666;
text-align: center;
text-decoration: none;
cursor: pointer;
}

.navbar-oauth > li a:hover:not(.active) {
background-color: #ddd;
}

.navbar-oauth > li a.active {
color: white;
background-color: #4CAF50;
}
162 changes: 162 additions & 0 deletions HandsOnTable/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
$(document).ready(function(){
$('#aggregateModal').on('click', '.go_button', function () {
executeAggregateQuery(generateQuery());
});
$('#aggregateButton').click (function() {
var aggregateModal = $('#aggregateModal');
aggregateModal.find(".go_button").show();
$("#openNewTableButton").hide();
aggregateModal.find(".modal-header").removeClass("aggregateHeaderTable");
aggregateModal.find('.modal-title').html("Create aggregation");
aggregateModal.find('.modal-body').html("");
aggregateModal.find('.modal-body').append("<div id='aggregateSelectionDiv'><form class='form-horizontal'><fieldset id='aggregateFieldset'></fieldset></form></div>");
$("#aggregateFieldset").append('<div class="aggregateSection"> </div>');
addAggregateSection(0);
addGroupBySection ();
});

});

function addAggregateSection (rowNumber) {

$(".addMoreButton").hide();
inputsectionId = 'aggregateInputSection'+rowNumber;
$(".aggregateSection").append('<div id="'+inputsectionId+'" class="aggregateInputSection well"><div>')
var inputSectionText = '<div class="form-group">'+
'<label class="control-label col-sm-3" for="aggregateType">Function</label>'+
'<div class="col-sm-8">'+
'<select id="aggregateType'+rowNumber+'" name="aggregateType" class="input-xlarge form-control">'+
'<option>None</option>'+
'<option>Count</option>'+
'<option>Sum</option>'+
'<option>Average</option>'+
'<option>Max</option>'+
'<option>Min</option>'+
'</select></div>';
if (rowNumber>0) {
inputSectionText+='<span id="deleteRow'+rowNumber+'" class="glyphicon glyphicon-remove" aria-hidden="true"></span>';
}
inputSectionText+='</div>';
$("#"+inputsectionId).append(inputSectionText);
$("#deleteRow"+rowNumber).click (function() {
$('#aggregateInputSection'+(rowNumber-1)).remove();
rowNumber-=1;
})

$("#"+inputsectionId).append('<div class="control-group form-group">'+
'<label class="control-label col-sm-3" for="aggregateType">Column</label>'+
'<div class="col-sm-8">'+
'<select id="aggregateColumn'+rowNumber+'" name="aggregateType" class="input-xlarge form-control">'+
'</select>'+
'</div></div>');

var columns = $("#results").handsontable("getColHeader");
i=0;
for (var i = 0; i < columns.length; i++) {
$("#aggregateColumn"+rowNumber).append('<option>'+columns[i]+'</option>');
}

rowNumber = rowNumber+1;

$("#"+inputsectionId).append("<a class='col-xs-offset-2 addMoreButton' onclick='addAggregateSection("+rowNumber+")'>+Add more</a>");

}

function addGroupBySection () {
$("#aggregateFieldset").append('<div class="container-fluid"> <div class="row well aggregateGroupBySection"></br></div></div>');
$(".aggregateGroupBySection").append('<div class="form-group">'+
'<label class="control-label col-sm-3" for="aggregateType">Group By</label>'+
'<div class="col-sm-8">'+
'<select id="groupByColumn" name="aggregateType" class="input-xlarge form-control">'+
'<option>None</option>'+
'</select>'+
'</div></div>');
var columns = $("#results").handsontable("getColHeader");
// console.log(columns);
for (i = 0; i < columns.length; i++) {
$("#groupByColumn").append('<option>'+columns[i]+'</option>');
}

}

function addAggregateOption (rowNumber) {
$('#aggregateSelectionDiv').append("</br>"+generateAggregateSection(rowNumber));
}

function generateQuery () {
numRows = $(".aggregateInputSection").length;
var query = "SELECT ";
if ($("#groupByColumn").val() != "None") {
query += $("#groupByColumn").val() +", ";
}
for (i = 0; i < numRows; i++) {
var currentaggregateColumn = $("#aggregateColumn"+i).val();
var currentaggregateType = $("#aggregateType"+i).val();
if ($("#aggregateType"+i).val()=='None') {
query+= $("#aggregateColumn"+i).val()+" ";
} else {
if ($("#aggregateType"+i).val()=='Sum') {
query+= "sum(cast("+$("#aggregateColumn"+i).val()+" as float)) ";
} else if ($("#aggregateType"+i).val()=='Average') {
query+= "avg(cast("+$("#aggregateColumn"+i).val()+" as float)) ";
} else {
query+= $("#aggregateType"+i).val()+"("+$("#aggregateColumn"+i).val()+") ";
}
query += "as "+currentaggregateType+currentaggregateColumn+" ";
}
if(i<numRows-1) {
query+=",";
}
}
query+="from "+fullTableName;
if ($("#groupByColumn").val() !="None") {
query+=" group by "+$("#groupByColumn").val();
}
$("#aggregateModal").find(".go_button").unbind("click");
$("#aggregateModal").find(".go_button").hide(); //TODO close modal and switch to other context
return query;
}

function executeAggregateQuery(query) {
res = executeQuery(query)
var aggregateModal = $("#aggregateModal");

aggregateModal.find(".modal-body").html("<table id='aggregateResults'></table>");
var data = res.tuples.map(function (tuple) { return tuple.cells; });
var columnNames = res.field_names;
$('#aggregateResults').handsontable({
data: data,
minSpareRows: 1,
colHeaders: columnNames,
contextMenu: true
});
aggregateModal.find(".modal-title").html('<div id="createTable"><button type="button" class="btn btn-default" id="save_button">Save as new Table</button></div>');
$("#save_button").click (function () {
aggregateModal.find(".modal-header").addClass("aggregateHeaderTable");
$("#createTable").html('<div class="col-sm-8"><input class="form-control" id="newNameInput" placeholder="Type New Name Here"></div><button type="button" class="btn btn-default col-sm-3" id="save_button">Save</button>');
$("#save_button").click(function() {
aggregateModal.find(".modal-header").removeClass("aggregateHeaderTable");
var newName = $("#newNameInput").val();
newquery="CREATE TABLE "+accountName+"."+repoName+"."+newName+" AS ("+ query+")";
executeQuery(newquery);
aggregateModal.find(".modal-title").html("Table "+newName+' successfully created!');
$("#openNewTableButton").html("Open "+newName);
$("#openNewTableButton").show();
$("#openNewTableButton").click(function () {
updateCurrentTable(repoName,newName);
});
updateRepo(repoName, fullTableName);
});
});
}

// function executeQuery(query) {
// transport = new Thrift.Transport("http://datahub.csail.mit.edu/service/json"),
// protocol = new Thrift.Protocol(transport),
// client = new DataHubClient(protocol),
// con_params = new ConnectionParams({'user': accountName, 'password': password}),
// con = client.open_connection(con_params),
// res = client.execute_sql(con, query);
// return res;
// }

115 changes: 115 additions & 0 deletions HandsOnTable/alter_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
$(document).ready(function(){
$('#schemaModal').find("#submitAlts").click(function() {
schemaAlts = [];
// find what changed
var res = client.get_schema(con, fullTableName);
res.tuples.forEach(function (tuple, index) {
if (tuple.cells[1] !== $('#dropdown' + index).text()) {
schemaAlts.push({ col: tuple.cells[0], newType: $('#dropdown' + index).text() });
}
});
schemaAlts.forEach(function (schemaAlt) {
viewQuery = buildGetViewDefQuery(fullTableName + '_view');
console.log(schemaAlt);
var colName = schemaAlt['col'];
executeSQL('begin;', function (res) {
queryList = [buildAddColumnQuery(fullTableName, 'temp', schemaAlt.newType)];
queryList.push(buildUpdateQuery(fullTableName, {'temp' : 'cast(' + schemaAlt.col + ' as ' + schemaAlt.newType + ')'}));
queryList.push(buildDropColumnQuery(fullTableName, schemaAlt.col, true));
queryList.push(buildAddColumnQuery(fullTableName, schemaAlt.col, schemaAlt.newType));
changesObj = {};
changesObj[schemaAlt.col] = 'temp';
queryList.push(buildUpdateQuery(fullTableName, changesObj));
queryList.push(buildDropColumnQuery(fullTableName, 'temp'));
queryList.push(buildCreateViewQuery(fullTableName + '_view', viewQuery));
executeSQL(queryList.join('; '), function (res) {
// commit the transaction
executeSQL('commit;', function (res) {
// TODO (jennya)
return;
}, function (err) {
// TODO (jennya)
return;
});
}, function (err) {
// abort the transaction
console.log('aborting...');
executeSQL('rollback;', function (res) {
// TODO (jennya)
return;
}, function (err) {
// TODO (jennya)
return;
});
});
}, function (err) {
// TODO (jennya)
return;
});
});
});
$(document).on('click', '#alterSchemaButton', function() {
$('#schemaModal').find('.modal-title').html("Alter the Schema");
$('#schemaModal').find('.modal-body').html('<table class="table table-striped" id="schemaList"></table>');

var res = client.get_schema(con, fullTableName);
res.tuples.forEach(function (tuple, index) {
// console.log(tuple);
$('#schemaModal').find('#schemaList').append(
'<tr>' +
'<td>' +
tuple.cells[0] +
'</td>' +
'<td>' +
'<div class="btn-group"> <a id="dropdown' + index + '" class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="#">' + tuple.cells[1] + '<span class="caret"></span></a>' +
'<ul class="dropdown-menu">' +
'<li><a href="#">bigint</a></li>' +
'<li><a href="#">bit</a></li>' +
'<li><a href="#">decimal</a></li>' +
'<li><a href="#">int</a></li>' +
'<li><a href="#">money</a></li>' +
'<li><a href="#">numeric</a></li>' +
'<li><a href="#">smallint</a></li>' +
'<li><a href="#">smallmoney</a></li>' +
'<li><a href="#">tinyint</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">float</a></li>' +
'<li><a href="#">real</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">date</a></li>' +
'<li><a href="#">datetime2</a></li>' +
'<li><a href="#">datetime</a></li>' +
'<li><a href="#">datetimeoffset</a></li>' +
'<li><a href="#">smalldatetime</a></li>' +
'<li><a href="#">time</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">char</a></li>' +
'<li><a href="#">text</a></li>' +
'<li><a href="#">varchar</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">nchar</a></li>' +
'<li><a href="#">ntext</a></li>' +
'<li><a href="#">nvarchar</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">binary</a></li>' +
'<li><a href="#">image</a></li>' +
'<li><a href="#">varbinary</a></li>' +
'<li class="divider"></li>' +
'<li><a href="#">cursor</a></li>' +
'<li><a href="#">hierarchyid</a></li>' +
'<li><a href="#">sql_variant</a></li>' +
'<li><a href="#">table</a></li>' +
'<li><a href="#">timestamp</a></li>' +
'<li><a href="#">uniqueidentifier</a></li>' +
'<li><a href="#">xml</a></li>' +
'</ul>' +
'</div>' +
'</td>' +
'</tr>');
});
});
$(document).on('click', '.dropdown-menu li a', function(){
var selText = $(this).text();
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+'<span class="caret"></span>');
});
});
Loading