Skip to content

Commit

Permalink
Add 'telemetryMiddleware' and a preliminary 'start' telemetry action
Browse files Browse the repository at this point in the history
  • Loading branch information
abrauninger committed Oct 5, 2018
1 parent 174aaa9 commit 117f83e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/TelemetryActionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TODO: Move to a different location?

export const enum TelemetryActionType {
START = 'TELEMETRY/START',
}
7 changes: 7 additions & 0 deletions src/TelemetryActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// TODO: Move to a better location?

import { action } from 'typesafe-actions';
import { TelemetryActionType } from './TelemetryActionType';

export const start = () =>
action(TelemetryActionType.START);
7 changes: 6 additions & 1 deletion src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Store, createStore, applyMiddleware } from 'redux';
import { History } from 'history';

import { IQuestionnaireState, surveyReducer, routerMiddleware, startHistoryListener } from './store';
import { createTelemetrySession, telemetryMiddleware } from './telemetry';

export default function configureStore(history: History, initialState: IQuestionnaireState): Store<IQuestionnaireState> {
const session = createTelemetrySession();

const store = createStore(
surveyReducer,
initialState,
applyMiddleware(routerMiddleware(history)));
applyMiddleware(
routerMiddleware(history),
telemetryMiddleware(session)));

startHistoryListener(history, store);

Expand Down
47 changes: 47 additions & 0 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ActionType } from 'typesafe-actions';
import { TelemetryActionType } from './TelemetryActionType';
import * as telemetryActions from './TelemetryActions';

// Courtesy of 'broofa's answer in https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
/*function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}*/

function uniqueId(): string {
// TODO: This is almost certainly not a good way to generate IDs.
return Math.random().toString();
}

export class TelemetrySession {
private sessionId: string;

constructor() {
this.sessionId = uniqueId();
}

public recordStart() {
// TODO: Post to the API endpoint
}

// TODO: Remove
public getSessionId() {
return this.sessionId;
}
}

type TelemetryAction = ActionType<typeof telemetryActions>;

export function createTelemetrySession(): TelemetrySession {
return new TelemetrySession();
}

export const telemetryMiddleware = (session: TelemetrySession) => () => (next: any) => (action: TelemetryAction) => {
switch (action.type) {
case TelemetryActionType.START: {
session.recordStart();
break;
}
}
}

0 comments on commit 117f83e

Please sign in to comment.