Skip to content

Commit 73499da

Browse files
authored
添加RL方面的功能,同时去掉了redis依赖 (#1776)
* added RL functions * removed redis
1 parent ece59e4 commit 73499da

10 files changed

Lines changed: 553 additions & 226 deletions

File tree

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ EESAST 后端 API
2323
- node 16 / npm
2424
- yarn
2525
- TypeScript
26-
- Redis
27-
28-
### Before Dev
29-
30-
需要在测试端安装和启动Redis
31-
具体见[Redis安装文档](https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis/)
3226

3327
### 工具
3428

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"graphql": "16.14.0",
2929
"graphql-request": "6.1.0",
3030
"html-to-text": "10.0.0",
31-
"ioredis": "5.10.1",
3231
"isemail": "3.2.0",
3332
"js-yaml": "4.1.1",
3433
"jsonwebtoken": "9.0.2",

src/hasura/contest.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,55 @@ export const get_team_software_final_submission_count: any = async (
15821582
);
15831583
};
15841584

1585+
export const add_team_RL_code: any = async (
1586+
team_id: string,
1587+
code_url: string,
1588+
) => {
1589+
const insert_team_RL_code: any = await client.request(
1590+
gql`
1591+
mutation add_team_RL_code($team_id: uuid!, $url: String!) {
1592+
insert_contest_team_RL_code_one(
1593+
object: { team_id: $team_id, url: $url }
1594+
) {
1595+
submit_id
1596+
team_id
1597+
created_at
1598+
url
1599+
}
1600+
}
1601+
`,
1602+
{
1603+
team_id: team_id,
1604+
url: code_url,
1605+
},
1606+
);
1607+
1608+
return insert_team_RL_code.insert_contest_team_RL_code_one;
1609+
};
1610+
1611+
export const get_team_RL_code_submissions: any = async (team_id: string) => {
1612+
const query_team_RL_code_submissions: any = await client.request(
1613+
gql`
1614+
query get_team_RL_code_submissions($team_id: uuid!) {
1615+
contest_team_RL_code(
1616+
where: { team_id: { _eq: $team_id } }
1617+
order_by: { created_at: asc }
1618+
) {
1619+
submit_id
1620+
team_id
1621+
created_at
1622+
url
1623+
}
1624+
}
1625+
`,
1626+
{
1627+
team_id: team_id,
1628+
},
1629+
);
1630+
1631+
return query_team_RL_code_submissions.contest_team_RL_code;
1632+
};
1633+
15851634
/**
15861635
*
15871636
* @param {uuid} contest_id ID
@@ -2713,6 +2762,37 @@ export const get_software_contest_deadline: any = async (
27132762
);
27142763
};
27152764

2765+
export const set_software_contest_deadline: any = async (
2766+
contest_id: string,
2767+
deadline: string,
2768+
) => {
2769+
const upsert_software_contest_deadline: any = await client.request(
2770+
gql`
2771+
mutation set_software_contest_deadline(
2772+
$contest_id: uuid!
2773+
$deadline: timestamptz!
2774+
) {
2775+
insert_contest_soft_code_deadline_one(
2776+
object: { contest_id: $contest_id, deadline: $deadline }
2777+
on_conflict: {
2778+
constraint: contest_soft_code_deadline_pkey
2779+
update_columns: [deadline]
2780+
}
2781+
) {
2782+
contest_id
2783+
deadline
2784+
}
2785+
}
2786+
`,
2787+
{
2788+
contest_id: contest_id,
2789+
deadline: deadline,
2790+
},
2791+
);
2792+
2793+
return upsert_software_contest_deadline.insert_contest_soft_code_deadline_one;
2794+
};
2795+
27162796
export const get_contest_id_from_team_id: any = async (team_id: string) => {
27172797
const query_contest_id_from_team_id: any = await client.request(
27182798
gql`
@@ -2728,3 +2808,58 @@ export const get_contest_id_from_team_id: any = async (team_id: string) => {
27282808
);
27292809
return query_contest_id_from_team_id.contest_team[0]?.contest_id ?? undefined;
27302810
};
2811+
2812+
/**
2813+
* Get all RL scores for a contest, ordered by score descending.
2814+
* @param {string} contest_id
2815+
* @returns {Array<{team_id, score, team_name}>}
2816+
*/
2817+
export const get_all_RL_scores: any = async (contest_id: string) => {
2818+
const result: any = await client.request(
2819+
gql`
2820+
query GetAllRLScores($contest_id: uuid!) {
2821+
contest_team_RL_score(
2822+
where: { contest_team: { contest_id: { _eq: $contest_id } } }
2823+
order_by: { score: desc_nulls_last }
2824+
) {
2825+
team_id
2826+
score
2827+
contest_team {
2828+
team_name
2829+
}
2830+
}
2831+
}
2832+
`,
2833+
{ contest_id },
2834+
);
2835+
return result.contest_team_RL_score ?? [];
2836+
};
2837+
2838+
/**
2839+
* Upsert (insert or update) a team's RL score.
2840+
* @param {string} team_id
2841+
* @param {number} score
2842+
* @returns {string} team_id
2843+
*/
2844+
export const upsert_team_RL_score: any = async (
2845+
team_id: string,
2846+
score: number,
2847+
) => {
2848+
const result: any = await client.request(
2849+
gql`
2850+
mutation UpsertTeamRLScore($team_id: uuid!, $score: Int!) {
2851+
insert_contest_team_RL_score_one(
2852+
object: { team_id: $team_id, score: $score }
2853+
on_conflict: {
2854+
constraint: contest_team_RL_score_pkey
2855+
update_columns: [score]
2856+
}
2857+
) {
2858+
team_id
2859+
}
2860+
}
2861+
`,
2862+
{ team_id, score },
2863+
);
2864+
return result.insert_contest_team_RL_score_one?.team_id ?? undefined;
2865+
};

src/hasura/llm.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ export const set_llm_usage_by_uuid = async (
5151
return query.update_llm_usage_by_pk;
5252
};
5353

54+
export const update_llm_usage_by_uuid = async (
55+
uuid: string,
56+
tokens_to_add: number,
57+
) => {
58+
const query: any = await client.request(
59+
gql`
60+
mutation UpdateLlmUsageByUuid(
61+
$uuid: uuid!
62+
$tokens_to_add: bigint!
63+
$updated_at: timestamptz!
64+
) {
65+
update_llm_usage_by_pk(
66+
pk_columns: { uuid: $uuid }
67+
_inc: { total_tokens_used: $tokens_to_add }
68+
_set: { last_updated_at: $updated_at }
69+
) {
70+
uuid
71+
total_tokens_used
72+
token_limit
73+
last_updated_at
74+
}
75+
}
76+
`,
77+
{
78+
uuid,
79+
tokens_to_add,
80+
updated_at: new Date().toISOString(),
81+
},
82+
);
83+
return query.update_llm_usage_by_pk;
84+
};
85+
5486
export const get_user_llm_usage = async (student_no: string) => {
5587
const query: any = await client.request(
5688
gql`

src/helpers/llm_cron.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/helpers/redis.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import http from "http";
44
import app from "./app";
55
import { GraphQLClient } from "graphql-request";
66
import { queue_element } from "./helpers/docker_queue";
7-
import { llm_cron } from "./helpers/llm_cron";
87
// import docker_cron from "./helpers/docker_queue";
98

109
const debug = Debug("eesast-api");
@@ -36,8 +35,6 @@ export const docker_queue: queue_element[] = [];
3635
// weekly_cron();
3736
// weekly_init();
3837

39-
llm_cron();
40-
4138
const port = normalizePort(process.env.PORT || "28888");
4239
app.set("port", port);
4340

0 commit comments

Comments
 (0)