enabling ai fork and multi-calculated

This commit is contained in:
morsh 2017-03-29 19:51:55 +03:00
Родитель 3a8514594e
Коммит 02e5aa9f46
5 изменённых файлов: 86 добавлений и 184 удалений

Просмотреть файл

@ -7,8 +7,10 @@ import FontIcon from 'react-md/lib/FontIcons';
export default class Scorecard extends GenericComponent<any, any> {
render() {
var { value, icon, className } = this.state;
var { title } = this.props;
let { value, icon, className } = this.state;
let { title } = this.props;
value = (value || '').toString();
return (
<Card>

Просмотреть файл

@ -260,6 +260,15 @@ export class DataSourceConnector {
});
}
if (Array.isArray(calculated)) {
calculated.forEach(calc => {
var additionalValues = calc(state) || {};
Object.keys(additionalValues).forEach(key => {
result[key] = additionalValues[key];
});
});
}
return result;
}
}

Просмотреть файл

@ -3,7 +3,7 @@
import * as _ from 'lodash';
import {DataSourcePlugin, IDataSourceOptions} from '../DataSourcePlugin';
//import ActionsCommon from './actions-common';
import { appInsightsUri, appId, apiKey } from './common';
import { appInsightsUri } from './common';
declare var process : any;

Просмотреть файл

@ -27,13 +27,9 @@ export default class ApplicationInsightsQuery extends DataSourcePlugin {
var props = this._props;
var params: any = props.params;
if (!params.query) {
throw new Error('AIAnalyticsEvents requires a query to run and dependencies that trigger updates.');
}
if (!props.dependencies.queryTimespan) {
throw new Error('AIAnalyticsEvents requires dependencies: timespan; queryTimespan');
}
// Validating params
this.validateParams(props, params);
}
/**
@ -62,12 +58,38 @@ export default class ApplicationInsightsQuery extends DataSourcePlugin {
};
}
var { queryTimespan } = dependencies;
let { queryTimespan } = dependencies;
let params: any = this._props.params;
let tableNames: Array<string> = [];
let mappings: Array<any> = [];
// Checking if this is a single query or a fork query
let query: string;
let isForked = !params.query && params.table;
if (!isForked) {
query = this.compileQuery(params.query, dependencies);
mappings.push(params.mappings);
} else {
let queries: Array<any> = params.queries || [];
let table: string = params.table;
query = ` ${params.table} | fork `;
_.keys(queries).forEach(queryKey => {
let queryParams = queries[queryKey];
tableNames.push(queryKey);
mappings.push(queryParams.mappings);
let subquery = this.compileQuery(queryParams.query, dependencies);
query += ` (${subquery}) `;
});
}
var params: any = this._props.params;
var query = typeof params.query === 'function' ? params.query(dependencies) : params.query;
var mappings = params.mappings;
var queryspan = queryTimespan;
var url = `${appInsightsUri}/${appId}/query?timespan=${queryspan}&query=${encodeURIComponent(query)}`;
return (dispatch) => {
@ -102,18 +124,22 @@ export default class ApplicationInsightsQuery extends DataSourcePlugin {
values: (resultTables.length && resultTables[0]) || null
};
tableNames.forEach((table: string, idx: number) => {
returnedResults[table] = resultTables.length > idx ? resultTables[idx] : null;
});
return dispatch(returnedResults);
});
}
}
private mapAllTables(results: IQueryResults, mappings: IDictionary): any[][] {
private mapAllTables(results: IQueryResults, mappings: Array<IDictionary>): any[][] {
if (!results || !results.Tables || !results.Tables.length) {
return [];
}
return results.Tables.map(table => this.mapTable(table, mappings));
return results.Tables.map((table, idx) => this.mapTable(table, mappings[idx]));
}
/**
@ -142,4 +168,37 @@ export default class ApplicationInsightsQuery extends DataSourcePlugin {
return row;
});
}
private compileQuery(query: any, dependencies: any): string {
return typeof query === 'function' ? query(dependencies) : query;
}
private validateParams(props: any, params: any): void {
if (!props.dependencies.queryTimespan) {
throw new Error('AIAnalyticsEvents requires dependencies: timespan; queryTimespan');
}
if (params.query) {
if (params.table || params.queries) {
throw new Error('Application Insights query should either have { query } or { table, queries } under params.');
}
if (typeof params.query !== 'string' && typeof params.query !== 'function') {
throw new Error('{ query } param should either be a function or a string.');
}
}
if (params.table) {
if (!params.queries) {
return this.failure(new Error('Application Insights query should either have { query } or { table, queries } under params.'));
}
if (typeof params.table !== 'string' || typeof params.queries !== 'object' || Array.isArray(params.queries)) {
throw new Error('{ table, queries } should be of types { "string", { query1: {...}, query2: {...} } }.');
}
}
if (!params.query && !params.table) {
throw new Error('{ table, queries } should be of types { "string", { query1: {...}, query2: {...} } }.');
}
}
}

Просмотреть файл

@ -1,168 +0,0 @@
// import $ from 'jquery';
// import _ from 'lodash';
// const appInsights = {
// uri: 'https://api.applicationinsights.io/beta/apps'
// };
// function getUrlVars()
// {
// var vars = [], hash;
// var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
// for(var i = 0; i < hashes.length; i++)
// {
// hash = hashes[i].split('=');
// vars.push(hash[0]);
// vars[hash[0]] = hash[1];
// }
// return vars;
// }
// // Using http://uri?appId={appId}&apiKey={apiKey}
// var variables = getUrlVars();
// var appId = process.env.REACT_APP_APP_INSIGHTS_APPID || variables['appId'];
// var apiKey = process.env.REACT_APP_APP_INSIGHTS_APIKEY || variables['apiKey'];
// class EventsQueryConfig {
// /** @type {string} */
// timespan;
// /** @type {string=} */
// search;
// /** @type {number=} */
// top;
// /** @type {number=} */
// skip;
// }
// class QueryConfig {
// /** @type {string} */
// query;
// /** @type {string} */
// timespan;
// /** @type {(string|object)[]} */
// mappings;
// }
// export default class ActionsCommon {
// static appInsights = appInsights;
// static appInsightsAppId = appId;//appInsights.apps[app].appId;
// static appInsightsApiKey = apiKey;//appInsights.apps[app].apiKey;
// static timespanToQueryspan(timespan) {
// return timespan === '24 hours' ? 'PT24H' : timespan === '1 week' ? 'P7D' : 'P30D';
// }
// static timespanToGranularity(timespan) {
// return timespan === '24 hours' ? '5m' : timespan === '1 week' ? '1d' : '1d';
// }
// /**
// * Prepare results to ship via callback
// * @param {string} property Property name to set
// * @param {object} val value to put in property
// * @returns {object} object to be returned
// */
// static prepareResult(property, val) {
// var obj = {};
// obj[property] = val;
// return obj;
// }
// /**
// * Translates timespan into its start date
// * @param {string} timespan - Configuration for the query
// * @returns {Date}
// */
// static timespanStartDate(timespan) {
// var date = new Date();
// var days = timespan === '24 hours' ? 1 : timespan === '1 week' ? 7 : 30;
// date.setDate(date.getDate() - days);
// return date;
// }
// /**
// * Execute a query with the application insights query API.
// * @param {QueryConfig} config - Configuration for the query
// * @param {function} callback
// */
// static fetchQuery(config, callback) {
// var {
// timespan,
// query,
// mappings
// } = config || new QueryConfig();
// var queryspan = ActionsCommon.timespanToQueryspan(timespan);
// var url = `${appInsights.uri}/${ActionsCommon.appInsightsAppId}/query?timespan=${queryspan}&query=${query}`;
// $.ajax({
// url,
// method: "GET",
// headers: {
// "x-api-key": ActionsCommon.appInsightsApiKey
// }
// })
// .then(json => {
// var resultRows = json.Tables[0].Rows;
// if (!mappings || mappings.length === 0) {
// return callback(null, resultRows);
// }
// var rows = resultRows.map(row => {
// var item = {};
// mappings.forEach((mapping, index) => {
// var key = typeof mapping === 'string' ? mapping : mapping.key;
// var idx = mapping.idx ? mapping.idx : index;
// var def = mapping.def ? mapping.def : null;
// item[key] = mapping.val && row[idx] && mapping.val(row[index]) || row[idx] || def;
// });
// return item;
// });
// return callback(null, rows);
// })
// .fail((err) => {
// return callback(err);
// });
// }
// /**
// * Execute a query with the application insights events API.
// * @param {EventsQueryConfig} config - Configuration for the query
// * @param {function} callback
// */
// static fetchEvents(config, callback) {
// var {
// timespan,
// search,
// top,
// skip
// } = config || new EventsQueryConfig();
// var queryspan = ActionsCommon.timespanToQueryspan(timespan);
// var url = `${appInsights.uri}/${ActionsCommon.appInsightsAppId}/events/exceptions?timespan=${queryspan}` +
// search ? `&$search=${encodeURIComponent(search)}` : '' +
// `&&$orderby=timestamp` +
// top ? `&$top=${top}` : '' +
// skip ? `&$skip=${skip}` : '';
// $.ajax({
// url,
// method: "GET",
// headers: {
// "x-api-key": ActionsCommon.appInsightsApiKey
// }
// })
// .then(json => {
// return callback(null, json.value);
// })
// .fail((err) => {
// return callback(err);
// });
// }
// }