public static StatsModel readRecord(SqlDataReader dr) { StatsModel currentRecord = new StatsModel(); currentRecord.id = dr["id"].ToString(); //reader.GetDateTime(reader.GetOrdinal("Timestamp")); currentRecord.logtime = dr.GetDateTime(dr.GetOrdinal("logtime")); currentRecord.is_interacting = Convert.ToBoolean(dr["is_interacting"]); currentRecord.pcy = Convert.ToInt32(dr["pcy"]); currentRecord.cpu = Convert.ToInt32(dr["cpu"]); currentRecord.vss = Convert.ToInt32(dr["vss"]); currentRecord.rss = Convert.ToInt32(dr["rss"]); currentRecord.threads = Convert.ToInt32(dr["threads"]); currentRecord.priority = Convert.ToInt32(dr["priority"]); currentRecord.status = dr["status"].ToString(); currentRecord.bg_up_data = Convert.ToInt32(dr["bg_up_data"]) == -1 ? 0 : Convert.ToInt32(dr["bg_up_data"]); ; currentRecord.bg_down_data = Convert.ToInt32(dr["bg_down_data"]) == -1 ? 0 : Convert.ToInt32(dr["bg_down_data"]); ; currentRecord.fg_up_data = Convert.ToInt32(dr["fg_up_data"]) == -1 ? 0 : Convert.ToInt32(dr["fg_up_data"]); currentRecord.fg_down_data = Convert.ToInt32(dr["fg_down_data"]) == -1 ? 0 : Convert.ToInt32(dr["fg_down_data"]); ; currentRecord.bg_up_wifi = Convert.ToInt32(dr["bg_up_wifi"]) == -1 ? 0 : Convert.ToInt32(dr["bg_up_wifi"]); ; currentRecord.bg_down_wifi = Convert.ToInt32(dr["bg_down_wifi"]) == -1 ? 0 : Convert.ToInt32(dr["bg_down_wifi"]); ; currentRecord.fg_up_wifi = Convert.ToInt32(dr["fg_up_wifi"]) == -1 ? 0 : Convert.ToInt32(dr["fg_up_wifi"]); ; currentRecord.fg_down_wifi = Convert.ToInt32(dr["fg_down_wifi"]) == -1 ? 0 : Convert.ToInt32(dr["fg_down_wifi"]); ; //Console.WriteLine("Date Read: " + dt); return currentRecord; }
public static int getTimeDiff(StatsModel currentStats, StatsModel nextStats) { DateTime dtCurrent =currentStats.logtime; DateTime dtNext = nextStats.logtime; TimeSpan diff = dtNext.Subtract(dtCurrent); return 0; //nextStats.logtime.d }
public bool isSame(StatsModel stats) { bool result = false; //Console.WriteLine("Comparing two records: " + id + " and " + stats.id); result = is_interacting == stats.is_interacting && pcy == stats.pcy && cpu == stats.cpu && vss == stats.vss && rss == stats.rss && threads == stats.threads && priority == stats.priority && status.Equals(stats.status) && bg_up_data == stats.bg_up_data && bg_down_data == stats.bg_down_data && fg_up_data == stats.fg_up_data && fg_down_data == stats.fg_down_data && bg_up_wifi == stats.bg_up_wifi && bg_down_wifi == stats.bg_down_wifi && fg_up_wifi == stats.fg_up_wifi && fg_down_wifi == stats.fg_down_wifi ? true : false; return result; }
private int optimize_app(bool isMainProcess, string applicationId) { string query = "select * from stats where application_id=@application_id and is_main_process=@is_main_process order by logtime"; con.Open(); SqlCommand command = new SqlCommand(query, con); command.CommandTimeout = 300; command.Parameters.AddWithValue("application_id", applicationId);//8444 command.Parameters.AddWithValue("is_main_process", isMainProcess); List<string> duplicateList = new List<string>(); List<StatsModel> updateList = new List<StatsModel>(); try { SqlDataReader dr = command.ExecuteReader(); StatsModel baseRecord = new StatsModel(); StatsModel currentRecord = new StatsModel(); if (dr.Read()) baseRecord = StatsModel.readRecord(dr); //read the first record as the base record and compare it with next records. else { con.Close(); return 0; } currentRecord = baseRecord; while (dr.Read()) { StatsModel nextRecord = StatsModel.readRecord(dr); //compare base record with current record. int diff = StatsModel.getTimeDiff(currentRecord, nextRecord); currentRecord = nextRecord; if (baseRecord.isSame(nextRecord) && diff <= 310) //next record is duplicate here... 310 is maximum log time difference is allowed. { duplicateList.Add(nextRecord.id); baseRecord.duplicate++; baseRecord.last_logtime = nextRecord.logtime; updateList = updateUpdateList(updateList, baseRecord); } else { baseRecord = nextRecord; } } con.Close(); } catch (Exception ex) { con.Close(); Logger.writeLog(ex.ToString()); } int updated = updateRecords(updateList); int deleted = deleteRecord(duplicateList); if (duplicateList.Count > 0) Logger.writeLog("APP_ID:\t" + applicationId + "Optimized. MainProcess:"+isMainProcess+ "\tDuplicate Removed: " + deleted + ".\tUpdated: " + updated); return deleted; }
private List<StatsModel> updateUpdateList(List<StatsModel> list, StatsModel baseStats) { for (int i = 0; i < list.Count; i++) { if (list[i].id == baseStats.id) list.RemoveAt(i); } list.Add(baseStats); return list; }