// /** // * File: SkyEntities.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using ln.types.odb; using ln.skyscanner.entities; using System.IO; using ln.http.resources; using System.Linq; using ln.types.odb.mapped; using ln.types.net; using ln.skyscanner.crawl; using ln.skyscanner.checks; using ln.logging; using System.Collections.Generic; namespace ln.skyscanner { public class SkyEntities { public SkyScanner SkyScanner { get; } public string BasePath => Path.Combine(SkyScanner.BasePath, "entities"); public GlobalNetwork GlobalNetwork { get; private set; } public ODB ODB { get; private set; } public ODBCollection NodeCollection { get; private set; } public ODBCollection SubnetCollection { get; private set; } public ODBCollection PointOfPresenceCollection { get; private set; } public ODBCollection L2SegmentCollection { get; private set; } public ODBCollection CrawledHosts { get; private set; } public ODBCollection CrawledSubnets { get; private set; } public ODBCollection BlockedNetworks { get; private set; } public ODBCollection SkyCheckStates { get; private set; } public SkyEntities(SkyScanner skyScanner) { SkyScanner = skyScanner; Logging.Log(LogLevel.INFO, "SkyEntities: initializing"); ODB = new ODB(BasePath); NodeCollection = ODB.GetCollection(); NodeCollection.EnableStrongCache(true); NodeCollection.EnsureIndex("uniqueIdentity", true); NodeCollection.EnsureIndeces( "PrimaryIP", "Interfaces[].ConfiguredIPs[].IP", "Interfaces[].ConfiguredIPs[].Network" ); Network4 n192 = Network4.Parse("192.168.0.0/16"); /* Preload all nodes to increase load speed*/ foreach (Node node in NodeCollection.ToArray()) { //if (n192.Contains(node.PrimaryIP)) //NodeCollection.Delete(node); } SubnetCollection = ODB.GetCollection(); SubnetCollection.EnsureIndeces("Network"); PointOfPresenceCollection = ODB.GetCollection(); PointOfPresenceCollection.EnsureIndeces("ForeignName"); L2SegmentCollection = ODB.GetCollection(); L2SegmentCollection.EnsureIndeces("Network","PoPs"); CrawledHosts = ODB.GetCollection(); CrawledHosts.EnsureIndeces("PrimaryIP","IPAddresses[]"); CrawledSubnets = ODB.GetCollection(); BlockedNetworks = ODB.GetCollection("blockedNetworks"); SkyCheckStates = ODB.GetCollection(); SkyCheckStates.EnableStrongCache(true); SkyCheckStates.EnsureIndeces("Node"); SkyCheckStates.EnsureUniqueness("Node", "CheckName"); //foreach (SkyCheckState checkState in SkyCheckStates.ToArray()) //{ // if (object.ReferenceEquals(checkState.Node, null)) // SkyCheckStates.Delete(checkState); //} Logging.Log(LogLevel.INFO, "SkyEntities: initialized"); GlobalNetwork = new GlobalNetwork(); } public PerformanceValue LookupPerformanceValue(String perfName) { string[] resourcePath = perfName.Split('/'); Node node = NodeCollection.Query("uniqueIdentity", resourcePath[0]).FirstOrDefault(); if (node == null) throw new KeyNotFoundException(); SkyCheckState skyCheckState = SkyCheckStates.Query( Query.AND( Query.Equals("Node", node.ID), Query.Equals("CheckName", resourcePath[1]) ) ).FirstOrDefault(); if (skyCheckState == null) throw new KeyNotFoundException(); foreach (PerformanceValue performanceValue in skyCheckState.PerformanceValues) if (performanceValue.PerfName.Equals(perfName)) return performanceValue; throw new KeyNotFoundException(); } } }