|
2 | 2 |
|
3 | 3 | import org.springframework.stereotype.Service;
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.math.RoundingMode; |
| 7 | + |
5 | 8 | import org.springframework.boot.configurationprocessor.json.JSONObject;
|
| 9 | +import org.springframework.boot.configurationprocessor.json.JSONArray; |
6 | 10 | import org.springframework.boot.configurationprocessor.json.JSONException;
|
7 | 11 | import okhttp3.Request;
|
8 | 12 | import okhttp3.OkHttpClient;
|
@@ -35,14 +39,78 @@ public StatsResponse getStats(String username) {
|
35 | 39 |
|
36 | 40 | if (response.isSuccessful()) {
|
37 | 41 | // Parse GraphQL response
|
38 |
| - return new StatsResponse("success", "retrieved", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); |
| 42 | + |
| 43 | + // User not found |
| 44 | + if (jsonObject.has("errors")) { |
| 45 | + return new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 46 | + } else { // Parse user info |
| 47 | + return decodeGraphqlJson(jsonObject); |
| 48 | + } |
39 | 49 | } else {
|
40 |
| - return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 50 | + return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
41 | 51 | }
|
42 |
| - } catch (IOException ex) { |
43 |
| - return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 52 | + } catch (IOException ex) { // Post request error |
| 53 | + return new StatsResponse("error", ex.getMessage(), 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
44 | 54 | } catch (JSONException ex) { // Query serialization error
|
45 |
| - return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 55 | + return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private StatsResponse decodeGraphqlJson(JSONObject json) { |
| 60 | + int totalSolved = 0; |
| 61 | + int totalQuestions = 0; |
| 62 | + int easySolved = 0; |
| 63 | + int totalEasy = 0; |
| 64 | + int mediumSolved = 0; |
| 65 | + int totalMedium = 0; |
| 66 | + int hardSolved = 0; |
| 67 | + int totalHard = 0; |
| 68 | + float acceptanceRate = 0; |
| 69 | + int ranking = 0; |
| 70 | + int contributionPoints = 0; |
| 71 | + int reputation = 0; |
| 72 | + |
| 73 | + try { |
| 74 | + JSONObject data = json.getJSONObject("data"); |
| 75 | + JSONArray allQuestions = data.getJSONArray("allQuestionsCount"); |
| 76 | + JSONObject matchedUser = data.getJSONObject("matchedUser"); |
| 77 | + JSONObject submitStats = matchedUser.getJSONObject("submitStats"); |
| 78 | + JSONArray actualSubmissions = submitStats.getJSONArray("acSubmissionNum"); |
| 79 | + JSONArray totalSubmissions = submitStats.getJSONArray("totalSubmissionNum"); |
| 80 | + |
| 81 | + // Fill in total counts |
| 82 | + totalQuestions = allQuestions.getJSONObject(0).getInt("count"); |
| 83 | + totalEasy = allQuestions.getJSONObject(1).getInt("count"); |
| 84 | + totalMedium = allQuestions.getJSONObject(2).getInt("count"); |
| 85 | + totalHard = allQuestions.getJSONObject(3).getInt("count"); |
| 86 | + |
| 87 | + // Fill in solved counts |
| 88 | + totalSolved = actualSubmissions.getJSONObject(0).getInt("count"); |
| 89 | + easySolved = actualSubmissions.getJSONObject(1).getInt("count"); |
| 90 | + mediumSolved = actualSubmissions.getJSONObject(2).getInt("count"); |
| 91 | + hardSolved = actualSubmissions.getJSONObject(3).getInt("count"); |
| 92 | + |
| 93 | + // Fill in etc |
| 94 | + float totalAcceptCount = actualSubmissions.getJSONObject(0).getInt("submissions"); |
| 95 | + float totalSubCount = totalSubmissions.getJSONObject(0).getInt("submissions"); |
| 96 | + if (totalSubCount != 0) { |
| 97 | + acceptanceRate = round((totalAcceptCount / totalSubCount) * 100, 2); |
| 98 | + } |
| 99 | + |
| 100 | + contributionPoints = matchedUser.getJSONObject("contributions").getInt("points"); |
| 101 | + reputation = matchedUser.getJSONObject("profile").getInt("reputation"); |
| 102 | + ranking = matchedUser.getJSONObject("profile").getInt("ranking"); |
| 103 | + |
| 104 | + } catch (JSONException ex) { |
| 105 | + return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
46 | 106 | }
|
| 107 | + |
| 108 | + return new StatsResponse("success", "retrieved", totalSolved, totalQuestions, easySolved, totalEasy, mediumSolved, totalMedium, hardSolved, totalHard, acceptanceRate, ranking, contributionPoints, reputation); |
| 109 | + } |
| 110 | + |
| 111 | + private float round(float d, int decimalPlace) { |
| 112 | + BigDecimal bd = new BigDecimal(Float.toString(d)); |
| 113 | + bd = bd.setScale(decimalPlace, RoundingMode.HALF_UP); |
| 114 | + return bd.floatValue(); |
47 | 115 | }
|
48 | 116 | }
|
0 commit comments