1
1
import { capitalize } from "@/common.js" ;
2
2
import { api } from "@/axios.js" ;
3
3
4
+ const last_rating_cache = new Map ( ) ;
5
+ const last_stats_cache = new Map ( ) ;
6
+
7
+ function fetch_error_handler ( fetch , username , last_cache ) {
8
+ return new Promise ( ( resolve , reject ) => {
9
+ const timeoutID = setTimeout ( function ( ) {
10
+ const res = last_cache . get ( username ) ;
11
+ if ( res != null ) resolve ( res ) ;
12
+ else reject ( { status : 500 , error : "Codeforces Server Error" } ) ;
13
+ } , 2000 ) ;
14
+ fetch ( )
15
+ . then ( ( result ) => {
16
+ clearTimeout ( timeoutID ) ;
17
+ resolve ( result ) ;
18
+ } )
19
+ . catch ( ( error ) => {
20
+ if ( error . status === 400 ) {
21
+ clearTimeout ( timeoutID ) ;
22
+ reject ( error ) ;
23
+ }
24
+ } ) ;
25
+ } ) ;
26
+ }
27
+
4
28
function count_submissions ( submissions ) {
5
29
let alreadySolved = { } ;
6
30
let problemID ;
@@ -16,22 +40,29 @@ function count_submissions(submissions) {
16
40
}
17
41
18
42
export function get_rating ( username , cache_seconds ) {
19
- return new Promise ( ( resolve , reject ) => {
20
- api
21
- . get ( `/user.info?handles=${ username } ` , {
22
- cache : {
23
- ttl : cache_seconds * 1000 ,
24
- } ,
25
- } )
26
- . then ( ( response ) => {
27
- resolve ( response . data . result [ 0 ] . rating || 0 ) ;
28
- } )
29
- . catch ( ( error ) => {
30
- if ( error . response . status === 400 )
31
- reject ( { status : 400 , error : "Codeforces Handle Not Found" } ) ;
32
- else reject ( { status : 500 , error : "Codeforces Server Error" } ) ;
33
- } ) ;
34
- } ) ;
43
+ return fetch_error_handler (
44
+ ( ) =>
45
+ new Promise ( ( resolve , reject ) => {
46
+ api
47
+ . get ( `/user.info?handles=${ username } ` , {
48
+ cache : {
49
+ ttl : cache_seconds * 1000 ,
50
+ } ,
51
+ } )
52
+ . then ( ( response ) => {
53
+ const res = response . data . result [ 0 ] . rating || 0 ;
54
+ last_rating_cache . set ( username , res ) ;
55
+ resolve ( res ) ;
56
+ } )
57
+ . catch ( ( error ) => {
58
+ if ( error . response . status === 400 )
59
+ reject ( { status : 400 , error : "Codeforces Handle Not Found" } ) ;
60
+ else reject ( { status : 500 , error : "Codeforces Server Error" } ) ;
61
+ } ) ;
62
+ } ) ,
63
+ username ,
64
+ last_rating_cache
65
+ ) ;
35
66
}
36
67
37
68
export function get_stats ( username , cache_seconds ) {
@@ -40,53 +71,61 @@ export function get_stats(username, cache_seconds) {
40
71
ttl : cache_seconds * 1000 ,
41
72
} ,
42
73
} ;
43
- return new Promise ( ( resolve , reject ) => {
44
- Promise . all ( [
45
- api . get ( `/user.info?handles=${ username } ` , apiConfig ) ,
46
- api . get ( `/user.rating?handle=${ username } ` , apiConfig ) ,
47
- api . get ( `/user.status?handle=${ username } ` , apiConfig ) ,
48
- ] )
49
- . then ( ( responses ) => {
50
- let {
51
- firstName,
52
- lastName,
53
- rating,
54
- rank,
55
- maxRank,
56
- maxRating,
57
- friendOfCount,
58
- contribution,
59
- } = responses [ 0 ] . data . result [ 0 ] ;
74
+ return fetch_error_handler (
75
+ ( ) =>
76
+ new Promise ( ( resolve , reject ) => {
77
+ Promise . all ( [
78
+ api . get ( `/user.info?handles=${ username } ` , apiConfig ) ,
79
+ api . get ( `/user.rating?handle=${ username } ` , apiConfig ) ,
80
+ api . get ( `/user.status?handle=${ username } ` , apiConfig ) ,
81
+ ] )
82
+ . then ( ( responses ) => {
83
+ let {
84
+ firstName,
85
+ lastName,
86
+ rating,
87
+ rank,
88
+ maxRank,
89
+ maxRating,
90
+ friendOfCount,
91
+ contribution,
92
+ } = responses [ 0 ] . data . result [ 0 ] ;
60
93
61
- rating = rating ? rating : 0 ;
62
- maxRating = maxRating ? maxRating : 0 ;
63
- rank = rank ? capitalize ( rank ) : "Unrated" ;
64
- maxRank = maxRank ? capitalize ( maxRank ) : "Unrated" ;
94
+ rating = rating ? rating : 0 ;
95
+ maxRating = maxRating ? maxRating : 0 ;
96
+ rank = rank ? capitalize ( rank ) : "Unrated" ;
97
+ maxRank = maxRank ? capitalize ( maxRank ) : "Unrated" ;
65
98
66
- const fullName = `${ firstName } ${ lastName } `
67
- . replace ( "undefined" , "" )
68
- . replace ( "undefined" , "" )
69
- . trim ( ) ;
70
- const contestsCount = responses [ 1 ] . data . result . length ;
71
- const problemsSolved = count_submissions ( responses [ 2 ] . data . result ) ;
99
+ const fullName = `${ firstName } ${ lastName } `
100
+ . replace ( "undefined" , "" )
101
+ . replace ( "undefined" , "" )
102
+ . trim ( ) ;
103
+ const contestsCount = responses [ 1 ] . data . result . length ;
104
+ const problemsSolved = count_submissions ( responses [ 2 ] . data . result ) ;
72
105
73
- resolve ( {
74
- username,
75
- fullName,
76
- rating,
77
- maxRating,
78
- rank,
79
- maxRank,
80
- contestsCount,
81
- problemsSolved,
82
- friendOfCount,
83
- contribution,
84
- } ) ;
85
- } )
86
- . catch ( ( error ) => {
87
- if ( error . response . status === 400 )
88
- reject ( { status : 400 , error : "Codeforces Handle Not Found" } ) ;
89
- else reject ( { status : 500 , error : "Codeforces Server Error" } ) ;
90
- } ) ;
91
- } ) ;
106
+ const res = {
107
+ username,
108
+ fullName,
109
+ rating,
110
+ maxRating,
111
+ rank,
112
+ maxRank,
113
+ contestsCount,
114
+ problemsSolved,
115
+ friendOfCount,
116
+ contribution,
117
+ } ;
118
+
119
+ last_stats_cache . set ( username , res ) ;
120
+ resolve ( res ) ;
121
+ } )
122
+ . catch ( ( error ) => {
123
+ if ( error . response . status === 400 )
124
+ reject ( { status : 400 , error : "Codeforces Handle Not Found" } ) ;
125
+ else reject ( { status : 500 , error : "Codeforces Server Error" } ) ;
126
+ } ) ;
127
+ } ) ,
128
+ username ,
129
+ last_stats_cache
130
+ ) ;
92
131
}
0 commit comments