public Sample(RrdDb parentDb, long time) { this.parentDb = parentDb; this.time = time; this.dsNames = parentDb.getDsNames(); clearValues(); }
public void StoreData(rrd4n.DataAccess.Data.Sample sample) { RrdDb rrdDb = null; try { rrdDb = new RrdDb(DataPath + sample.DatabasePath, false); Sample coreSample = new Sample(rrdDb.getPath(),rrdDb.getDsNames(), sample.Time); coreSample.setValues(sample.Values); rrdDb.store(coreSample); } finally { if (rrdDb != null) rrdDb.close(); } }
public DatabaseData ReloadDatabase(DatabaseData srcDatabase) { if (!databases.ContainsKey(srcDatabase.Definition.Path)) throw new ApplicationException("Database to reload don't exist"); try { RrdDb database = new RrdDb(srcDatabase.Definition.Path); DatabaseData data = new DatabaseData(); data.Saved = true; data.Definition = database.getRrdDef(); data.LastUpdated = database.getLastUpdateDateTime(); data.LastValue = database.getLastDatasourceValue(database.getDsNames()[0]); databases[srcDatabase.Definition.Path] = data; database.close(); return data; } catch (Exception ex) { Logger.Error("Fail to add database", ex); throw; } }
public DatabaseData AddDatabase(string databasePath) { if (databases.ContainsKey(databasePath)) return databases[databasePath]; try { RrdDb database = new RrdDb(databasePath); DatabaseData data = new DatabaseData(); data.Saved = true; data.Definition = database.getRrdDef(); data.LastUpdated = database.getLastUpdateDateTime(); data.LastValue = database.getLastDatasourceValue(database.getDsNames()[0]); databases.Add(databasePath, data); database.close(); return databases[databasePath]; } catch (Exception ex) { Logger.Error("Fail to add database", ex); throw; } }
public void ImportData(string dataPath, DatabaseData databaseData, TimeSpan expectedTick ) { if (model.ReadOnly) throw new ApplicationException("Can't import data. Database readonly"); List<string> columns = new List<string>(); List<FetchedData> unifiedData = ReadAndUnifyData(dataPath, out columns); string databasePath = databaseData.Definition.Path; RrdDb database = new RrdDb(databasePath, false); int[] dsIndexes = new int[columns.Count]; for (int i = 0; i < columns.Count; i++) { dsIndexes[i] = database.getDsIndex(columns[i]); } string[] dsNames = database.getDsNames(); rrd4n.DataAccess.Data.Sample sample = new rrd4n.DataAccess.Data.Sample(databasePath, dsNames, 0); foreach (var data in unifiedData) { sample.setDateTime(data.TimeStamp); for (int i = 0; i < data.Values.Count; i++ ) { sample.setValue(dsIndexes[i], data.Values[i]); } try { // When using file access abstraction //dbAccess.StoreData(sample); //Without abstraction database.store(sample); sample.clearValues(); } catch (ArgumentException) { } model.DatabaseDirty = true; } database.close(); OpenDatabase(databasePath); }
public FetchData fetchData(FetchRequest request) { long arcStep = getArcStep(); long fetchStart = Util.normalize(request.FetchStart, arcStep); long fetchEnd = Util.normalize(request.FetchEnd, arcStep); if (fetchEnd < request.FetchEnd) { fetchEnd += arcStep; } long startTime = getStartTime(); long endTime = getEndTime(); String[] dsToFetch = request.getFilter(); if (dsToFetch == null) { dsToFetch = parentDb.getDsNames(); } int dsCount = dsToFetch.Length; int ptsCount = (int)((fetchEnd - fetchStart) / arcStep + 1); long[] timestamps = new long[ptsCount]; double[][] values = new double[dsCount][]; for (int i = 0; i < dsCount; i++) { values[i] = new double[ptsCount]; } long matchStartTime = Math.Max(fetchStart, startTime); long matchEndTime = Math.Min(fetchEnd, endTime); double[][] robinValues = null; if (matchStartTime <= matchEndTime) { // preload robin values int matchCount = (int)((matchEndTime - matchStartTime) / arcStep + 1); int matchStartIndex = (int)((matchStartTime - startTime) / arcStep); robinValues = new double[dsCount][]; for (int i = 0; i < dsCount; i++) { int dsIndex = parentDb.getDsIndex(dsToFetch[i]); robinValues[i] = robins[dsIndex].getValues(matchStartIndex, matchCount); } } for (int ptIndex = 0; ptIndex < ptsCount; ptIndex++) { long time = fetchStart + ptIndex * arcStep; timestamps[ptIndex] = time; for (int i = 0; i < dsCount; i++) { double value = Double.NaN; if (time >= matchStartTime && time <= matchEndTime) { // inbound time int robinValueIndex = (int)((time - matchStartTime) / arcStep); Debug.Assert(robinValues != null); value = robinValues[i][robinValueIndex]; } values[i][ptIndex] = value; } } FetchData fetchData = new FetchData(steps.get(), endTime, parentDb.getDsNames()); fetchData.setTimestamps(timestamps); fetchData.setValues(values); return(fetchData); }