Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
feat(time series): add actions to store for containing diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
rashley-iqt committed Jan 11, 2019
1 parent 5b8cf2e commit 108b4c8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
46 changes: 44 additions & 2 deletions src/domain/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
} from "ramda";

const defaultState = {
datasets: {}
datasets: {},
diffs:[]
};
const defaultItemState = {
owner: "",
Expand Down Expand Up @@ -119,8 +120,10 @@ const valuesFor = (dataset, configuration) => {
*/
const setDataset = createAction("SET_DATASET");
const setFilteredDataset = createAction("SET_FILTERED_DATASET");
const setDatasetDiff = createAction("SET_DATASET_DIFF");
const removeDataset = createAction("REMOVE_DATASET");
const removeFilteredDataset = createAction("REMOVE_FILTERED_DATASET");
const removeDatasetDiff = createAction("REMOVE_DATASET_DIFF");
const setIsFetching = createAction("SET_IS_FETCHING");

// REDUCERS
Expand Down Expand Up @@ -154,6 +157,25 @@ const reducer = handleActions(
state.datasets[owner].filtered = filtered;
return { ...state};
},
[setDatasetDiff]: (state, { payload }) => {
const start = payload.start;
const end = payload.end;
const differences = payload.differences
const newDiff = {
'start': start,
'end': end,
'differences': differences
}

const idx = state.diffs.findIndex(d => d.start === start && d.end === end);

if(idx === -1){
state.diffs.push(newDiff);
} else {
state.diffs[idx] = newDiff;
}
return { ...state};
},
[removeDataset]: (state, { payload }) => {
const owner = payload.owner;
if(state.datasets.hasOwnProperty(owner))
Expand All @@ -168,6 +190,17 @@ const reducer = handleActions(

return { ...state }
},
[removeDatasetDiff]: (state, { payload }) => {
const start = payload.start;
const end = payload.end;
const idx = state.diffs.findIndex(d => d.start === start && d.end === end);

if(idx !== -1){
state.diffs.splice(idx, 1);
}

return { ...state }
},
[setIsFetching]: (state, { payload }) => {
const owner = payload.owner;
const isFetching = !!payload.isFetching;
Expand All @@ -183,6 +216,15 @@ const reducer = handleActions(

const selectDataset = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].dataset ? state.dataset.datasets[owner].dataset : defaultItemState.dataset;
const selectFilteredDataset = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].filtered ? state.dataset.datasets[owner].filtered : defaultItemState.filtered;
const selectDatasetDiff = (state, start, end) => {
let diff = null;
const idx = state.dataset.diffs.findIndex(d => d.start === start && d.end === end);

if(idx !== -1){
diff = state.dataset.diffs[idx].differences;;
}
return diff;
}
const selectConfiguration = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].configuration ? state.dataset.datasets[owner].configuration : defaultItemState.configuration;
const selectMergedConfiguration = (state) => {
let fields = [];
Expand Down Expand Up @@ -223,4 +265,4 @@ const getLastUpdated = (state, owner) => state.dataset.datasets[owner] && state.
export default reducer;

export { setDataset, selectDataset, removeDataset, setFilteredDataset, selectFilteredDataset, removeFilteredDataset, selectConfiguration, selectMergedConfiguration, selectValues,
selectMergedValues, getFieldId, configurationFor, setIsFetching, getIsFetching, getLastUpdated, valuesFor };
selectMergedValues, getFieldId, configurationFor, setIsFetching, getIsFetching, getLastUpdated, valuesFor, setDatasetDiff, removeDatasetDiff, selectDatasetDiff };
63 changes: 62 additions & 1 deletion src/domain/dataset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
selectValues,
selectMergedValues,
setIsFetching,
getIsFetching
getIsFetching,
setDatasetDiff,
removeDatasetDiff,
selectDatasetDiff
} from "./dataset";

import { combineReducers } from "redux";
Expand Down Expand Up @@ -313,5 +316,63 @@ describe("Dataset Reducer", () => {
done();
});
});

describe("setDatasetDiffs", () => {
it("sets the diff between 2 datasets", (done) => {
const ds1Owner = uuidv4();
const ds2Owner = uuidv4();
const ds1 = [
{ 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } },
{ 'uid': "uid2", 'role': { 'role': "role1", 'confidence': 80 } }
];
const ds2 = [
{ 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } },
{ 'uid': "uid2", 'role': { 'role': "role2", 'confidence': 80 } }
];
const differences =[{
key:"uid2",
fields: [{
field: { 'path': ["role", "role"], 'displayName': "Role", 'groupable': false },
startValue: "role1",
endValue: "role2"
}]
}];

const action = setDatasetDiff({ 'start': ds1Owner, 'end': ds2Owner, 'differences': differences });
const result = reducer({}, action);

expect(selectDatasetDiff(result, ds1Owner, ds2Owner)).to.deep.equal(differences);

done();
});

it("removes the diff between 2 datasets", (done) => {
const ds1Owner = uuidv4();
const ds2Owner = uuidv4();
const ds1 = [
{ 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } },
{ 'uid': "uid2", 'role': { 'role': "role1", 'confidence': 80 } }
];
const ds2 = [
{ 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } },
{ 'uid': "uid2", 'role': { 'role': "role2", 'confidence': 80 } }
];
const differences =[{
key:"uid2",
fields: [{
field: { 'path': ["role", "role"], 'displayName': "Role", 'groupable': false },
startValue: "role1",
endValue: "role2"
}]
}];

const action = removeDatasetDiff({ 'start': ds1Owner, 'end': ds2Owner});
const result = reducer({ diffs:[{ 'start': ds1Owner, 'end': ds2Owner, 'differences': differences }] }, action);

expect(selectDatasetDiff(result, ds1Owner, ds2Owner)).to.equal(null);

done();
});
});
});
});

0 comments on commit 108b4c8

Please sign in to comment.