generated from giis-uniovi/samples-giis-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c8c48c
commit e87d963
Showing
16 changed files
with
253 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
net/QACover/Giis.Tdrules.Model.IO/ModelJsonSerializer.N.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace Giis.Tdrules.Model.IO | ||
{ | ||
// Temporal location, to be moved to tdrules-model | ||
public class ModelJsonSerializer | ||
{ | ||
/** | ||
* Recommended serialization for models (exclude null and empty attributes) | ||
*/ | ||
public string Serialize(object model, bool prettyPrint) | ||
{ | ||
JsonSerializerSettings settings = new JsonSerializerSettings | ||
{ | ||
NullValueHandling = NullValueHandling.Ignore | ||
}; | ||
JsonSerializer serializer = new JsonSerializer(); | ||
string result = JsonConvert.SerializeObject(model, prettyPrint ? Formatting.Indented : Formatting.None, settings); | ||
return result; | ||
} | ||
|
||
/** | ||
* Recommended deserialization for models | ||
*/ | ||
public object Deserialize(string json, Type clazz) | ||
{ | ||
return JsonConvert.DeserializeObject(json, clazz); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// THIS FILE HAS BEEN AUTOMATICALLY CONVERTED FROM THE JAVA SOURCES. DO NOT EDIT /////// | ||
///////////////////////////////////////////////////////////////////////////////////////////// | ||
using Giis.Tdrules.Openapi.Model; | ||
|
||
|
||
namespace Giis.Qacover.Core.Services | ||
{ | ||
/// <summary>Temporal implementation of the client api, to be moved to tdrules-client</summary> | ||
public class TdRulesApi : Giis.Tdrules.Client.TdRulesApi | ||
{ | ||
private bool useCache; | ||
|
||
private string cacheLocation; | ||
|
||
public TdRulesApi(string uri) | ||
: base(uri) | ||
{ | ||
this.cacheLocation = Giis.Qacover.Core.Services.Configuration.GetInstance().GetCacheRulesLocation(); | ||
this.useCache = !string.Empty.Equals(Coalesce(cacheLocation)); | ||
} | ||
|
||
/// <summary>Gets the fpc rules for a query executed under the specified schema</summary> | ||
public SqlRules GetRules(DbSchema schema, string query, string options) | ||
Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs GitHub Actions / test-net
Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs GitHub Actions / test-net
Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs GitHub Actions / test-net
|
||
{ | ||
SqlRulesBody request = new SqlRulesBody(); | ||
request.SetSchema(schema); | ||
request.SetSql(query); | ||
request.SetOptions(Coalesce(options)); | ||
TdRulesCache cache = GetCache("rulesPost", request); | ||
if (UseCache() && cache.Hit()) | ||
{ | ||
return (SqlRules)cache.GetPayload(typeof(SqlRules)); | ||
} | ||
SqlRules result = base.RulesPost(request); | ||
if (UseCache()) | ||
{ | ||
cache.PutPayload(result); | ||
} | ||
return result; | ||
} | ||
|
||
public virtual SqlTableListBody GetTables(string sql) | ||
{ | ||
TdRulesCache cache = GetCache("sqlTablesPost", sql); | ||
if (UseCache() && cache.Hit()) | ||
{ | ||
return (SqlTableListBody)cache.GetPayload(typeof(SqlTableListBody)); | ||
} | ||
SqlTableListBody result = base.SqlTablesPost(sql); | ||
if (UseCache()) | ||
{ | ||
cache.PutPayload(result); | ||
} | ||
return result; | ||
} | ||
|
||
public virtual SqlParametersBody GetParameters(string sql) | ||
{ | ||
TdRulesCache cache = GetCache("sqlParametersPost", sql); | ||
if (UseCache() && cache.Hit()) | ||
{ | ||
return (SqlParametersBody)cache.GetPayload(typeof(SqlParametersBody)); | ||
} | ||
SqlParametersBody result = base.SqlParametersPost(sql); | ||
if (UseCache()) | ||
{ | ||
cache.PutPayload(result); | ||
} | ||
return result; | ||
} | ||
|
||
// Cache management | ||
private bool UseCache() | ||
{ | ||
return this.useCache; | ||
} | ||
|
||
private TdRulesCache GetCache(string endpoint, object request) | ||
{ | ||
return UseCache() ? new TdRulesCache(this.cacheLocation, endpoint, request) : null; | ||
} | ||
|
||
private string Coalesce(string value) | ||
{ | ||
return value == null ? string.Empty : value; | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////// | ||
/////// THIS FILE HAS BEEN AUTOMATICALLY CONVERTED FROM THE JAVA SOURCES. DO NOT EDIT /////// | ||
///////////////////////////////////////////////////////////////////////////////////////////// | ||
using System; | ||
using Giis.Portable.Util; | ||
using Giis.Tdrules.Model.IO; | ||
using NLog; | ||
|
||
|
||
namespace Giis.Qacover.Core.Services | ||
{ | ||
/// <summary> | ||
/// Temporal implementation of a local storage cache for payloads sent to | ||
/// TdRules, to be moved to tdrules-client | ||
/// </summary> | ||
public class TdRulesCache | ||
{ | ||
private static readonly Logger log = NLogUtil.GetLogger(typeof(Giis.Qacover.Core.Services.TdRulesCache)); | ||
|
||
private ModelJsonSerializer serializer; | ||
|
||
internal string endpoint; | ||
|
||
internal string payload; | ||
|
||
internal string hash; | ||
|
||
internal string cacheFile; | ||
|
||
internal string hit; | ||
|
||
public TdRulesCache(string cacheFolder, string endpoint, object request) | ||
{ | ||
serializer = new ModelJsonSerializer(); | ||
this.endpoint = endpoint; | ||
this.payload = serializer.Serialize(request, true); | ||
this.hash = JavaCs.GetHash(payload); | ||
this.EnsureCacheFolder(cacheFolder, endpoint); | ||
this.cacheFile = GetCacheFile(cacheFolder, endpoint, hash); | ||
this.hit = FileUtil.FileRead(cacheFile, false); | ||
log.Debug("Cache {} {} hit: {}", endpoint, hash, this.hit != null); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the payload of a given request from cache, if does not exists returns | ||
/// null | ||
/// </summary> | ||
public virtual bool Hit() | ||
{ | ||
return this.hit != null; | ||
} | ||
|
||
public virtual object GetPayload(Type clazz) | ||
{ | ||
return serializer.Deserialize(hit, clazz); | ||
} | ||
|
||
/// <summary>Saves the response payload of a given request to cache</summary> | ||
public virtual void PutPayload(object result) | ||
{ | ||
FileUtil.FileWrite(cacheFile, serializer.Serialize(result, true)); | ||
log.Debug("Cache {} {} update: {}", endpoint, hash, result); | ||
} | ||
|
||
private void EnsureCacheFolder(string cacheFolder, string endpoint) | ||
{ | ||
FileUtil.CreateDirectory(FileUtil.GetPath(cacheFolder, endpoint)); | ||
} | ||
|
||
private string GetCacheFile(string cacheFolder, string endpoint, string hash) | ||
{ | ||
return FileUtil.GetPath(cacheFolder, endpoint, hash + ".json"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.