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

feat(mis): 管理系统平台数据统计作业提交用户前十数横坐标改为userName #1206

Merged
merged 17 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 7 additions & 0 deletions .changeset/great-carrots-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@scow/mis-server": patch
"@scow/mis-web": patch
"@scow/grpc-api": patch
---

管理系统下的平台数据统计提交作业前十的用户数横坐标改为以 userName 的方式显示.
48 changes: 48 additions & 0 deletions apps/mis-server/src/services/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { JobInfo as JobInfoEntity } from "src/entities/JobInfo";
import { JobPriceChange } from "src/entities/JobPriceChange";
import { AmountStrategy, JobPriceItem } from "src/entities/JobPriceItem";
import { Tenant } from "src/entities/Tenant";
import { User } from "src/entities/User";
import { queryWithCache } from "src/utils/cache";
import { toGrpc } from "src/utils/job";
import { logger } from "src/utils/logger";
Expand Down Expand Up @@ -412,6 +413,53 @@ export const jobServiceServer = plugin((server) => {
];
},

// 返回用户名,需要联表查询
getUsersWithMostJobSubmissions: async ({ request, em }) => {
// topRank不传默认为10,最大限制为10
const { startTime, endTime, topNUsers = 10 } = ensureNotUndefined(request, ["startTime", "endTime"]);
usaveh marked this conversation as resolved.
Show resolved Hide resolved

// 获取JobInfoEntity中基于时间范围的前N个userId和计数
const qb = em.createQueryBuilder(JobInfoEntity, "j");
qb
.select([raw("j.user as userId"), raw("COUNT(*) as count")])
.where({ timeSubmit: { $gte: startTime } })
.andWhere({ timeSubmit: { $lte: endTime } })
.groupBy("j.user")
.orderBy({ [raw("COUNT(*)")]: QueryOrder.DESC })
.limit(Math.min(topNUsers, 10));

const jobInfoResults: {userId: string, count: number}[] = await queryWithCache({
em,
queryKeys: ["top_submit_job_users", `${startTime}`, `${endTime}`, `${topNUsers}`],
queryQb: qb,
});


// 提取所有的userIds
const userIds = jobInfoResults.map((jobInfo) => jobInfo.userId);
// 根据userId一次性获取userName
const users = await em.find(User, { userId: { $in: userIds } });
usaveh marked this conversation as resolved.
Show resolved Hide resolved
const userMap = new Map(users.map((user) => [user.userId, user.name]));

// 对结果进行处理
const results: {userName: string, userId: string, count: number}[] = [];
for (const jobInfo of jobInfoResults) {
results.push({
userName:userMap.get(jobInfo.userId) || "Unknown", // 使用Map获取用户名
userId:jobInfo.userId,
count:jobInfo.count,
});
}

// 直接返回构建的结果
return [
{
results,
},
];
},


getNewJobCount: async ({ request, em }) => {
const { startTime, endTime, timeZone = "UTC" } = ensureNotUndefined(request, ["startTime", "endTime"]);

Expand Down
2 changes: 2 additions & 0 deletions apps/mis-web/src/apis/api.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ export const mockApi: MockApi<typeof api> = {

getTopSubmitJobUser: async () => ({ results: [{ userId: "test", count:10 }]}),

getUsersWithMostJobSubmissions: async () => ({ results: [{ userName: "name1", userId: "test1", count:10 }]}),

getNewJobCount: async () => ({ results: [{ date: { year: 2023, month: 12, day: 21 }, count: 10 }]}),

getTenantUsers: async () => ({ results: mockUsers }),
Expand Down
2 changes: 2 additions & 0 deletions apps/mis-web/src/apis/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type { GetTenantUsersSchema } from "src/pages/api/admin/getTenantUsers";
import type { GetTopChargeAccountSchema } from "src/pages/api/admin/getTopChargeAccount";
import type { GetTopPayAccountSchema } from "src/pages/api/admin/getTopPayAccount";
import type { GetTopSubmitJobUserSchema } from "src/pages/api/admin/getTopSubmitJobUser";
import type { GetUsersWithMostJobSubmissionsSchema } from "src/pages/api/admin/getUsersWithMostJobSubmissions";
import type { ImportUsersSchema } from "src/pages/api/admin/importUsers";
import type { GetAlarmDbIdSchema } from "src/pages/api/admin/monitor/getAlarmDbId";
import type { GetAlarmLogsSchema } from "src/pages/api/admin/monitor/getAlarmLogs";
Expand Down Expand Up @@ -172,6 +173,7 @@ export const api = {
queryJobTimeLimit: apiClient.fromTypeboxRoute<typeof QueryJobTimeLimitSchema>("GET", "/api/job/queryJobTimeLimit"),
getRunningJobs: apiClient.fromTypeboxRoute<typeof GetRunningJobsSchema>("GET", "/api/job/runningJobs"),
getTopSubmitJobUser: apiClient.fromTypeboxRoute<typeof GetTopSubmitJobUserSchema>("GET", "/api/admin/getTopSubmitJobUser"),
getUsersWithMostJobSubmissions: apiClient.fromTypeboxRoute<typeof GetUsersWithMostJobSubmissionsSchema>("GET", "/api/admin/getUsersWithMostJobSubmissions"),
getNewJobCount: apiClient.fromTypeboxRoute<typeof GetNewJobCountSchema>("GET", "/api/admin/getNewJobCount"),
getOperationLogs: apiClient.fromTypeboxRoute<typeof GetOperationLogsSchema>("GET", "/api/log/getOperationLog"),
getCustomEventTypes: apiClient.fromTypeboxRoute<typeof GetCustomEventTypesSchema>("GET", "/api/log/getCustomEventTypes"),
Expand Down
25 changes: 18 additions & 7 deletions apps/mis-web/src/pages/admin/statistic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,25 @@ requireAuth((u) => u.platformRoles.includes(PlatformRole.PLATFORM_ADMIN))

const { data: dailyPay, isLoading: dailyPayLoading } = useAsync({ promiseFn: getDailyPayFn });

const getTopSubmitJobUserFn = useCallback(async () => {
return await api.getTopSubmitJobUser({ query: {
// const getTopSubmitJobUserFn = useCallback(async () => {
// return await api.getTopSubmitJobUser({ query: {
// startTime: query.filterTime[0].startOf("day").toISOString(),
// endTime: query.filterTime[1].endOf("day").toISOString(),
// } });
// }, [query]);

// const { data: topSubmitJobUser, isLoading: topSubmitJobUserLoading } =
// useAsync({ promiseFn: getTopSubmitJobUserFn });

const getUsersWithMostJobSubmissionsFn = useCallback(async () => {
return await api.getUsersWithMostJobSubmissions({ query: {
startTime: query.filterTime[0].startOf("day").toISOString(),
endTime: query.filterTime[1].endOf("day").toISOString(),
} });
}, [query]);

const { data: topSubmitJobUser, isLoading: topSubmitJobUserLoading } = useAsync({ promiseFn: getTopSubmitJobUserFn });
const { data: topSubmitJobUserName, isLoading: topSubmitJobUserNameLoading } =
useAsync({ promiseFn: getUsersWithMostJobSubmissionsFn });
usaveh marked this conversation as resolved.
Show resolved Hide resolved

const getNewJobCountFn = useCallback(async () => {
return await api.getNewJobCount({ query: {
Expand Down Expand Up @@ -263,11 +274,11 @@ requireAuth((u) => u.platformRoles.includes(PlatformRole.PLATFORM_ADMIN))

const topSubmitJobUserData = useMemo(() => {

return topSubmitJobUser?.results.map((r) => ({
x: r.userId,
return topSubmitJobUserName?.results.map((r) => ({
x: r.userName,
y: r.count,
})) || [];
}, [query, topSubmitJobUser]);
}, [query, topSubmitJobUserName]);

const newJobCountData = useMemo(() => {
if (dailyNewJobCount) {
Expand Down Expand Up @@ -484,7 +495,7 @@ requireAuth((u) => u.platformRoles.includes(PlatformRole.PLATFORM_ADMIN))
<DataBarChart
data={topSubmitJobUserData}
title={t(p("topTenSubmitJobUser"))}
isLoading={topSubmitJobUserLoading}
isLoading={topSubmitJobUserNameLoading}
xLabel={t(p("userName"))}
toolTipFormatter={(value) => [value, t(p("jobCount"))]}
/>
Expand Down
76 changes: 76 additions & 0 deletions apps/mis-web/src/pages/api/admin/getUsersWithMostJobSubmissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2022 Peking University and Peking University Institute for Computing and Digital Economy
* SCOW is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { asyncClientCall } from "@ddadaal/tsgrpc-client";
import { JobServiceClient } from "@scow/protos/build/server/job";
import { Static, Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
import { PlatformRole } from "src/models/User";
import { getClient } from "src/utils/client";

export const GetUsersWithMostJobSubmissionsResponse = Type.Object({
results: Type.Array(Type.Object({
userName: Type.String(),
userId:Type.String(),
count: Type.Number(),
})),
});

export type GetUsersWithMostJobSubmissionsResponse = Static<typeof GetUsersWithMostJobSubmissionsResponse>;


export const GetUsersWithMostJobSubmissionsSchema = typeboxRouteSchema({
method: "GET",

query: Type.Object({

startTime: Type.String({ format: "date-time" }),

endTime: Type.String({ format: "date-time" }),

// 最大为10,不传默认为10
topNUsers: Type.Optional(Type.Number()),
usaveh marked this conversation as resolved.
Show resolved Hide resolved

}),

responses: {
200: GetUsersWithMostJobSubmissionsResponse,
},
});

const auth = authenticate((info) => info.platformRoles.includes(PlatformRole.PLATFORM_ADMIN));

export default typeboxRoute(GetUsersWithMostJobSubmissionsSchema,
async (req, res) => {

const info = await auth(req, res);
if (!info) {
return;
}

const { startTime, endTime, topNUsers } = req.query;

const client = getClient(JobServiceClient);

const { results } = await asyncClientCall(client, "getUsersWithMostJobSubmissions", {
startTime,
endTime,
topNUsers,
});

return {
200: {
results,
},
};
});
20 changes: 20 additions & 0 deletions protos/server/job.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ message GetTopSubmitJobUsersResponse {
repeated SubmitJobUser results = 1;
}

message GetUsersWithMostJobSubmissionsRequest {
google.protobuf.Timestamp start_time = 1;
google.protobuf.Timestamp end_time = 2;

//需要获取top 多少
//最大为10
optional uint32 top_n_users = 3;
}

message GetUsersWithMostJobSubmissionsResponse {
message UserInfo {
string user_name = 1;
string user_id = 2;
uint32 count = 3;
}
repeated UserInfo results = 1;
}

message GetNewJobCountRequest {
google.protobuf.Timestamp start_time = 1;
google.protobuf.Timestamp end_time = 2;
Expand Down Expand Up @@ -215,6 +233,8 @@ service JobService {

rpc GetTopSubmitJobUsers(GetTopSubmitJobUsersRequest) returns (GetTopSubmitJobUsersResponse);

rpc GetUsersWithMostJobSubmissions(GetUsersWithMostJobSubmissionsRequest) returns (GetUsersWithMostJobSubmissionsResponse);

rpc GetNewJobCount(GetNewJobCountRequest) returns (GetNewJobCountResponse);

rpc GetJobTotalCount(GetJobTotalCountRequest) returns (GetJobTotalCountResponse);
Expand Down
Loading