Esempio n. 1
0
      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();
         }
      }
Esempio n. 2
0
      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();
         }
      }
Esempio n. 3
0
 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();
    }
 }
Esempio n. 4
0
 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();
    }
 }
Esempio n. 5
0
      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;
      }
Esempio n. 6
0
      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();
         }
      }
Esempio n. 7
0
      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;
         }
      }
Esempio n. 8
0
 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;
    }
 }
Esempio n. 9
0
      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();
         }
      }
Esempio n. 10
0
      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);
      }
Esempio n. 11
0
      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);
      }