Skip to content

Commit

Permalink
Merge pull request #405 from keen/refactor/filters-serialize
Browse files Browse the repository at this point in the history
Refactor Schema Serialize
  • Loading branch information
maciejrybaniec authored Sep 7, 2020
2 parents ec90b72 + 350fb95 commit 2b6b53b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
14 changes: 13 additions & 1 deletion lib/js/app/queryCreator/modules/events/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ import {
FETCH_COLLECTION_SCHEMA,
FETCH_COLLECTION_SCHEMA_ERROR,
FETCH_COLLECTION_SCHEMA_SUCCESS,
SCHEMA_COMPUTED,
} from './constants';

import { EventsActions } from './types';
import { EventsActions, CollectionSchema } from './types';

export const setEventsCollections = (collections: string[]): EventsActions => ({
type: SET_EVENTS_COLLECTIONS,
payload: { collections },
});

export const computeSchemaSuccess = (
collection: string,
schema: Partial<CollectionSchema>
): EventsActions => ({
type: SCHEMA_COMPUTED,
payload: {
collection,
schema,
},
});

export const setCollectionSchemaLoading = (
colletion: string,
isLoading: boolean
Expand Down
1 change: 1 addition & 0 deletions lib/js/app/queryCreator/modules/events/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const FETCH_COLLECTION_SCHEMA_ERROR =
'@query-creator/FETCH_COLLECTION_SCHEMA_ERROR';
export const SET_COLLETION_SCHEMA_LOADING =
'@query-creator/SET_COLLETION_SCHEMA_LOADING';
export const SCHEMA_COMPUTED = '@query-creator/SCHEMA_COMPUTED';
9 changes: 8 additions & 1 deletion lib/js/app/queryCreator/modules/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fetchCollectionSchema,
fetchCollectionSchemaSuccess,
fetchCollectionSchemaError,
computeSchemaSuccess,
} from './actions';
import {
getEventsCollections,
Expand All @@ -15,6 +16,7 @@ import {
import {
FETCH_COLLECTION_SCHEMA,
FETCH_COLLECTION_SCHEMA_SUCCESS,
SCHEMA_COMPUTED,
} from './constants';
import { ReducerState, FetchCollectionSchemaAction } from './types';

Expand All @@ -30,7 +32,12 @@ export {
getCollectionSchema,
getSchemas,
getSchemaLoading,
computeSchemaSuccess,
ReducerState,
FetchCollectionSchemaAction,
};
export { FETCH_COLLECTION_SCHEMA, FETCH_COLLECTION_SCHEMA_SUCCESS };
export {
FETCH_COLLECTION_SCHEMA,
FETCH_COLLECTION_SCHEMA_SUCCESS,
SCHEMA_COMPUTED,
};
20 changes: 15 additions & 5 deletions lib/js/app/queryCreator/modules/events/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ReducerState, EventsActions } from './types';

import { createTree } from '../../utils/createTree';
import { createCollection } from '../../utils/createCollection';

import {
SET_EVENTS_COLLECTIONS,
SET_COLLETION_SCHEMA_LOADING,
FETCH_COLLECTION_SCHEMA_SUCCESS,
SCHEMA_COMPUTED,
} from './constants';

export const initialState: ReducerState = {
Expand All @@ -29,15 +27,27 @@ export const eventsReducer = (
(collection) => collection !== action.payload.colletion
),
};
case SCHEMA_COMPUTED:
return {
...state,
schemas: {
...state.schemas,
[action.payload.collection]: {
...state.schemas[action.payload.collection],
...action.payload.schema,
},
},
};
case FETCH_COLLECTION_SCHEMA_SUCCESS:
return {
...state,
schemas: {
...state.schemas,
[action.payload.collection]: {
...state.schemas[action.payload.collection],
schema: action.payload.schema,
tree: createTree(action.payload.schema),
list: createCollection(action.payload.schema),
tree: {},
list: {},
},
},
};
Expand Down
12 changes: 11 additions & 1 deletion lib/js/app/queryCreator/modules/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FETCH_COLLECTION_SCHEMA_ERROR,
FETCH_COLLECTION_SCHEMA_SUCCESS,
SET_COLLETION_SCHEMA_LOADING,
SCHEMA_COMPUTED,
} from './constants';

export type CollectionSchema = {
Expand All @@ -18,6 +19,14 @@ export type ReducerState = {
schemas: Record<string, CollectionSchema>;
};

export interface SchemaComputedAction {
type: typeof SCHEMA_COMPUTED;
payload: {
collection: string;
schema: Partial<CollectionSchema>;
};
}

export interface SetCollectionSchemaLoadingAction {
type: typeof SET_COLLETION_SCHEMA_LOADING;
payload: {
Expand Down Expand Up @@ -60,4 +69,5 @@ export type EventsActions =
| SetEventsCollectionsAction
| FetchCollectionSchemaAction
| FetchCollectionSchemaSuccessAction
| FetchCollectionSchemaErrorAction;
| FetchCollectionSchemaErrorAction
| SchemaComputedAction;
40 changes: 32 additions & 8 deletions lib/js/app/queryCreator/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
takeLatest,
getContext,
fork,
take,
put,
} from 'redux-saga/effects';
import moment from 'moment-timezone';
Expand Down Expand Up @@ -51,12 +52,17 @@ import {
setEventsCollections,
setCollectionSchemaLoading,
getSchemas,
computeSchemaSuccess,
FETCH_COLLECTION_SCHEMA_SUCCESS,
FETCH_COLLECTION_SCHEMA,
SCHEMA_COMPUTED,
} from './modules/events';

import { serializeOrderBy, serializeFilters } from './serializers';

import { createTree } from './utils/createTree';
import { createCollection } from './utils/createCollection';

import { Filter, OrderBy } from './types';
import { SetGroupByAction } from './modules/query/types';

Expand All @@ -83,6 +89,21 @@ function* fetchProject() {
}
}

function* transformSchema(
collection: string,
properties: Record<string, string>
) {
const tree = yield createTree(properties);
const list = yield createCollection(properties);

const schema = {
tree,
list,
};

yield put(computeSchemaSuccess(collection, schema));
}

function* fetchSchema(action: FetchCollectionSchemaAction) {
const collection = action.payload.collection;
const client = yield getContext('keenClient');
Expand All @@ -96,6 +117,7 @@ function* fetchSchema(action: FetchCollectionSchemaAction) {
const { properties } = yield fetch(url).then((response) => response.json());

yield put(fetchCollectionSchemaSuccess(collection, properties));
yield fork(transformSchema, collection, properties);
} catch (err) {
yield put(fetchCollectionSchemaError(err));
} finally {
Expand Down Expand Up @@ -149,16 +171,18 @@ function* storeEventSchemas() {
}

function* transformFilters(collection: string, filters: Filter[]) {
const schemas = yield select(getSchemas);
let schemas = yield select(getSchemas);
let collectionSchema = schemas[collection];

if (!collectionSchema) {
const client = yield getContext('keenClient');
const url = client.url(`/3.0/projects/{projectId}/events/${collection}`, {
api_key: client.config.readKey,
});
const { properties } = yield fetch(url).then((response) => response.json());
collectionSchema = { schema: properties };
while (true) {
yield take(FETCH_COLLECTION_SCHEMA_SUCCESS);
schemas = yield select(getSchemas);
if (schemas[collection]) {
collectionSchema = schemas[collection];
break;
}
}
}

const { schema } = collectionSchema;
Expand Down Expand Up @@ -260,7 +284,7 @@ function* watcher() {
yield takeLatest(FETCH_COLLECTION_SCHEMA, fetchSchema);
yield takeLatest(SELECT_TIMEZONE, selectTimezone);
yield takeLatest(SELECT_EVENT_COLLECTION, selectCollection);
yield takeLatest(FETCH_COLLECTION_SCHEMA_SUCCESS, storeEventSchemas);
yield takeLatest(SCHEMA_COMPUTED, storeEventSchemas);
yield takeLatest(
SELECT_FUNNEL_STEP_EVENT_COLLECTION,
selectFunnelStepCollection
Expand Down

0 comments on commit 2b6b53b

Please sign in to comment.