forked from hull-ships/hull-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected.html
More file actions
executable file
·242 lines (220 loc) · 9.06 KB
/
connected.html
File metadata and controls
executable file
·242 lines (220 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<html><head>
<title>Hull SQL</title>
<link rel="stylesheet" href="//dd04rofzygnm0.cloudfront.net/releases/master/865f04865d2448286626bac92c518a8f8ea8bafe/stylesheets/neue.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css">
</head>
<body>
<div class="row">
<div class="col-xs-offset-1 col-xs-10 col-lg-10 col-lg-offset-1 panel mt-2">
<div class="panel-body">
<div class="mb-1 ps-3">
<h1 class="mb-0 mt-05 text-center"><i class="icon icon-hull" style="font-size:64px;"></i> </h1>
<div class="mb-1 media pb-1">
<div class="media-left">
<div class="media-object pr-1 pt-1">
<div class="check valid"><i class="icon icon-valid"></i></div>
</div>
</div>
<div class="media-body pt-1">
<h4 class="m-0 pt-05 text-muted">Connected to your <%= db_type %> database</h4>
</div>
</div>
<% if (last_sync_at) { %>
<h5>Last Sync <small class="pull-right">on <span data-time=""><%= last_sync_at %></span></small></h5>
<% } %>
<hr>
<div class="row">
<div class="col-sm-12">
<h4 class="mt-0 text-muted">SQL Query</h4>
<textarea name="" rows="15" id="querying" class="form-control"><%= query %></textarea>
<div class="pt-1 pb-1 text-muted">
<h6>How to write queries</h6>
<p>
Valid queries MUST expose an `external_id` column or an `email` column. Lines with no `external_id` or `email` will be ignored.
<br />
All other fields will be imported as traits on the matching users.
<br />
For example, the following query will map the column `users.id` to your Hull users' `external_id`.
<pre><code>
SELECT user_id as external_id, plan_name, monthly_amount FROM users_subscriptions
</code></pre>
</p>
<h6>Using incremental queries</h6>
<p>
You can run incremental queries based on the date of the last sync.
<br />
The query will be rewritten to replace <code>:last_updated_at</code> with the date of the last successful sync operation.
<br /><br />
For example to import entries from a <code>users</code> table with a date column named <code>updated_at</code>, you can write :
<pre><code>
SELECT id as external_id, email as email, firstname as first_name
FROM users
WHERE updated_at >= :last_updated_at
</code></pre>
</p>
</div>
<div class="pt-1 pb-1">
<button id="button_preview" class="btn-pill btn-rounded btn-success btn to-disable"><i class="icon icon-eye_open"></i> Preview</button>
<div class="btn-group pl-1 pull-right">
<button id="button_import" class="btn-pill btn-rounded btn-danger btn to-disable"><i class="icon icon-reset"></i> Import everything</button>
</div>
</div>
</div>
</div>
<hr class="afterPreview">
<h4 class="text-muted afterPreview">Results Preview <span id="results-title"></span></h4>
<p id="error-query"></p>
<table id="result" class="afterPreview table table-striped table-responsive table-bordered table-condensed">
<thead>
<tr></tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<style type="text/css" media="screen">
.afterPreview, #error-query {
display: none;
}
.check i.icon {
color: rgba(75, 222, 122, 0.52);
font-size: 2pc;
position: relative;
top: -11px;
left: 3px;
}
.check.valid {
border-color: rgba(75, 222, 122, 0.52);
}
.check {
border-radius: 100px;
border: 2px solid transparent;
padding: 0 5px;
height: 37px;
width: 37px;
position: relative;
}
</style>
<script>
var original_query = `<%- query %>`;
var good_query;
$(function() {
$('.to-disable').prop('disabled', false);
$('#button_import').click(function () {
if ($('#querying').val() !== original_query) {
swal('Unsaved query', 'The current query is not the query you saved. Please save your query first.', 'warning');
} else {
swal({
title: 'Import the users from the current query? ',
text: "If you continue, we will import the users from the currently saved query.",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Let\'s Go',
closeOnConfirm: false
}, function (isConfirm) {
if (isConfirm === true) {
$('.afterPreview').css('display', 'none');
$('#button_import').prop('disabled', true);
$('#button_import').text('Importing...');
$('#result thead tr').empty();
$('#result tbody').empty();
$('#error-query').empty();
$('#results-title').empty();
swal('Started importing users. Results will be available shortly in Hull!');
$.ajax({
url: '/import' + window.location.search,
type: 'post',
data: {
query: $('#querying').val(),
incremental: true
},
success: function (data) {
$('.to-disable').prop('disabled', false);
$('#button_import').replaceWith('<button id="button_import" class="btn-pill btn-rounded btn-danger btn to-disable"><i class="icon icon-reset"></i> Import everything</button>');
},
error: function (err) {
$('.to-disable').prop('disabled', false);
$('#error-query').empty().css('display', 'block').append(err.message);
}
});
}
});
}
});
$('#button_preview').click(function () {
$('.afterPreview').css('display', 'none');
$('.to-disable').prop('disabled', true);
$('#result thead tr').empty();
$('#result tbody').empty();
$('#error-query').empty();
$('#results-title').empty();
good_query = null;
$.ajax({
url: '/run' + window.location.search,
type: 'post',
data: {
query: $('#querying').val()
},
success: function (data) {
$('.to-disable').prop('disabled', false);
$('.afterPreview').css('display', 'block');
try {
if (data.entries && data.entries.length) {
for (var columnName in data.entries[0]) {
$('#result thead tr').append('<th>' + columnName + '</th>');
}
data.entries.forEach(function(element, index) {
var currentRow = '';
$.each(element, function(key , value) {
currentRow = currentRow.concat('<td>' + value + '</td>');
});
$('#result tbody').append('<tr>' + currentRow + '<tr>');
});
} else {
$('#error-query').empty().css('display', 'block').append('No results for this query.');
}
good_query = $('#querying').val();
} catch (err) {
good_query = original_query;
$('#error-query').empty().css('display', 'block').append(data.message);
} finally {
if (good_query !== original_query) {
window.parent.postMessage(JSON.stringify({
from: 'embedded-ship',
action: 'update',
ship: {
private_settings: {
query: good_query
}
}
}), '*');
}
}
},
error: function (res) {
var err = res.responseJSON;
$('.to-disable').prop("disabled", false);
if (err) {
$("#error-query").empty().css("display", "block").append(err.message);
good_query = original_query;
window.parent.postMessage(JSON.stringify({
from: "embedded-ship",
action: "update",
ship: {
private_settings: {
query: good_query
}
}
}), "*");
}
}
});
});
});
</script>
</body></html>