Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to new analytics #321

Merged
merged 16 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/deployDemo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ jobs:
# `--acl=public-read` is neceesary because the prod account is uploading the files to the staging bucket,
# which prevents the default bucket policy from applying to these objects
- name: Upload to S3
run: "aws s3 cp ./demoRepo/workspaces/ui/build/ s3://$BUCKET/ --recursive --sse=AES256 --acl=public-read"
run: |
aws s3 cp ./demoRepo/workspaces/ui/build/index.html "s3://$BUCKET" \
--sse=AES256 \
--acl=public-read \
--metadata='{"Cache-Control":"no-store"}' \
--cache-control=no-store
aws s3 cp ./demoRepo/workspaces/ui/build/ "s3://$BUCKET/" \
--recursive \
--exclude=index.html \
--sse=AES256 \
--acl=public-read
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"bump-core-snapshot": "node ./workspaces/scripts/bump-core-local.js"
},
"workspaces": [
"workspaces/analytics",
"workspaces/saas-types",
"workspaces/cli-config",
"workspaces/cli-shared",
Expand Down
2 changes: 1 addition & 1 deletion sourceme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export OPTIC_SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && p
echo "Optic development scripts will run from $OPTIC_SRC_DIR"
export OPTIC_DEBUG_ENV_FILE="$OPTIC_SRC_DIR/.env"

alias apidev="OPTIC_DAEMON_ENABLE_DEBUGGING=yes OPTIC_UI_HOST=http://localhost:3000 OPTIC_AUTH_UI_HOST=http://localhost:4005 $OPTIC_SRC_DIR/workspaces/local-cli/bin/run"
alias apidev="OPTIC_DAEMON_ENABLE_DEBUGGING=yes OPTIC_DEVELOPMENT=yes OPTIC_UI_HOST=http://localhost:3000 OPTIC_AUTH_UI_HOST=http://localhost:4005 $OPTIC_SRC_DIR/workspaces/local-cli/bin/run"
alias apistage="OPTIC_DAEMON_ENABLE_DEBUGGING=yes $OPTIC_SRC_DIR/workspaces/local-cli/bin/run"
alias cidev="$OPTIC_SRC_DIR/workspaces/ci-cli/bin/run"
alias agentdev="$OPTIC_SRC_DIR/workspaces/agent-cli/bin/run"
Expand Down
4 changes: 2 additions & 2 deletions workspaces/agent-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useoptic/agent-cli",
"version": "8.2.11",
"version": "8.3.0",
"author": "@useoptic",
"bin": {
"optic-agent": "./bin/run"
Expand All @@ -15,7 +15,7 @@
"@oclif/command": "^1",
"@oclif/config": "^1",
"@oclif/plugin-help": "^3",
"@useoptic/cli-shared": "8.2.11",
"@useoptic/cli-shared": "8.3.0",
"dotenv": "^8.2.0",
"jwt-decode": "^2.2.0",
"tslib": "^1",
Expand Down
4 changes: 4 additions & 0 deletions workspaces/analytics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
node_modules
.idea
lib
11 changes: 11 additions & 0 deletions workspaces/analytics/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { defaults: tsjPreset } = require('ts-jest/presets');
// const { jsWithTs: tsjPreset } = require('ts-jest/presets');
// const { jsWithBabel: tsjPreset } = require('ts-jest/presets');

module.exports = {
// [...]
transform: {
...tsjPreset.transform,
// [...]
}
}
31 changes: 31 additions & 0 deletions workspaces/analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@useoptic/analytics",
"version": "8.3.0",
"scripts": {
"ws:build": "tsc -b --verbose",
"ws:clean": "rm -rf build/*",
"ws:test": "jest"
},
"repository": {
"type": "git",
"url": "ssh://git@github.com/useoptic/optic-package.git",
"directory": "workspaces/cli-client"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"dependencies": {
"@hapi/joi": "^17.1.1",
"joi-extract-type": "^15.0.8",
"keymirror": "^0.1.1",
"uuid": "^8.3.0"
},
"devDependencies": {
"@types/jest": "^26.0.9",
"@types/node": "^14.0.27",
"jest": "^26.4.0",
"ts-jest": "^26.2.0"
},
"files": [
"/lib"
]
}
2 changes: 2 additions & 0 deletions workspaces/analytics/src/consistentAnonymousId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { v4 as uuidv4 } from 'uuid';
export const consistentAnonymousId = `anonymous_user_${uuidv4()}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

might want to document here that it must start with anonymous_, or capture that in some test

33 changes: 33 additions & 0 deletions workspaces/analytics/src/eventbus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { EventEmitter } from 'events';
import { v4 as uuidv4 } from 'uuid';

import {
ClientContext,
TrackingEventBase,
} from './interfaces/TrackingEventBase';
import exp from 'constants';

export function newAnalyticsEventBus(
getContext: (batchId: string) => Promise<ClientContext>
): AnalyticsEventBus {
const eventEmitter = new EventEmitter();
return {
emit: async (...events: TrackingEventBase<any>[]) => {
const batchId = uuidv4();
const context = await getContext(batchId);
events.forEach((event) => {
eventEmitter.emit('event', { ...event, context });
});
},
eventEmitter,
listen: (callback: (e: TrackingEventBase<any>) => void): void => {
eventEmitter.on('event', callback);
},
};
}

export interface AnalyticsEventBus {
eventEmitter: EventEmitter;
emit: (...events: TrackingEventBase<any>[]) => void;
listen: (callback: (e: TrackingEventBase<any>) => void) => void;
}
227 changes: 227 additions & 0 deletions workspaces/analytics/src/events/diffs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import { DescribeEvent, RegisteredEvent } from '../interfaces/RegisterEvent';
import { Events } from '../interfaces/Events';
// @ts-ignore
import * as Joi from '@hapi/joi';
import 'joi-extract-type';

// Sent whenever an API is created
const UserChangedCaptureOverviewTabSchema = Joi.object({
currentTab: Joi.string().required(),
diffCount: Joi.number().required(),
undocumentedUrlCount: Joi.number().required(),
});
type UserChangedCaptureOverviewTabProperties = Joi.extractType<
typeof UserChangedCaptureOverviewTabSchema
>;
export const UserChangedCaptureOverviewTab = DescribeEvent<
UserChangedCaptureOverviewTabProperties
>(
Events.UserChangedCaptureOverviewTab,
UserChangedCaptureOverviewTabSchema,
(props) =>
`User changed to ${props.currentTab} and there are ${props.diffCount} diffs + ${props.undocumentedUrlCount} undocumented urls`
);

const NewBodyDiffRenderedSchema = Joi.object({
requestCount: Joi.number().required(),
responseCount: Joi.number().required(),
regions: Joi.array().required().items(Joi.string()),
});
type NewBodyDiffRenderedProperties = Joi.extractType<
typeof NewBodyDiffRenderedSchema
>;
export const NewBodyDiffRendered = DescribeEvent<NewBodyDiffRenderedProperties>(
Events.NewBodyDiffRendered,
NewBodyDiffRenderedSchema,
(props) => `Diff was rendered in regions ${props.regions.join(', \n')}`
);

const InferPolymorphismEnabledSchema = Joi.object({
captureId: Joi.string().required(),
endpointId: Joi.string().required(),
});
type InferPolymorphismEnabledProperties = Joi.extractType<
typeof InferPolymorphismEnabledSchema
>;
export const UserEnabledInferPolymorphism = DescribeEvent<
InferPolymorphismEnabledProperties
>(
Events.UserEnabledInferPolymorphism,
InferPolymorphismEnabledSchema,
(props) =>
`Infer Polymorphism was turned on for ${props.captureId}, endpoint ${props.endpointId}`
);

const UserBeganAddingNewUrlSchema = Joi.object({
captureId: Joi.string().required(),
method: Joi.string().required(),
path: Joi.string().required(),
knownPathId: Joi.string().optional(),
});
type UserBeganAddingNewUrlProperties = Joi.extractType<
typeof UserBeganAddingNewUrlSchema
>;
export const UserBeganAddingNewUrl = DescribeEvent<
UserBeganAddingNewUrlProperties
>(
Events.UserBeganAddingNewUrl,
UserBeganAddingNewUrlSchema,
(props) =>
`User Began Adding New Url ${props.method} ${props.path} in ${props.captureId}`
);

const UserFinishedAddingNewUrlSchema = Joi.object({
purpose: Joi.string().required(),
captureId: Joi.string().required(),
method: Joi.string().required(),
pathExpression: Joi.string().optional(),
});
type UserFinishedAddingNewUrlProperties = Joi.extractType<
typeof UserFinishedAddingNewUrlSchema
>;
export const UserFinishedAddingNewUrl = DescribeEvent<
UserFinishedAddingNewUrlProperties
>(
Events.UserFinishedAddingNewUrl,
UserFinishedAddingNewUrlSchema,
(props) =>
`User Added New Url ${props.method} ${props.pathExpression} in ${props.captureId} with purpose '${props.pathExpression}'`
);

const SuggestionDisplayedSchema = Joi.object({
suggestion: Joi.string().required(),
});
type SuggestionDisplayedProperties = Joi.extractType<
typeof SuggestionDisplayedSchema
>;
export const SuggestionDisplayed = DescribeEvent<SuggestionDisplayedProperties>(
Events.SuggestionDisplayed,
SuggestionDisplayedSchema,
(props) =>
`Summary: '${props.suggestion}' `
);



const SuggestionAcceptedSchema = Joi.object({
captureId: Joi.string().required(),
suggestion: Joi.string().required(),
});
type SuggestionAcceptedProperties = Joi.extractType<
typeof SuggestionAcceptedSchema
>;
export const UserAcceptedSuggestion = DescribeEvent<
SuggestionAcceptedProperties
>(
Events.UserAcceptedSuggestion,
SuggestionAcceptedSchema,
(props) => `Suggestion to '${props.suggestion}' was accepted`
);

const UserPreviewedSuggestionSchema = Joi.object({
captureId: Joi.string().required(),
diff: Joi.string().required(),
diffAssertion: Joi.string().required(),
suggestion: Joi.string().required(),
});
type UserPreviewedSuggestionProperties = Joi.extractType<
typeof UserPreviewedSuggestionSchema
>;
export const UserPreviewedSuggestion = DescribeEvent<
UserPreviewedSuggestionProperties
>(
Events.UserPreviewedSuggestion,
UserPreviewedSuggestionSchema,
(props) =>
`Suggestion to '${props.suggestion}' was previewed in response to diff: ${props.diff}`
);

const UserResetDiffSchema = Joi.object({});
type UserResetDiffProperties = Joi.extractType<typeof UserResetDiffSchema>;
export const UserResetDiff = DescribeEvent<UserResetDiffProperties>(
Events.UserResetDiff,
UserResetDiffSchema,
(props) => `The Diff Was Reset`
);

const ShowCommitCardSchema = Joi.object({
message: Joi.string().required(),
captureId: Joi.string().required(),
suggestions: Joi.number().required(),
});
type ShowCommitCardProperties = Joi.extractType<
typeof ShowCommitCardSchema
>;
export const ShowCommitCard = DescribeEvent<ShowCommitCardProperties>(
Events.ShowCommitCard,
ShowCommitCardSchema,
(props) => `Commit Card Rendered`
);

const UserCommittedChangesSchema = Joi.object({
message: Joi.string().required(),
captureId: Joi.string().required(),
suggestions: Joi.number().required(),
});
type UserCommittedChangesProperties = Joi.extractType<
typeof UserCommittedChangesSchema
>;
export const UserCommittedChanges = DescribeEvent<
UserCommittedChangesProperties
>(
Events.UserCommittedChanges,
UserCommittedChangesSchema,
(props) => `The Diff Was Reset`
);

const AddUrlModalNamingSchema = Joi.object({
method: Joi.string().required(),
path: Joi.string().required(),
});
type AddUrlModalNamingProperties = Joi.extractType<
typeof AddUrlModalNamingSchema
>;
export const AddUrlModalNaming = DescribeEvent<AddUrlModalNamingProperties>(
Events.AddUrlModalNaming,
AddUrlModalNamingSchema,
(props) => `Currently naming ${props.method} ${props.path}`
);

const AddUrlModalIdentifyingPathComponentsSchema = Joi.object({
method: Joi.string().required(),
path: Joi.string().required(),
});
type AddUrlModalIdentifyingPathComponentsProperties = Joi.extractType<
typeof AddUrlModalIdentifyingPathComponentsSchema
>;
export const AddUrlModalIdentifyingPathComponents = DescribeEvent<AddUrlModalIdentifyingPathComponentsProperties>(
Events.AddUrlModalIdentifyingPathComponents,
AddUrlModalIdentifyingPathComponentsSchema,
(props) => `Currently identifying path components for ${props.method} ${props.path}`
);

const ShowInitialDocumentingViewSchema = Joi.object({
});
type ShowInitialDocumentingViewProperties = Joi.extractType<
typeof ShowInitialDocumentingViewSchema
>;
export const ShowInitialDocumentingView = DescribeEvent<ShowInitialDocumentingViewProperties>(
Events.ShowInitialDocumentingView,
ShowInitialDocumentingViewSchema,
() => `Showing initial documenting view`
);

const UpdateContributionSchema = Joi.object({
id: Joi.string().required(),
purpose: Joi.string().required(),
value: Joi.string().required(),
});
type UpdateContributionProperties = Joi.extractType<
typeof UpdateContributionSchema
>;
export const UpdateContribution = DescribeEvent<UpdateContributionProperties>(
Events.UpdateContribution,
UpdateContributionSchema,
({id, purpose, value}) => `Updated ${id} - ${purpose} to be ${value}`
);

34 changes: 34 additions & 0 deletions workspaces/analytics/src/events/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DescribeEvent, RegisteredEvent } from '../interfaces/RegisterEvent';
import { Events } from '../interfaces/Events';
// @ts-ignore
import * as Joi from '@hapi/joi';
import 'joi-extract-type';

const RequirementForDiffsToHaveASuggestionFailedSchema = Joi.object({
diff: Joi.string().required(),
});
type RequirementForDiffsToHaveASuggestionFailedProperties = Joi.extractType<
typeof RequirementForDiffsToHaveASuggestionFailedSchema
>;
export const RequirementForDiffsToHaveASuggestionFailed = DescribeEvent<
RequirementForDiffsToHaveASuggestionFailedProperties
>(
Events.RequirementForDiffsToHaveASuggestionFailed,
RequirementForDiffsToHaveASuggestionFailedSchema,
(props) => `Suggestions should never be empty for diff: ${props.diff}`
);

const JavascriptErrorDetectedInFrontendSchema = Joi.object({
message: Joi.string().required(),
stack: Joi.string().required(),
});
type JavascriptErrorDetectedInFrontendProperties = Joi.extractType<
typeof JavascriptErrorDetectedInFrontendSchema
>;
export const JavascriptErrorDetectedInFrontend = DescribeEvent<
JavascriptErrorDetectedInFrontendProperties
>(
Events.JavascriptErrorDetectedInFrontend,
JavascriptErrorDetectedInFrontendSchema,
(props) => `Error on Frontend: ${props.message}`
);
Loading