Search in sources :

Example 6 with Sample

use of edu.berkeley.cs.amplab.carat.thrift.Sample in project carat by amplab.

the class SampleReader method writeSample.

/**
     * For simplicity, this method relies on the fact that
     * only the piList of Sample has any List type elements.
     * This will fail to record those from substructs (NetworkDetails, BatteryDetails, CpuStatus),
     * and will need to be changed if those are added.
     * 
     * Does not record CallInfo, CellInfo, or CallMonth types.
     */
public static final HashMap<String, String> writeSample(Sample s) {
    HashMap<String, String> m = new HashMap<String, String>();
    for (_Fields sf : Sample.metaDataMap.keySet()) {
        FieldMetaData md = Sample.metaDataMap.get(sf);
        switch(md.valueMetaData.type) {
            case org.apache.thrift.protocol.TType.STRING:
                m.put(sf.getFieldName(), cleanStr(s.getFieldValue(sf).toString()));
                break;
            case org.apache.thrift.protocol.TType.I32:
            case org.apache.thrift.protocol.TType.DOUBLE:
                m.put(sf.getFieldName(), s.getFieldValue(sf).toString());
                break;
            case org.apache.thrift.protocol.TType.STRUCT:
                if (md.fieldName.equals(Sample._Fields.NETWORK_DETAILS.getFieldName()) && s.networkDetails != null) {
                    int len = NetworkDetails._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.networkDetails.getFieldValue(NetworkDetails._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.BATTERY_DETAILS.getFieldName()) && s.batteryDetails != null) {
                    int len = BatteryDetails._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.batteryDetails.getFieldValue(BatteryDetails._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.CPU_STATUS.getFieldName()) && s.cpuStatus != null) {
                    int len = CpuStatus._Fields.values().length;
                    StringBuilder b = new StringBuilder();
                    for (int i = 1; i <= len; i++) {
                        b.append(cleanStr("" + s.cpuStatus.getFieldValue(CpuStatus._Fields.findByThriftId(i))));
                        if (i < len)
                            b.append("\n");
                    }
                    m.put(sf.getFieldName(), b.toString());
                }
                /*
                  * else if (md.fieldName.equals("CallInfo")){ }
                  */
                break;
            case org.apache.thrift.protocol.TType.LIST:
                if (md.fieldName.equals(Sample._Fields.EXTRA.getFieldName()) && s.extra != null) {
                    StringBuilder b = new StringBuilder();
                    for (Feature f : s.extra) {
                        b.append(cleanStr(f.key) + ";" + cleanStr(f.value) + "\n");
                    }
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.LOCATION_PROVIDERS.getFieldName()) && s.locationProviders != null) {
                    StringBuilder b = new StringBuilder();
                    for (String lp : s.locationProviders) b.append(lp + "\n");
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                } else if (md.fieldName.equals(Sample._Fields.PI_LIST.getFieldName()) && s.piList != null) {
                    StringBuilder b = new StringBuilder();
                    for (ProcessInfo pi : s.piList) {
                        int len = ProcessInfo._Fields.values().length;
                        for (int i = 1; i <= len; i++) {
                            ProcessInfo._Fields pif = ProcessInfo._Fields.findByThriftId(i);
                            FieldMetaData pmd = ProcessInfo.metaDataMap.get(pif);
                            if (pmd.valueMetaData.type == org.apache.thrift.protocol.TType.LIST) {
                                if (pi.appSignatures != null) {
                                    for (int j = 0; j < pi.appSignatures.size(); j++) {
                                        String sig = pi.appSignatures.get(j);
                                        b.append(sig);
                                        if (j + 1 < len)
                                            b.append("#");
                                    }
                                }
                            } else {
                                b.append(cleanStr("" + pi.getFieldValue(pif)));
                            }
                            if (i < len)
                                b.append(";");
                        }
                        b.append("\n");
                    }
                    if (b.length() > 1)
                        b.deleteCharAt(b.lastIndexOf("\n"));
                    m.put(sf.getFieldName(), b.toString());
                }
                break;
            default:
        }
    }
    return m;
}
Also used : Sample._Fields(edu.berkeley.cs.amplab.carat.thrift.Sample._Fields) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) HashMap(java.util.HashMap) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature)

Example 7 with Sample

use of edu.berkeley.cs.amplab.carat.thrift.Sample in project carat by amplab.

the class SampleReader method readSample.

/**
     * Read a Sample from a HashMap stored in the Carat Sample db.
     * @param data
     * @return
     */
public static final Sample readSample(Object data) {
    Sample s = null;
    if (data != null && data instanceof HashMap<?, ?>) {
        HashMap<String, String> m = (HashMap<String, String>) data;
        s = new Sample();
        NetworkDetails n = new NetworkDetails();
        BatteryDetails bd = new BatteryDetails();
        // CellInfo ci = new CellInfo();
        // CallInfo calli = new CallInfo();
        // CallMonth cm = new CallMonth();
        CpuStatus cs = new CpuStatus();
        // Set single fields automatically:
        for (String k : m.keySet()) {
            _Fields sf = Sample._Fields.findByName(k);
            if (sf != null) {
                // Top level Sample field.
                FieldMetaData md = Sample.metaDataMap.get(sf);
                String cleaned = origStr(m.get(k));
                switch(md.valueMetaData.type) {
                    case org.apache.thrift.protocol.TType.STRING:
                        s.setFieldValue(sf, cleaned);
                        break;
                    case org.apache.thrift.protocol.TType.I32:
                        try {
                            s.setFieldValue(sf, Integer.parseInt(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.DOUBLE:
                        try {
                            s.setFieldValue(sf, Double.parseDouble(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.STRUCT:
                        if (md.fieldName.equals(Sample._Fields.NETWORK_DETAILS.getFieldName())) {
                            fillNetworkDetails(m.get(k), n);
                            s.setNetworkDetails(n);
                        } else if (md.fieldName.equals(Sample._Fields.BATTERY_DETAILS.getFieldName())) {
                            fillBatteryDetails(m.get(k), bd);
                            s.setBatteryDetails(bd);
                        } else if (md.fieldName.equals(Sample._Fields.CPU_STATUS.getFieldName())) {
                            fillCpuStatusDetails(m.get(k), cs);
                            s.setCpuStatus(cs);
                        }
                        /*
                          * else if (md.fieldName.equals("CallInfo")){ }
                          */
                        break;
                    case org.apache.thrift.protocol.TType.LIST:
                        if (md.fieldName.equals(Sample._Fields.EXTRA.getFieldName())) {
                            List<Feature> list = new LinkedList<Feature>();
                            String[] extras = m.get(k).split("\n");
                            for (String e : extras) {
                                Feature f = new Feature();
                                String[] feat = e.split(";");
                                if (feat.length > 1) {
                                    f.setKey(origStr(feat[0]));
                                    f.setValue(origStr(feat[1]));
                                }
                                list.add(f);
                            }
                            s.setExtra(list);
                        } else if (md.fieldName.equals(Sample._Fields.LOCATION_PROVIDERS.getFieldName())) {
                            List<String> list = new LinkedList<String>();
                            String[] arr = m.get(k).split("\n");
                            for (String lp : arr) list.add(lp);
                            s.setLocationProviders(list);
                        } else if (md.fieldName.equals(Sample._Fields.PI_LIST.getFieldName())) {
                            // Set piList fields automatically:
                            LinkedList<ProcessInfo> piList = new LinkedList<ProcessInfo>();
                            String[] processes = m.get(md.fieldName).split("\n");
                            for (String process : processes) {
                                String[] items = process.split(";");
                                ProcessInfo pi = new ProcessInfo();
                                /*
                                 * Items are in the same order as they appear in ProcessInfo
                                 * protocol class, so I can use Thrift ID for setting the fields
                                 * automatically.
                                 */
                                for (int i = 1; i <= items.length; i++) {
                                    if (items[i - 1] == null)
                                        continue;
                                    ProcessInfo._Fields pif = ProcessInfo._Fields.findByThriftId(i);
                                    FieldMetaData pmd = ProcessInfo.metaDataMap.get(pif);
                                    cleaned = origStr(items[i - 1]);
                                    switch(pmd.valueMetaData.type) {
                                        case org.apache.thrift.protocol.TType.STRING:
                                            pi.setFieldValue(pif, cleaned);
                                            break;
                                        case org.apache.thrift.protocol.TType.I32:
                                            try {
                                                pi.setFieldValue(pif, Integer.parseInt(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.DOUBLE:
                                            try {
                                                pi.setFieldValue(pif, Double.parseDouble(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.BOOL:
                                            try {
                                                pi.setFieldValue(pif, Boolean.parseBoolean(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a bool");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.LIST:
                                            List<String> list = new LinkedList<String>();
                                            String[] arr = cleaned.split("#");
                                            for (String sig : arr) list.add(sig);
                                            pi.setFieldValue(pif, list);
                                            break;
                                        default:
                                    }
                                }
                                piList.add(pi);
                            }
                            s.setPiList(piList);
                        }
                        break;
                    default:
                }
            }
        }
    }
    return s;
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) Sample._Fields(edu.berkeley.cs.amplab.carat.thrift.Sample._Fields) HashMap(java.util.HashMap) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) NetworkDetails(edu.berkeley.cs.amplab.carat.thrift.NetworkDetails) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature) LinkedList(java.util.LinkedList) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) BatteryDetails(edu.berkeley.cs.amplab.carat.thrift.BatteryDetails) List(java.util.List) LinkedList(java.util.LinkedList)

Example 8 with Sample

use of edu.berkeley.cs.amplab.carat.thrift.Sample in project carat by amplab.

the class CommunicationManager method uploadSamples.

public int uploadSamples(Collection<Sample> samples) {
    CaratService.Client instance = null;
    int succeeded = 0;
    ArrayList<Sample> samplesLeft = new ArrayList<Sample>();
    registerLocal();
    try {
        instance = ProtocolClient.open(a.getApplicationContext());
        registerOnFirstRun(instance);
        for (Sample s : samples) {
            boolean success = false;
            try {
                success = instance.uploadSample(s);
            } catch (Throwable th) {
                Log.e(TAG, "Error uploading sample.", th);
            }
            if (success)
                succeeded++;
            else
                samplesLeft.add(s);
        }
        safeClose(instance);
    } catch (Throwable th) {
        Log.e(TAG, "Error refreshing main reports.", th);
        safeClose(instance);
    }
    // Do not try again. It can cause a massive sample attack on the server.
    return succeeded;
}
Also used : CaratService(edu.berkeley.cs.amplab.carat.thrift.CaratService) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) ArrayList(java.util.ArrayList)

Example 9 with Sample

use of edu.berkeley.cs.amplab.carat.thrift.Sample in project carat by amplab.

the class CaratSampleDB method queryOldestSamples.

public SortedMap<Long, Sample> queryOldestSamples(int howmany) {
    SortedMap<Long, Sample> results = new TreeMap<Long, Sample>();
    try {
        synchronized (dbLock) {
            if (db == null || !db.isOpen()) {
                try {
                    db = helper.getWritableDatabase();
                } catch (android.database.sqlite.SQLiteException ex) {
                    Log.e(TAG, "Could not open database", ex);
                    return results;
                }
            }
            String[] columns = mColumnMap.keySet().toArray(new String[mColumnMap.size()]);
            Cursor cursor = query(null, null, columns, null, null, COLUMN_TIMESTAMP + " ASC LIMIT " + howmany);
            if (cursor == null) {
                // There are no results
                return results;
            } else {
                // Log.d("CaratSampleDB", "query is successfull!");
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    Sample s = fillSample(cursor);
                    if (s != null) {
                        results.put(cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)), s);
                        cursor.moveToNext();
                    }
                }
                cursor.close();
            }
        }
    } catch (Throwable th) {
        Log.e(TAG, "Failed to query oldest samples!", th);
    }
    return results;
}
Also used : Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) TreeMap(java.util.TreeMap) Cursor(android.database.Cursor)

Example 10 with Sample

use of edu.berkeley.cs.amplab.carat.thrift.Sample in project carat by amplab.

the class CaratSampleDB method fillSample.

/*
     * Read a sample from the current position of the cursor. TODO: Needs to be
     * updated when fields update.
     */
private Sample fillSample(Cursor cursor) {
    Sample s = null;
    byte[] sampleB = cursor.getBlob(cursor.getColumnIndex(CaratSampleDB.COLUMN_SAMPLE));
    if (sampleB != null) {
        ObjectInputStream oi;
        try {
            oi = new ObjectInputStream(new ByteArrayInputStream(sampleB));
            Object o = oi.readObject();
            if (o != null)
                s = SampleReader.readSample(o);
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    return s;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Sample (edu.berkeley.cs.amplab.carat.thrift.Sample)9 Feature (edu.berkeley.cs.amplab.carat.thrift.Feature)3 ProcessInfo (edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)3 HashMap (java.util.HashMap)3 SharedPreferences (android.content.SharedPreferences)2 Cursor (android.database.Cursor)2 CaratSampleDB (edu.berkeley.cs.amplab.carat.android.storage.CaratSampleDB)2 BatteryDetails (edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)2 CpuStatus (edu.berkeley.cs.amplab.carat.thrift.CpuStatus)2 NetworkDetails (edu.berkeley.cs.amplab.carat.thrift.NetworkDetails)2 Sample._Fields (edu.berkeley.cs.amplab.carat.thrift.Sample._Fields)2 FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)2 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)1 Context (android.content.Context)1 CaratService (edu.berkeley.cs.amplab.carat.thrift.CaratService)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 StreamCorruptedException (java.io.StreamCorruptedException)1 SimpleDateFormat (java.text.SimpleDateFormat)1