This commit is contained in:
Alessandro Pieve 2017-02-17 14:55:03 +00:00
parent 78e50b04fb
commit 45e3a42c37
6 changed files with 192 additions and 62 deletions

View File

@ -72,8 +72,12 @@ public class QuotaCheck {
DiscoveryListUser discoveryListUser= new DiscoveryListUser(this.context);
log.debug("QuotaCheck - for context:{} , list user:{}",context,discoveryListUser.getListUser());
//verify for each user if have a specify quota and Overwrite if exist and traduce into object for query accounting
quotaUtility.verifyListUser(discoveryListUser.getListUser());
log.debug("QuotaCheck - quotaUtility.getUsageToBeVerified():{}",quotaUtility.getUsageToBeVerified());
Boolean error=true;
@ -100,12 +104,12 @@ public class QuotaCheck {
//insert into db
for(UsageValue usageIndex:quotaUtility.getUsageToBeVerified()){
if (usageIndex.getClz()==QuotaType.STORAGE.getQuotaTypeClass()){
log.debug("save quota storage identifier:{},temporalConstraint:{}",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint());
log.debug("----Elaborate a identifier:{}, temporalConstraint:{} , insert a quota storage",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint());
QuotaUsageStorageValue usageStorVal=(QuotaUsageStorageValue) usageIndex;
queryQuotaUsage.insertStorageQuota(usageStorVal);
}
if (usageIndex.getClz()==QuotaType.SERVICE.getQuotaTypeClass()){
log.debug("save quota service identifier:{},temporalConstraint:{}",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint());
log.debug("----Elaborate a identifier:{}, temporalConstraint:{} , insert a quota service",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint());
QuotaUsageServiceValue usageSerVal=(QuotaUsageServiceValue) usageIndex;
queryQuotaUsage.insertServiceQuota(usageSerVal);
}

View File

@ -142,7 +142,7 @@ public class QuotaUsage {
Double percent=((usageStorVal.getD()/usageStorVal.getdQuota())*100);
percent=Math.round(percent * 100.0) / 100.0;
log.debug("Quota Percent quota:{} used:{},use:{}",usageStorVal.getdQuota(),usageStorVal.getD(),percent);
log.debug("Quota Percent quota:{} used:{} percent:{}",usageStorVal.getdQuota(),usageStorVal.getD(),percent);
String queryVerify="SELECT NOTIFICATIONWARNING1,NOTIFICATIONWARNING2,NOTIFICATIONEXCEED,QUOTAASSIGNED FROM "+ConstantsDb.DATABASE_PREFIX+ConstantsDb.SEPARATOR+ConstantsDb.DATABASE_TYPE_STORAGE+ConstantsDb.SEPARATOR+periodQuota+""
+ " WHERE IDENTIFIER ='"+usageStorVal.getIdentifier()+"'";
@ -154,32 +154,37 @@ public class QuotaUsage {
quotaAssignedOld= rs.getDouble("QUOTAASSIGNED");
}
if (quotaAssignedOld!=usageStorVal.getdQuota()){
if (!quotaAssignedOld.equals(usageStorVal.getdQuota())){
//verify if your quota assigned is changed
log.debug("quota changed old value:{} new value:{}",quotaAssignedOld,usageStorVal.getdQuota());
if (!quotaAssignedOld.equals(0.0)){
//verify if your old quota is consistent
log.debug("your old quota is consistent:{}",quotaAssignedOld);
sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"change");
}
}
//verify if a getdQuota() == -1 considery a infinity quota
log.debug("usageStorVal.getdQuota()!=-1 :"+usageStorVal.getdQuota());
if (usageStorVal.getdQuota()!=-1){
if (!usageStorVal.getdQuota().equals(-1)){
if ((percent>Constants.LIMIT_MSG_QUOTA_PERC_USAGE_1)&&(percent<Constants.LIMIT_MSG_QUOTA_PERC_USAGE_2)){
//verify if your quota is between 90 and 95 % first warning
if (!notificationWarning1){
sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning");
notificationWarning1=true;
notificationWarning2=false;
notificationExceed=false;
if (sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning")){
log.debug("notification Send! warning level1");
notificationWarning1=true;
notificationWarning2=false;
notificationExceed=false;
}
}
}
else if ((percent>Constants.LIMIT_MSG_QUOTA_PERC_USAGE_2)&&(percent<100)){
//verify if your quota is between 95 and 100 % second warning
if (!notificationWarning2){
sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning");
notificationWarning2=true;
notificationExceed=false;
if (sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning")){
log.debug("notification Send! warning level2");
notificationWarning2=true;
notificationExceed=false;
}
}
}
else if ((percent>=100)){
@ -187,10 +192,11 @@ public class QuotaUsage {
log.debug("Quota exceed for:{} identifier:{} notification:{} ",percent,usageStorVal.getIdentifier(),notificationExceed);
if (!notificationExceed){
sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"exceed");
if(sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"exceed")){
log.debug("notification Send! warning level3");
notificationExceed=true;
}
userExceedQuota.add(usageStorVal.getIdentifier());
notificationExceed=true;
}
}
else if (notificationWarning1 || notificationWarning2 || notificationExceed){
@ -199,13 +205,18 @@ public class QuotaUsage {
usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD());
log.debug("---Quota reset for notificationWarning1:{} and notificationWarning2 :{} and notificationExceed:{}",
notificationWarning1,notificationWarning2,notificationExceed);
sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"reset");
notificationWarning1=false;
notificationWarning2=false;
notificationExceed=false;
if(sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"reset")){
log.debug("reset notification Send!");
notificationWarning1=false;
notificationWarning2=false;
notificationExceed=false;
}
}
}
else{
log.debug("quote infinite for identifier:{}",usageStorVal.getIdentifier());
}
queryVerify="SELECT count(*) FROM "+ConstantsDb.DATABASE_PREFIX+ConstantsDb.SEPARATOR+ConstantsDb.DATABASE_TYPE_STORAGE+ConstantsDb.SEPARATOR+periodQuota+""
+ " WHERE IDENTIFIER ='"+usageStorVal.getIdentifier()+"'";
@ -257,7 +268,13 @@ public class QuotaUsage {
}
}
/**
* selectStorageQuota
* @param identifier
* @param interval
* @return Quota Storage Status
* @throws Exception
*/
public QuotaStorageStatus selectStorageQuota(String identifier, TimeInterval interval)throws Exception {
Connection connection = getDBConnection();
@ -294,8 +311,6 @@ public class QuotaUsage {
connection.close();
}
return quotaStorageStatus;
}
/**
@ -321,8 +336,8 @@ public class QuotaUsage {
* @return connection
*/
private Connection getDBConnection() {
Connection dbConnection = null;
Connection dbConnection = null;
try {
Class.forName(ConstantsDb.DB_DRIVER);
} catch (ClassNotFoundException e) {

View File

@ -34,11 +34,17 @@ public class DiscoveryConfiguration {
private String textQuotaUserExceedSubject="ATTENTION Your storage space exceeds the assigned quota capacity";
private String textQuotaUserExceed="Hi {identifier}, your storage space is full, Your quota occupation is {percent}% , you are using {quotaUsage} out of {quotaAssigned} available. Please free up some space.";
private String textQuotaUserChangeSubject="INFO your quota has changed";
private String textQuotaUserChange="Hi {identifier},your quota has changed to {quotaAssigned}. Your are currently occupying {percent}% of the available space.";
private String textQuotaAdminExceedSubject="INFO List User quota Exceed";
private String textQuotaAdminExceed="Dear, now this user have a quota exceed: {listuser}";
private String textQuotaUserReset="Hi {identifier}, Your quota occupation decreased and is now {percent}%, you are using {quotaUsage}, out of {quotaAssigned} available. Good job!";
private String textQuotaUserResetSubject="INFO Your storage space is now sufficiently below the quota capacity";
private String roleNotifier="Administrator";
@ -57,6 +63,9 @@ public class DiscoveryConfiguration {
private final static String TEXT_QUOTA_USER_EXCEED_SUBJECT="textQuotaUserExceedSubject";
private final static String TEXT_QUOTA_USER_EXCEED="textQuotaUserExceed";
private final static String TEXT_QUOTA_USER_CHANGE_SUBJECT="textQuotaUserChangeSubject";
private final static String TEXT_QUOTA_USER_CHANGE="textQuotaUserChange";
private final static String TEXT_QUOTA_ADMIN_EXCEED_SUBJECT="textQuotaAdminExceedSubject";
private final static String TEXT_QUOTA_ADMIN_EXCEED="textQuotaAdminExceed";
@ -153,6 +162,12 @@ public class DiscoveryConfiguration {
case TEXT_QUOTA_USER_EXCEED:
textQuotaUserExceed=value;
break;
case TEXT_QUOTA_USER_CHANGE_SUBJECT:
textQuotaUserChangeSubject=value;
break;
case TEXT_QUOTA_USER_CHANGE:
textQuotaUserChange=value;
break;
case TEXT_QUOTA_ADMIN_EXCEED_SUBJECT:
textQuotaAdminExceedSubject=value;
break;
@ -182,13 +197,25 @@ public class DiscoveryConfiguration {
@Override
public String toString() {
return "DiscoveryConfiguration [databasePath=" + databasePath
+ ", usernameDb=" + usernameDb + ", pwdnameDb=" + pwdnameDb
+ ", dbname=" + dbname + ", notifierUser=" + notifierUser
+ ", notifierAdmin=" + notifierAdmin + ", refreshTimeQuota="
+ refreshTimeQuota + "]";
+ refreshTimeQuota + ", textQuotaUserWarning="
+ textQuotaUserWarning + ", textQuotaUserWarningSubject="
+ textQuotaUserWarningSubject + ", textQuotaUserExceedSubject="
+ textQuotaUserExceedSubject + ", textQuotaUserExceed="
+ textQuotaUserExceed + ", textQuotaUserChangeSubject="
+ textQuotaUserChangeSubject + ", textQuotaUserChange="
+ textQuotaUserChange + ", textQuotaAdminExceedSubject="
+ textQuotaAdminExceedSubject + ", textQuotaAdminExceed="
+ textQuotaAdminExceed + ", textQuotaUserReset="
+ textQuotaUserReset + ", textQuotaUserResetSubject="
+ textQuotaUserResetSubject + ", roleNotifier=" + roleNotifier
+ "]";
}
public String getDatabasePath() {
@ -251,5 +278,16 @@ public class DiscoveryConfiguration {
public String getRoleNotifier() {
return roleNotifier;
}
public String getTextQuotaUserChangeSubject() {
return textQuotaUserChangeSubject;
}
public String getTextQuotaUserChange() {
return textQuotaUserChange;
}
}

View File

@ -7,6 +7,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.slf4j.Logger;
@ -22,10 +23,17 @@ import com.google.gson.Gson;
public class DiscoveryListUser {
private static Logger log = LoggerFactory.getLogger(DiscoveryListUser.class);
private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-usernames?gcube-token";
//private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-usernames?gcube-token";
private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-fullnames-and-usernames?gcube-token";
private List<String> listUser = null;
private static Map<String, String> listFullUser;
public DiscoveryListUser(String context) {
String token =SecurityTokenProvider.instance.get();
@ -34,13 +42,13 @@ public class DiscoveryListUser {
String urlService=discoveryList.getBasePath()+NAME_SERVICE_ALL_USERNAMES+"="+token;
log.debug("service DiscoveryServiceListUser:"+urlService);
String data = getJSON(urlService);
log.debug("data read:{}",data);
ListUser msg = new Gson().fromJson(data, ListUser.class);
//TODO for debug limit a list user:
//listUser=msg.getResult();
//REMOVE TODO
//log.debug("data read:{}",data);
ListUserInfo msg = new Gson().fromJson(data, ListUserInfo.class);
listFullUser=msg.getResult();
listUser = new ArrayList<String>(listFullUser.keySet());
log.debug("listuser:{}",listUser);
//ONLY DEBUG
/*
listUser=new ArrayList<String>();
listUser.add("lucio.lelii");
listUser.add("alessandro.pieve");
@ -49,6 +57,8 @@ public class DiscoveryListUser {
listUser.add("costantino.perciante");
listUser.add("luca.frosini");
listUser.add("roberto.cirillo");
log.debug("list username:{}",listUser);
*/
}
/**
@ -59,6 +69,12 @@ public class DiscoveryListUser {
return listUser;
}
public static String getMapUser(String identifier) {
return listFullUser.get(identifier);
}
/**
*
* @param url

View File

@ -0,0 +1,38 @@
package org.gcube.resource.management.quota.manager.util;
import java.util.Map;
/**
*
* @author pieve
*
*/
public class ListUserInfo {
/*
"success" : false,
"message" : "String index out of range: 5",
"result" : null
*/
private Boolean success;
private String message;
private Map<String, String> result;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, String> getResult() {
return result;
}
public void setResult(Map<String, String> result) {
this.result = result;
}
}

View File

@ -34,7 +34,7 @@ public class SendNotification {
private DiscoveryConfiguration discoveryCheck;
private final static String WRITE_MESSAGE = "2/messages/write-message";
private final static String NAME_USERNAME_GLOBAL_ROLE = "2/users/get-usernames-by-global-role?role-name=Administrator&gcube-token";
//private final static String NAME_USERNAME_GLOBAL_ROLE = "2/users/get-usernames-by-global-role?role-name=Administrator&gcube-token";
private static Logger log = LoggerFactory.getLogger(QuotaUsage.class);
public SendNotification(DiscoveryConfiguration discoveryCheck){
@ -51,9 +51,10 @@ public class SendNotification {
this.percent=percent;
}
public void sendNotificationUser(String identifier, Double quotaAssigned, Double quotaUsage, Double percent, String type) throws JSONException{
public boolean sendNotificationUser(String identifier, Double quotaAssigned, Double quotaUsage, Double percent, String type) throws JSONException{
Boolean resultSend= false;
if (discoveryCheck.getNotifierUser()){
Boolean resultSend= false;
configure(identifier, quotaAssigned, quotaUsage, percent);
log.debug("Notification Quota limit used:{} identifier:{} quotaAssigned:{} quotaUsage:{} ",this.percent,this.identifier,this.quotaAssigned,this.quotaUsage);
@ -71,18 +72,20 @@ public class SendNotification {
body=CleanNotification(discoveryCheck.getTextQuotaUserReset());
subject=discoveryCheck.getTextQuotaUserResetSubject();
}
if (type=="change"){
body=CleanNotification(discoveryCheck.getTextQuotaUserChange());
subject=discoveryCheck.getTextQuotaUserChangeSubject();
}
String token = SecurityTokenProvider.instance.get();
JSONObject urlParameters = new JSONObject();
List<String> user=new ArrayList<String>();
//TODO delete
//user.add("alessandro.pieve");
user.add(identifier);
List<String> user=new ArrayList<String>();
user.add("alessandro.pieve");
//user.add(identifier);
urlParameters.put("recipients", user);
urlParameters.put("body", body);
urlParameters.put("subject", subject);
try {
resultSend = writeMsg(urlService, urlParameters,token);
resultSend = writeMsg(urlService, urlParameters,token);
} catch (Exception e) {
e.printStackTrace();
}
@ -91,7 +94,7 @@ public class SendNotification {
else{
log.debug("Not notification Quota limit used:{} identifier:{} quotaAssigned:{} quotaUsage:{} ",percent,identifier,quotaAssigned,quotaUsage);
}
return resultSend;
}
@ -100,17 +103,18 @@ public class SendNotification {
* @param userExceedQuota
* @throws Exception
*/
public void SendNotificationAdmin(List<String> userExceedQuota) throws Exception{
public boolean SendNotificationAdmin(List<String> userExceedQuota) throws Exception{
log.debug("SendNotificationAdmin for user:{}",userExceedQuota.toString());
Boolean resultSend= false;
if (userExceedQuota.size()>0){
if (discoveryCheck.getNotifierAdmin()){
Boolean resultSend= false;
String token = SecurityTokenProvider.instance.get();
String context = ScopeProvider.instance.get();
DiscoveryService discoveryList= new DiscoveryService(context);
List<String> sendersList=sendersList(discoveryList.getBasePath()+NAME_USERNAME_GLOBAL_ROLE,token,discoveryCheck.getRoleNotifier());
//List<String> sendersList=sendersList(discoveryList.getBasePath()+NAME_USERNAME_GLOBAL_ROLE,token,discoveryCheck.getRoleNotifier());
configure(identifier, quotaAssigned, quotaUsage, percent);
String text=CleanNotification(discoveryCheck.getTextQuotaAdminExceed());
@ -128,12 +132,16 @@ public class SendNotification {
JSONObject urlParameters = new JSONObject();
List<String> user=new ArrayList<String>();
identifier="alessandro.pieve";
//identifier="alessandro.pieve";
List<String> sendersList =new ArrayList<String>();
sendersList.add("alessandro.pieve");
//sendersList.add("scarponi");
for (String cc :sendersList){
// user.add(cc);
user.add(cc);
}
user.add(identifier);
//user.add(identifier);
urlParameters.put("recipients", user);
urlParameters.put("body", text);
urlParameters.put("subject", discoveryCheck.getTextQuotaAdminExceedSubject());
@ -148,7 +156,7 @@ public class SendNotification {
log.debug("not send notification quota exceed :{}",userExceedQuota.toString());
}
}
return resultSend;
}
@ -158,19 +166,30 @@ public class SendNotification {
* @return notification with user,quota and percent
*/
private String CleanNotification(String notification){
String fullname=DiscoveryListUser.getMapUser(identifier);
log.debug("Clean Notification from username:{} to fullname:{}",identifier,fullname);
if (notification.contains("{identifier}")){
notification=notification.replace("{identifier}", identifier);
notification=notification.replace("{identifier}", fullname);
}
String quotaAssignedLabel;
String quotaUsageLabel;
String percentLabel;
if (quotaAssigned>1024){
quotaAssignedLabel=String.valueOf(Math.round((quotaAssigned/1024) * 100.0) / 100.0)+"GB";
quotaUsageLabel= String.valueOf(Math.round((quotaUsage/1024) * 100.0) / 100.0)+"GB";
percentLabel=percent.toString();
}
else{
quotaAssignedLabel=String.valueOf(quotaAssigned)+"MB";
quotaUsageLabel= String.valueOf(quotaUsage)+"MB";
percentLabel=percent.toString();
}
if (quotaAssigned==-1){
quotaAssignedLabel="";
percentLabel="0";
}
if (notification.contains("{quotaAssigned}")){
@ -180,7 +199,7 @@ public class SendNotification {
notification=notification.replace("{quotaUsage}", quotaUsageLabel);
}
if (notification.contains("{percent}")){
notification=notification.replace("{percent}", percent.toString());
notification=notification.replace("{percent}",percentLabel);
}
return notification;
}
@ -206,14 +225,13 @@ public class SendNotification {
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
log.debug("writeMsg path:{} urlParameters:{} request:{}",path,urlParameters,request.toString());
/*
log.info("urlParameters:{}",urlParameters.toString());
log.info("request:{}",request.toString());
*/
int status = response.getStatusLine().getStatusCode();
// check the response status and look if it was a redirect problem
if (status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP ||
if ((status ==HttpURLConnection.HTTP_INTERNAL_ERROR)|| (status==HttpURLConnection.HTTP_BAD_GATEWAY)){
result=false;
}
else if (status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP ||
status == HttpURLConnection.HTTP_MOVED_PERM ||
status == HttpURLConnection.HTTP_SEE_OTHER)) {
@ -231,7 +249,8 @@ public class SendNotification {
request.setEntity(params);
response = httpClient.execute(request);
log.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
} else{
result=true;
}else{
log.debug(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
result=true;
}