Skip to content

Commit

Permalink
Merge pull request #13 from GridProtectionAlliance/TDAP-23
Browse files Browse the repository at this point in the history
Tdap 23
  • Loading branch information
clackner-gpa authored Jul 5, 2024
2 parents daece77 + 7bdef30 commit 9fb2bca
Show file tree
Hide file tree
Showing 47 changed files with 2,870 additions and 1,225 deletions.
60 changes: 33 additions & 27 deletions TrenDAP.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,66 @@ CREATE TABLE Setting
)
GO

CREATE TABLE DataSourceType
CREATE TABLE DataSet
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
Name VARCHAR(200) NOT NULL,
[Context] VARCHAR(11) NOT NULL,
[RelativeValue] FLOAT NOT NULL,
[RelativeWindow] VARCHAR(5) NOT NULL,
[From] Date NOT NULL,
[To] Date NOT NULL,
[Hours] INT NOT NULL,
[Days] INT NOT NULL,
[Weeks] BIGINT NOT NULL,
[Months] INT NOT NULL,
[Name] VARCHAR(200) NOT NULL,
[User] VARCHAR(MAX) NOT NULL,
[Public] bit NULL DEFAULT 0,
UpdatedOn DATETIME NULL DEFAULT GETUTCDATE()
)
GO

INSERT INTO DataSourceType (Name) VALUES ('TrenDAPDB')
GO
INSERT INTO DataSourceType (Name) VALUES ('OpenHistorian')
GO
INSERT INTO DataSourceType (Name) VALUES ('Sapphire')
GO

CREATE TABLE DataSource
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
Name VARCHAR(200) NULL,
[User] VARCHAR(MAX) NOT NULL,
DataSourceTypeID INT NOT NULL REFERENCES DataSourceType(ID),
Type VARCHAR(50) NOT NULL,
URL VARCHAR(MAX) NULL,
[Public] bit NULL DEFAULT 0,
RegistrationKey VARCHAR(50) NOT NULL UNIQUE,
APIToken VARCHAR(50) NOT NULL,
Expires DATETIME NULL,
SettingsString VARCHAR(MAX) NOT NULL DEFAULT '{}'
)
GO

CREATE TABLE DataSet
CREATE TABLE DataSourceDataSet
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
[Context] VARCHAR(11) NOT NULL,
[RelativeValue] FLOAT NOT NULL,
[RelativeWindow] VARCHAR(5) NOT NULL,
[From] Date NOT NULL,
[To] Date NOT NULL,
[Hours] INT NOT NULL,
[Days] INT NOT NULL,
[Weeks] BIGINT NOT NULL,
[Months] INT NOT NULL,
[Name] VARCHAR(200) NOT NULL,
DataSourceID INT NOT NULL REFERENCES DataSource(ID),
DataSetID INT NOT NULL REFERENCES DataSet(ID),
SettingsString VARCHAR(MAX) NOT NULL DEFAULT '{}'
)
GO

CREATE TABLE EventSource
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
Name VARCHAR(200) NULL,
[User] VARCHAR(MAX) NOT NULL,
Type VARCHAR(50) NOT NULL,
URL VARCHAR(MAX) NULL,
[Public] bit NULL DEFAULT 0,
UpdatedOn DATETIME NULL DEFAULT GETUTCDATE()
RegistrationKey VARCHAR(50) NOT NULL,
APIToken VARCHAR(50) NOT NULL,
SettingsString VARCHAR(MAX) NOT NULL DEFAULT '{}'
)
GO

CREATE TABLE DataSourceDataSet
CREATE TABLE EventSourceDataSet
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
DataSourceID INT NOT NULL REFERENCES DataSource(ID),
EventSourceID INT NOT NULL REFERENCES EventSource(ID),
DataSetID INT NOT NULL REFERENCES DataSet(ID),
SettingsString VARCHAR(MAX) NOT NULL DEFAULT '{}'
)
Expand Down Expand Up @@ -137,8 +145,6 @@ GO
INSERT INTO ApplicationRole(Name, Description) VALUES('Administrator', 'Admin Role')
GO



CREATE TABLE SecurityGroup
(
ID UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY,
Expand Down
51 changes: 51 additions & 0 deletions TrenDAP/Attributes/CustomViewAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//******************************************************************************************************
// ParentKeyAttribute.cs - Gbtc
//
// Copyright © 2021, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/01/2024 - Gabriel Santos
// Generated original version of source code.
//
//******************************************************************************************************

using System;

namespace GSF.Data.Model
{
/// <summary>
/// Defines an attribute that will allow setting a custom view a modeled table.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class CustomViewAttribute : Attribute
{
/// <summary>
/// Gets field name to use for property.
/// </summary>
public string CustomView
{
get;
}

/// <summary>
/// Creates a new <see cref="CustomViewAttribute"/>.
/// </summary>
/// <param name="customView">SQL to generate custom view.</param>
public CustomViewAttribute(string customView)
{
CustomView = customView;
}
}
}
52 changes: 52 additions & 0 deletions TrenDAP/Attributes/ParentKeyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//******************************************************************************************************
// ParentKeyAttribute.cs - Gbtc
//
// Copyright © 2021, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/01/2024 - Gabriel Santos
// Generated original version of source code.
//
//******************************************************************************************************

using System;

namespace TrenDAP.Attributes
{
/// <summary>
/// Defines an attribute that will allow a foreign key in the model to point back to parent table
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class ParentKeyAttribute : Attribute
{
/// <summary>
/// Gets field name to use for property.
/// </summary>
public Type Model
{
get;
}


/// <summary>
/// Creates a new <see cref="ParentKeyAttribute"/>.
/// </summary>
/// <param name="model">Type of modeled table that key points back to.</param>
public ParentKeyAttribute(Type model)
{
Model = model;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,15 @@ public OpenHistorianController(IConfiguration configuration)

#region [ Http Methods ]
[HttpGet, Route("{dataSourceID:int}/{table?}")]
public virtual ActionResult Get(int dataSourceID, string table = "")
public virtual ActionResult GetTable(int dataSourceID, string table = "")
{
using (AdoDataConnection connection = new AdoDataConnection(Configuration["SystemSettings:ConnectionString"], Configuration["SystemSettings:DataProviderString"]))
{
try
{
DataSource dataSource = new TableOperations<DataSource>(connection).QueryRecordWhere("ID = {0}", dataSourceID);
DataSourceHelper helper = new DataSourceHelper(dataSource);
Task<string> rsp = helper.GetAsync($"api/trendap/{table}");
return Ok(rsp.Result);
if (dataSource.Type == "OpenHistorian") return GetOpenXDA(dataSource, table);
else return StatusCode(StatusCodes.Status500InternalServerError, "Only TrenDAPDB datasources are supported by this endpoint.");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -215,6 +214,22 @@ public Task<HttpResponseMessage> Query(int dataSourceID, Post post, Cancellation

#region [ Static ]

private ActionResult GetOpenXDA(DataSource dataSource, string table, JObject filter = null)
{
try
{
DataSourceHelper helper = new DataSourceHelper(dataSource);
Task<string> rsp;
if (filter is null) rsp = helper.GetAsync($"api/{table}");
else rsp = helper.PostAsync($"api/{table}/SearchableList", new StringContent(filter.ToString(), Encoding.UTF8, "application/json"));
return Ok(rsp.Result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}

public static Task<HttpResponseMessage> Query(int dataSourceID, Post post, IConfiguration configuration, CancellationToken cancellationToken)
{
using (AdoDataConnection connection = new AdoDataConnection(configuration["SystemSettings:ConnectionString"], configuration["SystemSettings:DataProviderString"]))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class PostData
public string OrderBy { get; set; }
public bool Ascending { get; set; }
}

public class XDADataSetData
{
public string By { get; set; }
Expand Down Expand Up @@ -95,22 +94,16 @@ public TrenDAPDBController(IConfiguration configuration)
#endregion

#region [ Http Methods ]
[HttpGet, Route("{dataSourceID:int}/{table?}")]
public virtual ActionResult GetTable(int dataSourceID, string table = "")
[HttpGet, Route("{sourceID:int}/{table?}")]
public virtual ActionResult GetTable(int sourceID, string table = "")
{
using (AdoDataConnection connection = new AdoDataConnection(Configuration["SystemSettings:ConnectionString"], Configuration["SystemSettings:DataProviderString"]))
{

try
{
DataSource dataSource = new TableOperations<DataSource>(connection).QueryRecordWhere("ID = {0}", dataSourceID);
string type = connection.ExecuteScalar<string>("SELECT Name FROM DataSourceType WHERE ID = {0}", dataSource.DataSourceTypeID);

if (type == "TrenDAPDB")
return GetOpenXDA(dataSource, table);
else if (type == "OpenHistorian")
return GetOpenHistorian(dataSource, table);
else return StatusCode(StatusCodes.Status400BadRequest, "Datasource type not supported");
DataSource dataSource = new TableOperations<DataSource>(connection).QueryRecordWhere("ID = {0}", sourceID);
if (dataSource.Type == "TrenDAPDB") return GetOpenXDA(dataSource, table);
else return StatusCode(StatusCodes.Status500InternalServerError, "Only TrenDAPDB datasources are supported by this endpoint.");
}
catch (Exception ex)
{
Expand All @@ -128,12 +121,11 @@ public virtual ActionResult GetChannels(int dataSourceID, [FromBody] JObject fil
{
DataSource dataSource = new TableOperations<DataSource>(connection).QueryRecordWhere("ID = {0}", dataSourceID);
DataSourceHelper helper = new DataSourceHelper(dataSource);
string type = connection.ExecuteScalar<string>("SELECT Name FROM DataSourceType WHERE ID = {0}", dataSource.DataSourceTypeID);
Task<string> rsp;

if (type == "TrenDAPDB")
if (dataSource.Type == "TrenDAPDB")
{
rsp = helper.PostAsync("api/Channel/GetTrendSearchData", new StringContent(filter.ToString(), Encoding.UTF8, "application/json"));
rsp = helper.PostAsync("api/Channel/TrenDAP", new StringContent(filter.ToString(), Encoding.UTF8, "application/json"));
}
else return StatusCode(StatusCodes.Status500InternalServerError, "Only TrenDAPDB datasources supported by this endpoint.");

Expand All @@ -145,7 +137,7 @@ public virtual ActionResult GetChannels(int dataSourceID, [FromBody] JObject fil
}
}
}

private ActionResult GetOpenXDA(DataSource dataSource, string table, JObject filter = null)
{
try
Expand All @@ -162,20 +154,6 @@ private ActionResult GetOpenXDA(DataSource dataSource, string table, JObject fil
}
}

private ActionResult GetOpenHistorian(DataSource dataSource, string table)
{
try
{
DataSourceHelper helper = new DataSourceHelper(dataSource);
Task<string> rsp = helper.GetAsync($"api/trendap/{table}");
return Ok(rsp.Result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}


[HttpGet, Route("HIDS/{dataSourceID:int}/{dataSetID:int}")]
public ActionResult Get(int dataSourceID, int dataSetID, CancellationToken cancellationToken)
Expand Down
Loading

0 comments on commit 9fb2bca

Please sign in to comment.