Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Created August 28, 2012 18:51
Show Gist options
  • Select an option

  • Save gastaldi/3502224 to your computer and use it in GitHub Desktop.

Select an option

Save gastaldi/3502224 to your computer and use it in GitHub Desktop.
GoogleAnalytics
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.shell.analytics;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.jboss.forge.env.Configuration;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.ShellColor;
import org.jboss.forge.shell.ShellPrompt;
import org.jboss.forge.shell.events.PostStartup;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.Command;
import org.jboss.forge.shell.plugins.DefaultCommand;
import org.jboss.forge.shell.plugins.Option;
import org.jboss.forge.shell.plugins.Plugin;
@Alias("analytics")
public class GoogleAnalyticsPlugin implements Plugin
{
private static final String ANALYTICS_ENABLED = "analytics.enabled";
private static final String ANALYTICS_LAST_RUN = "analytics.lastrun";
private static final String ANALYTICS_DATE_MASK = "yyyyMMdd";
@Inject
private Configuration configuration;
@Inject
private Shell shell;
@Inject
private ShellPrompt prompt;
public void onStartup(@Observes PostStartup startup)
{
Boolean enabled = configuration.getBoolean(ANALYTICS_ENABLED, null);
if (enabled == null)
{
enabled = prompt
.promptBoolean(
"Will you allow Forge team to receive anonymous usage statistics for this Forge installation ?",
true);
configuration.setProperty(ANALYTICS_ENABLED, enabled);
}
if (enabled)
{
Date lastRunDate = getLastRunDate();
if (lastRunDate == null || lastRunDate.before(new Date()))
{
run();
setLastRunDate(new Date());
}
}
}
@DefaultCommand
public void setState(@Option(name = "enabled", required = true) Boolean enabled)
{
configuration.setProperty(ANALYTICS_ENABLED, enabled);
}
/**
* Display info about the analytics usage
*/
@Command("info")
protected void displayInfo()
{
String state = configuration.getBoolean(ANALYTICS_ENABLED, Boolean.FALSE) ? "enabled"
: "disabled";
shell.print("Analytics is: ");
shell.println(ShellColor.BOLD, state);
Date lastRunDate = getLastRunDate();
if (lastRunDate != null)
{
DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.MEDIUM);
shell.print("Analytics was last run at: ");
shell.println(ShellColor.BOLD, dateInstance.format(lastRunDate));
}
}
private Date getLastRunDate()
{
Date date = null;
String lastRunDate = configuration.getString(ANALYTICS_LAST_RUN, null);
if (lastRunDate != null)
{
SimpleDateFormat sdf = new SimpleDateFormat(ANALYTICS_DATE_MASK);
try
{
return sdf.parse(lastRunDate);
}
catch (ParseException e)
{
return null;
}
}
return date;
}
private void setLastRunDate(Date date)
{
SimpleDateFormat sdf = new SimpleDateFormat(ANALYTICS_DATE_MASK);
configuration.setProperty(ANALYTICS_LAST_RUN, sdf.format(date));
}
private void run()
{
shell.println("PINGAR O GA");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment