Skip to content

DM-4591: GWT conversion: System notifications #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/firefly/html/css/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
content: '×'; /* UTF-8 symbol */
}

/*------------------- loading mask ---------------------*/
/*-------------------< loading mask ---------------------*/
.loading-mask {
position: absolute;
width: 100%;
Expand Down Expand Up @@ -311,7 +311,7 @@
animation-iteration-count: infinite;
animation-timing-function: linear;
}
/*------------------- loading mask ---------------------*/
/*------------------- loading mask >---------------------*/

.banner-background {
background: url('/images/ipac_bar.jpg');
Expand All @@ -323,4 +323,22 @@
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
}

.alerts__msg {
color: #1A2739;
text-align: center;
background-color: #FFF7C2;
padding: 5px 20px;
border: 1px solid #BDB05D;
font: 12px sans-serif;
text-shadow: none;
vertical-align: middle;
width: 100%;
box-sizing: border-box;
}

.alerts__msg a {
color: blue;
text-decoration: none;
}
5 changes: 5 additions & 0 deletions src/firefly/java/edu/caltech/ipac/firefly/data/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class Alert implements Serializable {

public Alert() {}

public Alert(String msg, long lastModDate) {
this.msg = msg;
this.lastModDate = lastModDate;
}

public Alert(String url, String title, boolean isNew) {
this.url = url;
this.msg = title;
Expand Down
105 changes: 105 additions & 0 deletions src/firefly/java/edu/caltech/ipac/firefly/server/AlertsMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package edu.caltech.ipac.firefly.server;

import edu.caltech.ipac.firefly.data.Alert;
import edu.caltech.ipac.firefly.data.ServerEvent;
import edu.caltech.ipac.firefly.server.events.FluxAction;
import edu.caltech.ipac.firefly.server.events.ServerEventManager;
import edu.caltech.ipac.firefly.server.util.Logger;
import edu.caltech.ipac.util.AppProperties;
import edu.caltech.ipac.util.FileUtil;
import edu.caltech.ipac.util.StringUtils;
import edu.jhu.util.StringUtil;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
* Date: 6/16/16
*
* @author loi
* @version $Id: $
*/
public class AlertsMonitor {
private static final Logger.LoggerImpl LOG = Logger.getLogger();
private static final String ALERTS_DIR = AppProperties.getProperty("alerts.dir", "/hydra/alerts/");
private static final File alertDir = new File(ALERTS_DIR);
private static Alert alerts = null;
private static Timer timer;

/**
* check for alerts
* @param forceSend if true, send alerts regardless
*/
public static void checkAlerts(boolean forceSend) {
long lastMod = alerts == null ? 0 : alerts.getLastModDate();
File[] files = alertDir.listFiles(file -> file.isFile() && !file.isHidden());
long mod = Math.max(lastMod, alertDir.lastModified());
String msg = "";
if (files != null && files.length > 0) {
// found alerts
for (File f : files) {
mod = Math.max(mod, f.lastModified());
try {
if (msg.length() > 0) {
msg += " <br> ";
}
msg += FileUtil.readFile(f);
} catch (IOException e) {
LOG.error(e, "Error reading alerts.");
}
}
}
if (mod > lastMod ) {
alerts = new Alert(msg, mod);
sendAlerts(alerts);
} else if (forceSend && alerts != null) {
sendAlerts(alerts);
}
}

public static void startMonitor() {
if (timer != null) {
timer.cancel();
}
timer = new Timer("AlertsMonitor", true);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
checkAlerts(false);
}
}, 0, 1000 * 10); // check for update every 10 seconds
}

private static void sendAlerts(Alert alerts) {
FluxAction action = new FluxAction("app_data.setAlerts"); // AppDataCntlr.SET_ALERTS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know we can fire flux action from server side. Somehow it missed it before.

action.setValue(alerts.getMsg(), "msg");
ServerEventManager.fireAction(action, ServerEvent.Scope.WORLD);
}

public static void main(String[] args) {
AlertsMonitor.startMonitor();
}
}
/*
* THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE CALIFORNIA
* INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. GOVERNMENT CONTRACT WITH
* THE NATIONAL AERONAUTICS AND SPACE ADMINISTRATION (NASA). THE SOFTWARE
* IS TECHNOLOGY AND SOFTWARE PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS
* AND IS PROVIDED AS-IS TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND,
* INCLUDING ANY WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC 2312-2313)
* OR FOR ANY PURPOSE WHATSOEVER, FOR THE SOFTWARE AND RELATED MATERIALS,
* HOWEVER USED.
*
* IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA BE LIABLE
* FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT LIMITED TO, INCIDENTAL
* OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING ECONOMIC DAMAGE OR INJURY TO
* PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE
* ADVISED, HAVE REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
*
* RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE
* AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY CALTECH AND NASA FOR
* ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS OF RECIPIENT IN THE USE
* OF THE SOFTWARE.
*/
36 changes: 20 additions & 16 deletions src/firefly/java/edu/caltech/ipac/firefly/server/RequestOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ public RequestAgent getRequestAgent() {

public void setRequestAgent(RequestAgent requestAgent) {
this.requestAgent = requestAgent;
host = requestAgent.getHeader("host");
referrer = requestAgent.getHeader("Referer");

String sei = requestAgent.getCookie("seinfo");
if (!StringUtils.isEmpty(sei)) {
String [] parts = sei.split("_");
if (parts.length > 1) {
eventConnID = parts[0];
eventChannel = parts[1];
if (requestAgent != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious: when the requestAgent would be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When RequestOwner is referenced outside of a http request thread. Somewhere in the our cache code, it's doing it.

host = requestAgent.getHeader("host");
referrer = requestAgent.getHeader("Referer");

String sei = requestAgent.getCookie("seinfo");
if (!StringUtils.isEmpty(sei)) {
String [] parts = sei.split("_");
if (parts.length > 1) {
eventConnID = parts[0];
eventChannel = parts[1];
}
}
}
}
Expand All @@ -91,7 +93,7 @@ public WorkspaceManager getWsManager() {

public String getUserKey() {
if (userKey == null) {
String userKeyAndName = requestAgent.getCookie(USER_KEY);
String userKeyAndName = requestAgent == null ? null : requestAgent.getCookie(USER_KEY);
userKey = userKeyAndName == null ? null :
userKeyAndName.split("/", 2)[0];

Expand Down Expand Up @@ -238,12 +240,14 @@ private String newUserKey() {

private void updateUserKey(String userName) {
String nVal = userKey + "/" + userName;
String cVal = requestAgent.getCookie(USER_KEY);
if (!nVal.equals(String.valueOf(cVal))) {
Cookie cookie = new Cookie(USER_KEY, userKey + "/" + userName);
cookie.setMaxAge(3600 * 24 * 7 * 2); // to live for two weeks
cookie.setPath("/"); // to make it available to all subpasses within base URL
requestAgent.sendCookie(cookie);
if (requestAgent != null) {
String cVal = requestAgent.getCookie(USER_KEY);
if (!nVal.equals(String.valueOf(cVal))) {
Cookie cookie = new Cookie(USER_KEY, userKey + "/" + userName);
cookie.setMaxAge(3600 * 24 * 7 * 2); // to live for two weeks
cookie.setPath("/"); // to make it available to all subpasses within base URL
requestAgent.sendCookie(cookie);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import edu.caltech.ipac.firefly.server.query.SearchProcessorFactory;
import edu.caltech.ipac.firefly.server.util.Logger;
import edu.caltech.ipac.firefly.server.visualize.VisContext;
import edu.caltech.ipac.util.AppProperties;
import edu.caltech.ipac.util.Assert;
import edu.caltech.ipac.util.ClientLog;
import edu.caltech.ipac.util.StringUtils;
import edu.caltech.ipac.util.*;
import edu.caltech.ipac.util.cache.CacheManager;
import org.apache.log4j.PropertyConfigurator;
import org.json.simple.JSONArray;
Expand All @@ -21,14 +18,8 @@
import javax.servlet.http.HttpServletResponse;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -102,6 +93,9 @@ public static void init() {

// initialize search processors
SearchProcessorFactory.init();

// alerts monitoring
AlertsMonitor.startMonitor();
}

public static void configInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static void fireAction(FluxAction action, String channel) {
fireJsonAction(action.toString(), ServerEvent.Scope.CHANNEL, channel);
}

public static void fireAction(FluxAction action, ServerEvent.Scope scope) {
fireJsonAction(action.toString(), scope, null);
}

public static void fireJsonAction(String actionStr, String channel) {
fireJsonAction(actionStr, ServerEvent.Scope.CHANNEL, channel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package edu.caltech.ipac.firefly.server.events;

import edu.caltech.ipac.firefly.data.ServerEvent;
import edu.caltech.ipac.firefly.server.AlertsMonitor;
import edu.caltech.ipac.firefly.server.ServerContext;
import edu.caltech.ipac.firefly.server.util.Logger;
import edu.caltech.ipac.firefly.util.event.Name;
Expand Down Expand Up @@ -44,6 +45,8 @@ public void onOpen(final Session session) {
ServerEventManager.addEventQueue(eventQueue);
// notify clients within the same channel
updateClientConnections(CONN_UPDATED, channelID);
// check for alerts
AlertsMonitor.checkAlerts(true);

} catch (Exception e) {
LOG.error(e, "Unable to open websocket connection:" + session.getId());
Expand Down
9 changes: 9 additions & 0 deletions src/firefly/js/core/AppDataCntlr.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const ADD_PREF = `${APP_DATA_PATH}.addPreference`;
export const REMOVE_PREF = `${APP_DATA_PATH}.removePreference`;
export const REINIT_RESULT_VIEW = `${APP_DATA_PATH}.reinitResultView`;
export const ROOT_URL_PATH = `${APP_DATA_PATH}.rootUrlPath`;
export const SET_ALERTS = `${APP_DATA_PATH}.setAlerts`;

export const HELP_LOAD = `${APP_DATA_PATH}.helpLoad`;

Expand Down Expand Up @@ -189,6 +190,10 @@ function appDataReducer(state, action={}) {
case WS_CONN_UPDATED :
return updateSet(state, ['connections'], action.payload);

case SET_ALERTS :
const {msg=''} = action.payload || {};
return updateSet(state, ['alerts'], {msg});

default:
return state;
}
Expand Down Expand Up @@ -276,6 +281,10 @@ export function isAppReady() {
export function getMenu() {
return get(flux.getState(), [APP_DATA_PATH, 'menu']);
}

export function getAlerts() {
return get(flux.getState(), [APP_DATA_PATH, 'alerts'], {});
}
/*---------------------------- PRIVATE -----------------------------*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/firefly/js/ui/DropDownContainer.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
.DD-ToolBar__content {
box-shadow:inset 0 0 3px #000000;
background-color: #E5E5E5;
margin: 3px;
margin: 5px 3px;
padding: 3px;
}

Expand Down
Loading