use of org.json.JSONObject in project clutchandroid by clutchio.
the class ClutchStats method getABLogs.
public ArrayList<ABRow> getABLogs() {
SQLiteDatabase db = getReadableDatabase();
String[] args = {};
ArrayList<ABRow> res = new ArrayList<ABRow>();
Cursor cur = db.rawQuery("SELECT uuid, ts, data FROM ablog ORDER BY ts", args);
cur.moveToFirst();
while (!cur.isAfterLast()) {
String uuid = cur.getString(0);
double ts = cur.getDouble(1);
JSONObject data;
try {
data = new JSONObject(cur.getString(2));
} catch (JSONException e) {
Log.w(TAG, "Could not serialize to JSON: " + cur.getString(3));
cur.moveToNext();
continue;
}
res.add(new ABRow(uuid, ts, data));
cur.moveToNext();
}
db.close();
return res;
}
use of org.json.JSONObject in project clutchandroid by clutchio.
the class ClutchStats method setNumChoices.
public void setNumChoices(String name, int numChoices, boolean hasData) {
JSONObject data = new JSONObject();
try {
data.put("action", "num-choices");
data.put("name", name);
data.put("num_choices", numChoices);
data.put("has_data", hasData);
} catch (JSONException e) {
Log.e(TAG, "Could not create JSON for insert into testChosen (name: " + name + ", numChoices: " + numChoices + ")");
return;
}
logABData(data);
}
use of org.json.JSONObject in project clutchandroid by clutchio.
the class ClutchSync method background.
public static void background(final ClutchStats clutchStats) {
ArrayList<StatRow> logs = clutchStats.getLogs();
if (logs.size() == 0) {
return;
}
final StatRow lastRow = logs.get(logs.size() - 1);
JSONArray jsonLogs = new JSONArray();
for (StatRow row : logs) {
JSONObject rowObj = new JSONObject();
try {
rowObj.put("uuid", row.uuid);
rowObj.put("ts", row.ts);
rowObj.put("action", row.action);
rowObj.put("data", row.data);
} catch (JSONException e1) {
// TODO: Don't discard the row.
Log.e(TAG, "Could not properly encode the logs into JSON for upload to Clutch. Discarding the row.");
continue;
}
jsonLogs.put(rowObj);
}
HashMap<String, JSONArray> params = new HashMap<String, JSONArray>();
params.put("logs", jsonLogs);
ClutchAPIClient.callMethod("stats", params, new ClutchAPIResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
if ("ok".equals(response.optString("status"))) {
clutchStats.deleteLogs(lastRow.ts);
} else {
Log.e(TAG, "Failed to send the Clutch stats logs to the server.");
}
}
@Override
public void onFailure(Throwable e, JSONObject errorResponse) {
Log.e(TAG, "Failed to send logs to Clutch: " + errorResponse);
}
});
}
use of org.json.JSONObject in project clutchandroid by clutchio.
the class ClutchAB method sendABLogs.
private static void sendABLogs() {
ArrayList<ABRow> logs = stats.getABLogs();
if (logs.size() == 0) {
return;
}
final ABRow lastRow = logs.get(logs.size() - 1);
JSONArray jsonLogs = new JSONArray();
for (ABRow row : logs) {
JSONObject rowObj = new JSONObject();
try {
rowObj.put("uuid", row.uuid);
rowObj.put("ts", row.ts);
rowObj.put("data", row.data);
} catch (JSONException e1) {
// TODO: Don't discard the row.
Log.e(TAG, "Could not properly encode the AB logs into JSON for upload to Clutch. Discarding the row.");
continue;
}
jsonLogs.put(rowObj);
}
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("logs", jsonLogs);
params.put("guid", ClutchAPIClient.getFakeGUID());
ClutchAPIClient.callMethod("send_ab_logs", params, new ClutchAPIResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
if ("ok".equals(response.optString("status"))) {
stats.deleteABLogs(lastRow.ts);
} else {
Log.e(TAG, "Failed to send the Clutch AB logs to the server.");
}
}
@Override
public void onFailure(Throwable e, JSONObject errorResponse) {
Log.e(TAG, "Failed to send AB logs to Clutch: " + errorResponse);
}
});
}
use of org.json.JSONObject in project clutchandroid by clutchio.
the class ClutchAB method test.
public static void test(String name, ClutchABDataTest testInstance) {
JSONObject metaMeta = getLatestMeta();
if (metaMeta.length() == 0) {
stats.testFailure(name, "no-meta");
stats.setNumChoices(name, 0, true);
return;
}
JSONObject meta = metaMeta.optJSONObject(name);
if (meta == null) {
stats.testFailure(name, "no-meta-name");
stats.setNumChoices(name, 0, true);
return;
}
JSONArray weights = meta.optJSONArray("weights");
JSONArray allData = meta.optJSONArray("data");
if (allData == null || weights == null) {
stats.testFailure(name, "no-data");
return;
}
if (weights.length() == 0) {
return;
}
int choice = cachedChoice(name, doubleJSONArrayToList(weights));
stats.testChosen(name, choice, weights.length());
if (choice == -1) {
return;
}
JSONObject data = allData.optJSONObject(choice);
testInstance.action(data);
}
Aggregations