|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * MIT License |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files |
| 9 | + * (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + * subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 17 | + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 18 | + * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + */ |
| 20 | +package com.microsoft.azuretools.core.survey; |
| 21 | + |
| 22 | +import static com.microsoft.azuretools.Constants.FILE_NAME_SURVEY_CONFIG; |
| 23 | +import static com.microsoft.azuretools.telemetry.TelemetryConstants.SURVEY; |
| 24 | +import static com.microsoft.azuretools.telemetry.TelemetryConstants.SYSTEM; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.io.FileReader; |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.charset.Charset; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import org.apache.commons.io.IOUtils; |
| 36 | +import org.eclipse.core.runtime.Platform; |
| 37 | +import org.eclipse.swt.program.Program; |
| 38 | +import org.eclipse.swt.widgets.Display; |
| 39 | +import org.joda.time.LocalDateTime; |
| 40 | +import org.osgi.framework.Version; |
| 41 | + |
| 42 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 43 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 45 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 46 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 47 | +import com.fasterxml.jackson.datatype.joda.deser.LocalDateTimeDeserializer; |
| 48 | +import com.fasterxml.jackson.datatype.joda.ser.LocalDateTimeSerializer; |
| 49 | +import com.microsoft.azuretools.authmanage.CommonSettings; |
| 50 | +import com.microsoft.azuretools.core.Activator; |
| 51 | +import com.microsoft.azuretools.telemetry.AppInsightsClient; |
| 52 | +import com.microsoft.azuretools.telemetrywrapper.EventType; |
| 53 | +import com.microsoft.azuretools.telemetrywrapper.EventUtil; |
| 54 | +import com.microsoft.azuretools.telemetrywrapper.Operation; |
| 55 | +import com.microsoft.azuretools.telemetrywrapper.TelemetryManager; |
| 56 | + |
| 57 | +import rx.Observable; |
| 58 | +import rx.schedulers.Schedulers; |
| 59 | + |
| 60 | +public class CustomerSurveyHelper { |
| 61 | + |
| 62 | + private static final int POP_UP_DELAY = 30; |
| 63 | + private static final int INIT_SURVEY_DELAY_BY_DAY = 10; |
| 64 | + private static final int PUT_OFF_DELAY_BY_DAY = 30; |
| 65 | + private static final int TAKE_SURVEY_DELAY_BY_DAY = 180; |
| 66 | + // Eclipse pop up window can't reset dispost time, so use a longer value here which differs IntelliJ(10s) |
| 67 | + private static final int DISPOSE_TIME = 20; |
| 68 | + |
| 69 | + private static final String SURVEY_URL = "https://microsoft.qualtrics.com/jfe/form/SV_5nhMbnPVKPLu2pv?" |
| 70 | + + "toolkit=%s&ide=%s&os=%s&jdk=%s&id=%s"; |
| 71 | + |
| 72 | + private static final String TELEMETRY_KEY_RESPONSE = "response"; |
| 73 | + private static final String TELEMETRY_VALUE_NEVER_SHOW = "neverShowAgain"; |
| 74 | + private static final String TELEMETRY_VALUE_PUT_OFF = "putOff"; |
| 75 | + private static final String TELEMETRY_VALUE_ACCEPT = "accept"; |
| 76 | + |
| 77 | + private boolean isShown = false; |
| 78 | + private SurveyConfig surveyConfig; |
| 79 | + private Operation operation; |
| 80 | + |
| 81 | + public CustomerSurveyHelper() { |
| 82 | + loadConfiguration(); |
| 83 | + } |
| 84 | + |
| 85 | + public void showFeedbackNotification() { |
| 86 | + if (isAbleToPopUpSurvey()) { |
| 87 | + Observable.timer(POP_UP_DELAY, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).take(1).subscribe(next -> { |
| 88 | + Display.getDefault().syncExec(() -> { |
| 89 | + SurveyNotificationPopup dialog = new SurveyNotificationPopup(Display.getDefault(), |
| 90 | + () -> { |
| 91 | + takeSurvey(); |
| 92 | + }, |
| 93 | + () -> { |
| 94 | + putOff(); |
| 95 | + }, |
| 96 | + () -> { |
| 97 | + neverShowAgain(); |
| 98 | + }); |
| 99 | + |
| 100 | + dialog.setFadingEnabled(false); |
| 101 | + dialog.setDelayClose(DISPOSE_TIME * 1000); |
| 102 | + dialog.open(); |
| 103 | + synchronized (CustomerSurveyHelper.class) { |
| 104 | + if (operation != null) { |
| 105 | + operation.complete(); |
| 106 | + } |
| 107 | + operation = TelemetryManager.createOperation(SYSTEM, SURVEY); |
| 108 | + operation.start(); |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void takeSurvey() { |
| 116 | + Program.launch(getRequestUrl()); |
| 117 | + surveyConfig.surveyTimes++; |
| 118 | + surveyConfig.lastSurveyDate = LocalDateTime.now(); |
| 119 | + surveyConfig.nextSurveyDate = LocalDateTime.now().plusDays(TAKE_SURVEY_DELAY_BY_DAY); |
| 120 | + saveConfiguration(); |
| 121 | + sendTelemetry(TELEMETRY_VALUE_ACCEPT); |
| 122 | + } |
| 123 | + |
| 124 | + private void neverShowAgain() { |
| 125 | + surveyConfig.isAcceptSurvey = false; |
| 126 | + saveConfiguration(); |
| 127 | + sendTelemetry(TELEMETRY_VALUE_NEVER_SHOW); |
| 128 | + } |
| 129 | + |
| 130 | + private void putOff() { |
| 131 | + surveyConfig.nextSurveyDate = LocalDateTime.now().plusDays(PUT_OFF_DELAY_BY_DAY); |
| 132 | + saveConfiguration(); |
| 133 | + sendTelemetry(TELEMETRY_VALUE_PUT_OFF); |
| 134 | + } |
| 135 | + |
| 136 | + private void loadConfiguration() { |
| 137 | + try (final FileReader fileReader = new FileReader(getConfigFile())) { |
| 138 | + String configString = IOUtils.toString(fileReader); |
| 139 | + ObjectMapper mapper = new ObjectMapper(); |
| 140 | + surveyConfig = mapper.readValue(configString, SurveyConfig.class); |
| 141 | + } catch (IOException e) { |
| 142 | + surveyConfig = new SurveyConfig(); |
| 143 | + saveConfiguration(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private void saveConfiguration() { |
| 148 | + try { |
| 149 | + File configFile = getConfigFile(); |
| 150 | + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); |
| 151 | + IOUtils.write(ow.writeValueAsString(surveyConfig), new FileOutputStream(configFile), |
| 152 | + Charset.defaultCharset()); |
| 153 | + } catch (IOException e) { |
| 154 | + // swallow this exception as survey config should not bother user |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private synchronized void sendTelemetry(String response) { |
| 159 | + if (operation == null) { |
| 160 | + return; |
| 161 | + } |
| 162 | + Map<String, String> properties = new HashMap<>(); |
| 163 | + properties.put(TELEMETRY_KEY_RESPONSE, response); |
| 164 | + EventUtil.logEvent(EventType.info, operation, properties); |
| 165 | + operation.complete(); |
| 166 | + } |
| 167 | + |
| 168 | + private synchronized boolean isAbleToPopUpSurvey() { |
| 169 | + if (isShown) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + isShown = true; |
| 173 | + return surveyConfig.isAcceptSurvey && LocalDateTime.now().isAfter(surveyConfig.nextSurveyDate); |
| 174 | + } |
| 175 | + |
| 176 | + private static File getConfigFile() { |
| 177 | + return new File(CommonSettings.getSettingsBaseDir(), FILE_NAME_SURVEY_CONFIG); |
| 178 | + } |
| 179 | + |
| 180 | + private String getRequestUrl() { |
| 181 | + final Version version = Platform.getBundle("org.eclipse.platform").getVersion(); |
| 182 | + String ide = String.format("%s %s", "eclipse", version.toString()); |
| 183 | + String os = System.getProperty("os.name"); |
| 184 | + String jdk = String.format("%s %s", System.getProperty("java.vendor"), System.getProperty("java.version")); |
| 185 | + String id = AppInsightsClient.getInstallationId(); |
| 186 | + String toolkitVersion = Activator.getDefault().getBundle().getVersion().toString(); |
| 187 | + return String.format(SURVEY_URL, toolkitVersion, ide, os, jdk, id).replace(' ', '+'); |
| 188 | + } |
| 189 | + |
| 190 | + static class SurveyConfig { |
| 191 | + @JsonProperty("surveyTimes") |
| 192 | + private int surveyTimes = 0; |
| 193 | + @JsonProperty("isAcceptSurvey") |
| 194 | + private boolean isAcceptSurvey = true; |
| 195 | + @JsonDeserialize(using = LocalDateTimeDeserializer.class) |
| 196 | + @JsonSerialize(using = LocalDateTimeSerializer.class) |
| 197 | + private LocalDateTime lastSurveyDate = null; |
| 198 | + @JsonDeserialize(using = LocalDateTimeDeserializer.class) |
| 199 | + @JsonSerialize(using = LocalDateTimeSerializer.class) |
| 200 | + private LocalDateTime nextSurveyDate = LocalDateTime.now().plusDays(INIT_SURVEY_DELAY_BY_DAY); |
| 201 | + } |
| 202 | +} |
0 commit comments