Skip to content

Commit

Permalink
feat: add user_count to system usage stats
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Jan 1, 2025
1 parent 10ef99a commit c7fc8a0
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "insiders"]
path = ee/insiders
url = git@github.com:karrioapi/karrio-insiders.git
[submodule "ee/platform"]
[submodule "platform"]
path = ee/platform
url = git@github.com:karrioapi/karrio-platform.git
2 changes: 1 addition & 1 deletion ee/insiders
2 changes: 1 addition & 1 deletion ee/platform
22 changes: 15 additions & 7 deletions modules/core/karrio/server/core/dataunits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from django.urls import reverse
from rest_framework.request import Request

from karrio.server.conf import settings
import karrio.lib as lib
import karrio.server.conf as conf
import karrio.core.units as units
import karrio.references as references
import karrio.server.providers.models as providers


PACKAGE_MAPPERS = references.collect_providers_data()
Expand Down Expand Up @@ -46,14 +46,17 @@ def contextual_metadata(request: Request):
host = _host[:-1] if _host[-1] == "/" else _host

return {
"VERSION": settings.VERSION,
"APP_NAME": settings.APP_NAME,
"APP_WEBSITE": settings.APP_WEBSITE,
"VERSION": conf.settings.VERSION,
"APP_NAME": conf.settings.APP_NAME,
"APP_WEBSITE": conf.settings.APP_WEBSITE,
"HOST": f"{host}/",
"ADMIN": f"{host}/admin",
"GRAPHQL": f"{host}/graphql",
"OPENAPI": f"{host}/openapi",
**{flag: getattr(settings, flag, None) for flag, _ in settings.FEATURE_FLAGS},
**{
flag: getattr(conf.settings, flag, None)
for flag, _ in conf.settings.FEATURE_FLAGS
},
}


Expand Down Expand Up @@ -101,7 +104,12 @@ def _get_generic_carriers():
}
extra_services = {
f"{c.credentials.get('custom_carrier_name') or 'generic'}": {
s.service_code: s.service_code for s in c.services.all()
s.service_code: s.service_code
for s in c.services
or [
lib.to_object(lib.models.ServiceLevel, _)
for _ in references["service_levels"][c.ext]
]
}
for c in custom_carriers
}
Expand Down
2 changes: 2 additions & 0 deletions modules/core/karrio/server/providers/models/carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ def config(self) -> dict:

@property
def services(self) -> typing.Optional[typing.List[dict]]:

if self.rate_sheet is None:
return None

return self.rate_sheet.services.all()

@property
Expand Down
2 changes: 1 addition & 1 deletion modules/core/karrio/server/providers/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def decorator(model: models.Model):
"service_list",
property(
lambda self: (
self.services.all()
self.services
if self.rate_sheet is None and hasattr(self, "services")
else self.rate_sheet.services.all()
)
Expand Down
5 changes: 4 additions & 1 deletion modules/graph/karrio/server/graph/schemas/base/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class SystemUsageType:
total_trackers: typing.Optional[int] = None
total_shipments: typing.Optional[int] = None
organization_count: typing.Optional[int] = None
user_count: typing.Optional[int] = None
total_shipping_spend: typing.Optional[float] = None
api_errors: typing.Optional[typing.List[utils.UsageStatType]] = None
api_requests: typing.Optional[typing.List[utils.UsageStatType]] = None
Expand Down Expand Up @@ -282,7 +283,8 @@ def resolve(
total_shipping_spend = lib.to_money(
sum([item["count"] for item in shipping_spend], 0.0)
)
organization_count = 0
user_count = User.objects.count()
organization_count = 1

if conf.settings.MULTI_ORGANIZATIONS:
import karrio.server.orgs.models as orgs
Expand All @@ -296,6 +298,7 @@ def resolve(
total_trackers=total_trackers,
total_shipments=total_shipments,
organization_count=organization_count,
user_count=user_count,
total_shipping_spend=total_shipping_spend,
api_errors=[utils.UsageStatType.parse(item) for item in api_errors],
api_requests=[utils.UsageStatType.parse(item) for item in api_requests],
Expand Down
114 changes: 114 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions packages/insiders/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use client";

import * as React from "react";
import { Button } from "@karrio/insiders/components/ui/button";
import { ChevronLeftIcon, ChevronRightIcon } from "@radix-ui/react-icons";

interface PaginationProps {
currentPage: number;
totalPages: number;
onPageChange: (page: number) => void;
}

export function Pagination({
currentPage,
totalPages,
onPageChange,
}: PaginationProps) {
return (
<div className="flex items-center justify-center gap-2">
<Button
variant="outline"
size="icon"
onClick={() => onPageChange(currentPage - 1)}
disabled={currentPage <= 1}
>
<ChevronLeftIcon className="h-4 w-4" />
</Button>
<div className="flex items-center gap-1">
<span className="text-sm">
Page {currentPage} of {totalPages}
</span>
</div>
<Button
variant="outline"
size="icon"
onClick={() => onPageChange(currentPage + 1)}
disabled={currentPage >= totalPages}
>
<ChevronRightIcon className="h-4 w-4" />
</Button>
</div>
);
}
9 changes: 6 additions & 3 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ export class KarrioClient implements KarrioClientInterface {
this.documents = new DocumentsApi(config, config.basePath, axiosInstance);
this.manifests = new ManifestsApi(config, config.basePath, axiosInstance);
this.batches = new BatchesApi(config, config.basePath, axiosInstance);
this.graphql = new GraphQLApi(config.basePath, axiosInstance);
this.admin = new GraphQLApi(`${config.basePath}/admin`, axiosInstance);
this.graphql = new GraphQLApi(`${config.basePath}/graphql`, axiosInstance);
this.admin = new GraphQLApi(
`${config.basePath}/admin/graphql`,
axiosInstance,
);
}
}

Expand All @@ -111,7 +114,7 @@ export class GraphQLApi {
...config
} = args || {};
try {
const APIUrl = url || `${this.host}/graphql`;
const APIUrl = url || `${this.host}`;
const variables = data ? { data } : reqVariables;
const { data: response } = await this.axiosInstance.post<{
data?: T;
Expand Down
3 changes: 3 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"dependencies": {
"axios": "^1.6.2",
"graphql-tag": "^2.12.6"
},
"devDependencies": {
"@types/graphql": "^14.2.3"
}
}

0 comments on commit c7fc8a0

Please sign in to comment.