Skip to content

Commit

Permalink
Revert "fix: use lerna to share code instead of copying resources (#214
Browse files Browse the repository at this point in the history
…)" (#216)

This reverts commit eecfea0.

This reintroduces code duplication that had been removed. The implementation causes the plugin to break in dashboards app. Reverting this now and will re-introduce a better solution with webpack bundling soon.

The reason this is being rushed in now is because we are migrating the data-visualizer-app to a mon-repo.
  • Loading branch information
jenniferarnesen committed Feb 27, 2019
1 parent 2538bf2 commit de00b4f
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/app/i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2019-02-11T15:01:16.680Z\n"
"PO-Revision-Date: 2019-02-11T15:01:16.680Z\n"
"POT-Creation-Date: 2019-01-21T11:22:11.007Z\n"
"PO-Revision-Date: 2019-01-21T11:22:11.007Z\n"

msgid "Rename successful"
msgstr ""
Expand Down Expand Up @@ -44,10 +44,10 @@ msgstr ""
msgid "Select All"
msgstr ""

msgid "Search dimensions"
msgid "Remove"
msgstr ""

msgid "Remove"
msgid "Search dimensions"
msgstr ""

msgid "Dimension recommended with selected data"
Expand Down
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"d2": "31.2.1",
"d2-charts-api": "31.0.12",
"d2-manifest": "^1.0.0",
"data-visualizer-plugin": "^32.0.2",
"data-visualizer-plugin": "github:d2-ci/data-visualizer-plugin",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.4.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/plugin/src/ChartPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import PropTypes from 'prop-types';

import { createChart } from 'd2-charts-api';

import { apiFetchVisualization } from 'data-visualizer-app/src/api/visualization';
import { apiFetchVisualization } from './api/visualization';
import {
apiFetchAnalytics,
apiFetchAnalyticsForYearOverYear,
} from 'data-visualizer-app/src/api/analytics';
import { isYearOverYear } from 'data-visualizer-app/src/modules/chartTypes';
import { getOptionsForRequest } from 'data-visualizer-app/src/modules/options';
import { computeGenericPeriodNames } from 'data-visualizer-app/src/modules/analytics';
import { BASE_FIELD_YEARLY_SERIES } from 'data-visualizer-app/src/modules/fields/baseFields';
import LoadingMask from 'data-visualizer-app/src/widgets/LoadingMask';
} from './api/analytics';
import { isYearOverYear } from './modules/chartTypes';
import { getOptionsForRequest } from './modules/options';
import { computeGenericPeriodNames } from './modules/analytics';
import { BASE_FIELD_YEARLY_SERIES } from './modules/fields/baseFields';
import LoadingMask from './widgets/LoadingMask';

class ChartPlugin extends Component {
constructor(props) {
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin/src/__tests__/ChartPlugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { shallow } from 'enzyme';
import LoadingMask from 'data-visualizer-app/src/widgets/LoadingMask';
import LoadingMask from '../widgets/LoadingMask';
import ChartPlugin from '../ChartPlugin';
import * as chartsApi from 'd2-charts-api';
import * as api from 'data-visualizer-app/src/api/analytics';
import * as apiViz from 'data-visualizer-app/src/api/visualization';
import * as options from 'data-visualizer-app/src/modules/options';
import { YEAR_OVER_YEAR_LINE, COLUMN } from 'data-visualizer-app/src/modules/chartTypes';
import * as api from '../api/analytics';
import * as apiViz from '../api/visualization';
import * as options from '../modules/options';
import { YEAR_OVER_YEAR_LINE, COLUMN } from '../modules/chartTypes';

jest.mock('d2-charts-api');

Expand Down
58 changes: 58 additions & 0 deletions packages/plugin/src/api/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { getInstance } from 'd2';

const peId = 'pe';

export const apiFetchAnalytics = async (current, options) => {
const d2 = await getInstance();

const req = new d2.analytics.request()
.fromModel(current)
.withParameters(options);

const rawResponse = await d2.analytics.aggregate.get(req);

return [new d2.analytics.response(rawResponse)];
};

export const apiFetchAnalyticsForYearOverYear = async (current, options) => {
const d2 = await getInstance();

let yearlySeriesReq = new d2.analytics.request()
.addPeriodDimension(current.yearlySeries)
.withSkipData(true)
.withSkipMeta(false)
.withIncludeMetadataDetails(true);

if (options.relativePeriodDate) {
yearlySeriesReq = yearlySeriesReq.withRelativePeriodDate(
options.relativePeriodDate
);
}

const yearlySeriesRes = await d2.analytics.aggregate.fetch(yearlySeriesReq);

const requests = [];
const yearlySeriesLabels = [];

const now = new Date();
const currentDay = ('' + now.getDate()).padStart(2, 0);
const currentMonth = ('' + (now.getMonth() + 1)).padStart(2, 0);

yearlySeriesRes.metaData.dimensions[peId].forEach(period => {
yearlySeriesLabels.push(yearlySeriesRes.metaData.items[period].name);

const startDate = `${period}-${currentMonth}-${currentDay}`;

const req = new d2.analytics.request()
.fromModel(current)
.withParameters(options)
.withRelativePeriodDate(startDate);

requests.push(d2.analytics.aggregate.get(req));
});

return Promise.all(requests).then(responses => ({
responses: responses.map(res => new d2.analytics.response(res)),
yearlySeriesLabels,
}));
};
9 changes: 9 additions & 0 deletions packages/plugin/src/api/visualization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getInstance } from 'd2';
import { getFieldsStringByType } from '../modules/fields';

export const apiFetchVisualization = (type, id) =>
getInstance().then(d2 =>
d2.models[type].get(id, {
fields: getFieldsStringByType(type),
})
);
29 changes: 29 additions & 0 deletions packages/plugin/src/modules/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const computeGenericPeriodNames = responses => {
const xAxisRes = responses.reduce((out, res) => {
if (out.metaData) {
if (
res.metaData.dimensions.pe.length >
out.metaData.dimensions.pe.length
) {
out = res;
}
} else {
out = res;
}

return out;
}, {});

const metadata = xAxisRes.metaData;

return metadata.dimensions.pe.reduce((genericPeriodNames, periodId) => {
const name = metadata.items[periodId].name;

// until the day the backend will support this in the API:
// trim off the trailing year in the period name
// english names should all have the year at the end of the string
genericPeriodNames.push(name.replace(/\s+\d{4}$/, ''));

return genericPeriodNames;
}, []);
};
6 changes: 6 additions & 0 deletions packages/plugin/src/modules/chartTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const YEAR_OVER_YEAR_LINE = 'YEAR_OVER_YEAR_LINE';
export const YEAR_OVER_YEAR_COLUMN = 'YEAR_OVER_YEAR_COLUMN';

const yearOverYearTypes = [YEAR_OVER_YEAR_LINE, YEAR_OVER_YEAR_COLUMN];

export const isYearOverYear = type => yearOverYearTypes.includes(type);
167 changes: 167 additions & 0 deletions packages/plugin/src/modules/fields/baseFields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
export const BASE_FIELD_NAME = 'name';
export const BASE_FIELD_TYPE = 'type';
export const BASE_FIELD_YEARLY_SERIES = 'yearlySeries';

const getFieldObject = (name, props = {}) => ({
[BASE_FIELD_NAME]: name,
...props,
});

// fields by type

export const fieldsByType = {
reportTable: [
getFieldObject('cumulative', { option: true }),
getFieldObject('hideEmptyColumns', { option: true }),
getFieldObject('legendDisplayStyle', { option: true }),
getFieldObject('measureCriteria', { option: true }),
getFieldObject('numberType', { option: true }),
getFieldObject('regression', { option: true }),
getFieldObject('reportParams', { option: true }),
getFieldObject('skipRounding', { option: true }),
],
chart: [
getFieldObject('category', { excluded: true }),
getFieldObject('series', { excluded: true }),
getFieldObject(BASE_FIELD_YEARLY_SERIES),
],
eventReport: [getFieldObject('dataType')],
reportTable_eventReport: [
getFieldObject('colSubTotals', { option: true }),
getFieldObject('colTotals', { option: true }),
getFieldObject('displayDensity', { option: true }),
getFieldObject('fontSize', { option: true }),
getFieldObject('hideEmptyRows', { option: true }),
getFieldObject('rowSubTotals', { option: true }),
getFieldObject('rowTotals', { option: true }),
getFieldObject('showDimensionLabels', { option: true }),
getFieldObject('showHierarchy', { option: true }),
],
chart_eventChart: [
getFieldObject('baseLineLabel', { option: true }),
getFieldObject('baseLineValue', { option: true }),
getFieldObject('colorSet', { option: true }),
getFieldObject('cumulativeValues', { option: true }),
getFieldObject('domainAxisLabel', { option: true }),
getFieldObject('hideEmptyRowItems', { option: true }),
getFieldObject('hideLegend', { option: true }),
getFieldObject('noSpaceBetweenColumns', { option: true }),
getFieldObject('percentStackedValues', { option: true }),
getFieldObject('rangeAxisDecimals', { option: true }),
getFieldObject('rangeAxisLabel', { option: true }),
getFieldObject('rangeAxisMaxValue', { option: true }),
getFieldObject('rangeAxisMinValue', { option: true }),
getFieldObject('rangeAxisSteps', { option: true }),
getFieldObject('regressionType', { option: true }),
getFieldObject('showData', { option: true }),
getFieldObject('targetLineLabel', { option: true }),
getFieldObject('targetLineValue', { option: true }),
getFieldObject(BASE_FIELD_TYPE, { option: true }),
],
eventReport_eventChart: [
getFieldObject('attributeValueDimension'),
getFieldObject('collapseDataDimensions'),
getFieldObject('dataElementValueDimension'),
getFieldObject('endDate'),
getFieldObject('eventStatus', { option: true }),
getFieldObject('hideNaData', { option: true }),
getFieldObject('outputType', { option: true }),
getFieldObject('program'),
getFieldObject('programStage'),
getFieldObject('programStatus', { option: true }),
getFieldObject('startDate'),
getFieldObject('value'),
],
reportTable_chart_eventReport: [
getFieldObject('legendDisplayStrategy', { option: true }),
getFieldObject('legendSet', { option: true }),
],
reportTable_eventReport_eventChart: [
getFieldObject('columnDimensions', { excluded: true }),
getFieldObject('rowDimensions', { excluded: true }),
],
reportTable_chart_eventReport_eventChart: [
getFieldObject('access'),
getFieldObject('aggregationType', { option: true }),
getFieldObject('attributeDimensions', { excluded: true }),
getFieldObject('attributeValues', { excluded: true }),
getFieldObject('categoryDimensions', { excluded: true }),
getFieldObject('categoryOptionGroupSetDimensions', { excluded: true }),
getFieldObject('code', { excluded: true }),
getFieldObject('columns'),
getFieldObject('completedOnly', { option: true }),
getFieldObject('created'),
getFieldObject('dataDimensionItems', { excluded: true }),
getFieldObject('dataElementDimensions', { excluded: true }),
getFieldObject('dataElementGroupSetDimensions', { excluded: true }),
getFieldObject('description'),
getFieldObject('digitGroupSeparator'),
getFieldObject('displayDescription'),
getFieldObject('displayName'),
getFieldObject('displayShortName'),
getFieldObject('externalAccess', { excluded: true }),
getFieldObject('favorite'),
getFieldObject('favorites'),
getFieldObject('filterDimensions', { excluded: true }),
getFieldObject('filters'),
getFieldObject('hideSubtitle', { option: true }),
getFieldObject('hideTitle', { option: true }),
getFieldObject('href', { excluded: true }),
getFieldObject('id'),
getFieldObject('interpretations'),
getFieldObject('itemOrganisationUnitGroups', { excluded: true }),
getFieldObject('lastUpdated'),
getFieldObject('lastUpdatedBy'),
getFieldObject('name'),
getFieldObject('organisationUnitGroupSetDimensions', {
excluded: true,
}),
getFieldObject('organisationUnitLevels', { excluded: true }),
getFieldObject('organisationUnits', { excluded: true }),
getFieldObject('parentGraphMap'),
getFieldObject('periods', { excluded: true }),
getFieldObject('programIndicatorDimensions', { excluded: true }),
getFieldObject('publicAccess'),
getFieldObject('relativePeriods', { excluded: true }),
getFieldObject('rows'),
getFieldObject('shortName'),
getFieldObject('sortOrder', { option: true }),
getFieldObject('subscribed'),
getFieldObject('subscribers'),
getFieldObject('subtitle', { option: true }),
getFieldObject('timeField'),
getFieldObject('title', { option: true }),
getFieldObject('topLimit', { option: true }),
getFieldObject('translations'),
getFieldObject('user'),
getFieldObject('userAccesses'),
getFieldObject('userGroupAccesses'),
getFieldObject('userOrganisationUnit', { excluded: true }),
getFieldObject('userOrganisationUnitChildren', { excluded: true }),
getFieldObject('userOrganisationUnitGrandChildren', { excluded: true }),
],
};

// actions

export const extractName = propObj => propObj[BASE_FIELD_NAME];

export const markExcluded = fieldObj =>
fieldObj.excluded === true
? { ...fieldObj, [BASE_FIELD_NAME]: `!${fieldObj[BASE_FIELD_NAME]}` }
: fieldObj;

export const moveExcludedToEnd = (acc, current, curIndex, array) => {
!acc && (acc = array.slice());
current.charAt(0) === '!' && acc.push(acc.shift());
return acc;
};

// getters

export const getAllFieldObjectsByType = type =>
Object.entries(fieldsByType).reduce(
(fields, [key, value]) =>
key.includes(type) ? fields.concat(value) : fields,
[]
);
29 changes: 29 additions & 0 deletions packages/plugin/src/modules/fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
getAllFieldObjectsByType,
extractName,
markExcluded,
moveExcludedToEnd,
} from './baseFields';
import { extendFields } from './nestedFields';

export { getAllFieldObjectsByType };

export const getOptionsByType = type =>
getAllFieldObjectsByType(type).filter(fieldObj => fieldObj.option === true);

export const getIncludedByType = type =>
getAllFieldObjectsByType(type).filter(fieldObj => !fieldObj.excluded);

export const getExcludedByType = type =>
getAllFieldObjectsByType(type).filter(
fieldObj => fieldObj.excluded === true
);

export const getFieldsStringByType = type =>
getAllFieldObjectsByType(type)
.map(markExcluded)
.map(extractName)
.sort()
.reduce(moveExcludedToEnd, null)
.map(extendFields)
.join(',');
Loading

0 comments on commit de00b4f

Please sign in to comment.