-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5497 from BigFunger/url-shortener
Url shortener
- Loading branch information
Showing
17 changed files
with
326 additions
and
62 deletions.
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, { | ||
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); | ||
} | ||
}; | ||
}); |
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.