Skip to content

Commit

Permalink
Merge pull request #124 from RevealBi/feat/google-sheet-datasource
Browse files Browse the repository at this point in the history
Feat/google sheet datasource
  • Loading branch information
brianlagunas authored Dec 12, 2024
2 parents 7761aaf + 601593a commit d3f58c7
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 32 deletions.
24 changes: 23 additions & 1 deletion e2e/Sandbox/DashboardCreators/GoogleSheetDashboard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Reveal.Sdk.Dom;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -13,7 +15,27 @@ internal class GoogleSheetDashboard : IDashboardCreator

public RdashDocument CreateDashboard()
{
throw new NotImplementedException();
var googleSheetDS = new GoogleSheetsDataSourceItem("Google Sheet Data Source", "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms")
{
Title = "Google Sheet Data Source",
Subtitle = "Google Sheet Data Source Subtitle",
Sheet = "Class Data",
Fields = new List<IField>
{
new TextField("Student Name"),
new TextField("Gender"),
new TextField("Class Level"),
new TextField("Home State"),
new TextField("Major"),
new TextField("Extracurricular Activity")
}
};

var document = new RdashDocument("My Dashboard");

document.Visualizations.Add(new GridVisualization("Class List", googleSheetDS).SetColumns("Student Name", "Gender", "Major", "Extracurricular Activity"));

return document;
}
}
}
13 changes: 13 additions & 0 deletions e2e/Sandbox/Data/GoogleServiceAccountAuth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "testreveal-henry",
"private_key_id": "",
"private_key": "",
"client_email": "[email protected]",
"client_id": "108057201146814272043",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "",
"universe_domain": "googleapis.com"
}
46 changes: 41 additions & 5 deletions e2e/Sandbox/Reveal/AuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Reveal.Sdk.Data;
using Reveal.Sdk.Data;
using Reveal.Sdk.Data.Amazon.Athena;
using Reveal.Sdk.Data.Amazon.S3;
using Reveal.Sdk.Data.Microsoft.AnalysisServices;
Expand All @@ -17,6 +17,9 @@
using Reveal.Sdk.Data.OData;
using Reveal.Sdk.Data.Oracle;
using Reveal.Sdk.Data.Microsoft.SynapseAnalytics;
using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Text;

namespace Sandbox.RevealSDK
{
Expand All @@ -40,6 +43,12 @@ public Task<IRVDataSourceCredential> ResolveCredentialsAsync(RVDashboardDataSour
else if (dataSource is RVNativeAnalysisServicesDataSource)
{
userCredential = new RVUsernamePasswordDataSourceCredential("username", "password", "domain");
}
else if (dataSource is RVGoogleDriveDataSource)
{
var _token = RetrieveGoogleDriveBearerToken();

userCredential = new RVBearerTokenDataSourceCredential(_token, null);
}
else if (dataSource is RVSnowflakeDataSource)
{
Expand Down Expand Up @@ -72,10 +81,6 @@ public Task<IRVDataSourceCredential> ResolveCredentialsAsync(RVDashboardDataSour
else if (dataSource is RVGoogleDriveDataSource)
{
userCredential = new RVBearerTokenDataSourceCredential("token", null);
}
else if(dataSource is RVGoogleSheetDataSource)
{

}
else if(dataSource is RVHttpAnalysisServicesDataSource)
{
Expand Down Expand Up @@ -112,5 +117,36 @@ public Task<IRVDataSourceCredential> ResolveCredentialsAsync(RVDashboardDataSour

return Task.FromResult(userCredential);
}

internal string RetrieveGoogleDriveBearerToken()
{
var jsonKey = @"
{
""type"": ""service_account"",
""project_id"": ""testreveal-henry"",
""private_key_id"": ""***"",
""private_key"": ""******"",
""client_email"": ""[email protected]"",
""client_id"": ""***"",
""auth_uri"": ""https://accounts.google.com/o/oauth2/auth"",
""token_uri"": ""https://oauth2.googleapis.com/token"",
""auth_provider_x509_cert_url"": ""https://www.googleapis.com/oauth2/v1/certs"",
""client_x509_cert_url"": ""https://www.googleapis.com/robot/v1/metadata/x509/henrytestreveal%40testreveal-henry.iam.gserviceaccount.com"",
""universe_domain"": ""googleapis.com""
}
";

var memoryStream = new MemoryStream();
byte[] jsonKeyBytes = Encoding.UTF8.GetBytes(jsonKey);
memoryStream.Write(jsonKeyBytes, 0, jsonKeyBytes.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
var credential = GoogleCredential.FromStream(memoryStream).CreateScoped("https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile");

var accessToken = credential.UnderlyingCredential.GetAccessTokenForRequestAsync().Result;

return accessToken;
}
}
}
4 changes: 4 additions & 0 deletions e2e/Sandbox/Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth.AspNetCore" Version="1.65.0" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.Athena" Version="1.7.1" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.Redshift" Version="1.7.1" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.S3" Version="1.7.1" />
Expand Down Expand Up @@ -48,6 +49,9 @@
<None Update="Dashboards\Sales.rdash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\GoogleServiceAccountAuth.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Samples.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public void Identitifer_SaveValueAndProperties_WhenSet()
var identifier = "IdentitiferTest";

// Act
dataSourceItem.Identitifer = identifier;
dataSourceItem.Identifier = identifier;

// Assert
Assert.Equal(identifier, dataSourceItem.Identitifer);
Assert.Equal(identifier, dataSourceItem.Properties.GetValue<string>("Identitifer"));
Assert.Equal(identifier, dataSourceItem.Identifier);
Assert.Equal(identifier, dataSourceItem.Properties.GetValue<string>("Identifier"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,106 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data.DataSourceItems
{
public class GoogleSheetsDataSourceItemFixture
{
[Fact]
public void Constructor_SetsTitleAndDatasource_AsProvided()
public void Constructor_SetsTitleAndIdentifier_AsProvided()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var title = "Test title";
var identifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem(title, dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem(title, identifier);

// Assert
Assert.Equal(title, dataSourceItem.Title);
Assert.Equal(dataSource, dataSourceItem.DataSource);
Assert.Equal(identifier, dataSourceItem.Identifier);
}

[Fact]
public void Constructor_SetsResourceItemToGoogleDrive_WhenCreate()
{
// Arrange
var title = "Test title";
var identifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem(title, identifier);

// Assert
Assert.Equal(DataSourceProvider.GoogleDrive, dataSourceItem.ResourceItemDataSource.Provider);
Assert.Equal(identifier, dataSourceItem.Identifier);
}

[Fact]
public void FirstRowContainsLabels_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");

// Act 1
dataSourceItem.FirstRowContainsLabels = true;

// Assert
Assert.True(dataSourceItem.FirstRowContainsLabels);
Assert.True(dataSourceItem.Properties.GetValue<bool>("FirstRowContainsLabels"));
Assert.True(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));

// Act 1
dataSourceItem.FirstRowContainsLabels = false;

// Assert
Assert.False(dataSourceItem.FirstRowContainsLabels);
Assert.False(dataSourceItem.Properties.GetValue<bool>("FirstRowContainsLabels"));
Assert.False(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));
}

[Fact]
public void Identifier_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var testIdentifier = "anotherTestIdentifier";

// Act 1
dataSourceItem.Identifier = testIdentifier;

// Assert
Assert.Equal(testIdentifier, dataSourceItem.Identifier);
Assert.Equal(testIdentifier, dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void ResourceItem_IsGoogleDriveDataSourceItem_WhenCreated()
{
// Arrange
var testIdentifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", testIdentifier);

// Assert
Assert.Equal(DataSourceProvider.GoogleDrive, dataSourceItem.ResourceItem.DataSource.Provider);
Assert.IsType<GoogleDriveDataSourceItem>(dataSourceItem.ResourceItem);
Assert.Equal(testIdentifier, dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void NamedRange_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var namedRange = "NamedRangeTest";

// Act
Expand All @@ -69,7 +116,7 @@ public void PivotTable_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var pivotTable = "PivotTableTest";

// Act
Expand All @@ -85,7 +132,7 @@ public void Sheet_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var sheet = "SheetTest";

// Act
Expand All @@ -95,5 +142,74 @@ public void Sheet_SaveValueAndProperties_WhenSet()
Assert.Equal(sheet, dataSourceItem.Sheet);
Assert.Equal(sheet, dataSourceItem.Properties.GetValue<string>("Sheet"));
}

[Fact]
public void RDashDocument_CreateTwoDataSources_WhenUseGoogleSheetDataSourceItem()
{
// Arrange
var document = new RdashDocument("Test");
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("My Google Sheet", "testIdentifier").SetFields(new List<IField>() { new TextField("Test") });

dataSourceItem.Sheet = "Sheet1";
dataSourceItem.FirstRowContainsLabels = true;

document.Visualizations.Add(new GridVisualization(dataSourceItem));

// Act
document.Validate();

// Assert
Assert.Equal(2, document.DataSources.Count);
Assert.Equal(DataSourceIds.GSHEET, document.DataSources[0].Id);
Assert.Equal(DataSourceProvider.GoogleSheets, document.DataSources[0].Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, document.DataSources[1].Provider);
}

[Fact]
public void RDashDocument_HasCorrectDataSourceItem_WhenLoadFromFile()
{
// Arrange
var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestGoogleSheet.rdash");

// Act
var document = RdashDocument.Load(filePath);
var gsheetDataSource = document.DataSources[0];
var driveDataSource = document.DataSources[1];
var dataSourceItem = document.Visualizations[0].DataDefinition.DataSourceItem;

var value = dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW");

// Assert
Assert.Equal(gsheetDataSource.Id, dataSourceItem.DataSourceId);
Assert.Equal(DataSourceProvider.GoogleSheets, gsheetDataSource.Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, driveDataSource.Provider);
Assert.Equal("Class Data", dataSourceItem.Properties.GetValue<string>("Sheet"));
Assert.True(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));
Assert.Equal("1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms", dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void RDashDocument_CreateExpectedJson_WhenExport()
{
// Arrange
var document = new RdashDocument("Test");
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("My Google Sheet", "testIdentifier").SetFields(new List<IField>() { new TextField("Test") });

dataSourceItem.Sheet = "Sheet1";
dataSourceItem.FirstRowContainsLabels = true;

document.Visualizations.Add(new GridVisualization(dataSourceItem));

// Act
document.Validate();

// Assert
Assert.Equal(2, document.DataSources.Count);
Assert.Equal(DataSourceIds.GSHEET, document.DataSources[0].Id);
Assert.Equal(DataSourceProvider.GoogleSheets, document.DataSources[0].Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, document.DataSources[1].Provider);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Data;
using System;
using System.Collections.Generic;
Expand All @@ -21,5 +22,15 @@ public void Constructor_SetProviderToGoogleSheets_WhenConstructed()
// Assert
Assert.Equal(DataSourceProvider.GoogleSheets, dataSource.Provider);
}

[Fact]
public void Constructor_SetIdToGSheet_WhenConstructed()
{
// Act
var dataSource = new GoogleSheetsDataSource();

// Assert
Assert.Equal(DataSourceIds.GSHEET, dataSource.Id);
}
}
}
Loading

0 comments on commit d3f58c7

Please sign in to comment.