ln.skyscanner/SkyEntities.cs

132 lines
4.5 KiB
C#

// /**
// * 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<Node> NodeCollection { get; private set; }
public ODBCollection<Subnet> SubnetCollection { get; private set; }
public ODBCollection<PointOfPresence> PointOfPresenceCollection { get; private set; }
public ODBCollection<L2Segment> L2SegmentCollection { get; private set; }
public ODBCollection<CrawledHost> CrawledHosts { get; private set; }
public ODBCollection<CrawledSubnet> CrawledSubnets { get; private set; }
public ODBCollection<Network4> BlockedNetworks { get; private set; }
public ODBCollection<SkyCheckState> SkyCheckStates { get; private set; }
public SkyEntities(SkyScanner skyScanner)
{
SkyScanner = skyScanner;
Logging.Log(LogLevel.INFO, "SkyEntities: initializing");
ODB = new ODB(BasePath);
NodeCollection = ODB.GetCollection<Node>();
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<Subnet>();
SubnetCollection.EnsureIndeces("Network");
PointOfPresenceCollection = ODB.GetCollection<PointOfPresence>();
PointOfPresenceCollection.EnsureIndeces("ForeignName");
L2SegmentCollection = ODB.GetCollection<L2Segment>();
L2SegmentCollection.EnsureIndeces("Network","PoPs");
CrawledHosts = ODB.GetCollection<CrawledHost>();
CrawledHosts.EnsureIndeces("PrimaryIP","IPAddresses[]");
CrawledSubnets = ODB.GetCollection<CrawledSubnet>();
BlockedNetworks = ODB.GetCollection<Network4>("blockedNetworks");
SkyCheckStates = ODB.GetCollection<SkyCheckState>();
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<SkyCheckState>("Node", node.ID),
Query.Equals<SkyCheckState>("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();
}
}
}