运维获取统计数据方式优化,避免数据库冲突

serial
Matthew 1 year ago
parent 7f209f15c0
commit 5ce977e846

@ -56,4 +56,28 @@ public class FileUtils {
return true;
}
public static byte[] int2Byte(int intValue) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
//System.out.print(Integer.toBinaryString(b[i])+" ");
//System.out.print((b[i] & 0xFF) + " ");
}
return b;
}
/**
* 4
* @param b
* @return
*/
public static int byte2Int(byte[] b) {
int intValue = 0;
for (int i = 0; i < b.length; i++) {
intValue += (b[i] & 0xFF) << (8 * (3 - i));
}
return intValue;
}
}

@ -27,6 +27,7 @@ android {
cmake {
abiFilters 'arm64-v8a'
cppFlags ''
arguments "-DTERM_CORE_ROOT=" + coreroot
}
}
}

@ -7,6 +7,7 @@
cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
add_definitions(-DTERMINAL_CLIENT)
project("mpmaster")
@ -14,6 +15,7 @@ project("mpmaster")
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(${TERM_CORE_ROOT})
add_library( # Sets the name of the library.
mpmaster

@ -21,10 +21,14 @@
#include <unistd.h>
#include <sys/ioctl.h>
#include <Client/Database.h>
#define IOT_PARAM_WRITE 0xAE
#define IOT_PARAM_READ 0xAF
#define MAX_STRING_LEN 32
#define PATH_MPAPP_STATS "/sdcard/com.xypower.mpapp/data/stats/"
typedef struct
{
int cmd;
@ -52,3 +56,30 @@ Java_com_xypower_mpmaster_MpMasterService_getInt(JNIEnv* env, jclass cls, jint c
return -1;
}
extern "C" JNIEXPORT jintArray JNICALL
Java_com_xypower_mpmaster_MpMasterService_getStats(JNIEnv* env, jclass cls, jlong ts) {
std::string path = PATH_MPAPP_STATS;
path += std::to_string(ts);
FILE* file = fopen(path.c_str(), "r");
if (file == NULL)
{
return NULL;
}
MP_STATS stats = { 0 };
size_t length = fread(&stats, 1, sizeof(stats), file);
fclose(file);
if (length != sizeof(stats))
{
return NULL;
}
size_t intLength = sizeof(stats) / sizeof(jint);
jintArray result = env->NewIntArray(intLength);
env->SetIntArrayRegion(result, 0, intLength, (const jint*)&stats);
return result;
}

@ -218,24 +218,7 @@ public class AppMaster {
}
postParams.add(new Pair<String, String>("freeROM", getFreeROM()));
DBHandler dbHandler = null;
try {
dbHandler = new DBHandler(mService);
dbHandler.readStats(startTime, endTime, postParams);
dbHandler.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (dbHandler != null) {
try {
dbHandler.close();
} catch (Exception ex) {
ex.printStackTrace();
}
dbHandler = null;
}
}
buildStats(startTime, postParams);
try {
MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService);
@ -278,6 +261,52 @@ public class AppMaster {
}
}
private void buildStats(long startTime, List<Pair<String, String>> stats) {
try {
/*
struct MP_STATS
{
int hb;
int hbAck;
int scheduledCount;
int takingTimes;
int succeededPhotos;
int failedPhotos;
int failedTimes;
int uploaded;
int devReboots;
int appReboots;
};
*/
int[] items = MpMasterService.getStats(startTime);
if (items == null || items.length != 10) {
return;
}
stats.add(new Pair<String, String>("numberOfHb", Integer.toString(items[0])));
stats.add(new Pair<String, String>("numberOfHbAck", Integer.toString(items[1])));
stats.add(new Pair<String, String>("recv", Integer.toString(items[2])));
stats.add(new Pair<String, String>("photoTimes", Integer.toString(items[3])));
stats.add(new Pair<String, String>("success", Integer.toString(items[4])));
stats.add(new Pair<String, String>("failure", Integer.toString(items[5])));
stats.add(new Pair<String, String>("failedTimes", Integer.toString(items[6])));
stats.add(new Pair<String, String>("uploads", Integer.toString(items[7])));
if (items[8] > 0) {
stats.add(new Pair<String, String>("rebootTimes", Integer.toString(items[8])));
}
if (items[9] > 0) {
stats.add(new Pair<String, String>("i1RebootTimes", Integer.toString(items[9])));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String getBatteryVoltage() {
int val = 0;
for (int idx = 0; idx < 3; idx++) {

@ -1,172 +0,0 @@
package com.xypower.mpmaster;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Pair;
import com.xypower.common.MicroPhotoContext;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class DBHandler extends SQLiteOpenHelper {
// creating a constant variables for our database.
// below variable is for our database name.
private static final String DB_NAME = "/sdcard/" + MicroPhotoContext.PACKAGE_NAME_MPAPP + "/data/App.db";
// below int is our database version
private static final int DB_VERSION = 1;
// below variable is for our table name.
private static final String TABLE_NAME_SCHEDULES = "Schedules";
// below variable is for our table name.
private static final String TABLE_NAME_HEARTBEATS = "Heartbeats";
private static final String TABLE_NAME_REBOOTS = "Reboots";
// creating a constructor for our database handler.
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
// below method is for creating a database by running a sqlite query
@Override
public void onCreate(SQLiteDatabase db) {
// on below line we are creating
// an sqlite query and we are
// setting our column names
// along with their data types.
}
// we have created a new method for reading all the courses.
public boolean readStats(long startTime, long endTime, List<Pair<String, String>> stats) {
// on below line we are creating a
// database for reading our database.
SQLiteDatabase db = this.getReadableDatabase();
// on below line we are creating a cursor with query to read data from database.
Cursor cursor = db.rawQuery("SELECT scheduleTime,channel,preset,scheduled,takingTime,result,retries,postTime FROM " + TABLE_NAME_SCHEDULES + " WHERE scheduleTime >=" + Long.toString(startTime) + " AND scheduleTime<=" + Long.toString(endTime), null);
int scheduledCount = 0;
int takingTimes = 0;
int retries = 0;
int succeededPhotos = 0;
int failedPhotos = 0;
int failedTimes = 0;
int scheduled = 0;
int uploaded = 0;
int photoResult = 0;
long photoTime = 0;
// moving our cursor to first position.
if (cursor.moveToFirst()) {
do {
scheduled = cursor.getInt(3);
photoTime = cursor.getLong(4);
if (scheduled != 0)
{
scheduledCount ++;
}
if (photoTime != 0)
{
takingTimes++;
photoResult = cursor.getInt(5);
if (photoResult != 0)
{
succeededPhotos++;
}
else
{
failedPhotos++;
}
retries = cursor.getInt(6);
if (retries > 0)
{
failedTimes += retries - ((photoResult == 0) ? 0 : 1);
}
if (cursor.getInt(7) != 0)
{
uploaded++;
}
}
} while (cursor.moveToNext());
// moving our cursor to next.
}
// at last closing our cursor
// and returning our array list.
cursor.close();
cursor = null;
cursor = db.rawQuery("SELECT COUNT(hb),SUM(CASE WHEN ack=0 THEN 0 ELSE 1 END) FROM " + TABLE_NAME_HEARTBEATS + " WHERE hb >=" + Long.toString(startTime) + " AND hb<=" + Long.toString(endTime), null);
// moving our cursor to first position.
int hb = 0;
int hbAck = 0;
if (cursor.moveToFirst()) {
hb = cursor.getInt(0);
hbAck = cursor.getInt(1);
}
// at last closing our cursor
// and returning our array list.
cursor.close();
cursor = null;
cursor = db.rawQuery("SELECT COUNT(*),appType FROM " + TABLE_NAME_REBOOTS + " WHERE rebootTime >=" + Long.toString(startTime) + " AND rebootTime<=" + Long.toString(endTime) + " GROUP BY appType", null);
// moving our cursor to first position.
int appReboots = 0;
int systemReboots = 0;
int appType = 0;
if (cursor.moveToFirst()) {
do {
appType = cursor.getInt(1);
if (appType == 0) {
appReboots = cursor.getInt(0);
} else if (appType == 1) {
systemReboots = cursor.getInt(0);
}
} while (cursor.moveToNext());
// moving our cursor to next.
}
// at last closing our cursor
// and returning our array list.
cursor.close();
cursor = null;
stats.add(new Pair<String, String>("numberOfHb", Integer.toString(hb)));
stats.add(new Pair<String, String>("numberOfHbAck", Integer.toString(hbAck)));
stats.add(new Pair<String, String>("recv", Integer.toString(scheduledCount)));
stats.add(new Pair<String, String>("photoTimes", Integer.toString(takingTimes)));
stats.add(new Pair<String, String>("success", Integer.toString(succeededPhotos)));
stats.add(new Pair<String, String>("failure", Integer.toString(succeededPhotos)));
stats.add(new Pair<String, String>("uploads", Integer.toString(uploaded)));
if (systemReboots > 0) {
stats.add(new Pair<String, String>("rebootTimes", Integer.toString(systemReboots)));
}
if (appReboots > 0) {
stats.add(new Pair<String, String>("i1RebootTimes", Integer.toString(appReboots)));
}
return true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// this method is called to check if the table exists already.
}
}

@ -736,6 +736,7 @@ public class MpMasterService extends Service {
public native static int getInt(int cmd);
public native static int[] getStats(long ts);
////////////////////////GPS////////////////////

Loading…
Cancel
Save