|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { LoadingState } from "@podkit/loading/LoadingState"; |
| 8 | +import { Heading2, Subheading } from "@podkit/typography/Headings"; |
| 9 | +import classNames from "classnames"; |
| 10 | +import { useCallback, useMemo, useState } from "react"; |
| 11 | +import { Accordion } from "./components/accordion/Accordion"; |
| 12 | +import Alert from "./components/Alert"; |
| 13 | +import Header from "./components/Header"; |
| 14 | +import { Item, ItemField, ItemsList } from "./components/ItemsList"; |
| 15 | +import { useWorkspaceSessions } from "./data/insights/list-workspace-sessions-query"; |
| 16 | +import { WorkspaceSessionGroup } from "./insights/WorkspaceSessionGroup"; |
| 17 | +import { gitpodHostUrl } from "./service/service"; |
| 18 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@podkit/select/Select"; |
| 19 | +import dayjs from "dayjs"; |
| 20 | +import { Timestamp } from "@bufbuild/protobuf"; |
| 21 | +import { LoadingButton } from "@podkit/buttons/LoadingButton"; |
| 22 | +import { TextMuted } from "@podkit/typography/TextMuted"; |
| 23 | +import { DownloadInsightsToast } from "./insights/download/DownloadInsights"; |
| 24 | +import { useCurrentOrg } from "./data/organizations/orgs-query"; |
| 25 | +import { useToast } from "./components/toasts/Toasts"; |
| 26 | +import { useTemporaryState } from "./hooks/use-temporary-value"; |
| 27 | +import { DownloadIcon } from "lucide-react"; |
| 28 | +import { Button } from "@podkit/buttons/Button"; |
| 29 | + |
| 30 | +export const Insights = () => { |
| 31 | + const [prebuildsFilter, setPrebuildsFilter] = useState<"week" | "month" | "year">("week"); |
| 32 | + const [upperBound, lowerBound] = useMemo(() => { |
| 33 | + const from = dayjs().subtract(1, prebuildsFilter).startOf("day"); |
| 34 | + |
| 35 | + const fromTimestamp = Timestamp.fromDate(from.toDate()); |
| 36 | + const toTimestamp = Timestamp.fromDate(new Date()); |
| 37 | + return [fromTimestamp, toTimestamp]; |
| 38 | + }, [prebuildsFilter]); |
| 39 | + const { |
| 40 | + data, |
| 41 | + error: errorMessage, |
| 42 | + isLoading, |
| 43 | + isFetchingNextPage, |
| 44 | + hasNextPage, |
| 45 | + fetchNextPage, |
| 46 | + } = useWorkspaceSessions({ |
| 47 | + from: upperBound, |
| 48 | + to: lowerBound, |
| 49 | + }); |
| 50 | + |
| 51 | + const hasMoreThanOnePage = (data?.pages.length ?? 0) > 1; |
| 52 | + const sessions = useMemo(() => data?.pages.flatMap((p) => p) ?? [], [data]); |
| 53 | + const grouped = Object.groupBy(sessions, (ws) => ws.workspace?.id ?? "unknown"); |
| 54 | + const [page, setPage] = useState(0); |
| 55 | + |
| 56 | + return ( |
| 57 | + <> |
| 58 | + <Header title="Insights" subtitle="Insights into workspace sessions in your organization" /> |
| 59 | + <div className="app-container pt-5"> |
| 60 | + <div |
| 61 | + className={classNames( |
| 62 | + "flex flex-col items-start space-y-3 justify-between", |
| 63 | + "md:flex-row md:items-center md:space-x-4 md:space-y-0", |
| 64 | + )} |
| 65 | + > |
| 66 | + <Select value={prebuildsFilter} onValueChange={(v) => setPrebuildsFilter(v as any)}> |
| 67 | + <SelectTrigger className="w-[180px]"> |
| 68 | + <SelectValue placeholder="Select time range" /> |
| 69 | + </SelectTrigger> |
| 70 | + <SelectContent> |
| 71 | + <SelectItem value="week">Last 7 days</SelectItem> |
| 72 | + <SelectItem value="month">Last 30 days</SelectItem> |
| 73 | + <SelectItem value="year">Last 365 days</SelectItem> |
| 74 | + </SelectContent> |
| 75 | + </Select> |
| 76 | + <DownloadUsage from={upperBound} to={lowerBound} /> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div |
| 80 | + className={classNames( |
| 81 | + "flex flex-col items-start space-y-3 justify-between px-3", |
| 82 | + "md:flex-row md:items-center md:space-x-4 md:space-y-0", |
| 83 | + )} |
| 84 | + ></div> |
| 85 | + |
| 86 | + {errorMessage && ( |
| 87 | + <Alert type="error" className="mt-4"> |
| 88 | + {errorMessage instanceof Error ? errorMessage.message : "An error occurred."} |
| 89 | + </Alert> |
| 90 | + )} |
| 91 | + |
| 92 | + <div className="flex flex-col w-full mb-8"> |
| 93 | + <ItemsList className="mt-2 text-pk-content-secondary"> |
| 94 | + <Item header={false} className="grid grid-cols-12 gap-x-3 bg-pk-surface-tertiary"> |
| 95 | + <ItemField className="col-span-2 my-auto"> |
| 96 | + <span>Type</span> |
| 97 | + </ItemField> |
| 98 | + <ItemField className="col-span-5 my-auto"> |
| 99 | + <span>ID</span> |
| 100 | + </ItemField> |
| 101 | + <ItemField className="col-span-3 my-auto"> |
| 102 | + <span>User</span> |
| 103 | + </ItemField> |
| 104 | + <ItemField className="col-span-2 my-auto"> |
| 105 | + <span>Sessions</span> |
| 106 | + </ItemField> |
| 107 | + </Item> |
| 108 | + |
| 109 | + {isLoading && ( |
| 110 | + <div className="flex items-center justify-center w-full space-x-2 text-pk-content-primary text-sm pt-16 pb-40"> |
| 111 | + <LoadingState /> |
| 112 | + <span>Loading usage...</span> |
| 113 | + </div> |
| 114 | + )} |
| 115 | + |
| 116 | + {!isLoading && ( |
| 117 | + <Accordion type="multiple" className="w-full"> |
| 118 | + {Object.entries(grouped).map(([id, sessions]) => { |
| 119 | + if (!sessions?.length) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + return <WorkspaceSessionGroup key={id} id={id} sessions={sessions} />; |
| 124 | + })} |
| 125 | + </Accordion> |
| 126 | + )} |
| 127 | + |
| 128 | + {/* No results */} |
| 129 | + {!isLoading && sessions.length === 0 && !errorMessage && ( |
| 130 | + <div className="flex flex-col w-full mb-8"> |
| 131 | + <Heading2 className="text-center mt-8">No sessions found.</Heading2> |
| 132 | + <Subheading className="text-center mt-1"> |
| 133 | + Have you started any |
| 134 | + <a className="gp-link" href={gitpodHostUrl.asWorkspacePage().toString()}> |
| 135 | + {" "} |
| 136 | + workspaces |
| 137 | + </a>{" "} |
| 138 | + in the last 30 days or checked your other organizations? |
| 139 | + </Subheading> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </ItemsList> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className="mt-4 mb-8 flex flex-row justify-center"> |
| 146 | + {hasNextPage ? ( |
| 147 | + <LoadingButton |
| 148 | + variant="secondary" |
| 149 | + onClick={() => { |
| 150 | + setPage(page + 1); |
| 151 | + fetchNextPage(); |
| 152 | + }} |
| 153 | + loading={isFetchingNextPage} |
| 154 | + > |
| 155 | + Load more |
| 156 | + </LoadingButton> |
| 157 | + ) : ( |
| 158 | + hasMoreThanOnePage && <TextMuted>All workspace sessions are loaded</TextMuted> |
| 159 | + )} |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + </> |
| 163 | + ); |
| 164 | +}; |
| 165 | + |
| 166 | +type DownloadUsageProps = { |
| 167 | + from: Timestamp; |
| 168 | + to: Timestamp; |
| 169 | +}; |
| 170 | +export const DownloadUsage = ({ from, to }: DownloadUsageProps) => { |
| 171 | + const { data: org } = useCurrentOrg(); |
| 172 | + const { toast } = useToast(); |
| 173 | + // When we start the download, we disable the button for a short time |
| 174 | + const [downloadDisabled, setDownloadDisabled] = useTemporaryState(false, 1000); |
| 175 | + |
| 176 | + const handleDownload = useCallback(async () => { |
| 177 | + if (!org) { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + setDownloadDisabled(true); |
| 182 | + toast( |
| 183 | + <DownloadInsightsToast |
| 184 | + organizationName={org?.slug ?? org?.id} |
| 185 | + organizationId={org.id} |
| 186 | + from={from} |
| 187 | + to={to} |
| 188 | + />, |
| 189 | + { |
| 190 | + autoHide: false, |
| 191 | + }, |
| 192 | + ); |
| 193 | + }, [org, setDownloadDisabled, toast, from, to]); |
| 194 | + |
| 195 | + return ( |
| 196 | + <Button variant="secondary" onClick={handleDownload} className="gap-1" disabled={downloadDisabled}> |
| 197 | + <DownloadIcon strokeWidth={3} className="w-4" /> |
| 198 | + <span>Export as CSV</span> |
| 199 | + </Button> |
| 200 | + ); |
| 201 | +}; |
| 202 | + |
| 203 | +export default Insights; |
0 commit comments