public void UpdateDataSource(DatabaseData srcDatabase, DsDef updatedDsDef, DsDef originalDsDef) { RrdDb database = null; try { database = new RrdDb(srcDatabase.Definition.Path, false); Datasource datasource = database.getDatasource(originalDsDef.getDsName()); if (datasource == null) { throw new ArgumentException(updatedDsDef.getDsName() + " datasource don't exist"); } if (datasource.DsName != updatedDsDef.DsName) { datasource.setDsName(updatedDsDef.getDsName()); } datasource.setDsType(updatedDsDef.getDsType()); datasource.setHeartbeat(updatedDsDef.getHeartbeat()); datasource.setMaxValue(updatedDsDef.getMaxValue(), true); datasource.setMinValue(updatedDsDef.getMinValue(), true); } catch (FileNotFoundException ex) { Logger.Error("Update datasource failed", ex); throw new ApplicationException("Can't update datasource until database saved!", ex); } finally { if (database != null) { database.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 SetDatabaseAsEdit(DatabaseData srcDatabaseData) { if (!databases.ContainsKey(srcDatabaseData.Definition.Path)) { throw new ApplicationException("Database not open in model"); } // Make a clone of the source database definition and give it a new name RrdDb rrdDb = new RrdDb(srcDatabaseData.Definition.Path, true); databaseDefinition = rrdDb.getRrdDef(); rrdDb.close(); DatabaseData dstDatabaseData = new DatabaseData(); dstDatabaseData.SourceDatabasePath = srcDatabaseData.Definition.Path; dstDatabaseData.Saved = false; dstDatabaseData.Definition = databaseDefinition; dstDatabaseData.Definition.Path = Path.GetFileNameWithoutExtension(databaseDefinition.Path) + "_"; dstDatabaseData.LastUpdated = dstDatabaseData.Definition.getStartDateTime(); dstDatabaseData.LastValue = double.NaN; DatabaseDirty = true; EditingDatabaseData = dstDatabaseData; databases.Add(dstDatabaseData.Definition.Path, dstDatabaseData); return(dstDatabaseData); }
public RrdDb GetDb() { const int heartBeat = 5 * 60; // * 1000 string _dbPath = Settings.Default.GetAppDataPath() + "xgsnapshots.db"; try { var db = new RrdDb(_dbPath); var sourcesToAdd = new HashSet <int>(); for (int a = 0; a <= 29; a++) { if (!db.containsDs(a + "")) { sourcesToAdd.Add(a); } } if (sourcesToAdd.Count > 0) { db.close(); foreach (int a in sourcesToAdd) { var dsDef = new DsDef(a + "", DsTypes.DT_GAUGE, heartBeat * 2, 0, Double.MaxValue); RrdToolkit.addDatasource(_dbPath, _dbPath + ".new", dsDef); FileSystem.DeleteFile(_dbPath); FileSystem.MoveFile(_dbPath + ".new", _dbPath); } db = new RrdDb(_dbPath); } return(db); } catch (FileNotFoundException) { var rrdDef = new RrdDef(_dbPath, heartBeat); for (int a = 0; a <= 29; a++) { rrdDef.addDatasource(a + "", DsTypes.DT_GAUGE, heartBeat * 2, 0, Double.MaxValue); } rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 1, 12 * 24); // one day > 1 step = 5 minutes, 12 times per hour * 24 hours rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 12, 24 * 7); // one week > 12 steps = 1 hour, 24 times per day * 7 days rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 4 * 12, 6 * 31); // one month > 4 * 12 steps = 4 hours, 6 times per day * 31 days rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 2 * 24 * 12, 183); // one year > 2 * 24 * 12 steps = 2 days, 183 days try { new RrdDb(rrdDef).close(); } catch (NullReferenceException) {} return(new RrdDb(_dbPath)); } }
public RemoteFetchData FetchData(string databaseName, long startTimeTick, long endTimeTick, string consolFunctionName, long resolution) { RrdDb database = null; try { log.DebugFormat("Read data from {0}", databaseName); var nameValueCollection = (NameValueCollection)ConfigurationManager.GetSection("rrdbfileserver"); string dataBasePath = nameValueCollection["databasepath"]; log.DebugFormat("Database path:{0}", dataBasePath + databaseName); database = new RrdDb(dataBasePath + databaseName, true); FetchRequest fetchRequest = new FetchRequest(null, consolFunctionName, startTimeTick, endTimeTick, resolution); FetchData data = database.fetchData(fetchRequest); database.close(); RemoteFetchData remoteData = new RemoteFetchData(); remoteData.Timestamps = data.getTimestamps(); remoteData.Values = data.getValues(); remoteData.ArchiveEndTimeTicks = data.getArcEndTime(); remoteData.ArchiveSteps = data.getArcStep(); remoteData.DatasourceNames = data.getDsNames(); if (debugEnabled) { log.DebugFormat("Read data from {0} to {1}.", rrd4n.Common.Util.getDate(startTimeTick), rrd4n.Common.Util.getDate(endTimeTick)); } return(remoteData); } catch (Exception ex) { log.Error("Fetchdata exception", ex); throw; } finally { if (database != null) { database.close(); } } }
public RrdDb GetDb() { const int heartBeat = 5 * 60; // * 1000 string _dbPath = Settings.Default.GetAppDataPath() + "xgsnapshots.db"; try { var db = new RrdDb(_dbPath); var sourcesToAdd = new HashSet<int>(); for (int a = 0; a <= 29; a++) { if (!db.containsDs(a + "")) { sourcesToAdd.Add(a); } } if (sourcesToAdd.Count > 0) { db.close(); foreach (int a in sourcesToAdd) { var dsDef = new DsDef(a + "", DsTypes.DT_GAUGE, heartBeat * 2, 0, Double.MaxValue); RrdToolkit.addDatasource(_dbPath, _dbPath + ".new", dsDef); FileSystem.DeleteFile(_dbPath); FileSystem.MoveFile(_dbPath + ".new", _dbPath); } db = new RrdDb(_dbPath); } return db; } catch (FileNotFoundException) { var rrdDef = new RrdDef(_dbPath, heartBeat); for (int a = 0; a <= 29; a++) { rrdDef.addDatasource(a + "", DsTypes.DT_GAUGE, heartBeat * 2, 0, Double.MaxValue); } rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 1, 12 * 24); // one day > 1 step = 5 minutes, 12 times per hour * 24 hours rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 12, 24 * 7); // one week > 12 steps = 1 hour, 24 times per day * 7 days rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 4 * 12, 6 * 31); // one month > 4 * 12 steps = 4 hours, 6 times per day * 31 days rrdDef.addArchive(ConsolFuns.CF_AVERAGE, 0.5, 2 * 24 * 12, 183); // one year > 2 * 24 * 12 steps = 2 days, 183 days try { new RrdDb(rrdDef).close(); } catch (NullReferenceException) {} return new RrdDb(_dbPath); } }
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 void addDatasource(String sourcePath, String destPath, DsDef newDatasource) { if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); rrdDef.addDatasource(newDatasource); RrdDb rrdDest = new RrdDb(rrdDef); rrdSource.copyStateTo(rrdDest); rrdSource.close(); rrdDest.close(); }
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 void CreateDatabase(string databasePath) { if (model.EditingDatabaseData == null) { throw new ApplicationException("Not in edit mode"); } string oldpath = model.EditingDatabaseData.Definition.Path; RrdDb srcDatabase = null; if (!string.IsNullOrEmpty(model.EditingDatabaseData.SourceDatabasePath)) { srcDatabase = new RrdDb(model.EditingDatabaseData.SourceDatabasePath); } RrdDef rrdDef = model.EditingDatabaseData.Definition; rrdDef.setPath(databasePath); RrdDb dstDatabase = new RrdDb(rrdDef); if (srcDatabase != null) { srcDatabase.copyStateTo(dstDatabase); } if (srcDatabase != null) { srcDatabase.close(); } dstDatabase.close(); model.DatabaseDirty = false; model.EditingDatabaseData = null; DatabaseData databaseData = model.AddDatabase(databasePath); rrdDbForm.SetDatabaseDefinition(databaseData); TreeForm.RemoveDatabaseDefinition(oldpath); TreeForm.SetDatabaseDefinition(databaseData); TreeForm.SetEditMode(false); }
public void StoreData(string databaseName, long timeStamp, double[] newValues) { RrdDb database = null; try { database = new RrdDb(databaseName, false); database.store(timeStamp, newValues); } catch (Exception ex) { log.Error("Store data exception", ex); throw; } finally { if (database != null) { database.close(); } } }
public rrd4n.DataAccess.Data.FetchData GetData(rrd4n.DataAccess.Data.FetchRequest request) { RrdDb rrdDb = null; try { string dataPath; if (DataPath.Contains("${APPDATA}")) { dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); dataPath += DataPath.Substring(10); } else { dataPath = DataPath; } rrdDb = new RrdDb(dataPath + request.DatabasePath); FetchRequest coreRequest = new FetchRequest(rrdDb.getPath(), request.ConsolidateFunctionName, request.FetchStart, request.FetchEnd, request.Resolution); FetchData coreFetchData = rrdDb.fetchData(coreRequest); return(new rrd4n.DataAccess.Data.FetchData(coreFetchData.getArcStep(), coreFetchData.getArcEndTime(), coreFetchData.getDsNames()) { Values = coreFetchData.getValues(), Timestamps = coreFetchData.getTimestamps() }); } finally { if (rrdDb != null) { rrdDb.close(); } } }
public FetchData GetArchiveData(DatabaseData databaseData, string dataSourceName, ArcDef archiveDefinition) { RrdDb database = null; try { database = new RrdDb(databaseData.Definition.getPath(), true); int datasourceIndex = database.getDsIndex(dataSourceName); Archive archive = database.getArchive(new ConsolFun(archiveDefinition.getConsolFun().Name), archiveDefinition.Steps); Robin robin = archive.getRobin(datasourceIndex); double[] values = robin.getValues(); DateTime archiveStartTime = archive.getStartDateTime(); TimeSpan tick = new TimeSpan(archive.getArcStep() * TimeSpan.TicksPerSecond); FetchData fetchedData = new FetchData(archive.getArcStep(), archive.getEndTime(), new string[] { dataSourceName }); long[] timestamps = new long[archive.getRows()]; long offset = archive.getStartTime(); for (var i = 0; i < archive.getRows(); i++) { timestamps[i] = offset; offset += archive.getArcStep(); } fetchedData.setTimestamps(timestamps); double[][] value = new double[1][]; value[0] = values; fetchedData.setValues(value); return(fetchedData); } finally { if (database != null) { database.close(); } } }
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 setArcXff(String sourcePath, String consolFun, int steps, double newXff) { RrdDb rrd = new RrdDb(sourcePath); Archive arc = rrd.getArchive(consolFun, steps); arc.setXff(newXff); rrd.close(); }
public void setDsHeartbeat(String sourcePath, String datasourceName, long newHeartbeat) { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); ds.setHeartbeat(newHeartbeat); rrd.close(); }
public void setDsMinValue(String sourcePath, String datasourceName, double newMinValue, boolean filterArchivedValues) { RrdDb rrd = new RrdDb(sourcePath); Datasource ds = rrd.getDatasource(datasourceName); ds.setMinValue(newMinValue, filterArchivedValues); rrd.close(); }
public void resizeArchive(String sourcePath, String destPath, String consolFun, int numSteps, int newRows) { if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } if (newRows < 2) { throw new RrdException("New arcihve size must be at least 2"); } RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); ArcDef arcDef = rrdDef.findArchive(consolFun, numSteps); if (arcDef.getRows() != newRows) { arcDef.setRows(newRows); rrdDef.setPath(destPath); RrdDb rrdDest = new RrdDb(rrdDef); rrdSource.copyStateTo(rrdDest); rrdDest.close(); } rrdSource.close(); }
public void removeArchive(String sourcePath, String destPath, String consolFun, int steps) { if (Util.sameFilePath(sourcePath, destPath)) { throw new RrdException("Source and destination paths are the same"); } RrdDb rrdSource = new RrdDb(sourcePath); RrdDef rrdDef = rrdSource.getRrdDef(); rrdDef.setPath(destPath); rrdDef.removeArchive(consolFun, steps); RrdDb rrdDest = new RrdDb(rrdDef); rrdSource.copyStateTo(rrdDest); rrdSource.close(); rrdDest.close(); }