Exemple #1
0
        public void Update()
        {
            Console.WriteLine("Test updating element settings.");

            string    name = "Updated element", desc = "Updated description";
            AFElement element = GenerateElement();

            Console.WriteLine("Test element creation.");
            Assert.IsTrue(_db.CreateElement(element), "Assert creation passed");

            AFElement elem = AFElement.Find(_conn, name);

            Assert.Equals(elem.Name, name);
            Random ran = new Random();

            elem.Name        = name;
            elem.Description = desc;
            Assert.IsTrue(elem.IsDirty);
            elem.CheckIn();

            elem = AFElement.Find(_conn, name);
            Assert.Equals(elem.Name, name);
            Assert.Equals(elem.Description, desc);

            elem.Delete();
            elem.CheckIn();
        }
Exemple #2
0
        public void CreateAttribute()
        {
            AFElement element = new AFElement();

            string name = "Test Element 1";

            element.Name = name;
            Assert.Equals(element.Name, name);

            string desc = "Lazy PI Unit Test Element";

            element.Description = desc;
            Assert.Equals(element.Description, desc);

            Console.WriteLine("Test element creation.");

            Assert.IsTrue(_db.CreateElement(element), "Assert creation passed");

            element = _db.Elements[element.Name];

            //Check that the the element can be found through the AFDB
            Assert.IsNotNull(element, "Check AFDB element collection for new element.");

            AFAttribute attr = new AFAttribute();

            attr.Name        = "Test Attribute";
            attr.Description = "Created by WebAPI tests";

            element.Attributes.Add(attr);
            element.CheckIn();

            Assert.Equals(element.Attributes.Count, 1);
            attr = element.Attributes[attr.Name];

            Assert.IsNotNull(attr);
            Assert.IsNotNull(attr.ID);
            Assert.IsNotNull(attr.Name);
            Assert.IsNotNull(attr.Description);
            Assert.IsNotNull(attr.Path);

            string val = "Test string";

            // Test set and get of AFValue object
            attr.SetValue(new AFValue(val));
            AFValue valObj = attr.GetValue();

            Assert.Equals(valObj.Value, val);

            element.Delete();
            Assert.IsTrue(element.IsDeleted);

            element.CheckIn();
            Assert.IsNull(AFElement.Find(_conn, element.WebID));
        }
Exemple #3
0
        private void CreateAttributewithoutTagButton_Click(object sender, EventArgs e)
        {
            String AttributeName = Attribute_TextBox.Text.Trim();

            if (AttributeName == "")
            {
                MessageBox.Show("No Attribute Name defined");
            }
            else
            {
                if (afTreeView1.AFSelectedPath == afTreeView1.AFRootPath)
                {
                    MessageBox.Show("Exception:", "You did not select an element");
                }
                else
                {
                    try
                    {
                        //Don't use Selected tag
                        myElement = myAFDatabase.Elements[afTreeView1.AFSelectedPath];
                        myElement.Attributes.Add(AttributeName);
                        myElement.CheckIn();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
        // Writing Data to the AFServer
        /// <summary>
        /// Writes to AF, and checks in the element.
        /// </summary>
        /// <param name="element"> AFElement being written to.</param>
        /// <param name="attribute"> AFAttribute being changed.</param>
        /// <param name="value"> The new value being imported.</param>
        public static void writeToAF(AFElement element, AFAttribute attribute, double value)
        {
            AFValue input = new AFValue(attribute, value, AFTime.Now);

            attribute.SetValue(input);
            element.CheckIn();
        }
        /// <summary>
        /// Method that creates an AF Attribute on an AF Element
        /// </summary>
        /// <param name="element">AFElement object</param>
        /// <param name="type">Type of attribute to create</param>
        /// <param name="name">name of the new attribute that will be created</param>
        /// <param name="value">Depending on the type of attribute created, the value can be either a scalar value or the ConfigString Value</param>
        public void CreateAttribute(AFElement element, AttributeTypeEnum type, string name, string value)
        {
            var piSystem  = element.PISystem;
            var attribute = element.Attributes.Add(name);

            Logger.InfoFormat("Creating attribute {0}", (name));

            switch (type)
            {
            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute1 -v 10 -t AttDouble
            case AttributeTypeEnum.AttDouble:
                attribute.Type = typeof(double);
                attribute.SetValue(new AFValue(double.Parse(value)));
                break;

            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v \\tst-srv\sinusoid -t AttPIPoint
            case AttributeTypeEnum.AttPIPoint:
                attribute.DataReferencePlugIn = AFDataReference.GetPIPointDataReference(piSystem);
                attribute.ConfigString        = value;
                break;

            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "SELECT Name FROM Info WHERE Version = 1.5" -t AttTableLookup
            case AttributeTypeEnum.AttTableLookup:
                attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["Table Lookup"];
                attribute.Type         = typeof(string); // if a specific type is needed, you'll need to pass it and set it here.
                attribute.ConfigString = value;
                break;

            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "\\optimus\CDT158|sinusoid" -t AttPIPointArray
            case AttributeTypeEnum.AttPIPointArray:
                attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["PI Point Array"];
                attribute.ConfigString        = value;
                break;

            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "B=double;[B*10]" -t AttFormula
            case AttributeTypeEnum.AttFormula:
                attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["Formula"];
                attribute.ConfigString        = value;
                break;

            // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "Hello ;World" -t AttStringBuilder
            case AttributeTypeEnum.AttStringBuilder:
                attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["String Builder"];
                attribute.ConfigString        = value;
                break;
            }

            Logger.Info("Checking in the changes");

            element.CheckIn();

            Logger.Info("Attibute Created");
        }
Exemple #6
0
 public static void createConfig(AFElement elem)
 {
     foreach (var attr in elem.Attributes)
     {
         if (attr.DataReference != null)
         {
             attr.DataReference.CreateConfig();
         }
     }
     foreach (var child in elem.Elements)
     {
         createConfig(child);
     }
     elem.CheckIn();
 }
Exemple #7
0
 public static void resetElement(AFElement elem)
 {
     foreach (AFAttribute attr in elem.Attributes)
     {
         resetAttribute(attr);
     }
     foreach (AFElement child in elem.Elements)
     {
         resetElement(child);
     }
     foreach (AFAnalysis analysis in elem.Analyses)
     {
         resetAnalysis(analysis);
     }
     elem.CheckIn();
 }
Exemple #8
0
        public void CreateChild()
        {
            Console.WriteLine("Test adding a child element.");

            AFElement parent = new AFElement();

            parent.Name        = "Parent Element";
            parent.Description = "Parent Desciption";

            _db.CreateElement(parent);
            parent = AFElement.Find(_conn, parent.Name);

            Assert.IsNotNull(parent.ID);
            Assert.IsNotNull(parent.Path);

            AFElement child = new AFElement();

            child.Name        = "Child Element";
            child.Description = "Child Description";

            parent.Elements.Add(child);

            child = AFElement.FindByPath(_conn, child.Path);

            Assert.IsNotNull(child.Name);
            Assert.IsNotNull(child.ID);
            Assert.IsNotNull(child.Path);
            Assert.IsNotNull(child.Description);
            Assert.Equals(child.Parent.ID, parent.ID);

            parent = AFElement.FindByPath(_conn, parent.Path);

            //Assert.(parent.Elements.Count, 1);

            AFElement refChild = parent.Elements[0];

            Console.WriteLine("Test that original child and referenced child are identical.");
            Assert.Equals(child.Name, refChild.Name);
            Assert.Equals(child.Path, refChild.Path);

            parent.Delete();
            Assert.IsTrue(parent.IsDeleted);

            parent.CheckIn();
            Assert.IsNull(AFElement.Find(_conn, parent.ID), "Assert that parent no longer exists");
            Assert.IsNull(AFElement.Find(_conn, child.ID), "Assert that child no longer exists.");
        }
Exemple #9
0
        private void addToPreference_Click(object sender, EventArgs e)
        {
            if (calculationName.Text == "")
            {
                MessageBox.Show("Please specify the name of the calculation", "No Name specified", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            AFTreeNode node = (AFTreeNode)afTreeView.SelectedNode;
            string     path = node.AFPath;

            if (path == "" || path == null)
            {
                MessageBox.Show("Please select a attribute", "No attribute specified", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            string query  = queryTextBox.Text;
            double offset = Convert.ToDouble(offsetSetting.Text);
            Dictionary <string, string> limits = new Dictionary <string, string> {
            };

            foreach (AFAttributeTrait limit in AFAttributeTrait.AllLimits)
            {
                ComboBox comboBox = (ComboBox)panel1.Controls[limit.Name];
                if (comboBox.Text != "None")
                {
                    limits[limit.Name] = comboBox.Text;
                }
            }
            CalculationPreference preference = new CalculationPreference(path, query, offset, limits);

            AFElement preferenceRoot     = ((AFElement)configurationTreeView.AFRoot);
            AFValue   configurationValue = new AFValue(preference.JSON());
            AFElement preferenceElement  = preferenceRoot.Elements[calculationName.Text];

            if (preferenceElement == null)
            {
                preferenceElement = new AFElement(calculationName.Text);
                preferenceElement.Attributes.Add("Configuration");
                preferenceElement.Attributes["Configuration"].Type = typeof(string);
                preferenceRoot.Elements.Add(preferenceElement);
                preferenceRoot.CheckIn();
            }
            preferenceElement.Attributes["Configuration"].SetValue(configurationValue);
            preferenceElement.CheckIn();
            configurationTreeView.Refresh();
            configurationTreeView.AFSelect(preferenceElement, preferenceElement.Database, preferenceElement.GetPath());
        }
Exemple #10
0
        /// <summary>
        /// Simulates the case where two element exist with the same name on the same level.
        /// </summary>
        public void DuplicateNames()
        {
            AFElement ele1 = GenerateElement();
            AFElement ele2 = GenerateElement();

            _db.CreateElement(ele1);
            Assert.IsNotNull(_db.Elements[ele1.Name]);
            _db.CreateElement(ele2);
            Assert.IsNotNull(_db.Elements[ele2.Name]);

            ele2.Delete();
            ele1.Delete();
            ele1.CheckIn();
            ele2.CheckIn();

            Assert.IsNull(_db.Elements[ele1.Name]);
            Assert.IsNull(_db.Elements[ele2.Name]);
        }
        public void CreateTrafficStructure(AFElement repoElement)
        {
            if (!repoElement.Elements.Contains("Traffic"))
            {
                var traffic        = repoElement.Elements.Add("Traffic", repoElement.Database.ElementTemplates["Traffic"]);
                var referrers      = traffic.Elements.Add("Referrers", repoElement.Database.ElementTemplates["Referrers"]);
                var popularContent = traffic.Elements.Add("PopularContents", repoElement.Database.ElementTemplates["PopularContents"]);

                // Popular content is a fixed number, there is always only 10
                for (int i = 1; i < 11; i++)
                {
                    popularContent.Elements.Add(string.Format("Popular-{0:00}", i), repoElement.Database.ElementTemplates["PopularContent"]);
                }

                repoElement.CheckIn();
                GitHubCommon.CreateTags(repoElement);
            }
        }
Exemple #12
0
        private void CreateAttribute_Button_Click(object sender, EventArgs e)
        {
            String AttributeName = Attribute_TextBox.Text.Trim();

            if (AttributeName == "")
            {
                MessageBox.Show("No Attribute Name defined");
            }
            else
            {
                if (afTreeView1.AFSelectedPath == afTreeView1.AFRootPath)
                {
                    MessageBox.Show("You have not selected an element", "Exception");
                }
                else
                {
                    try
                    {
                        if (TagList.SelectedIndices.Count < 1)
                        {
                            //tag is not selected
                            MessageBox.Show("There is no selected tag");
                            //myElement = myAFDatabase.Elements[afTreeView1.AFSelectedPath];
                            //myElement.Attributes.Add(AttributeName);
                        }
                        else
                        {
                            //one or more tag is selected
                            myElement = myAFDatabase.Elements[afTreeView1.AFSelectedPath];
                            AFAttribute myAttribute = myElement.Attributes.Add(AttributeName);
                            AFPlugIn    PI_Point    = myAFDatabase.PISystem.DataReferencePlugIns["PI Point"];
                            myAttribute.DataReferencePlugIn = PI_Point;
                            myAttribute.ConfigString        = @"\\" + myPIServer.Name + @"\" + TagList.SelectedItems[0].Text;
                        }
                        myElement.CheckIn();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
Exemple #13
0
        public void CreateElement()
        {
            AFElement element = GenerateElement();

            Console.WriteLine("Test element creation.");
            Assert.IsTrue(_db.CreateElement(element), "Assert creation passed");

            //Check that the the element can be found through the AFDB
            Assert.IsNotNull(_db.Elements[element.Name], "Check AFDB element collection for new element.");
            Assert.IsNotNull(AFElement.Find(_conn, element.WebID));
            Assert.IsNotNull(AFElement.FindByPath(_conn, element.Path));
            //Assert.IsNotNull(AFElement.FindByTemplate());
            //Assert.IsNotNull(AFElement.FindByCategory(_conn, element.Categories.First()));

            //TODO: There should be more tests for finding the element
            element.Delete();
            Assert.IsTrue(element.IsDeleted);
            element.CheckIn();

            Assert.IsNull(AFElement.Find(_conn, element.WebID));
        }
Exemple #14
0
        static void CreateWeakReference(AFDatabase database)
        {
            Console.WriteLine("Creating a weak referenc of the Feeder01 under London");

            AFReferenceType weakRefType = database.ReferenceTypes["Weak Reference"];

            AFElement london = database.Elements["Geographical Locations"].Elements["London"];
            AFElement feeder = database.Elements["Feeders"].Elements["Feeder005"];

            if (feeder != null || london != null)
            {
                if (!london.Elements.Contains(feeder))
                {
                    london.Elements.Add(feeder, weakRefType);
                }
            }


            if (london.IsDirty)
            {
                london.CheckIn();
            }
        }
Exemple #15
0
        public async Task WriteAndRefreshDeadlock()
        {
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Task refresh = Task.Run(() =>
                {
                    cts.Token.Register(Thread.CurrentThread.Abort);

                    for (int i = 0; i < 100; i++)
                    {
                        myElement.Refresh();
                    }
                });

                Task writer = Task.Run(() =>
                {
                    cts.Token.Register(Thread.CurrentThread.Abort);

                    for (int i = 0; i < 100; i++)
                    {
                        myElement.Elements.Add("Child Element " + i);
                        myElement.CheckIn();
                    }
                });

                Task timeout = Task.Delay(TimeSpan.FromSeconds(10));

                await Task.WhenAny(refresh, writer, timeout);

                Assert.True(timeout.IsCompleted);
                Assert.False(refresh.IsCompleted);
                Assert.False(writer.IsCompleted);

                cts.Cancel();
            }
        }
        /// <summary>
        /// Method that creates an AF Attribute on an AF Element
        /// </summary>
        /// <param name="element">AFElement object</param>
        /// <param name="type">Type of attribute to create</param>
        /// <param name="name">name of the new attribute that will be created</param>
        /// <param name="value">Depending on the type of attribute created, the value can be either a scalar value or the ConfigString Value</param>
        public void CreateAttribute(AFElement element, AttributeTypeEnum type, string name, string value)
        {
            var piSystem = element.PISystem;
            var attribute = element.Attributes.Add(name);
            Logger.InfoFormat("Creating attribute {0}", (name));

            switch (type)
            {

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute1 -v 10 -t AttDouble
                case AttributeTypeEnum.AttDouble:
                    attribute.Type = typeof(double);
                    attribute.SetValue(new AFValue(double.Parse(value)));
                    break;

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v \\tst-srv\sinusoid -t AttPIPoint
                case AttributeTypeEnum.AttPIPoint:
                    attribute.DataReferencePlugIn = AFDataReference.GetPIPointDataReference(piSystem);
                    attribute.ConfigString = value;
                    break;

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "SELECT Name FROM Info WHERE Version = 1.5" -t AttTableLookup
                case AttributeTypeEnum.AttTableLookup:
                    attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["Table Lookup"];
                    attribute.Type = typeof (string); // if a specific type is needed, you'll need to pass it and set it here.
                    attribute.ConfigString = value;
                    break;

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "\\optimus\CDT158|sinusoid" -t AttPIPointArray
                case AttributeTypeEnum.AttPIPointArray:
                    attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["PI Point Array"];
                    attribute.ConfigString = value;
                    break;

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "B=double;[B*10]" -t AttFormula
                case AttributeTypeEnum.AttFormula:
                    attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["Formula"];
                    attribute.ConfigString = value;
                    break;

                // AFCreateAttribute -e \\tst-srv\db1\AFSDKExamples\CreateAttributes -n Attribute2 -v "Hello ;World" -t AttStringBuilder
                case AttributeTypeEnum.AttStringBuilder:
                    attribute.DataReferencePlugIn = piSystem.DataReferencePlugIns["String Builder"];
                    attribute.ConfigString = value;
                    break;

            }

            Logger.Info("Checking in the changes");

            element.CheckIn();

            Logger.Info("Attibute Created");
        }
 // Writing Data to the AFServer
 /// <summary>
 /// Writes to AF, and checks in the element.
 /// </summary>
 /// <param name="element"> AFElement being written to.</param>
 /// <param name="attribute"> AFAttribute being changed.</param>
 /// <param name="value"> The new value being imported.</param>
 public static void writeToAF(AFElement element, AFAttribute attribute, double value)
 {
     AFValue input = new AFValue(attribute, value, AFTime.Now);
     attribute.SetValue(input);
     element.CheckIn();
 }
        public override List <AFValue> ReadValues(AFElement orgElement)
        {
            var settings = GitHubCommon.GetAFSettings(orgElement);
            var github   = GitHubCommon.GetGitHubClient(settings);

            if (GitHubCommon.isGitHubRateLimitExceeded(github))
            {
                return(new List <AFValue>());
            }


            // Read repositories from GitHub
            var repos = github.Repository.GetAllForOrg(settings.GitHubOwner).Result;

            var values = new List <AFValue>();

            // for each repository, we create it if it does not exist and we retrieve repo values in AF
            foreach (var repo in repos)
            {
                var targetRepoElement = GitHubCommon.FindRepositoryById(orgElement, settings, repo.Id.ToString());

                AFElement repoElement;
                if (targetRepoElement.Count == 0)
                {
                    // create new repo element
                    repoElement = new AFElement(repo.Name, settings.RepositoryTemplate);
                    orgElement.Elements.Add(repoElement);
                    orgElement.CheckIn();

                    GitHubCommon.CreateTags(repoElement);
                }

                else
                {
                    // update
                    repoElement = targetRepoElement[0];
                }


                // if name has changed, we rename the element, we keep track of the repository by ids, so we can do that
                // it makes the AF structure easier to navigate
                if (repoElement.Name != repo.Name)
                {
                    repoElement.Name = repo.Name;
                }

                // pull requests
                var pullRequests      = github.PullRequest.GetAllForRepository(repo.Owner.Login, repo.Name);
                var pullRequestsCount = pullRequests.Result.Count;

                // commits
                var contributors      = github.Repository.Statistics.GetContributors(repo.Owner.Login, repo.Name);
                var contributorsCount = contributors.Result.Count;
                var totalCommits      = contributors.Result.ToList().Sum(contributor => contributor.Total);


                //Create AFValues based on the GitHub Readings
                values.AddRange(new List <AFValue>()
                {
                    new AFValue(repoElement.Attributes["Repository Id"], repo.Id, AFTime.Now),
                    new AFValue(repoElement.Attributes["Commits"], totalCommits, AFTime.Now),
                    new AFValue(repoElement.Attributes["Contributors"], contributorsCount, AFTime.Now),
                    new AFValue(repoElement.Attributes["Forks"], repo.ForksCount, AFTime.Now),
                    new AFValue(repoElement.Attributes["Name"], repo.Name, AFTime.Now),
                    new AFValue(repoElement.Attributes["Pull Requests"], pullRequestsCount, AFTime.Now),
                    new AFValue(repoElement.Attributes["Stars"], repo.StargazersCount, AFTime.Now),
                    new AFValue(repoElement.Attributes["Url"], repo.HtmlUrl, AFTime.Now),
                    new AFValue(repoElement.Attributes["UpdatedAt"], repo.UpdatedAt.LocalDateTime, AFTime.Now),
                    new AFValue(repoElement.Attributes["HasDownloads"], repo.HasDownloads, AFTime.Now),
                    new AFValue(repoElement.Attributes["HasIssues"], repo.HasIssues, AFTime.Now),
                    new AFValue(repoElement.Attributes["Open Issues"], repo.OpenIssuesCount, AFTime.Now),
                    new AFValue(repoElement.Attributes["HasWiki"], repo.HasWiki, AFTime.Now),
                    new AFValue(repoElement.Attributes["Watchers"], github.Activity.Watching.GetAllWatchers(settings.GitHubOwner, repo.Name).Result.Count, AFTime.Now),
                }
                                );
                if (AFSDKHelpers.GetAttributeValue <DateTime>(repoElement, "CreatedAt") <= new DateTime(1970, 1, 1))
                {
                    values.Add(new AFValue(repoElement.Attributes["CreatedAt"], repo.CreatedAt.LocalDateTime, AFTime.Now));
                }
            }

            var rateLimits = github.Miscellaneous.GetRateLimits().Result.Resources;

            Logger.InfoFormat("GitHub rate limits: Search:{0}, Core: {1}", rateLimits.Search.Remaining, rateLimits.Core.Remaining);

            return(values);
        }
Exemple #19
0
 private void CreateAttributewithoutTagButton_Click(object sender, EventArgs e)
 {
     String AttributeName = Attribute_TextBox.Text.Trim();
     if (AttributeName == "")
     {
         MessageBox.Show("No Attribute Name defined");
     }
     else
     {
         if (afTreeView1.AFSelectedPath == afTreeView1.AFRootPath)
         {
             MessageBox.Show("Exception:", "You did not select an element");
         }
         else
         {
             try
             {
                     //Don't use Selected tag
                 myElement = myAFDatabase.Elements[afTreeView1.AFSelectedPath];
                 myElement.Attributes.Add(AttributeName);
                 myElement.CheckIn();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
     }
 }