|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2017 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Microsoft Corporation - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.core; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +import com.google.gson.JsonElement; |
| 20 | +import com.microsoft.java.debug.core.adapter.AdapterUtils; |
| 21 | +import com.microsoft.java.debug.core.adapter.JsonUtils; |
| 22 | +import com.microsoft.java.debug.core.adapter.Messages.Request; |
| 23 | +import com.microsoft.java.debug.core.adapter.Messages.Response; |
| 24 | + |
| 25 | +public class UsageDataSession { |
| 26 | + private static final Logger usageDataLogger = Logger.getLogger(Configuration.USAGE_DATA_LOGGER_NAME); |
| 27 | + private static final long RESPONSE_MAX_DELAY_MS = 1000; |
| 28 | + private static final ThreadLocal<String> sessionGuid = new InheritableThreadLocal<>(); |
| 29 | + |
| 30 | + private long startAt = -1; |
| 31 | + private long stopAt = -1; |
| 32 | + private Map<String, Integer> commandCountMap = new HashMap<>(); |
| 33 | + private Map<String, Integer> breakpointCountMap = new HashMap<>(); |
| 34 | + private Map<Integer, RequestEvent> requestEventMap = new HashMap<>(); |
| 35 | + |
| 36 | + public static void setSessionGuid(String guid) { |
| 37 | + sessionGuid.set(guid); |
| 38 | + } |
| 39 | + |
| 40 | + public static String getSessionGuid() { |
| 41 | + return sessionGuid.get(); |
| 42 | + } |
| 43 | + |
| 44 | + class RequestEvent { |
| 45 | + Request request; |
| 46 | + long timestamp; |
| 47 | + |
| 48 | + RequestEvent(Request request, long timestamp) { |
| 49 | + this.request = request; |
| 50 | + this.timestamp = timestamp; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void reportStart() { |
| 55 | + startAt = System.currentTimeMillis(); |
| 56 | + } |
| 57 | + |
| 58 | + public void reportStop() { |
| 59 | + stopAt = System.currentTimeMillis(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Record usage data from request. |
| 64 | + */ |
| 65 | + public void recordRequest(Request request) { |
| 66 | + try { |
| 67 | + requestEventMap.put(request.seq, new RequestEvent(request, System.currentTimeMillis())); |
| 68 | + |
| 69 | + // cmd count |
| 70 | + commandCountMap.put(request.command, commandCountMap.getOrDefault(request.command, 0) + 1); |
| 71 | + |
| 72 | + // bp count |
| 73 | + if ("setBreakpoints".equals(request.command)) { |
| 74 | + String fileIdentifier = "unknown file"; |
| 75 | + JsonElement pathElement = request.arguments.get("source").getAsJsonObject().get("path"); |
| 76 | + JsonElement nameElement = request.arguments.get("source").getAsJsonObject().get("name"); |
| 77 | + if (pathElement != null) { |
| 78 | + fileIdentifier = pathElement.getAsString(); |
| 79 | + } else if (nameElement != null) { |
| 80 | + fileIdentifier = nameElement.getAsString(); |
| 81 | + } |
| 82 | + String filenameHash = AdapterUtils.getSHA256HexDigest(fileIdentifier); |
| 83 | + int bpCount = request.arguments.get("breakpoints").getAsJsonArray().size(); |
| 84 | + breakpointCountMap.put(filenameHash, breakpointCountMap.getOrDefault(filenameHash, 0) + bpCount); |
| 85 | + } |
| 86 | + } catch (Throwable e) { |
| 87 | + // ignore it |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Record usage data from response. |
| 93 | + */ |
| 94 | + public void recordResponse(Response response) { |
| 95 | + try { |
| 96 | + long responseMillis = System.currentTimeMillis(); |
| 97 | + long requestMillis = responseMillis; |
| 98 | + String command = null; |
| 99 | + |
| 100 | + RequestEvent requestEvent = requestEventMap.getOrDefault(response.request_seq, null); |
| 101 | + if (requestEvent != null) { |
| 102 | + command = requestEvent.request.command; |
| 103 | + requestMillis = requestEvent.timestamp; |
| 104 | + requestEventMap.remove(response.request_seq); |
| 105 | + } |
| 106 | + long duration = responseMillis - requestMillis; |
| 107 | + |
| 108 | + if (!response.success || duration > RESPONSE_MAX_DELAY_MS) { |
| 109 | + Map<String, Object> props = new HashMap<>(); |
| 110 | + props.put("duration", duration); |
| 111 | + props.put("command", command); |
| 112 | + props.put("success", response.success); |
| 113 | + // directly report abnormal response. |
| 114 | + usageDataLogger.log(Level.WARNING, "abnormal response", props); |
| 115 | + } |
| 116 | + } catch (Throwable e) { |
| 117 | + // ignore it |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Submit summary of usage data in current session. |
| 123 | + */ |
| 124 | + public void submitUsageData() { |
| 125 | + Map<String, String> props = new HashMap<>(); |
| 126 | + |
| 127 | + props.put("sessionStartAt", String.valueOf(startAt)); |
| 128 | + props.put("sessionStopAt", String.valueOf(stopAt)); |
| 129 | + props.put("commandCount", JsonUtils.toJson(commandCountMap)); |
| 130 | + props.put("breakpointCount", JsonUtils.toJson(breakpointCountMap)); |
| 131 | + usageDataLogger.log(Level.INFO, "session usage data summary", props); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments