Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ChartViewer = ({ dashboard, charts, meteorMethodCall, routeTo, setChartVie
prevArrow: <NavButton />,
nextArrow: <NavButton right />,
};

return (
<div
className="chart-viewer anchor-for-click"
Expand Down
1 change: 1 addition & 0 deletions client/modules/publishing/components/chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Chart = props => {
}
const { viewObject: { data, chartType, dataTimeStamp, pivot }, queryObject } = chart;
const needRedraw = !dataTimeStamp || Date.now() - dataTimeStamp < 1000;

if (chartType) {
return (
<ChartCanvas
Expand Down
47 changes: 36 additions & 11 deletions client/modules/workplace/actions/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,54 @@ export default {
const requestId = _.uniqueId();
const database = LocalState.get('CURRENT_DATABASE');
LocalState.set('QUASAR_REQUEST_ID', requestId);

Meteor.call('quasar.getData', database, query, requestId,
(err, { data = [], answerId } = {}) => {
if (err) cb({ error: true });
if (err) {
cb({ error: true });
}
else if (LocalState.get('QUASAR_REQUEST_ID') === answerId || isSingleRequest) {
const processedData = dataProcessing.process(data, fields);
cb({ data: processedData });
}
}
);
},
determineDefaultFields({ Notificator }, fields, chartType, pivot) {
let result;
if (pivot && pivot.model) {
const measuresId = pivot.model.values;

// todo !!!
// this function must be refactored in th future
determineDefaultFields({ Notificator, LocalState }, fields, chartType, pivot) {
const determineFieldsBySavedConstructors = (fieldsArr, constructors) =>
fieldsArr.map(field => {
if (constructors.dimensions.includes(field.id)) {
field.constructorType = 'dimensions';
} else if (constructors.measures.includes(field.id)) {
field.constructorType = 'measures';
}
return field;
});
const determineFieldsByModel = (fieldsArr, pivotModel) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

determineFieldsBy... functions could be defined outside of target determineDefaultFields, e.g. as plain file variables

const measuresId = pivotModel.values;
let dimensionsCounter = 0;
fields.forEach(f => {
if (!~measuresId.indexOf(f.id)) {
return fieldsArr.map(field => {
if (!~measuresId.indexOf(field.id)) {
dimensionsCounter++;
if (dimensionsCounter < 3) f.constructorType = 'dimensions';
if (dimensionsCounter < 3) field.constructorType = 'dimensions';
} else {
f.constructorType = 'measures';
field.constructorType = 'measures';
}
return field;
});
result = fields;
};
const viewObj = LocalState.get('VIEW_OBJECT');
let result;

if (viewObj.fieldsConstructors) {
return determineFieldsBySavedConstructors(fields, viewObj.fieldsConstructors);
}

if (pivot && pivot.model) {
result = determineFieldsByModel(fields, pivot.model);
} else {
const getNeededfields = () => {
const fieldsArray = [];
Expand Down Expand Up @@ -102,13 +126,14 @@ export default {
});
if (neededFields.filter(f => f.id).length) result = fields;
}

return result;
},

getNewChartModelFields(context, { chartType, fields }, { fieldId, isChecked, label }) {
const checkedFieldsNumber = fields.filter(f => f.constructorType === label).length;
const newFields = fields.map(f => _.extend(_.clone(f),
f.id === fieldId ? { constructorType: isChecked ? label : null } : {})
f.id === fieldId ? { constructorType: isChecked ? label : null } : {})
);
const maxCheckedFields = (() => {
let res = chartTypeRules[chartType][label];
Expand Down
35 changes: 33 additions & 2 deletions client/modules/workplace/actions/workplace_state.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { _ } from 'meteor/underscore';
import { pivotCellsLimit } from '/lib/constants';
import SQLParser from '/lib/sql_parser';
import { parseToQueryObject } from '/lib/sql_parser';

export default {
setSQLQuery({ LocalState }, query) {
const fields = LocalState.get('COLLECTION_FIELDS');
const oldQueryObj = LocalState.get('SQL_QUERY_OBJECT');
const queryObj = SQLParser.parseToQueryObject(query, fields);
const fieldsConstructors = getFieldsConstructorsType(oldQueryObj.fields);

if (fieldsConstructors) {
const viewObj = LocalState.get('VIEW_OBJECT');
viewObj.fieldsConstructors = fieldsConstructors;
LocalState.set('VIEW_OBJECT', viewObj);
}

const queryObj = parseToQueryObject(query, fields);
queryObj.from = oldQueryObj.from;
queryObj.on = oldQueryObj.on;

LocalState.set('SQL_QUERY_OBJECT', queryObj);
},

Expand Down Expand Up @@ -109,3 +118,25 @@ function resetData(LocalState) {
LocalState.set('VIEW_OBJECT', viewObj);
}
}

const isPropEmptyArray = (prop, obj) => !obj[prop].length;

const getFieldsConstructor = fields => fields.reduce(
(constructor, { constructorType }, index) => {
if (constructorType) {
constructor[constructorType].push(index);
}
return constructor;
}, {
measures: [],
dimensions: [],
}
);

function getFieldsConstructorsType(fields) {
const constructor = getFieldsConstructor(fields);
const isDimensions = !isPropEmptyArray('dimensions', constructor);
const isMeasures = !isPropEmptyArray('measures', constructor);

return (isDimensions || isMeasures) ? constructor : false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DataVisualisation extends React.Component {
updateSQLQueryObj } = this.props;
const { chartType } = viewObject;
const tableHeight = `${window.innerHeight - 74}px`;

return (
<div className="data-visualisation">
{tableType === 'simple' &&
Expand Down
1 change: 1 addition & 0 deletions client/modules/workplace/components/preview/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Preview extends React.Component {
const { savedQuery, queryObject, viewObject, tableType } = this.props;
const { isQueryChanged, isLive } = this.state;
const isFields = !_.isEmpty(queryObject.fields);

return (
<div
className="preview-wrap"
Expand Down
1 change: 1 addition & 0 deletions client/modules/workplace/containers/workplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const composer = ({ context, setSQLQueryObj }, onData) => {
const { chartType, query: savedQuery } = viewObject;
parseCollectionFieldNames(queryObject.collectionFields);
setSQLQueryObj(queryObject, tableType);

LocalState.set('VIEW_OBJECT', viewObject);
onData(null, { isSavedChart: true, tableType, chartType, savedQuery });
}
Expand Down
Loading