queue in db

This commit is contained in:
Milos Kozak 2017-02-24 13:02:44 +01:00
parent e25fb9845d
commit a9d8f3f806
6 changed files with 169 additions and 116 deletions

View file

@ -39,6 +39,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public static final String DATABASE_TEMPTARGETS = "TempTargets";
public static final String DATABASE_TREATMENTS = "Treatments";
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
public static final String DATABASE_DBREQUESTS = "DBRequests";
private static final int DATABASE_VERSION = 5;
@ -62,6 +63,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
} catch (SQLException e) {
log.error("Can't create database", e);
throw new RuntimeException(e);
@ -77,6 +79,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.dropTable(connectionSource, BgReading.class, true);
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
TableUtils.dropTable(connectionSource, DbRequest.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
log.error("Can't drop databases", e);
@ -122,6 +125,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.dropTable(connectionSource, BgReading.class, true);
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
//DbRequests can be cleared from NSClient fragment
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
@ -172,6 +176,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return getDao(DanaRHistoryRecord.class);
}
public Dao<DbRequest, String> getDaoDbRequest() throws SQLException {
return getDao(DbRequest.class);
}
public long size(String database) {
return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
}
public List<BgReading> getBgreadingsDataFromTime(long mills, boolean ascending) {
try {
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
@ -189,6 +201,62 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList<BgReading>();
}
// DbRequests handling
public void create(DbRequest dbr) {
try {
getDaoDbRequest().create(dbr);
} catch (SQLException e) {
e.printStackTrace();
}
}
public int delete(DbRequest dbr) {
try {
return getDaoDbRequest().delete(dbr);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int deleteDbRequest(String nsClientId) {
try {
return getDaoDbRequest().deleteById(nsClientId);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int deleteDbRequestbyMongoId(String action, String id) {
try {
QueryBuilder<DbRequest, String> queryBuilder = getDaoDbRequest().queryBuilder();
Where where = queryBuilder.where();
where.eq("_id", id).and().eq("action", action);
queryBuilder.limit(10L);
PreparedQuery<DbRequest> preparedQuery = queryBuilder.prepare();
List<DbRequest> dbList = getDaoDbRequest().query(preparedQuery);
if (dbList.size() != 1) {
log.error("deleteDbRequestbyMongoId query size: " + dbList.size());
} else {
//log.debug("Treatment findTreatmentById found: " + trList.get(0).log());
return delete(dbList.get(0));
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public void deleteAllDbRequests() {
try {
TableUtils.clearTable(connectionSource, DbRequest.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
// TREATMENT HANDLING
public boolean isDataUnchanged(long time) {

View file

@ -1,23 +1,47 @@
package info.nightscout.androidaps.plugins.NSClientInternal.data;
package info.nightscout.androidaps.db;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by mike on 27.02.2016.
* <p>
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
*/
@DatabaseTable(tableName = DatabaseHelper.DATABASE_DBREQUESTS)
public class DbRequest {
public String action = null;
public String collection = null;
public JSONObject data = null;
public String _id = null;
private static Logger log = LoggerFactory.getLogger(DbRequest.class);
public String getNsClientID() {
return nsClientID;
}
public void setNsClientID(String nsClientID) {
this.nsClientID = nsClientID;
}
@DatabaseField(id = true, useGetSet = true)
public String nsClientID = null;
@DatabaseField
public String action = null;
@DatabaseField
public String collection = null;
@DatabaseField
public String data = null;
@DatabaseField
public String _id = null;
public DbRequest() {
}
@ -25,7 +49,7 @@ public class DbRequest {
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
this.action = action;
this.collection = collection;
this.data = data;
this.data = data.toString();
this.nsClientID = nsClientID;
this._id = "";
}
@ -34,7 +58,7 @@ public class DbRequest {
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
this.action = action;
this.collection = collection;
this.data = data;
this.data = data.toString();
this.nsClientID = nsClientID;
this._id = _id;
}
@ -43,7 +67,7 @@ public class DbRequest {
public DbRequest(String action, String collection, String nsClientID, String _id) {
this.action = action;
this.collection = collection;
this.data = new JSONObject();
this.data = new JSONObject().toString();
this.nsClientID = nsClientID;
this._id = _id;
}
@ -57,7 +81,7 @@ public class DbRequest {
try {
object.put("action", action);
object.put("collection", collection);
object.put("data", data);
object.put("data", new JSONObject(data));
if (_id != null) object.put("_id", _id);
if (nsClientID != null) object.put("nsClientID", nsClientID);
} catch (JSONException e) {
@ -74,7 +98,7 @@ public class DbRequest {
if (jsonObject.has("collection"))
result.collection = jsonObject.getString("collection");
if (jsonObject.has("data"))
result.data = jsonObject.getJSONObject("data");
result.data = jsonObject.getJSONObject("data").toString();
if (jsonObject.has("_id"))
result._id = jsonObject.getString("_id");
if (jsonObject.has("nsClientID"))

View file

@ -106,7 +106,7 @@ public class NSClientInternalFragment extends Fragment implements FragmentBase,
getPlugin().clearLog();
break;
case R.id.nsclientinternal_clearqueue:
getPlugin().queue().reset();
getPlugin().queue().clearQueue();
break;
case R.id.nsclientinternal_showqueue:
MainApp.bus().post(new EventNSClientNewLog("QUEUE", getPlugin().queue().textList()));

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.plugins.NSClientInternal;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.j256.ormlite.dao.CloseableIterator;
import org.json.JSONArray;
import org.json.JSONException;
@ -9,13 +8,14 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastQueueStatus;
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.DbRequest;
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
import info.nightscout.utils.SP;
@ -25,37 +25,30 @@ import info.nightscout.utils.SP;
public class UploadQueue {
private static Logger log = LoggerFactory.getLogger(UploadQueue.class);
public static HashMap<String, DbRequest> queue = null;
public UploadQueue() {
loadMap();
}
public static String status() {
return "QUEUE: " + queue.size();
return "QUEUE: " + MainApp.getDbHelper().size(DatabaseHelper.DATABASE_DBREQUESTS);
}
public static int size() {
return queue.size();
public static long size() {
return MainApp.getDbHelper().size(DatabaseHelper.DATABASE_DBREQUESTS);
}
public static void add(final DbRequest dbr) {
NSClientService.handler.post(new Runnable() {
@Override
public void run() {
log.debug("QUEUE adding: " + dbr.data.toString());
queue.put(dbr.hash(), dbr);
saveMap();
log.debug("QUEUE adding: " + dbr.data);
MainApp.getDbHelper().create(dbr);
}
});
}
public static void reset() {
public static void clearQueue() {
NSClientService.handler.post(new Runnable() {
@Override
public void run() {
log.debug("QUEUE Reset");
queue.clear();
log.debug("QUEUE ClearQueue");
MainApp.getDbHelper().deleteAllDbRequests();
log.debug(status());
}
});
@ -66,31 +59,18 @@ public class UploadQueue {
@Override
public void run() {
try {
long id = -1L;
String id;
if (record.has("NSCLIENT_ID")) {
id = record.getLong("NSCLIENT_ID");
id = record.getString("NSCLIENT_ID");
} else {
return;
}
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
JSONObject data = dbr.data;
long nsclientId = -1;
if (data.has("NSCLIENT_ID")) {
nsclientId = data.getLong("NSCLIENT_ID");
if (nsclientId == id) {
log.debug("Removing item from UploadQueue");
iter.remove();
log.debug(UploadQueue.status());
return;
}
}
if (MainApp.getDbHelper().deleteDbRequest(id) == 1) {
log.debug("Removed item from UploadQueue. " + UploadQueue.status());
}
} catch (JSONException e) {
e.printStackTrace();
}
saveMap();
}
});
}
@ -99,59 +79,29 @@ public class UploadQueue {
NSClientService.handler.post(new Runnable() {
@Override
public void run() {
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
if (dbr.action.equals(action) && dbr._id.equals(_id)) {
log.debug("Removing item from UploadQueue");
iter.remove();
return;
} else {
log.debug("Failed removing item from UploadQueue");
}
}
saveMap();
MainApp.getDbHelper().deleteDbRequestbyMongoId(action, _id);
}
});
}
final static String KEY = "UploadQueue";
private static void saveMap() {
JSONArray jsonArray = new JSONArray();
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
jsonArray.put(dbr.toJSON());
}
SP.putString(KEY, jsonArray.toString());
}
private void loadMap() {
queue = new HashMap<String, DbRequest>();
try {
String jsonString = SP.getString(KEY, (new JSONArray()).toString());
JSONArray jsonArray = new JSONArray(jsonString);
for (int i=0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
DbRequest dbr = DbRequest.fromJSON(jsonObject);
queue.put(dbr.hash(), dbr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String textList() {
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
String result = "";
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
result += "<br>";
result += dbr.action.toUpperCase() + " ";
result += dbr.collection + ": ";
result += dbr.data.toString();
CloseableIterator<DbRequest> iterator = null;
try {
iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();
try {
while (iterator.hasNext()) {
DbRequest dbr = iterator.next();
result += "<br>";
result += dbr.action.toUpperCase() + " ";
result += dbr.collection + ": ";
result += dbr.data;
}
} finally {
iterator.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}

View file

@ -17,7 +17,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
import info.nightscout.androidaps.db.DbRequest;
public class DBAccessReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(DBAccessReceiver.class);

View file

@ -13,6 +13,7 @@ import android.preference.PreferenceManager;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.j256.ormlite.dao.CloseableIterator;
import com.squareup.otto.Subscribe;
import org.json.JSONArray;
@ -22,6 +23,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
@ -47,7 +49,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastP
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastSgvs;
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastStatus;
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastTreatment;
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
import info.nightscout.androidaps.db.DbRequest;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSCal;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
@ -518,7 +520,7 @@ public class NSClientService extends Service {
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
message.put("data", new JSONObject(dbr.data));
mSocket.emit("dbUpdate", message, ack);
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE " + dbr.collection, "Sent " + dbr._id));
} catch (JSONException e) {
@ -532,7 +534,7 @@ public class NSClientService extends Service {
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
message.put("data", new JSONObject(dbr.data));
mSocket.emit("dbUpdateUnset", message, ack);
MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET " + dbr.collection, "Sent " + dbr._id));
} catch (JSONException e) {
@ -568,7 +570,7 @@ public class NSClientService extends Service {
if (!isConnected || !hasWriteAuth) return;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("data", dbr.data);
message.put("data", new JSONObject(dbr.data));
mSocket.emit("dbAdd", message, ack);
MainApp.bus().post(new EventNSClientNewLog("DBADD " + dbr.collection, "Sent " + dbr.nsClientID));
} catch (JSONException e) {
@ -632,7 +634,7 @@ public class NSClientService extends Service {
}
public void resend(final String reason) {
if (UploadQueue.queue.size() == 0)
if (UploadQueue.size() == 0)
return;
if (!isConnected || !hasWriteAuth) return;
@ -643,26 +645,35 @@ public class NSClientService extends Service {
@Override
public void run() {
Logger log = LoggerFactory.getLogger(UploadQueue.class);
Iterator<Map.Entry<String, DbRequest>> iter = UploadQueue.queue.entrySet().iterator();
if (mSocket == null || !mSocket.connected()) return;
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
if (dbr.action.equals("dbAdd")) {
NSAddAck addAck = new NSAddAck();
dbAdd(dbr, addAck);
} else if (dbr.action.equals("dbRemove")) {
NSUpdateAck removeAck = new NSUpdateAck(dbr.action, dbr._id);
dbRemove(dbr, removeAck);
} else if (dbr.action.equals("dbUpdate")) {
NSUpdateAck updateAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdate(dbr, updateAck);
} else if (dbr.action.equals("dbUpdateUnset")) {
NSUpdateAck updateUnsetAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdateUnset(dbr, updateUnsetAck);
CloseableIterator<DbRequest> iterator = null;
try {
iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();
try {
while (iterator.hasNext()) {
DbRequest dbr = iterator.next();
if (dbr.action.equals("dbAdd")) {
NSAddAck addAck = new NSAddAck();
dbAdd(dbr, addAck);
} else if (dbr.action.equals("dbRemove")) {
NSUpdateAck removeAck = new NSUpdateAck(dbr.action, dbr._id);
dbRemove(dbr, removeAck);
} else if (dbr.action.equals("dbUpdate")) {
NSUpdateAck updateAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdate(dbr, updateAck);
} else if (dbr.action.equals("dbUpdateUnset")) {
NSUpdateAck updateUnsetAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdateUnset(dbr, updateUnsetAck);
}
}
} finally {
iterator.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason));
}
});