diff options
Diffstat (limited to 'src/com/google/devrel/cluestick/searchservice/EventLog.java')
-rw-r--r-- | src/com/google/devrel/cluestick/searchservice/EventLog.java | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/src/com/google/devrel/cluestick/searchservice/EventLog.java b/src/com/google/devrel/cluestick/searchservice/EventLog.java deleted file mode 100644 index 97b60e9..0000000 --- a/src/com/google/devrel/cluestick/searchservice/EventLog.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.devrel.cluestick.searchservice; - -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.TimeUnit; - -/** - * Logging helper for Cluestick. - */ -public class EventLog { - private static final long LOG_DELAY_SEC = 30; - - private final CluestickSearch cluestick; - private final Map<PendingEvent, Integer> pending; - private final Timer timer; - - public EventLog(CluestickSearch cluestick) { - this.cluestick = cluestick; - timer = new Timer(true); - pending = new HashMap<PendingEvent, Integer>(); - } - - /** - * Releases resources used by this {@link EventLog}. - */ - public void cancel() { - timer.cancel(); - } - - /** - * Asynchronously logs an event to the Cluestick service. - * - * @param action The string action, e.g. "copy". - * @param resultKey The result being operated on, if any. - * @param signal Either a positive or negative signal, e.g. for code result quality. - */ - public void logEvent(@Nullable String action, @Nullable String resultKey, int signal) { - boolean scheduleTask; - synchronized (pending) { - scheduleTask = pending.isEmpty(); - - PendingEvent eventKey = new PendingEvent(action, resultKey); - Integer signalObject = pending.get(eventKey); - if (signalObject == null) { - signalObject = new Integer(signal); - } else { - signalObject = new Integer(signalObject.intValue() + signal); - } - pending.put(eventKey, signalObject); - } - - if (scheduleTask) { - timer.schedule(new TimerTask() { - @Override - public void run() { - submitEvents(); - } - }, TimeUnit.SECONDS.toMillis(LOG_DELAY_SEC)); - } - } - - /** - * Submits all pending events to the Cluestick service. - */ - private void submitEvents() { - Map<PendingEvent, Integer> active; - synchronized (pending) { - active = new HashMap<PendingEvent, Integer>(pending); - pending.clear(); - } - for (Map.Entry<PendingEvent, Integer> entry : active.entrySet()) { - PendingEvent eventKey = entry.getKey(); - cluestick.logEvent(eventKey.action, eventKey.resultKey, entry.getValue().intValue()); - } - } - - private static class PendingEvent { - final String action; - final String resultKey; - - PendingEvent(String action, String resultKey) { - this.action = action; - this.resultKey = resultKey; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof PendingEvent)) { - return false; - } - PendingEvent other = (PendingEvent) o; - return Objects.equals(action, other.action) && Objects.equals(resultKey, other.resultKey); - } - - @Override - public int hashCode() { - return Objects.hash(action, resultKey); - } - } -} - |