Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37249 vikas 1
package com.spice.profitmandi.web.services;
2
 
3
import com.spice.profitmandi.common.web.client.RestClient;
4
import org.apache.logging.log4j.LogManager;
5
import org.apache.logging.log4j.Logger;
6
import org.json.JSONObject;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Service;
9
 
10
import java.util.HashMap;
11
import java.util.LinkedHashMap;
12
import java.util.Map;
13
 
14
/**
15
 * Quality check for visit remarks, backed by the analyze service.
16
 *
17
 * POST /apis/analyze/v1/remarks/score
18
 *   {"remark":"...","asm_id":41292,"visit_id":"98213","agenda":"Credit dues","commit":true}
19
 * →  {"verdict":"good|bad","score":0.899,"authored_by_asm":true,
20
 *     "counts_toward_scorecard":true,"reasons":[{"code":"TOO_SHORT","detail":"..."}],
21
 *     "model_version":"...","visit_id":"98213"}
22
 *
23
 * commit=true tells the scorer to persist the score on its side, so pass it only
24
 * for a real checkout — probes should use false.
25
 */
26
@Service
27
public class RemarkScoreService {
28
 
29
    private static final Logger LOGGER = LogManager.getLogger(RemarkScoreService.class);
30
 
31
    private static final String SCORE_URL = "https://team.smartdukaan.com/apis/analyze/v1/remarks/score";
32
 
33
    @Autowired
34
    private RestClient restClient;
35
 
36
    /**
37
     * Score one remark. Never throws and never returns null: a checkout is not
38
     * allowed to fail because the scorer is slow or down — the caller stores
39
     * whatever comes back, and an unreachable scorer is recorded as
40
     * {"error":"..."} so the gap is visible in remark_response instead of silent.
41
     */
42
    public JSONObject score(String remark, int asmId, String visitId, String agenda, boolean commit) {
43
        Map<String, Object> body = new LinkedHashMap<>();
44
        body.put("remark", remark);
45
        body.put("asm_id", asmId);
46
        body.put("visit_id", visitId);
47
        body.put("agenda", agenda != null ? agenda : "");
48
        body.put("commit", commit);
49
 
50
        Map<String, String> headers = new HashMap<>();
51
        headers.put("Content-Type", "application/json");
52
 
53
        try {
54
            String response = restClient.postJson(SCORE_URL, body, headers);
55
            JSONObject json = new JSONObject(response);
56
            if (!json.has("verdict")) {
57
                LOGGER.warn("Remark scorer returned no verdict for visitId={}: {}", visitId, response);
58
                return error("scorer returned no verdict");
59
            }
60
            return json;
61
        } catch (Exception e) {
62
            LOGGER.warn("Remark scoring failed for visitId={} agenda={}: {}", visitId, agenda, e.toString());
63
            return error(e.getClass().getSimpleName() + ": " + e.getMessage());
64
        }
65
    }
66
 
67
    private JSONObject error(String message) {
68
        return new JSONObject().put("error", message);
69
    }
70
}