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

Onboarding charts #359

Merged
merged 3 commits into from
Nov 28, 2023
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
99 changes: 88 additions & 11 deletions frontend/src/app/Usage/UsageInterval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import {
PageSection,
Card,
CardBody,
CardTitle,
Title,
Flex,
FlexItem,
LabelGroup,
Label,
} from "@patternfly/react-core";
import {
Chart,
Expand All @@ -20,6 +23,7 @@ import { ErrorConnection } from "../Errors/ErrorConnection";
import { Preloader } from "../Preloader/Preloader";
import { useQuery } from "@tanstack/react-query";
import { UsageListData } from "./UsageListData";
import { ForgeIcon } from "../Forge/ForgeIcon";

const fetchDataByGranularity = (granularity: UsageIntervalProps) =>
fetch(
Expand Down Expand Up @@ -148,9 +152,54 @@ const UsageInterval: React.FC<UsageIntervalProps> = (props) => {
);
}

function getListOfNewProjects(projects, categoryName, color) {
return (
<LabelGroup categoryName={getReadableName(categoryName) + ":"}>
{projects.map((project) => (
<Label
variant="outline"
color="blue"
href={project}
icon={<ForgeIcon url={project} />}
>
{project.replace("https://", "")}
</Label>
))}
</LabelGroup>
);
}

function getListOfNewProjectsForJobs(projectsForJobs) {
return (
<>
{Object.keys(projectsForJobs).map((job) => (
<>{getListOfNewProjects(projectsForJobs[job], job)}</>
))}
</>
);
}

return (
<>
<Card>
<CardTitle>Project activity</CardTitle>
<CardBody>
<Flex>
{getLineChart(
["active_projects"],
data,
"Number of active projects",
)}
{getLineChart(
Object.keys(data.events),
data.events,
"Number of processed events",
)}
</Flex>
</CardBody>
</Card>
<Card>
<CardTitle>Processed jobs</CardTitle>
<CardBody>
<Flex>
{getLineChart(
Expand All @@ -160,33 +209,61 @@ const UsageInterval: React.FC<UsageIntervalProps> = (props) => {
data.jobs,
"Number of processed jobs",
)}
{getLineChart(
["sync_release_runs"],
data.jobs,
"Number of synced releases",
)}
</Flex>
</CardBody>
</Card>
<Card>
<CardTitle>Active projects</CardTitle>
<CardBody>
<Flex>
{getLineChart(
Object.keys(data.jobs_project_count).filter(
(obj) => obj !== "sync_release_runs",
),
data.jobs_project_count,
"Number of projects with processed jobs of this type",
)}
{getLineChart(
["sync_release_runs"],
data.jobs,
"Number of synced releases",
)}
{getLineChart(
["sync_release_runs"],
data.jobs_project_count,
"Number of projects with synced releases",
)}
</Flex>
</CardBody>
</Card>
<Card>
<CardTitle>Onboarded projects</CardTitle>
<CardBody>
<Flex>
{getLineChart(
["active_projects"],
data,
"Number of active projects",
Object.keys(data.jobs_project_cumulative_count),
data.jobs_project_cumulative_count,
"Cumulative number of projects with at least a single job run",
)}
{getLineChart(
Object.keys(data.events),
data.events,
"Number of processed events",
["active_projects_cumulative"],
data,
"Active projects",
)}
<FlexItem>
<Card>
<CardTitle>New projects:</CardTitle>
<CardBody>
{getListOfNewProjects(
data.onboarded_projects,
"New setup",
)}
{getListOfNewProjectsForJobs(
data.onboarded_projects_per_job,
)}
</CardBody>
</Card>
</FlexItem>
</Flex>
</CardBody>
</Card>
Expand Down
44 changes: 42 additions & 2 deletions packit_dashboard/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
_CACHE_MAXSIZE = 100


__now = datetime.now()
_DATE_IN_THE_PAST = __now.replace(year=__now.year - 100)


@api.route("/api/copr-builds/")
def copr_builds():
page = escape(request.args.get("page"))
Expand Down Expand Up @@ -141,8 +145,7 @@ def usage_past_year():
@api.route("/api/usage/total")
@ttl_cache(maxsize=_CACHE_MAXSIZE, ttl=timedelta(days=1).seconds)
def usage_total():
now = datetime.now()
past_date = now.replace(year=now.year - 100).strftime("%Y-%m-%d")
past_date = _DATE_IN_THE_PAST.strftime("%Y-%m-%d")
return _get_usage_data_from_packit_api(usage_from=past_date)


Expand Down Expand Up @@ -171,8 +174,23 @@ def _get_usage_interval_data(

result_jobs: dict[str, CHART_DATA_TYPE] = {}
result_jobs_project_count: dict[str, CHART_DATA_TYPE] = {}
result_jobs_project_cumulative_count: dict[str, CHART_DATA_TYPE] = {}
result_events: dict[str, CHART_DATA_TYPE] = {}
result_active_projects: CHART_DATA_TYPE = []
result_active_projects_cumulative: CHART_DATA_TYPE = []

past_data = _get_usage_data_from_packit_api(
usage_from=_DATE_IN_THE_PAST, usage_to=days_legend[-1], top=100000
).json
cumulative_projects_past = set(
past_data["active_projects"]["top_projects_by_events_handled"].keys()
)
cumulative_projects = cumulative_projects_past.copy()
cumulative_projects_for_jobs_past = {
job: set(data["top_projects_by_job_runs"].keys())
for job, data in past_data["jobs"].items()
}
cumulative_projects_for_jobs = cumulative_projects_for_jobs_past.copy()

for day in reversed(days_legend):
day_from = (day - delta).isoformat()
Expand All @@ -191,21 +209,43 @@ def _get_usage_interval_data(
{"x": legend, "y": len(data["top_projects_by_job_runs"])}
)

cumulative_projects_for_jobs[job] |= data["top_projects_by_job_runs"].keys()
result_jobs_project_cumulative_count.setdefault(job, [])
result_jobs_project_cumulative_count[job].append(
{"x": legend, "y": len(cumulative_projects_for_jobs[job])}
)

for event, data in interval_result["events"].items():
result_events.setdefault(event, [])
result_events[event].append({"x": legend, "y": data["events_handled"]})

result_active_projects.append(
{"x": legend, "y": interval_result["active_projects"].get("project_count")}
)
cumulative_projects |= interval_result["active_projects"][
"top_projects_by_events_handled"
].keys()
result_active_projects_cumulative.append(
{"x": legend, "y": len(cumulative_projects)}
)

onboarded_projects_per_job = dict()
for job, data in past_data["jobs"].items():
onboarded_projects_per_job[job] = list(
cumulative_projects_for_jobs[job] - cumulative_projects_for_jobs_past[job]
)

return {
"jobs": result_jobs,
"jobs_project_count": result_jobs_project_count,
"jobs_project_cumulative_count": result_jobs_project_cumulative_count,
"events": result_events,
"from": days_legend[0].isoformat(),
"to": days_legend[-1].isoformat(),
"active_projects": result_active_projects,
"active_projects_cumulative": result_active_projects_cumulative,
"onboarded_projects": list(cumulative_projects - cumulative_projects_past),
"onboarded_projects_per_job": onboarded_projects_per_job,
}


Expand Down