Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReportsDataSource.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Events;
6using Waher.Things;
8
9namespace Waher.Reports
10{
16 {
20 public const string SourceID = "Reports";
21
22 private static readonly Dictionary<string, ReportNode> nodes = new Dictionary<string, ReportNode>();
23 private static readonly List<ReportNode> roots = new List<ReportNode>();
24 private static ReportsDataSource instance = null;
25 private static DateTime lastChanged = DateTime.UtcNow;
26
27 internal static ReportsDataSource Instance => instance;
28
34 {
35 if (instance is null)
36 instance = this;
37 }
38
42 string IDataSource.SourceID => SourceID;
43
47 public IEnumerable<IDataSource> ChildSources => null;
48
52 public bool HasChildren => false;
53
57 public DateTime LastChanged => lastChanged;
58
64 public Task<string> GetNameAsync(Language Language)
65 {
66 return Language.GetStringAsync(typeof(ReportsDataSource), 1, "Reports");
67 }
68
74 public Task<bool> CanViewAsync(RequestOrigin Caller)
75 {
76 return Task.FromResult(Caller.HasPrivilege("Source." + SourceID + ".View"));
77 }
78
82 public IEnumerable<INode> RootNodes
83 {
84 get
85 {
86 lock (roots)
87 {
88 return roots.ToArray();
89 }
90 }
91 }
92
98 public static async Task<bool> RegisterRootNode(ReportNode ReportRoot)
99 {
100 if (!await RegisterReportNode(ReportRoot))
101 return false;
102
103 lock (roots)
104 {
105 roots.Add(ReportRoot);
106 }
107
108 return true;
109 }
110
116 public static async Task<bool> RegisterReportNode(ReportNode ReportNode)
117 {
118 lock (nodes)
119 {
120 if (nodes.ContainsKey(ReportNode.NodeId))
121 return false;
122
124 lastChanged = DateTime.UtcNow;
125 }
126
127 await NewEvent(await NodeAdded.FromNode(ReportNode,
129
131 {
132 foreach (INode Child in await ReportNode.ChildNodes)
133 {
134 if (Child is ReportNode ChildReport)
135 await RegisterReportNode(ChildReport);
136 }
137 }
138
139 return true;
140 }
141
145 public event EventHandlerAsync<SourceEvent> OnEvent = null;
146
147 internal static async Task NewEvent(SourceEvent Event)
148 {
149 await (instance?.OnEvent?.Raise(instance, Event) ?? Task.CompletedTask);
150 }
151
157 public Task<INode> GetNodeAsync(IThingReference NodeRef)
158 {
159 if (NodeRef.SourceId != SourceID || !string.IsNullOrEmpty(NodeRef.Partition))
160 return Task.FromResult<INode>(null);
161
162 if (nodes.TryGetValue(NodeRef.NodeId, out ReportNode Result))
163 return Task.FromResult<INode>(Result);
164 else
165 return Task.FromResult<INode>(null);
166 }
167
168 }
169}
Class representing an event.
Definition: Event.cs:11
Abstract base class for report nodes.
Definition: ReportNode.cs:15
bool HasChildren
If the source has any child sources.
Definition: ReportNode.cs:74
string NodeId
ID of node.
Definition: ReportNode.cs:39
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: ReportNode.cs:123
Defines the Reports data source. This data source contains a tree structure of reports published by d...
IEnumerable< IDataSource > ChildSources
Child sources. If no child sources are available, null is returned.
static async Task< bool > RegisterReportNode(ReportNode ReportNode)
Registers a new report node.
EventHandlerAsync< SourceEvent > OnEvent
Event raised when a data source event has been raised.
Task< INode > GetNodeAsync(IThingReference NodeRef)
Gets the node, given a reference to it.
Task< bool > CanViewAsync(RequestOrigin Caller)
If the data source is visible to the caller.
static async Task< bool > RegisterRootNode(ReportNode ReportRoot)
Registers a new report root node.
ReportsDataSource()
Defines the Reports data source. This data source contains a tree structure of reports published by d...
bool HasChildren
If the source has any child sources.
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
IEnumerable< INode > RootNodes
Root node references. If no root nodes are available, null is returned.
const string SourceID
Source ID for the reports data source.
DateTime LastChanged
When the source was last updated.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Basic access point for runtime language localization.
Definition: Translator.cs:16
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Definition: Translator.cs:223
Tokens available in request.
Definition: RequestOrigin.cs:9
static readonly RequestOrigin Empty
Empty request origin.
bool HasPrivilege(string Privilege)
If the origin has a given privilege.
static Task< NodeAdded > FromNode(INode Node, Language Language, RequestOrigin Caller, bool Sniffable)
Creates an event object from a node object.
Definition: NodeAdded.cs:36
Abstract base class for all data source events.
Definition: SourceEvent.cs:13
Interface for datasources that are published through the concentrator interface.
Definition: IDataSource.cs:14
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Interface for thing references.
string Partition
Optional partition in which the Node ID is unique.
string SourceId
Optional ID of source containing node.