Example #1
0
        /// <summary>
        /// This method gets the configuration from the AF Database
        /// </summary>
        private bool GetConfiguration()
        {
            try
            {
                Logger.InfoFormat("Starting configuration");
                AFDatabase database;

                _afConnectionHelper = AFConnectionHelper.ConnectAndGetDatabase(AfServerName, AfDatabaseName, out database);

                if (!database.ElementTemplates.Contains(AfElementTemplateName))
                {
                    throw new AFElementTemplateDoNotExistException();
                }

                AFSDKHelpers.LoadElementsByTemplate(database, database.ElementTemplates[AfElementTemplateName], _afElementsQueue);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(false);
            }

            return(true);
        }
        /// <summary>
        ///     Service Main Entry Point
        /// </summary>
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;

            if (Environment.UserInteractive)
            {
                _logger.Info("Starting service interractively");

                var options = new CommandLineOptions();

                if (Parser.Default.ParseArguments(args, options))
                {
                    if (options.Run)
                    {
                        if (!Config.IsLoaded())
                        {
                            Environment.Exit(-1);
                        }

                        WebHost.Instance.Start();
                        Core.Program.RunScheduler();

                        Console.WriteLine("press a key to stop the data collection");
                        Console.ReadKey();


                        Core.Program.StopScheduler();

                        WebHost.Instance.Dispose();
                        Console.WriteLine("Stopped");
                    }

                    if (options.Test)
                    {
                        var dataWriter = new DataWriter();

                        // Here are added the configured data collectors
                        foreach (DataCollectorSettings collectorSettings in Config.Settings.DataCollectorsSettings.Where(c => c.LoadPlugin == 1))
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(collectorSettings.PluginFileName))
                                {
                                    var      applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? "";
                                    Assembly pluginAssembly       = Assembly.LoadFrom(Path.Combine(applicationDirectory, "plugins", collectorSettings.PluginFileName));
                                    foreach (var type in pluginAssembly.GetTypes())
                                    {
                                        if (type.GetInterface(typeof(IDataCollector).Name) != null)
                                        {
                                            var newCollector         = Activator.CreateInstance(type) as IDataCollector;
                                            var isValidDataCollector = true;


                                            // performs an additional test, when a plugin class name is provided
                                            if (collectorSettings.PluginClassName != null && newCollector != null)
                                            {
                                                isValidDataCollector = collectorSettings.PluginClassName == newCollector.GetType().Name;
                                            }

                                            if (newCollector != null && isValidDataCollector)

                                            {
                                                _logger.InfoFormat("Loading task of type: {0}. Task description: {1}", type.Name, collectorSettings.ReaderTaskDescription);
                                                newCollector.SetSettings(collectorSettings);
                                                newCollector.Inititialize();
                                                newCollector.CollectData();
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(ex);
                            }

                            dataWriter.FlushData();
                        }

                        Console.WriteLine("Test completed, press a key to exit");

                        Console.ReadKey();
                    }

                    if (options.Install)
                    {
                        ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                    }

                    if (options.Uninstall)
                    {
                        ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                    }


                    // this code should be taken elsewhere... leaving it here for now.  unrelevant to this app

                    if (options.Stats)
                    {
                        AFDatabase afdb = null;
                        var        conn = AFConnectionHelper.ConnectAndGetDatabase("Optimus", "GitHub", out afdb);
                        Console.WriteLine("repoName,views avg, views min, views max, views std dev, clones avg, clones min, clones max, clones std dev");

                        foreach (var element in afdb.Elements["OSIsoft"].Elements)
                        {
                            var viewsCount  = element.Elements["Traffic"].Attributes["views-count"];
                            var clonesCount = element.Elements["Traffic"].Attributes["clones-count"];

                            var timeRange = new AFTimeRange(AFTime.Parse("T-8d"), AFTime.Parse("T"));

                            var views = viewsCount.Data.Summary(timeRange, AFSummaryTypes.All,
                                                                AFCalculationBasis.EventWeighted, AFTimestampCalculation.Auto);

                            var clones = clonesCount.Data.Summary(timeRange, AFSummaryTypes.All,
                                                                  AFCalculationBasis.EventWeighted, AFTimestampCalculation.Auto);


                            Console.Write(element.Name + ",");
                            Console.Write(val(views[AFSummaryTypes.Average]) + ",");
                            Console.Write(val(views[AFSummaryTypes.Minimum]) + ",");
                            Console.Write(val(views[AFSummaryTypes.Maximum]) + ",");
                            Console.Write(val(views[AFSummaryTypes.StdDev]) + ",");

                            Console.Write(val(clones[AFSummaryTypes.Average]) + ",");
                            Console.Write(val(clones[AFSummaryTypes.Minimum]) + ",");
                            Console.Write(val(clones[AFSummaryTypes.Maximum]) + ",");
                            Console.Write(val(clones[AFSummaryTypes.StdDev]));
                            Console.Write(Environment.NewLine);
                        }
                        Console.ReadKey();
                    }



                    // exit ok
                    Environment.Exit(0);
                }
            }
            else
            {
                ServiceBase[] ServicesToRun =
                {
                    new Service()
                };
                ServiceBase.Run(ServicesToRun);
            }
        }