Skip to content

Commit

Permalink
Alert display fixed (#249)
Browse files Browse the repository at this point in the history
* Alert display fixed

* Full Trigger Setup

* Imporved Code quality according to Codacy conventions
  • Loading branch information
pg301 authored and KeenWarrior committed Aug 24, 2017
1 parent d339775 commit 9c7f080
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 64 deletions.
Expand Up @@ -15,22 +15,36 @@
* limitations under the License.
*/
package org.hawkular.client.android.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.hawkular.client.android.R;
import org.hawkular.client.android.backend.BackendClient;
import org.hawkular.client.android.backend.model.AvailabilityCondition;
import org.hawkular.client.android.backend.model.Error;
import org.hawkular.client.android.backend.model.FullTrigger;
import org.hawkular.client.android.backend.model.Metric;
import org.hawkular.client.android.backend.model.ThresholdCondition;
import org.hawkular.client.android.util.Intents;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
Expand Down Expand Up @@ -118,7 +132,7 @@ void setUpTriggerValues(){
}

else{
Toast.makeText(getApplicationContext(),"Trigger NAme cannot be empty",Toast.LENGTH_SHORT);
Toast.makeText(getApplicationContext(),"Trigger Name cannot be empty",Toast.LENGTH_SHORT);
}
trigger.setAutoDisable(switchAutoDisable.isChecked());
trigger.setType(spinnerType.getSelectedItem().toString());
Expand Down Expand Up @@ -147,7 +161,29 @@ private class TriggerCreateCallback implements Callback {
public void onResponse(Call call, Response response) {

if (response.code() == 200) {
finish();
final FullTrigger t = (FullTrigger) response.body();

CharSequence options[] = new CharSequence[] {"Add conditions to the trigger", "Finish Creating trigger"};
AlertDialog.Builder builder = new AlertDialog.Builder(TriggerSetupActivity.this);
builder.setTitle("Select one");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
if(getMetric().getConfiguration().getType().equalsIgnoreCase("gauge"))
showDialogBox(t.getId(),"gauge");
else if(getMetric().getConfiguration().getType().equalsIgnoreCase("availability"))
showDialogBox(t.getId(),"availability");

}
if(which == 1){
finish();
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();

}
else {
Gson gson = new GsonBuilder().create();
Expand All @@ -168,6 +204,124 @@ public void onFailure(Call call, Throwable t) {
}
}

void showDialogBox(final String t_id, final String type){
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(this);

if(type.equalsIgnoreCase("gauge")){
View promptsView = li.inflate(R.layout.custom_threshold, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);

final Spinner optional1 = (Spinner) promptsView.findViewById(R.id.spinner_threshold);
final EditText threshold = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput1);

// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
prepareCallBack(optional1,threshold,t_id, type);

}
})
.setNegativeButton("Cancel", null);

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}

if(type.equalsIgnoreCase("availability")){
View promptsView = li.inflate(R.layout.custom_availability, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);

final Spinner optional1 = (Spinner) promptsView.findViewById(R.id.spinner_avail);

// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
prepareCallBack(optional1,null,t_id, type);

}
})
.setNegativeButton("Cancel", null);

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}


}

void prepareCallBack (Spinner optional, EditText threshold, String id, String type){


if(type.equalsIgnoreCase("gauge")){

ArrayList<ThresholdCondition> list = new ArrayList<>();
ThresholdCondition condition = new ThresholdCondition();

String dataId = "hm_g_" + getMetric().getId();
condition.setType("THRESHOLD");
condition.setDataId(dataId);

condition.setOperator(optional.getSelectedItem().toString().toUpperCase());
condition.setThreshold(Integer.parseInt(threshold.getText().toString()));

list.add(0,condition);
BackendClient.of(this).putTriggerThresholdCondition(id,"FIRING",list,new ConditionCallback());
}

if(type.equalsIgnoreCase("availability")){

ArrayList<AvailabilityCondition> list = new ArrayList<>();
AvailabilityCondition condition = new AvailabilityCondition();

String dataId = "hm_a_" + getMetric().getId();
condition.setType("AVAILABILITY");
condition.setDataId(dataId);

condition.setOperator(optional.getSelectedItem().toString().toUpperCase());
list.add(0,condition);
BackendClient.of(this).putTriggerAvailabilityCondition(id,"FIRING",list,new ConditionCallback());
}


}

private class ConditionCallback implements Callback{


@Override
public void onResponse(Call call, Response response) {
finish();
}

@Override
public void onFailure(Call call, Throwable t) {
finish();
}
}
private Metric getMetric() {
return getIntent().getParcelableExtra(Intents.Extras.METRIC);
}

}

Expand Up @@ -26,17 +26,18 @@

import org.hawkular.client.android.HawkularApplication;
import org.hawkular.client.android.backend.model.Alert;
import org.hawkular.client.android.backend.model.AvailabilityCondition;
import org.hawkular.client.android.backend.model.Feed;
import org.hawkular.client.android.backend.model.FullTrigger;
import org.hawkular.client.android.backend.model.InventoryResponseBody;
import org.hawkular.client.android.backend.model.Metric;
import org.hawkular.client.android.backend.model.MetricAvailabilityBucket;
import org.hawkular.client.android.backend.model.MetricBucket;
import org.hawkular.client.android.backend.model.MetricCounterBucket;
import org.hawkular.client.android.backend.model.MetricGaugeBucket;
import org.hawkular.client.android.backend.model.Operation;
import org.hawkular.client.android.backend.model.OperationProperties;
import org.hawkular.client.android.backend.model.Resource;
import org.hawkular.client.android.backend.model.ThresholdCondition;
import org.hawkular.client.android.backend.model.Trigger;
import org.hawkular.client.android.service.AlertService;
import org.hawkular.client.android.service.MetricService;
Expand Down Expand Up @@ -141,7 +142,7 @@ public void updateTrigger(@NonNull String triggerId, Boolean enabled, @NonNull r
call.enqueue(callback);
}

public void createTrigger(@NonNull FullTrigger trigger, @NonNull retrofit2.Callback<List<String>> callback){
public void createTrigger(@NonNull FullTrigger trigger, @NonNull retrofit2.Callback<Trigger> callback){
TriggerService service = retrofit.create(TriggerService.class);
Call call = service.createTrigger(trigger);
call.enqueue(callback);
Expand Down Expand Up @@ -192,31 +193,6 @@ public void getMetrics(@NonNull Resource resource, @NonNull Callback<List<Metric
// TODO : after moving to retrofit complete
}


public void getMetricData(@NonNull Metric metric, long bucket,
@NonNull Date startTime, @NonNull Date finishTime,
@NonNull Callback<List<MetricBucket>> callback) {
Map<String, String> parameters = new HashMap<>();
parameters.put(BackendPipes.Parameters.START, String.valueOf(startTime.getTime()));
parameters.put(BackendPipes.Parameters.FINISH, String.valueOf(finishTime.getTime()));
parameters.put(BackendPipes.Parameters.BUCKETS, String.valueOf(bucket));

MetricService service = retrofit.create(MetricService.class);

Log.d("BackendClient",metric.getConfiguration().getType());
Call call = null;
if (metric.getConfiguration().getType().equalsIgnoreCase("AVAILABILITY")) {
call = service.getMetricAvailabilityData(metric.getId(), parameters);
} else if (metric.getConfiguration().getType().equalsIgnoreCase("COUNTER")){
// call = service.getMetricAvailabilityData(metric.getId(), parameters);
} else if (metric.getConfiguration().getType().equalsIgnoreCase("GAUGE")) {
//call = service.getMetricAvailabilityData(metric.getId(), parameters);
}

call.enqueue(callback);
}


public void getMetricAvailabilityData(@NonNull Metric metric, long bucket, @NonNull Date startTime,
@NonNull Date finishTime, @NonNull Callback<List<MetricAvailabilityBucket>> callback){
Map<String, String> parameters = new HashMap<>();
Expand Down Expand Up @@ -253,6 +229,19 @@ public void getMetricCounterData(@NonNull Metric metric, long bucket, @NonNull D
call.enqueue(callback);
}

public void putTriggerThresholdCondition(String triggerId, String triggerMode, List<ThresholdCondition> body, @NonNull retrofit2.Callback<String> callback){
TriggerService triggerService = retrofit.create(TriggerService.class);
Call call = triggerService.setConditionsForTrigger(triggerId,triggerMode,body);
call.enqueue(callback);
}

public void putTriggerAvailabilityCondition(String triggerId, String triggerMode, List<AvailabilityCondition> body, @NonNull retrofit2.Callback<String> callback){
TriggerService triggerService = retrofit.create(TriggerService.class);
Call call = triggerService.setConditionsForAvailabilityTrigger(triggerId,triggerMode,body);
call.enqueue(callback);
}


public void getMetricGaugeData(@NonNull Metric metric, long bucket, @NonNull Date startTime,
@NonNull Date finishTime, @NonNull Callback<List<MetricGaugeBucket>> callback){
Map<String, String> parameters = new HashMap<>();
Expand Down
Expand Up @@ -40,10 +40,10 @@ public final class Alert implements Parcelable {
private String status;

@SerializedName("ctime")
private long timestamp;
private long ctime;

@SerializedName("evalSets")
private List<Lister> evaluations;
private List<List<AlertEvaluation>> evalSets;

@SerializedName("notes")
private List<Note> notes;
Expand All @@ -52,11 +52,11 @@ public final class Alert implements Parcelable {
private Trigger trigger;

@VisibleForTesting
public Alert(@NonNull String id, long timestamp, @NonNull List<Lister> evaluations, @NonNull String severity,
public Alert(@NonNull String id, long timestamp, @NonNull List<List<AlertEvaluation>> evaluations, @NonNull String severity,
@NonNull String status, @NonNull List<Note> notes) {
this.id = id;
this.timestamp = timestamp;
this.evaluations = evaluations;
this.ctime = timestamp;
this.evalSets = evaluations;
this.severity = severity;
this.status = status;
this.notes = notes;
Expand All @@ -79,11 +79,11 @@ public String getStatus() {
}

public long getTimestamp() {
return timestamp;
return ctime;
}

public List<Lister> getEvaluations() {
return evaluations;
public List<List<AlertEvaluation>> getEvaluations() {
return evalSets;
}

public Trigger getTrigger() {
Expand All @@ -110,12 +110,12 @@ private Alert(Parcel parcel) {
this.id = parcel.readString();
this.severity = parcel.readString();
this.status = parcel.readString();
this.timestamp = parcel.readLong();
this.ctime = parcel.readLong();

evaluations = new ArrayList<>();
evalSets = new ArrayList<>();
notes = new ArrayList<>();

parcel.readList(evaluations, Lister.class.getClassLoader());
parcel.readList(evalSets, Lister.class.getClassLoader());
parcel.readList(notes, Note.class.getClassLoader());

this.trigger = parcel.readParcelable(Trigger.class.getClassLoader());
Expand All @@ -126,9 +126,9 @@ public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(id);
parcel.writeString(severity);
parcel.writeString(status);
parcel.writeLong(timestamp);
parcel.writeLong(ctime);

parcel.writeList(evaluations);
parcel.writeList(evalSets);
parcel.writeList(notes);

parcel.writeParcelable(trigger, flags);
Expand Down

0 comments on commit 9c7f080

Please sign in to comment.