Develop with LCC - Infomaker/Dashboard-Plugin GitHub Wiki
Using LCC Agent
Getting started
The first thing you need to do is to request a helper LCC class to aid you when performing Open Content queries or streaming QueryStreamer events.
Wait for the LCC plugin to be ready and then send the event io.infomaker.lcc:getLCCLib
. You'll get an io.infomaker.lcc:LCCLib
event containing the LCC class in reply. Instantiate the new LCC and hit start()
.
this.ready('io.infomaker.lcc', () => {
this.on('io.infomaker.lcc:LCCLib', userData => {
let LCC = userData.LCC
this.lcc = new LCC({
plugin : this,
onLccReadyStateChange : this.onLccReadyStateChange.bind(this)
})
this.lcc.start()
})
this.send('io.infomaker.lcc:getLCCLib', {v:1})
})
When the state of LCC change the onLccReadyStateChange()
callback will be called with true
or false
as a single argument.
onLccReadyStateChange(lccReady){
if(lccReady){
// Here you can restore saved queries etc..
}
}
Open Content
To access Open Content you call lcc.opencontent()
with the name of content provider you want to use (from config).
Access to search
and suggest
is simplified by two helper functions, but any OC REST API endpoint can be reached using the request
function.
The functions returns promises.
Open Content API Documentation
Search
let searchQuery = {
q: "*:*"
}
this.lcc.opencontent('im-demo').search(searchQuery).then(searchResult => {
console.log(searchResult)
})
Suggest
let suggestQuery = {
'field': ['Authors'],
'incompleteWord': ['P'],
'type': 'ngram'
}
this.lcc.opencontent('im-demo').suggest(suggestQuery).then(suggestResult => {
console.log(suggestResult);
})
Generic OC API request
let params = {
'endpoint': 'eventlog',
'method': 'GET',
'qs': {
'event' : -1
}
}
this.lcc.opencontent('im-demo').request(params).then(eventlogRes => {
console.log(eventlogRes)
})
QueryStreamer
To handle streams in QueryStreamer you call lcc.querystreamer()
with the name of content provider you want to use (from config), createStream and deleteStream is enabled on this instance.
QueryStreamer API Documentation
ElasticSearch Query Format Documentation
Cleanup
When a stream is no longer needed, delete it. Please don't forget call deleteStream(streamId)
in your componentWillUnmount
.
Create stream
When calling create stream, pass a request object and a callback function as parameters. This will return a promise that resolves with a stream ID. New events will land in the callback function you provided.
const BUNDLE = "@plugin_bundle";
const VERSION = "@plugin_version";
const onStreamEvent = (event) => {
const ocEvent = event.data.payload.result
const noLongerMatching = event.data.payload.parameters.noLongerMatchingQuery
}
const req = {
query : {
query_string: {
query : '*:*'
}
},
config : {
'notifyOnNoLongerMatchingQuery' : true
},
meta : {
origin : { // Add bundle and version to meta.origin. This helps when troubleshooting.
bundle : BUNDLE,
version : VERSION
}
}
}
const querystreamer = this.lcc.querystreamer('im-demo')
return querystreamer.createStream(req, onStreamEvent).then(res => {
this.streamId = res.streamId
})
Delete stream
this.lcc.querystreamer('im-demo').deleteStream(this.streamId)