-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Url shortener #5497
Merged
Merged
Url shortener #5497
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25c6627
[url shortener] initial prototype
BigFunger 5783f31
[url shortener] modified to use hash
BigFunger 7fcef6b
Addressed how modules were defined
BigFunger cef5afd
[url shortener] Can now share urls without app state in url
BigFunger 1049279
[url shortener] hides the embed section on the discover tab
BigFunger 0fb1450
[url shortener] reorganized code, added clipboard.js support
BigFunger e59750c
[url shortener] removed link-to-saved-object functionality
BigFunger 96b7404
[url shortener] Added clipboard cleanup, fixed server logging
BigFunger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,4 @@ | ||
<form role="form" class="vis-share"> | ||
|
||
<p> | ||
<div class="input-group"> | ||
<label> | ||
Embed this dashboard | ||
<small>Add to your html source. Note all clients must still be able to access kibana</small> | ||
</label> | ||
<div class="form-control" disabled>{{opts.shareData().embed}}</div> | ||
</div> | ||
</p> | ||
|
||
<p> | ||
<div class="input-group"> | ||
<label> | ||
Share a link | ||
</label> | ||
<div class="form-control" disabled>{{opts.shareData().link}}</div> | ||
</div> | ||
</p> | ||
</form> | ||
<share | ||
object-type="dashboard" | ||
object-id="{{opts.dashboard.id}}"> | ||
</share> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/plugins/kibana/public/discover/partials/share_search.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<share | ||
object-type="search" | ||
object-id="{{opts.savedSearch.id}}" | ||
allow-embed="false"> | ||
</share> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 4 additions & 22 deletions
26
src/plugins/kibana/public/visualize/editor/panels/share.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,4 @@ | ||
<form role="form" class="vis-share"> | ||
|
||
<p> | ||
<div class="form-group"> | ||
<label> | ||
Embed this visualization. | ||
<small>Add to your html source. Note all clients must still be able to access kibana</small> | ||
</label> | ||
<div class="form-control" disabled>{{conf.shareData().embed}}</div> | ||
</div> | ||
</p> | ||
|
||
<p> | ||
<div class="form-group"> | ||
<label> | ||
Share a link | ||
</label> | ||
<div class="form-control" disabled>{{conf.shareData().link}}</div> | ||
</div> | ||
</p> | ||
|
||
</form> | ||
<share | ||
object-type="visualization" | ||
object-id="{{conf.savedVis.id}}"> | ||
</share> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const crypto = require('crypto'); | ||
|
||
export default function (server) { | ||
async function updateMetadata(urlId, urlDoc) { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
try { | ||
await client.update({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId, | ||
body: { | ||
doc: { | ||
'accessDate': new Date(), | ||
'accessCount': urlDoc._source.accessCount + 1 | ||
} | ||
} | ||
}); | ||
} catch (err) { | ||
server.log('Warning: Error updating url metadata', err); | ||
//swallow errors. It isn't critical if there is no update. | ||
} | ||
} | ||
|
||
async function getUrlDoc(urlId) { | ||
const urlDoc = await new Promise((resolve, reject) => { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
client.get({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId | ||
}) | ||
.then(response => { | ||
resolve(response); | ||
}) | ||
.catch(err => { | ||
resolve(); | ||
}); | ||
}); | ||
|
||
return urlDoc; | ||
} | ||
|
||
async function createUrlDoc(url, urlId) { | ||
const newUrlId = await new Promise((resolve, reject) => { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
client.index({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId, | ||
body: { | ||
url, | ||
'accessCount': 0, | ||
'createDate': new Date(), | ||
'accessDate': new Date() | ||
} | ||
}) | ||
.then(response => { | ||
resolve(response._id); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
}); | ||
|
||
return newUrlId; | ||
} | ||
|
||
function createUrlId(url) { | ||
const urlId = crypto.createHash('md5') | ||
.update(url) | ||
.digest('hex'); | ||
|
||
return urlId; | ||
} | ||
|
||
return { | ||
async generateUrlId(url) { | ||
const urlId = createUrlId(url); | ||
|
||
const urlDoc = await getUrlDoc(urlId); | ||
if (urlDoc) return urlId; | ||
|
||
return createUrlDoc(url, urlId); | ||
}, | ||
async getUrl(urlId) { | ||
try { | ||
const urlDoc = await getUrlDoc(urlId); | ||
if (!urlDoc) throw new Error('Requested shortened url does note exist in kibana index'); | ||
|
||
updateMetadata(urlId, urlDoc); | ||
|
||
return urlDoc._source.url; | ||
} catch (err) { | ||
return '/'; | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const app = require('ui/modules').get('kibana'); | ||
|
||
app.directive('share', function () { | ||
return { | ||
restrict: 'E', | ||
scope: { | ||
objectType: '@', | ||
objectId: '@', | ||
setAllowEmbed: '&?allowEmbed' | ||
}, | ||
template: require('ui/share/views/share.html'), | ||
controller: function ($scope) { | ||
$scope.allowEmbed = $scope.setAllowEmbed ? $scope.setAllowEmbed() : true; | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const app = require('ui/modules').get('kibana'); | ||
const Clipboard = require('clipboard'); | ||
|
||
require('../styles/index.less'); | ||
|
||
app.directive('shareObjectUrl', function (Private, Notifier) { | ||
const urlShortener = Private(require('../lib/url_shortener')); | ||
|
||
return { | ||
restrict: 'E', | ||
scope: { | ||
getShareAsEmbed: '&shareAsEmbed' | ||
}, | ||
template: require('ui/share/views/share_object_url.html'), | ||
link: function ($scope, $el) { | ||
const notify = new Notifier({ | ||
location: `Share ${$scope.$parent.objectType}` | ||
}); | ||
|
||
$scope.textbox = $el.find('input.url')[0]; | ||
$scope.clipboardButton = $el.find('button.clipboard-button')[0]; | ||
|
||
const clipboard = new Clipboard($scope.clipboardButton, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need to be cleaned up on destroy? |
||
target(trigger) { | ||
return $scope.textbox; | ||
} | ||
}); | ||
|
||
clipboard.on('success', e => { | ||
notify.info('URL copied to clipboard.'); | ||
e.clearSelection(); | ||
}); | ||
|
||
clipboard.on('error', () => { | ||
notify.info('URL selected. Press Ctrl+C to copy.'); | ||
}); | ||
|
||
$scope.$on('$destroy', () => { | ||
clipboard.destroy(); | ||
}); | ||
|
||
$scope.clipboard = clipboard; | ||
}, | ||
controller: function ($scope, $location) { | ||
function updateUrl(url) { | ||
$scope.url = url; | ||
|
||
if ($scope.shareAsEmbed) { | ||
$scope.formattedUrl = `<iframe src="${$scope.url}" height="600" width="800"></iframe>`; | ||
} else { | ||
$scope.formattedUrl = $scope.url; | ||
} | ||
|
||
$scope.shortGenerated = false; | ||
} | ||
|
||
$scope.shareAsEmbed = $scope.getShareAsEmbed(); | ||
|
||
$scope.generateShortUrl = function () { | ||
if ($scope.shortGenerated) return; | ||
|
||
urlShortener.shortenUrl($scope.url) | ||
.then(shortUrl => { | ||
updateUrl(shortUrl); | ||
$scope.shortGenerated = true; | ||
}); | ||
}; | ||
|
||
$scope.getUrl = function () { | ||
return $location.absUrl(); | ||
}; | ||
|
||
$scope.$watch('getUrl()', updateUrl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use $on('$locationChangeSuccess' instead? |
||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require('./directives/share'); | ||
require('./directives/share_object_url'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts on letting the server throw instead of redirecting to /? similar to how url/app/asdf will throw an invalid app error, or other pages give 404s