apisearch-events-ui
Version:
Javascript User Interface to visualize all events data.
95 lines (89 loc) • 2.13 kB
JavaScript
import cloneDeep from 'clone-deep';
import ApisearchDispatcher from '../../dispatcher';
/**
* Get number of queries with results or without results
* depending
*
* This action is triggered when mounting a component
* receives two parameters:
* @param queryOptions -> given new query options
* @param appOptions -> current application options
*
* Finally dispatches an event with the modified query.
* @returns {{
* type: string,
* payload: {
* updatedQuery,
* result
* }
* }}
*/
export function getLastQueriesAction(
{
eventType,
from,
to,
queryType
},
{
currentQuery,
client
}
) {
const clonedQuery = cloneDeep(currentQuery);
const resultsRange = resolveSyntax(queryType);
clonedQuery
.filterUniverseByDateRange(
'occurred_on_day',
[`${from}..${to}`]
)
.filterByTypes(
[eventType],
true,
['_term', 'asc']
)
.filterByRange(
'q_length',
'q_length',
[],
[resultsRange],
8
)
.aggregateBy(
'occurred_on_day',
'occurred_on_day',
8,
['_term', 'asc']
)
;
client.events(clonedQuery.toJSON(), (result, error) => {
if (error) return;
ApisearchDispatcher.dispatch({
type: (queryType === 'queries_with_results')
? 'RENDER_QUERIES_WITH_RESULTS'
: 'RENDER_QUERIES_WITH_NO_RESULTS'
,
payload: {
updatedQuery: clonedQuery,
result
}
})
})
}
/**
* Syntax sugar resolver
*
* @param queryType
*/
function resolveSyntax(queryType) {
if (queryType === 'queries_with_no_results') {
return "0..1";
}
if (queryType === 'queries_with_results') {
return "1.."
}
throw new Error(`
Parameter QueryType must be a string
named "queries_with_results" or "queries_with_no_results".
`);
}