Facade for the reporting and metrics subsystem, covering saved queries, tables, metrics and the organisation and supplier filters used to scope a report to the current user. Delegates most of its work to QueryService and ApplicationManager. Registered in the templating data model and available to server-side JS as "queryManager", so most of its exported methods form the API a report template or JS controller uses to run a query, calculate a metric or resolve the currently selected organisations.

Group: Managers


Properties

PropertyReturnsDescription
availableOrgIdsList<Long>The internal ids of the organisations the current user has the ReportingAccess role on. This list is not expanded, so it does not include child organisations of those.
availableOrgsList<Organisation>The organisations directly available to the current user, ie where the user has the ReportingAccess role on that org, sorted by formatted name. This is a non-expanded list, so it does not include child organisations. This is an expensive operation as it resolves each organisation id individually.
availableSuppliersList<Organisation>Finds the supplier organisations available to the current user, based on the SupplierFilter role.
commonFinishDateDateThe end of the common reporting date range for the current request, typically resolved from a request parameter or cookie.
commonStartDateDateThe start of the common reporting date range for the current request, typically resolved from a request parameter or cookie.
currentTeamOrgOrganisationThe single organisation, of those currently selected by the current user, that should represent the user's team. Where multiple organisations are selected, the first resolved one is returned.
lagFinishDateDateThe common finish date shifted back by the configured reporting lag, used where a metric or query should be evaluated on data that has had time to settle.
lagStartDateDateThe common start date shifted back by the configured reporting lag, used where a metric or query should be evaluated on data that has had time to settle.
lookupTablesList<LookupTableBean>The lookup tables configured for the current account, used to enrich or translate values in reports.
metricsList<Metric>All configured metric definitions.
metricTypesList<MetricType>All metric types registered by installed applications, which define the calculation behaviour available to metric definitions.
orCreateReportParamsReportParamsReturns the current thread-bound report parameters, creating and binding an empty set if none exist yet.
paramsMap<String,Object>The report query parameters resolved from the current HTTP request.
queriesList<QueryBean>The saved queries available for reporting, combining the configured queries with any contributed by installed query applications, sorted by name.
queriesConfigQueriesThe raw queries configuration for the current account, including its tables, saved queries and lookup tables.
queryParametersList<QueryParameter>The static and query-specific parameters that can be substituted into a query definition, such as startDate, endDate and the currently selected organisations. Sourced from the query service's static parameter list.
selectedOrgIdsList<Long>The ids of the organisations currently selected by the current user for reporting, sorted numerically.
selectedOrgsList<OrgData>The organisation(s) currently selected by the current user, resolved as child organisations of the tenant's root organisation, excluding any that have been deleted.
selectedSupplierIdsList<Long>The ids of the supplier organisations currently selected by the current user, sorted numerically.
tablesList<Table>All tables available for reporting: the configured tables, tables contributed by installed query applications, and a results table (plus one per aggregation) generated for each saved query that has fields or aggregations chosen. This is an expensive operation since it assembles tables from several sources on each call.

Methods

getQueryParameters() · toReportBean(DirectoryNode rootDir, FileNode fn) · saveMetrics(List<Metric> metrics) · saveMetric(Metric m) · newMetricBuilder(String id, String metricTypeId) · deleteMetric(String metricId) · newCalcContext() · metric(String name) · calcMetric(Metric m) · calcMetric(Metric m, CalcContext cc) · calcMetric(Metric m, Date defaultStartDate, Date defaultEndDate, CalcContext calcContext) · calcMetric(Metric m, BaseEntity entity, Date defaultStartDate, Date defaultEndDate, CalcContext calcContext) · metricType(String id) · findApplicableMetrics(NamedObject source) · newRowsResult() · newRowsResult(List<String> headers) · getCommonStartDate() · getCommonFinishDate() · getLagStartDate() · getLagFinishDate() · getMetrics() · getMetricTypes() · getTables() · getTable(String name) · getQueriesConfig() · getQueries() · getLookupTables() · calcInterval(Date startDate, Date endDate) · queryParams() · queryParams(Date startDate, Date endDate) · queryParams(Date startDate, Date endDate, Map extraParams) · runQuery(String queryName) · runQuery(String queryName, Integer from, Integer pageSize) · runQuery(String queryName, Map<String,String> params) · runQuery(String queryName, Integer from, Integer pageSize, Map<String,String> params) · getAvailableSuppliers() · availableSuppliers(Profile user) · getSelectedSupplierIds() · selectedSupplierIds(Profile user) · getSelectedOrgIds() · selectedOrgIds(Profile user) · selectedOrgIDsStr() · getAvailableOrgIds() · availableOrgIds(Profile user) · getAvailableOrgs() · getAvailableOrgIds(Group group) · getAvailableOrgIds(List<Group> groups) · getSelectedOrgs() · getCurrentTeamOrg() · teamOrg(Profile user) · selectedOrgHasType(String orgType) · preProcessQuery(String json) · preProcessQuery2(String json) · preProcessQuery2(String json, Map<String,Object> params) · getParams() · saveTable(KCriteriaTable t) · getOrCreateReportParams() · getOrCreateReportParams(Function<ReportParams,T> callback) · enqueueQueryTableExport(String queryTableName, Date start, Date finish, List<Long> orgIds, Map<String,String> params) · exportTaskName(String queryTableName) · createDefaultQuery(String esFieldName, String operator, Object value) · createDefaultQuery(String esFieldName, String operator, Object value, boolean reverseRange) · findFieldOperatorsByType(String fieldType) · findActualFieldType(String fieldType) · processRows(String indexType, List<String> fields, List rows) · processRows(String indexType, List<String> fields, boolean defaultFieldsFormatting, List rows)

getQueryParameters()

Returns: List<QueryParameter>

The static and query-specific parameters that can be substituted into a query definition, such as startDate, endDate and the currently selected organisations. Sourced from the query service's static parameter list.

toReportBean(DirectoryNode rootDir, FileNode fn)

Returns: ReportBean

Reads a report definition file and converts it into a ReportBean describing its name, title and content. Wraps the underlying IOException as a RuntimeException.

ParameterDescription
rootDirthe root directory the report file is resolved relative to
fnthe report definition file to read

saveMetrics(List<Metric> metrics)

Returns: void

Persists a batch of metric definitions. Does nothing, and logs a message, if the list is null or empty.

ParameterDescription
metricsthe metric definitions to save

saveMetric(Metric m)

Returns: void

Persists a single metric definition. Does nothing, and logs a message, if the metric is null.

ParameterDescription
mthe metric definition to save

newMetricBuilder(String id, String metricTypeId)

Returns: MetricBuilder

Starts a new fluent builder for a metric of the given type, identified by the given id.

ParameterDescription
idthe unique id for the new metric
metricTypeIdthe id of the metric type which defines how the metric is calculated

deleteMetric(String metricId)

Returns: void

Deletes the metric definition with the given id.

ParameterDescription
metricIdthe id of the metric to delete

newCalcContext()

Returns: CalcContext

Creates a new, empty calculation context used to track recursion depth while calculating metrics.

metric(String name)

Returns: Metric

Looks up a metric definition by its name.

ParameterDescription
namethe name of the metric to find

calcMetric(Metric m)

Returns: BigDecimal

Calculates the value of a metric using the common date range and no specific entity. This is an expensive operation, typically involving a search query against the underlying data.

ParameterDescription
mthe metric to calculate

calcMetric(Metric m, CalcContext cc)

Returns: BigDecimal

Calculates the value of a metric using the common date range and no specific entity. This is an expensive operation, typically involving a search query against the underlying data.

ParameterDescription
mthe metric to calculate
ccthe calculation context used to capture key information, and guard against recursive metrics, during calculation

calcMetric(Metric m, Date defaultStartDate, Date defaultEndDate, CalcContext calcContext)

Returns: BigDecimal

Calculates the value of a metric for the given date range. The metric's own date mode may override the given dates, for example to use a constant range or the previous period instead. This is an expensive operation, typically involving a search query against the underlying data.

ParameterDescription
mthe metric to calculate
defaultStartDatethe start of the date range to use unless the metric's date mode overrides it
defaultEndDatethe end of the date range to use unless the metric's date mode overrides it
calcContextthe calculation context used to capture key information, and guard against recursive metrics, during calculation

calcMetric(Metric m, BaseEntity entity, Date defaultStartDate, Date defaultEndDate, CalcContext calcContext)

Returns: BigDecimal

Calculates the value of a metric for a specific entity and date range. The metric's own date mode may override the given dates, for example to use a constant range or the previous period instead. This is an expensive operation, typically involving a search query against the underlying data.

ParameterDescription
mthe metric to calculate
entitythe entity the metric is calculated against
defaultStartDatethe start of the date range to use unless the metric's date mode overrides it
defaultEndDatethe end of the date range to use unless the metric's date mode overrides it
calcContextthe calculation context used to capture key information, and guard against recursive metrics, during calculation

metricType(String id)

Returns: MetricType

Looks up a registered metric type by its id.

ParameterDescription
idthe id of the metric type to find

findApplicableMetrics(NamedObject source)

Returns: List<Metric>

Finds the metrics applicable to a given source object, such as a reward or some other item which metrics can apply to. Looks the source up by its class name and object name in the configured queries.

ParameterDescription
sourcea reward, or potentially some other item which metrics can apply to

newRowsResult()

Returns: RowsResult

Creates an empty rows result with no headers, useful as a default when a table query yields no data.

newRowsResult(List<String> headers)

Returns: RowsResult

Creates an empty rows result with the given column headers, useful as a default when a table query yields no data but the headers still need to be rendered.

ParameterDescription
headersthe column headers to include in the result

getCommonStartDate()

Returns: Date

The start of the common reporting date range for the current request, typically resolved from a request parameter or cookie.

getCommonFinishDate()

Returns: Date

The end of the common reporting date range for the current request, typically resolved from a request parameter or cookie.

getLagStartDate()

Returns: Date

The common start date shifted back by the configured reporting lag, used where a metric or query should be evaluated on data that has had time to settle.

getLagFinishDate()

Returns: Date

The common finish date shifted back by the configured reporting lag, used where a metric or query should be evaluated on data that has had time to settle.

getMetrics()

Returns: List<Metric>

All configured metric definitions.

getMetricTypes()

Returns: List<MetricType>

All metric types registered by installed applications, which define the calculation behaviour available to metric definitions.

getTables()

Returns: List<Table>

All tables available for reporting: the configured tables, tables contributed by installed query applications, and a results table (plus one per aggregation) generated for each saved query that has fields or aggregations chosen. This is an expensive operation since it assembles tables from several sources on each call.

getTable(String name)

Returns: Table

Finds a table, by id, from the combined list returned by getTables. Logs a warning and returns null if no table with that id exists.

ParameterDescription
namethe table id to find

getQueriesConfig()

Returns: Queries

The raw queries configuration for the current account, including its tables, saved queries and lookup tables.

getQueries()

Returns: List<QueryBean>

The saved queries available for reporting, combining the configured queries with any contributed by installed query applications, sorted by name.

getLookupTables()

Returns: List<LookupTableBean>

The lookup tables configured for the current account, used to enrich or translate values in reports.

calcInterval(Date startDate, Date endDate)

Returns: String

Returns an appropriate date histogram aggregation interval (for example "day" or "month") for the given date range, chosen so the number of buckets stays reasonable.

ParameterDescription
startDatethe start of the range to bucket
endDatethe end of the range to bucket

queryParams()

Returns: Map<String,Object>

The current query parameters resolved from the current HTTP request, such as the selected date range and organisations.

queryParams(Date startDate, Date endDate)

Returns: Map<String,Object>

The current query parameters resolved from the current HTTP request, overriding the resolved date range with the given start and end dates.

ParameterDescription
startDatethe start date to use in place of the request-resolved value
endDatethe end date to use in place of the request-resolved value

queryParams(Date startDate, Date endDate, Map extraParams)

Returns: Map<String,Object>

The current query parameters resolved from the current HTTP request, overriding the resolved date range with the given start and end dates, and merging in the given extra parameters.

ParameterDescription
startDatethe start date to use in place of the request-resolved value
endDatethe end date to use in place of the request-resolved value
extraParamsadditional parameters to merge into the resolved map

runQuery(String queryName)

Returns: KSearchResponse

Runs a saved query by name, using the query parameters resolved from the current HTTP request, with no paging applied. This is an expensive operation as it executes a search against the underlying index.

ParameterDescription
queryNamethe name of the saved query to run

runQuery(String queryName, Integer from, Integer pageSize)

Returns: KSearchResponse

Runs a saved query by name, using the query parameters resolved from the current HTTP request, with the given paging. This is an expensive operation as it executes a search against the underlying index.

ParameterDescription
queryNamethe name of the saved query to run
fromthe zero-based offset of the first result to return
pageSizethe maximum number of results to return

runQuery(String queryName, Map<String,String> params)

Returns: KSearchResponse

Runs a saved query by name, with no paging applied. The given parameters override the query parameters resolved from the current HTTP request. This is an expensive operation as it executes a search against the underlying index.

ParameterDescription
queryNamethe name of the saved query to run
paramsparameter values that override the request-resolved query parameters

runQuery(String queryName, Integer from, Integer pageSize, Map<String,String> params)

Returns: KSearchResponse

Runs a saved query by name, with the given paging applied. The given parameters are added to the query parameters resolved from the current HTTP request, without overriding an already-resolved value. This is an expensive operation as it executes a search against the underlying index.

ParameterDescription
queryNamethe name of the saved query to run
fromthe zero-based offset of the first result to return
pageSizethe maximum number of results to return
paramsadditional parameter values, used only where not already resolved from the request

getAvailableSuppliers()

Returns: List<Organisation>

Finds the supplier organisations available to the current user, based on the SupplierFilter role.

availableSuppliers(Profile user)

Returns: List<Organisation>

Finds the supplier organisations available to the given user, based on the SupplierFilter role. This is an expensive operation, involving a lookup of the user's supplier organisation ids.

ParameterDescription
userthe user to find available supplier organisations for

getSelectedSupplierIds()

Returns: List<Long>

The ids of the supplier organisations currently selected by the current user, sorted numerically.

selectedSupplierIds(Profile user)

Returns: List<Long>

The ids of the supplier organisations currently selected by the given user, sorted numerically. Selection is scoped to the supplier organisations available to the user via the SupplierFilter role.

ParameterDescription
userthe user to find selected supplier organisation ids for

getSelectedOrgIds()

Returns: List<Long>

The ids of the organisations currently selected by the current user for reporting, sorted numerically.

selectedOrgIds(Profile user)

Returns: List<Long>

The ids of the organisations currently selected by the given user for reporting, sorted numerically. Selection is scoped to the organisations the user has reporting access to.

ParameterDescription
userthe user to find selected organisation ids for

selectedOrgIDsStr()

Returns: String

The raw, unparsed cookie value holding the selected organisation id(s), if any.

getAvailableOrgIds()

Returns: List<Long>

The internal ids of the organisations the current user has the ReportingAccess role on. This list is not expanded, so it does not include child organisations of those.

availableOrgIds(Profile user)

Returns: List<Long>

Returns the org IDs which the given user has reporting access on

ParameterDescription
userthe user to find reporting-access organisation ids for

getAvailableOrgs()

Returns: List<Organisation>

The organisations directly available to the current user, ie where the user has the ReportingAccess role on that org, sorted by formatted name. This is a non-expanded list, so it does not include child organisations. This is an expensive operation as it resolves each organisation id individually.

getAvailableOrgIds(Group group)

Returns: List<Long>

The internal ids of the organisations the current user has the ReportingAccess role on, restricted to the given group. This list is not expanded, so it does not include child organisations of those.

ParameterDescription
groupthe group to restrict the reporting-access lookup to

getAvailableOrgIds(List<Group> groups)

Returns: List<Long>

The internal ids of the organisations the current user has the ReportingAccess role on, restricted to the given groups. This list is not expanded, so it does not include child organisations of those.

ParameterDescription
groupsthe groups to restrict the reporting-access lookup to

getSelectedOrgs()

Returns: List<OrgData>

The organisation(s) currently selected by the current user, resolved as child organisations of the tenant's root organisation, excluding any that have been deleted.

getCurrentTeamOrg()

Returns: Organisation

The single organisation, of those currently selected by the current user, that should represent the user's team. Where multiple organisations are selected, the first resolved one is returned.

teamOrg(Profile user)

Returns: Organisation

The single organisation, of those currently selected by the given user, that should represent that user's team. Where multiple organisations are selected, the first resolved one is returned.

ParameterDescription
userthe user to resolve the team organisation for

selectedOrgHasType(String orgType)

Returns: boolean

Checks whether any of the organisations currently selected by the current user has the given organisation type.

ParameterDescription
orgTypethe organisation type to look for

preProcessQuery(String json)

Returns: String

Substitutes query parameter values into a raw query JSON string, resolved from the current HTTP request. Superseded by preProcessQuery2, which should be used instead.

ParameterDescription
jsonthe raw query JSON to substitute parameters into

preProcessQuery2(String json)

Returns: String

Substitutes query parameter values, resolved from the current HTTP request, into a raw query JSON string. This improves on preProcessQuery and should be used instead: it handles simple parameter substitution and also handles parameters that resolve to lists.

ParameterDescription
jsonthe raw query JSON to substitute parameters into

preProcessQuery2(String json, Map<String,Object> params)

Returns: String

Substitutes the given query parameter values into a raw query JSON string. Handles simple parameter substitution and also handles parameters that resolve to lists.

ParameterDescription
jsonthe raw query JSON to substitute parameters into
paramsthe parameter values to substitute

getParams()

Returns: Map<String,Object>

The report query parameters resolved from the current HTTP request.

saveTable(KCriteriaTable t)

Returns: void

Persists a criteria table definition. Wraps the underlying IOException as a RuntimeException.

ParameterDescription
tthe criteria table to save

getOrCreateReportParams()

Returns: ReportParams

Returns the current thread-bound report parameters, creating and binding an empty set if none exist yet.

getOrCreateReportParams(Function<ReportParams,T> callback)

Returns: T

Runs the given callback with the current thread-bound report parameters, creating and binding an empty set first if none exist yet. If this call created the params, they are removed again once the callback returns.

ParameterDescription
callbackthe function to run with the report parameters

enqueueQueryTableExport(String queryTableName, Date start, Date finish, List<Long> orgIds, Map<String,String> params)

Returns: long

Creates a QueryTableExporter task and submits it to the AsyncJobManager for background execution. Normally the date range and organisation ids should be resolved via this class's own methods; the params map may be provided in addition where the query table uses extended parameters.

ParameterDescription
queryTableNamethe name of the query table to export
startstart date/time to filter the data by, if used by the query table
finishend date/time to filter the data by, if used by the query table
orgIdsorganisation ids to pass to the underlying query for filtering, if used
paramsadditional parameters used by the underlying query

exportTaskName(String queryTableName)

Returns: String

Builds the async job task name used to track a query table export, so its progress can be looked up later.

ParameterDescription
queryTableNamethe name of the query table being exported

createDefaultQuery(String esFieldName, String operator, Object value)

Returns: QueryBuilder

Builds a default search query clause for a field, operator and value, using the standard operator handling for the field's type.

ParameterDescription
esFieldNamethe elasticsearch field name to query
operatorthe operator to apply, such as an equality or range comparison
valuethe value to compare the field against

createDefaultQuery(String esFieldName, String operator, Object value, boolean reverseRange)

Returns: QueryBuilder

Builds a default search query clause for a field, operator and value, using the standard operator handling for the field's type, with the option to reverse the direction of a range comparison.

ParameterDescription
esFieldNamethe elasticsearch field name to query
operatorthe operator to apply, such as an equality or range comparison
valuethe value to compare the field against
reverseRangetrue to reverse the direction of a range comparison, false for the normal direction

findFieldOperatorsByType(String fieldType)

Returns: List<String>

Finds the operators available for a field of the given type, for example the comparisons a date field supports versus a keyword field.

ParameterDescription
fieldTypethe field type to find operators for

findActualFieldType(String fieldType)

Returns: String

Resolves the actual field type the query builder should use for a given declared field type.

ParameterDescription
fieldTypethe declared field type to resolve

processRows(String indexType, List<String> fields, List rows)

Returns: List<ProcessedHit>

Converts a list of raw search result rows into ProcessedHit objects, resolving any path-style fields (for example "assignedToProfileId/firstName") by joining to the related index and enriching the row with default field formatting. This is an expensive operation as it may execute additional search queries to fetch the joined data.

ParameterDescription
indexTypethe index type the rows were retrieved from
fieldsthe field names to include in the processed rows, including any path-style joined fields
rowsthe raw rows to process; each entry is either a KSearchHit or a Map of field name to value

processRows(String indexType, List<String> fields, boolean defaultFieldsFormatting, List rows)

Returns: List<ProcessedHit>

Converts a list of raw search result rows into ProcessedHit objects, resolving any path-style fields (for example "assignedToProfileId/firstName") by joining to the related index. This is an expensive operation as it may execute additional search queries to fetch the joined data.

ParameterDescription
indexTypethe index type the rows were retrieved from
fieldsthe field names to include in the processed rows, including any path-style joined fields
defaultFieldsFormattingwhether joined values should be resolved using the joined index's default display formatting
rowsthe raw rows to process; each entry is either a KSearchHit or a Map of field name to value
To get full access to the Kademi Hub existing customers can login here, or new customers can register here.