Skip to content

Daily Active User Metrics #3735

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

Merged
merged 28 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
502aa52
agent: add ConnStats
ammario Aug 29, 2022
3893045
agent: add StatsReporter
ammario Aug 29, 2022
c03ad90
Frontend tests pass
ammario Sep 1, 2022
1bd9cec
Split DAUChart into its own file
ammario Sep 1, 2022
e46329b
Get FE tests passing with real data!
ammario Sep 1, 2022
d472b13
Test frontend
ammario Sep 1, 2022
e0295e0
Fix compilation error
ammario Sep 1, 2022
2f1a423
Rename ConnStats to StatsConn
ammario Sep 1, 2022
0a50cc9
continues instead of returns
ammario Sep 1, 2022
7feab5e
Fix some test failures
ammario Sep 1, 2022
a4d2cf7
Redo tests
ammario Sep 1, 2022
52c9d10
Address review comments
ammario Sep 1, 2022
3f9901e
REVAMP — backend tests work
ammario Sep 1, 2022
7840509
Black triangle
ammario Sep 1, 2022
39170cf
Consolidate template state machines
ammario Sep 1, 2022
eb373d6
Move workspaceagent endpoint
ammario Sep 1, 2022
a3d87b8
Address most review comments
ammario Sep 1, 2022
31ba0c6
Improve contrast in chart
ammario Sep 1, 2022
5b906c1
Add more agent tests
ammario Sep 1, 2022
49d9386
Fix JS ci
ammario Sep 1, 2022
b14a077
A bunch of minor touch ups
ammario Sep 1, 2022
8da24c4
Stabilize protoc
ammario Sep 1, 2022
00ec953
Merge remote-tracking branch 'origin/main' into metrics
ammario Sep 1, 2022
4940319
Update lockfile
ammario Sep 1, 2022
22b2028
Address comments + attempt to fix protoc
ammario Sep 1, 2022
0157365
fixup! Address comments + attempt to fix protoc
ammario Sep 1, 2022
b166cdd
Try to fix protoc...
ammario Sep 1, 2022
4201998
PROTO!
ammario Sep 1, 2022
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
Prev Previous commit
Next Next commit
Black triangle
  • Loading branch information
ammario committed Sep 1, 2022
commit 784050938400f74ba552d0bad2112c45571820fb
6 changes: 4 additions & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
return response.data
}

export const getDAUs = async (): Promise<TypesGen.DAUsResponse> => {
const response = await axios.get("/api/v2/metrics/daus")
export const getTemplateDAUs = async (
templateId: string,
): Promise<TypesGen.TemplateDAUsResponse> => {
const response = await axios.get(`/api/v2/templates/${templateId}/daus`)
return response.data
}
10 changes: 5 additions & 5 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ export interface DAUEntry {
readonly daus: number
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: DAUEntry.daus feels a bit weird. Would also prefer for us to use consistent casing if possible: DAU vs. dau

Maybe total?

Copy link
Member Author

Choose a reason for hiding this comment

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

The casing is an unfortunate side effect of the code generator, which is outside the scope of this PR. I can rename to total if you feel strongly, but I think daus is clear enough.

}

// From codersdk/metrics.go
export interface DAUsResponse {
readonly entries: DAUEntry[]
}

// From codersdk/workspaceresources.go
export interface DERPRegion {
readonly preferred: boolean
Expand Down Expand Up @@ -380,6 +375,11 @@ export interface Template {
readonly created_by_name: string
}

// From codersdk/metrics.go
export interface TemplateDAUsResponse {
readonly entries: DAUEntry[]
}

// From codersdk/templateversions.go
export interface TemplateVersion {
readonly id: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("DAUChart", () => {
it("renders a helpful paragraph on empty state", async () => {
render(
<DAUChart
userMetricsData={{
templateMetricsData={{
entries: [],
}}
/>,
Expand All @@ -23,7 +23,7 @@ describe("DAUChart", () => {
it("renders a graph", async () => {
render(
<DAUChart
userMetricsData={{
templateMetricsData={{
entries: [{ date: "2020-01-01", daus: 1 }],
}}
/>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ import * as TypesGen from "../../api/typesGenerated"
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend)

export interface DAUChartProps {
userMetricsData: TypesGen.DAUsResponse
templateMetricsData: TypesGen.TemplateDAUsResponse
}
export const Language = {
loadingText: "DAU stats are loading. Check back later.",
chartTitle: "Daily Active Users",
}

export const DAUChart: FC<DAUChartProps> = ({ userMetricsData }) => {
export const DAUChart: FC<DAUChartProps> = ({ templateMetricsData }) => {
const theme: Theme = useTheme()

if (userMetricsData.entries.length === 0) {
if (templateMetricsData.entries.length === 0) {
return (
<div style={{ marginTop: "-20px" }}>
<p>{Language.loadingText}</p>
</div>
)
}

const labels = userMetricsData.entries.map((val) => {
const labels = templateMetricsData.entries.map((val) => {
return moment(val.date).format("l")
})

const data = userMetricsData.entries.map((val) => {
const data = templateMetricsData.entries.map((val) => {
return val.daus
})

Expand All @@ -70,7 +70,7 @@ export const DAUChart: FC<DAUChartProps> = ({ userMetricsData }) => {
ticks: {},
},
},
aspectRatio: 6 / 1,
aspectRatio: 10 / 1,
} as ChartOptions

return (
Expand Down
4 changes: 4 additions & 0 deletions site/src/pages/TemplatePage/TemplatePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
} from "../../testHelpers/renderHelpers"
import { TemplatePage } from "./TemplatePage"

Object.defineProperty(window, "ResizeObserver", {
value: ResizeObserver,
})

Comment on lines +16 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment as above, would be best to keep this in a setup and teardown function

describe("TemplatePage", () => {
it("shows the template name, readme and resources", async () => {
// Mocking the dayjs module within the createDayString file
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/TemplatePage/TemplatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const TemplatePage: FC<React.PropsWithChildren<unknown>> = () => {
organizationId,
},
})

const { template, activeTemplateVersion, templateResources, templateVersions } =
templateState.context
const isLoading = !template || !activeTemplateVersion || !templateResources
Expand Down
12 changes: 12 additions & 0 deletions site/src/pages/TemplatePage/TemplatePageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import Link from "@material-ui/core/Link"
import { makeStyles } from "@material-ui/core/styles"
import AddCircleOutline from "@material-ui/icons/AddCircleOutline"
import SettingsOutlined from "@material-ui/icons/SettingsOutlined"
import { useMachine } from "@xstate/react"
import frontMatter from "front-matter"
import { FC } from "react"
import ReactMarkdown from "react-markdown"
import { Link as RouterLink } from "react-router-dom"
import { firstLetter } from "util/firstLetter"
import { templateMetricsMachine } from "xServices/templateMetrics/templateMetricsXService"
import { Template, TemplateVersion, WorkspaceResource } from "../../api/typesGenerated"
import { Margins } from "../../components/Margins/Margins"
import {
Expand All @@ -21,6 +23,7 @@ import { TemplateResourcesTable } from "../../components/TemplateResourcesTable/
import { TemplateStats } from "../../components/TemplateStats/TemplateStats"
import { VersionsTable } from "../../components/VersionsTable/VersionsTable"
import { WorkspaceSection } from "../../components/WorkspaceSection/WorkspaceSection"
import { DAUChart } from "./DAUCharts"

const Language = {
settingsButton: "Settings",
Expand Down Expand Up @@ -48,6 +51,13 @@ export const TemplatePageView: FC<React.PropsWithChildren<TemplatePageViewProps>
const readme = frontMatter(activeTemplateVersion.readme)
const hasIcon = template.icon && template.icon !== ""

const [metricsState] = useMachine(templateMetricsMachine, {
context: {
templateId: template.id,
},
})
const { templateMetricsData } = metricsState.context

const getStartedResources = (resources: WorkspaceResource[]) => {
return resources.filter((resource) => resource.workspace_transition === "start")
}
Expand Down Expand Up @@ -95,6 +105,8 @@ export const TemplatePageView: FC<React.PropsWithChildren<TemplatePageViewProps>
</Stack>
</PageHeader>

{templateMetricsData && <DAUChart templateMetricsData={templateMetricsData} />}

<Stack spacing={2.5}>
<TemplateStats template={template} activeVersion={activeTemplateVersion} />
<WorkspaceSection
Expand Down
5 changes: 0 additions & 5 deletions site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { fireEvent, screen, waitFor, within } from "@testing-library/react"
import { rest } from "msw"
import { ResizeObserver } from "resize-observer"
import { Language as usersXServiceLanguage } from "xServices/users/usersXService"
import * as API from "../../api/api"
import { Role } from "../../api/typesGenerated"
Expand All @@ -21,10 +20,6 @@ import { permissionsToCheck } from "../../xServices/auth/authXService"
import { Language as UsersPageLanguage, UsersPage } from "./UsersPage"
import { Language as UsersViewLanguage } from "./UsersPageView"

Object.defineProperty(window, "ResizeObserver", {
value: ResizeObserver,
})

const suspendUser = async (setupActionSpies: () => void) => {
// Get the first user in the table
const users = await screen.findAllByText(/.*@coder.com/)
Expand Down
7 changes: 1 addition & 6 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useActor, useMachine } from "@xstate/react"
import { useActor } from "@xstate/react"
import { FC, ReactNode, useContext, useEffect } from "react"
import { Helmet } from "react-helmet-async"
import { useNavigate } from "react-router"
import { useSearchParams } from "react-router-dom"
import { userMetricsMachine } from "xServices/userMetrics/userMetricsXService"
import { ConfirmDialog } from "../../components/ConfirmDialog/ConfirmDialog"
import { ResetPasswordDialog } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
import { userFilterQuery } from "../../util/filters"
Expand Down Expand Up @@ -45,9 +44,6 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
const [rolesState, rolesSend] = useActor(xServices.siteRolesXService)
const { roles } = rolesState.context

const [metricsState] = useMachine(userMetricsMachine)
const { userMetricsData } = metricsState.context

// Is loading if
// - permissions are loading or
// - users are loading or
Expand Down Expand Up @@ -83,7 +79,6 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
<title>{pageTitle("Users")}</title>
</Helmet>
<UsersPageView
userMetricsData={userMetricsData}
roles={roles}
users={users}
openUserCreationDialog={() => {
Expand Down
5 changes: 0 additions & 5 deletions site/src/pages/UsersPage/UsersPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { PageHeader, PageHeaderTitle } from "../../components/PageHeader/PageHea
import { SearchBarWithFilter } from "../../components/SearchBarWithFilter/SearchBarWithFilter"
import { UsersTable } from "../../components/UsersTable/UsersTable"
import { userFilterQuery } from "../../util/filters"
import { DAUChart } from "./DAUCharts"

export const Language = {
pageTitle: "Users",
Expand All @@ -16,7 +15,6 @@ export const Language = {
allUsersFilterName: "All users",
}
export interface UsersPageViewProps {
userMetricsData?: TypesGen.DAUsResponse
users?: TypesGen.User[]
roles?: TypesGen.AssignableRoles[]
filter?: string
Expand All @@ -34,7 +32,6 @@ export interface UsersPageViewProps {
}

export const UsersPageView: FC<React.PropsWithChildren<UsersPageViewProps>> = ({
userMetricsData,
users,
roles,
openUserCreationDialog,
Expand Down Expand Up @@ -69,8 +66,6 @@ export const UsersPageView: FC<React.PropsWithChildren<UsersPageViewProps>> = ({
<PageHeaderTitle>{Language.pageTitle}</PageHeaderTitle>
</PageHeader>

{userMetricsData && <DAUChart userMetricsData={userMetricsData} />}

<div style={{ marginTop: "15px" }}>
<SearchBarWithFilter
filter={filter}
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FieldError } from "api/errors"
import * as Types from "../api/types"
import * as TypesGen from "../api/typesGenerated"

export const MockGetDAUResponse: TypesGen.DAUsResponse = {
export const MockTemplateDAUResponse: TypesGen.TemplateDAUsResponse = {
entries: [
{ date: "2022-08-27T00:00:00Z", daus: 1 },
{ date: "2022-08-29T00:00:00Z", daus: 2 },
Expand Down
4 changes: 2 additions & 2 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { permissionsToCheck } from "../xServices/auth/authXService"
import * as M from "./entities"

export const handlers = [
rest.get("/api/v2/metrics/daus", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockGetDAUResponse))
rest.get("/api/v2/templates/:templateId/daus", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockTemplateDAUResponse))
}),

// build info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { DAUsResponse } from "api/typesGenerated"
import { TemplateDAUsResponse } from "api/typesGenerated"
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"

export interface UserMetricsContext {
userMetricsData: DAUsResponse
export interface TemplateMetricsContext {
templateId: string
templateMetricsData: TemplateDAUsResponse
}

export const userMetricsMachine = createMachine(
export const templateMetricsMachine = createMachine(
{
id: "userMetrics",
id: "templateMetrics",
schema: {
context: {} as UserMetricsContext,
context: {} as TemplateMetricsContext,
services: {} as {
loadMetrics: {
data: DAUsResponse
data: TemplateDAUsResponse
}
},
},
tsTypes: {} as import("./userMetricsXService.typegen").Typegen0,
tsTypes: {} as import("./templateMetricsXService.typegen").Typegen0,
initial: "loadingMetrics",
states: {
loadingMetrics: {
Expand All @@ -37,11 +38,11 @@ export const userMetricsMachine = createMachine(
{
actions: {
assignDataMetrics: assign({
userMetricsData: (_, event) => event.data,
templateMetricsData: (_, event) => event.data,
}),
},
services: {
loadMetrics: () => API.getDAUs,
loadMetrics: (context) => API.getTemplateDAUs(context.templateId),
},
},
)