Files
ResInsight/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.cpp

906 lines
37 KiB
C++
Raw Normal View History

2024-02-28 08:10:52 +01:00
#include "RiaOsduConnector.h"
#include "RiaFileDownloader.h"
#include "RiaLogging.h"
#include "RiaOsduDefines.h"
2024-02-28 08:10:52 +01:00
#include "RiaOsduOAuthHttpServerReplyHandler.h"
#include <QAbstractOAuth>
#include <QDesktopServices>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QOAuth2AuthorizationCodeFlow>
#include <QOAuthHttpServerReplyHandler>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QUrl>
#include <QUrlQuery>
#include <limits>
#include "cafAssert.h"
2024-02-28 08:10:52 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaOsduConnector::RiaOsduConnector( QObject* parent,
const QString& server,
const QString& dataPartitionId,
const QString& authority,
const QString& scopes,
const QString& clientId )
: QObject( parent )
, m_server( server )
, m_dataPartitionId( dataPartitionId )
, m_authority( authority )
, m_scopes( scopes )
, m_clientId( clientId )
{
m_networkAccessManager = new QNetworkAccessManager( this );
m_osdu = new QOAuth2AuthorizationCodeFlow( this );
RiaLogging::debug( "SSL BUILD VERSION: " + QSslSocket::sslLibraryBuildVersionString() );
RiaLogging::debug( "SSL VERSION STRING: " + QSslSocket::sslLibraryVersionString() );
2024-06-26 12:33:18 +02:00
RiaLogging::debug( "OSDU config:" );
RiaLogging::debug( " server: '" + server + "'" );
RiaLogging::debug( " data partition id: '" + dataPartitionId + "'" );
RiaLogging::debug( " authority: '" + authority + "'" );
RiaLogging::debug( " scopes: '" + scopes + "'" );
RiaLogging::debug( " client id: '" + clientId + "'" );
2024-02-28 08:10:52 +01:00
int port = 35327;
connect( m_osdu,
&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
[]( QUrl url )
{
2024-06-24 10:26:55 +02:00
RiaLogging::debug( "Authorize with url: " + url.toString() );
2024-02-28 08:10:52 +01:00
QUrlQuery query( url );
url.setQuery( query );
QDesktopServices::openUrl( url );
} );
QString authUrl = constructAuthUrl( m_authority );
m_osdu->setAuthorizationUrl( QUrl( authUrl ) );
QString tokenUrl = constructTokenUrl( m_authority );
m_osdu->setAccessTokenUrl( QUrl( tokenUrl ) );
// App key
m_osdu->setClientIdentifier( m_clientId );
m_osdu->setScope( m_scopes );
auto replyHandler = new RiaOsduOAuthHttpServerReplyHandler( port, this );
m_osdu->setReplyHandler( replyHandler );
2024-06-24 10:26:55 +02:00
RiaLogging::debug( "Osdu server callback: " + replyHandler->callback() );
2024-02-28 08:10:52 +01:00
connect( m_osdu, SIGNAL( granted() ), this, SLOT( accessGranted() ) );
2024-06-24 10:26:55 +02:00
connect( m_osdu,
SIGNAL( error( const QString&, const QString&, const QUrl& ) ),
this,
SLOT( errorReceived( const QString&, const QString&, const QUrl& ) ) );
connect( m_osdu,
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
this,
SLOT( authorizationCallbackReceived( const QVariantMap& ) ) );
connect( this,
SIGNAL( parquetDownloadFinished( const QByteArray&, const QString&, const QString& ) ),
this,
SLOT( parquetDownloadComplete( const QByteArray&, const QString&, const QString& ) ) );
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::accessGranted()
{
2024-06-24 10:26:55 +02:00
RiaLogging::debug( "Access granted." );
2024-02-28 08:10:52 +01:00
m_token = m_osdu->token();
emit tokenReady( m_token );
}
2024-06-24 10:26:55 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::errorReceived( const QString& error, const QString& errorDescription, const QUrl& uri )
{
RiaLogging::debug( "OSDU Error Received: " + error + ". Description: " + errorDescription );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::authorizationCallbackReceived( const QVariantMap& data )
{
RiaLogging::debug( "Authorization callback received:" );
for ( const auto& [key, value] : data.toStdMap() )
{
RiaLogging::debug( " Key: " + key + " Value: " + value.toString() );
}
}
2024-02-28 08:10:52 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestToken()
{
if ( m_token.isEmpty() )
{
RiaLogging::debug( "Requesting token." );
m_osdu->grant();
}
else
{
RiaLogging::debug( "Has token: skipping token request." );
emit accessGranted();
}
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaOsduConnector::~RiaOsduConnector()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::clearCachedData()
{
QMutexLocker lock( &m_mutex );
m_fields.clear();
m_wells.clear();
m_wellbores.clear();
m_wellboreTrajectories.clear();
m_wellLogs.clear();
m_parquetData.clear();
m_parquetErrors.clear();
}
2024-02-28 08:10:52 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestFieldsByName( const QString& token, const QString& fieldName )
{
requestFieldsByName( m_server, m_dataPartitionId, token, fieldName );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestFieldsByName( const QString& fieldName )
{
requestFieldsByName( m_token, fieldName );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestFieldsByName( const QString& server, const QString& dataPartitionId, const QString& token, const QString& fieldName )
{
std::map<QString, QString> params;
params["kind"] = RiaDefines::osduFieldKind();
2024-02-28 08:10:52 +01:00
params["limit"] = "10000";
params["query"] = "data.FieldName:" + fieldName;
auto reply = makeSearchRequest( params, server, dataPartitionId, token );
2024-02-28 08:10:52 +01:00
connect( reply,
&QNetworkReply::finished,
2024-06-26 12:33:18 +02:00
[this, reply, fieldName]()
2024-02-28 08:10:52 +01:00
{
if ( reply->error() == QNetworkReply::NoError )
{
parseFields( reply );
}
2024-06-26 12:33:18 +02:00
else
{
QString errorMessage =
QString( "Download failed for fields by name (%1). Error: %2" ).arg( fieldName ).arg( reply->errorString() );
RiaLogging::error( errorMessage );
}
2024-02-28 08:10:52 +01:00
} );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellsByFieldId( const QString& fieldId )
{
requestWellsByFieldId( m_server, m_dataPartitionId, m_token, fieldId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellsByFieldId( const QString& server, const QString& dataPartitionId, const QString& token, const QString& fieldId )
{
std::map<QString, QString> params;
params["kind"] = RiaDefines::osduWellKind();
2024-02-28 08:10:52 +01:00
params["limit"] = "10000";
params["query"] = QString( "nested(data.GeoContexts, (FieldID:\"%1\"))" ).arg( fieldId );
auto reply = makeSearchRequest( params, server, dataPartitionId, token );
2024-02-28 08:10:52 +01:00
connect( reply,
&QNetworkReply::finished,
[this, reply, fieldId]()
{
if ( reply->error() == QNetworkReply::NoError )
{
parseWells( reply );
}
2024-06-26 12:33:18 +02:00
else
{
QString errorMessage =
QString( "Request failed for wells for field (%1). Error: %2" ).arg( fieldId ).arg( reply->errorString() );
RiaLogging::error( errorMessage );
}
2024-02-28 08:10:52 +01:00
} );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellboresByWellId( const QString& wellId )
{
requestWellboresByWellId( m_server, m_dataPartitionId, m_token, wellId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellboresByWellId( const QString& server, const QString& dataPartitionId, const QString& token, const QString& wellId )
{
std::map<QString, QString> params;
params["kind"] = RiaDefines::osduWellboreKind();
2024-02-28 08:10:52 +01:00
params["limit"] = "10000";
params["query"] = "data.WellID: \"" + wellId + "\"";
auto reply = makeSearchRequest( params, server, dataPartitionId, token );
2024-02-28 08:10:52 +01:00
connect( reply,
&QNetworkReply::finished,
[this, reply, wellId]()
{
if ( reply->error() == QNetworkReply::NoError )
{
parseWellbores( reply, wellId );
}
2024-06-26 12:33:18 +02:00
else
{
QString errorMessage =
QString( "Request failed for wellbores for well (%1). Error: %2" ).arg( wellId ).arg( reply->errorString() );
RiaLogging::error( errorMessage );
}
2024-02-28 08:10:52 +01:00
} );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduWellLog> RiaOsduConnector::requestWellLogsByWellboreIdBlocking( const QString& wellboreId )
2024-02-28 08:10:52 +01:00
{
QString token = requestTokenBlocking();
QEventLoop loop;
connect( this, SIGNAL( wellLogsFinished( const QString& ) ), &loop, SLOT( quit() ) );
requestWellLogsByWellboreId( m_server, m_dataPartitionId, token, wellboreId );
loop.exec();
return m_wellLogs[wellboreId];
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellLogsByWellboreId( const QString& wellboreId )
2024-02-28 08:10:52 +01:00
{
requestWellLogsByWellboreId( m_server, m_dataPartitionId, m_token, wellboreId );
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellLogsByWellboreId( const QString& server,
const QString& dataPartitionId,
const QString& token,
const QString& wellboreId )
2024-02-28 08:10:52 +01:00
{
std::map<QString, QString> params;
params["kind"] = RiaDefines::osduWellLogKind();
2024-02-28 08:10:52 +01:00
params["limit"] = "10000";
params["query"] = "data.WellboreID: \"" + wellboreId + "\"";
auto reply = makeSearchRequest( params, server, dataPartitionId, token );
2024-02-28 08:10:52 +01:00
connect( reply,
&QNetworkReply::finished,
[this, reply, wellboreId]()
{
if ( reply->error() == QNetworkReply::NoError )
{
parseWellLogs( reply, wellboreId );
2024-02-28 08:10:52 +01:00
}
2024-06-26 12:33:18 +02:00
else
{
QString errorMessage =
QString( "Request failed for well logs by wellbore (%1). Error: %2" ).arg( wellboreId ).arg( reply->errorString() );
RiaLogging::error( errorMessage );
}
2024-02-28 08:10:52 +01:00
} );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellboreTrajectoryByWellboreId( const QString& wellboreId )
{
requestWellboreTrajectoryByWellboreId( m_server, m_dataPartitionId, m_token, wellboreId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellboreTrajectoryByWellboreId( const QString& server,
const QString& dataPartitionId,
const QString& token,
const QString& wellboreId )
2024-02-28 08:10:52 +01:00
{
std::map<QString, QString> params;
params["kind"] = RiaDefines::osduWellboreTrajectoryKind();
params["limit"] = "10000";
params["query"] = "data.WellboreID: \"" + wellboreId + "\"";
auto reply = makeSearchRequest( params, server, dataPartitionId, token );
2024-02-28 08:10:52 +01:00
connect( reply,
&QNetworkReply::finished,
[this, reply, wellboreId]()
2024-02-28 08:10:52 +01:00
{
if ( reply->error() == QNetworkReply::NoError )
{
parseWellTrajectory( reply, wellboreId );
2024-02-28 08:10:52 +01:00
}
2024-06-26 12:33:18 +02:00
else
{
QString errorMessage =
QString( "Request failed for well trajectory by wellbore (%1). Error: %2" ).arg( wellboreId ).arg( reply->errorString() );
RiaLogging::error( errorMessage );
}
2024-02-28 08:10:52 +01:00
} );
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructSearchUrl( const QString& server )
{
return server + "/api/search/v2/query";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructFileDownloadUrl( const QString& server, const QString& fileId )
2024-02-28 08:10:52 +01:00
{
return server + "/api/file/v2/files/" + fileId + "/downloadURL";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructAuthUrl( const QString& authority )
{
return authority + "/oauth2/v2.0/authorize";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructTokenUrl( const QString& authority )
{
return authority + "/oauth2/v2.0/token";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructWellLogDownloadUrl( const QString& server, const QString& wellLogId )
{
return server + "/api/os-wellbore-ddms/ddms/v3/welllogs/" + wellLogId + "/data";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::constructWellboreTrajectoriesDownloadUrl( const QString& server, const QString& wellboreTrajectoryId )
{
return server + "/api/os-wellbore-ddms/ddms/v3/wellboretrajectories/" + wellboreTrajectoryId + "/data";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QNetworkReply* RiaOsduConnector::makeSearchRequest( const std::map<QString, QString>& parameters,
const QString& server,
const QString& dataPartitionId,
const QString& token )
2024-02-28 08:10:52 +01:00
{
QNetworkRequest m_networkRequest;
m_networkRequest.setUrl( QUrl( constructSearchUrl( server ) ) );
addStandardHeader( m_networkRequest, token, dataPartitionId, RiaDefines::contentTypeJson() );
2024-02-28 08:10:52 +01:00
QJsonObject obj;
for ( auto [key, value] : parameters )
{
obj.insert( key, value );
}
QJsonDocument doc( obj );
QString strJson( doc.toJson( QJsonDocument::Compact ) );
auto reply = m_networkAccessManager->post( m_networkRequest, strJson.toUtf8() );
return reply;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parseFields( QNetworkReply* reply )
{
QByteArray result = reply->readAll();
reply->deleteLater();
if ( reply->error() == QNetworkReply::NoError )
{
QJsonDocument doc = QJsonDocument::fromJson( result );
QJsonObject jsonObj = doc.object();
QJsonArray resultsArray = jsonObj["results"].toArray();
{
QMutexLocker lock( &m_mutex );
m_fields.clear();
for ( const QJsonValue& value : resultsArray )
{
QJsonObject resultObj = value.toObject();
QString id = resultObj["id"].toString();
QString kind = resultObj["kind"].toString();
QString fieldName = resultObj["data"].toObject()["FieldName"].toString();
m_fields.push_back( OsduField{ id, kind, fieldName } );
}
2024-06-26 12:33:18 +02:00
RiaLogging::debug( QString( "Found %1 fields." ).arg( m_fields.size() ) );
2024-02-28 08:10:52 +01:00
}
emit fieldsFinished();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parseWells( QNetworkReply* reply )
{
QByteArray result = reply->readAll();
reply->deleteLater();
if ( reply->error() == QNetworkReply::NoError )
{
QJsonDocument doc = QJsonDocument::fromJson( result );
QJsonObject jsonObj = doc.object();
QJsonArray resultsArray = jsonObj["results"].toArray();
{
QMutexLocker lock( &m_mutex );
m_wells.clear();
for ( const QJsonValue& value : resultsArray )
{
QJsonObject resultObj = value.toObject();
QString id = resultObj["id"].toString();
QString kind = resultObj["kind"].toString();
QString name = resultObj["data"].toObject()["FacilityName"].toString();
m_wells.push_back( OsduWell{ id, kind, name } );
}
2024-02-28 08:10:52 +01:00
}
emit wellsFinished();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parseWellbores( QNetworkReply* reply, const QString& wellId )
{
QByteArray result = reply->readAll();
reply->deleteLater();
if ( reply->error() == QNetworkReply::NoError )
{
QJsonDocument doc = QJsonDocument::fromJson( result );
QJsonObject jsonObj = doc.object();
QJsonArray resultsArray = jsonObj["results"].toArray();
{
QMutexLocker lock( &m_mutex );
m_wellbores[wellId].clear();
for ( const QJsonValue& value : resultsArray )
{
QJsonObject resultObj = value.toObject();
QString id = resultObj["id"].toString();
QString kind = resultObj["kind"].toString();
QString name = resultObj["data"].toObject()["FacilityName"].toString();
// Extract datum elevation. The DefaultVerticalMeasurementID is probably the datum elevation needed.
// Default to 0.0 if nothing is found, but finding nothing is suspicious.
double datumElevation = std::numeric_limits<double>::infinity();
QString defaultVerticalMeasurementId = resultObj["data"].toObject()["DefaultVerticalMeasurementID"].toString();
QJsonArray verticalMeasurementsArray = resultObj["data"].toObject()["VerticalMeasurements"].toArray();
for ( const QJsonValue& vma : verticalMeasurementsArray )
{
QString verticalMeasurementId = vma["VerticalMeasurementID"].toString();
if ( verticalMeasurementId == defaultVerticalMeasurementId )
{
double verticalMeasurement = vma["VerticalMeasurement"].toDouble( 0.0 );
datumElevation = verticalMeasurement;
}
}
if ( std::isinf( datumElevation ) )
{
RiaLogging::warning( QString( "Missing datum elevation for well bore '%1'. Id: %2" ).arg( name ).arg( id ) );
datumElevation = 0.0;
}
m_wellbores[wellId].push_back( OsduWellbore{ id, kind, name, wellId, datumElevation } );
}
2024-02-28 08:10:52 +01:00
}
emit wellboresFinished( wellId );
}
else
{
RiaLogging::error( "Failed to download well with id " + wellId + ": " + reply->errorString() );
}
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parseWellTrajectory( QNetworkReply* reply, const QString& wellboreId )
{
QByteArray result = reply->readAll();
reply->deleteLater();
if ( reply->error() == QNetworkReply::NoError )
{
QJsonDocument doc = QJsonDocument::fromJson( result );
QJsonObject jsonObj = doc.object();
QJsonArray resultsArray = jsonObj["results"].toArray();
{
QMutexLocker lock( &m_mutex );
m_wellboreTrajectories[wellboreId].clear();
for ( const QJsonValue& value : resultsArray )
{
QJsonObject resultObj = value.toObject();
QString id = resultObj["id"].toString();
QString kind = resultObj["kind"].toString();
m_wellboreTrajectories[wellboreId].push_back( OsduWellboreTrajectory{ id, kind, wellboreId } );
}
2024-02-28 08:10:52 +01:00
}
emit wellboreTrajectoryFinished( wellboreId, resultsArray.size(), "" );
}
else
{
emit wellboreTrajectoryFinished( wellboreId, 0, "Failed to download: " + reply->errorString() );
2024-02-28 08:10:52 +01:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parseWellLogs( QNetworkReply* reply, const QString& wellboreId )
2024-02-28 08:10:52 +01:00
{
QByteArray result = reply->readAll();
reply->deleteLater();
if ( reply->error() == QNetworkReply::NoError )
{
QJsonDocument doc = QJsonDocument::fromJson( result );
QJsonObject jsonObj = doc.object();
QJsonArray resultsArray = jsonObj["results"].toArray();
2024-02-28 08:10:52 +01:00
{
QMutexLocker lock( &m_mutex );
m_wellLogs[wellboreId].clear();
for ( const QJsonValue& value : resultsArray )
{
QJsonObject resultObj = value.toObject();
QString id = resultObj["id"].toString();
QString kind = resultObj["kind"].toString();
QJsonArray curvesArray = resultObj["data"].toObject()["Curves"].toArray();
QStringList curveIds;
RiaLogging::debug( QString( "Curves for '%1':" ).arg( id ) );
for ( const QJsonValue& curve : curvesArray )
{
QString curveId = curve["CurveID"].toString();
QString curveDescription = curve["CurveDescription"].toString();
double curveBaseDepth = curve["BaseDepth"].toDouble( 0.0 );
double curveTopDepth = curve["TopDepth"].toDouble( 0.0 );
curveIds << curveId;
RiaLogging::debug(
QString( "%1: '%2' (%3 - %4)" ).arg( curveId ).arg( curveDescription ).arg( curveTopDepth ).arg( curveBaseDepth ) );
}
QString name = curveIds.join( ", " );
m_wellLogs[wellboreId].push_back( OsduWellLog{ id, kind, wellboreId, name } );
}
}
2024-02-28 08:10:52 +01:00
emit wellLogsFinished( wellboreId );
2024-02-28 08:10:52 +01:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::addStandardHeader( QNetworkRequest& networkRequest,
const QString& token,
const QString& dataPartitionId,
const QString& contentType )
2024-02-28 08:10:52 +01:00
{
networkRequest.setHeader( QNetworkRequest::ContentTypeHeader, contentType );
2024-02-28 08:10:52 +01:00
networkRequest.setRawHeader( "Authorization", "Bearer " + token.toUtf8() );
networkRequest.setRawHeader( QByteArray( "Data-Partition-Id" ), dataPartitionId.toUtf8() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QNetworkReply*
RiaOsduConnector::makeDownloadRequest( const QString& url, const QString& dataPartitionId, const QString& token, const QString& contentType )
2024-02-28 08:10:52 +01:00
{
QNetworkRequest networkRequest;
networkRequest.setUrl( QUrl( url ) );
2024-02-28 08:10:52 +01:00
addStandardHeader( networkRequest, token, dataPartitionId, contentType );
2024-02-28 08:10:52 +01:00
auto reply = m_networkAccessManager->get( networkRequest );
2024-02-28 08:10:52 +01:00
return reply;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::server() const
{
return m_server;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::dataPartition() const
{
return m_dataPartitionId;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduField> RiaOsduConnector::fields() const
{
QMutexLocker lock( &m_mutex );
2024-02-28 08:10:52 +01:00
return m_fields;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduWell> RiaOsduConnector::wells() const
{
QMutexLocker lock( &m_mutex );
2024-02-28 08:10:52 +01:00
return m_wells;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduWellLog> RiaOsduConnector::wellLogs( const QString& wellboreId ) const
{
QMutexLocker lock( &m_mutex );
auto it = m_wellLogs.find( wellboreId );
if ( it != m_wellLogs.end() )
return it->second;
else
return {};
}
2024-02-28 08:10:52 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduWellbore> RiaOsduConnector::wellbores( const QString& wellId ) const
{
QMutexLocker lock( &m_mutex );
2024-02-28 08:10:52 +01:00
auto it = m_wellbores.find( wellId );
if ( it != m_wellbores.end() )
return it->second;
else
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::wellIdForWellboreId( const QString& wellboreId ) const
{
auto findWellIdForWellboreId = []( const std::vector<OsduWellbore>& wellbores, const QString& wellboreId )
{
auto it = std::find_if( wellbores.begin(), wellbores.end(), [wellboreId]( const OsduWellbore& w ) { return w.id == wellboreId; } );
if ( it != wellbores.end() )
return it->wellId;
else
return QString();
};
QMutexLocker lock( &m_mutex );
2024-02-28 08:10:52 +01:00
for ( auto [wellId, wellbores] : m_wellbores )
{
if ( auto res = findWellIdForWellboreId( wellbores, wellboreId ); !res.isEmpty() )
{
return wellId;
}
}
return QString();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<OsduWellboreTrajectory> RiaOsduConnector::wellboreTrajectories( const QString& wellboreId ) const
{
QMutexLocker lock( &m_mutex );
2024-02-28 08:10:52 +01:00
auto it = m_wellboreTrajectories.find( wellboreId );
if ( it != m_wellboreTrajectories.end() )
return it->second;
else
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellboreTrajectoryParquetDataById( const QString& wellboreTrajectoryId )
{
QString url = constructWellboreTrajectoriesDownloadUrl( m_server, wellboreTrajectoryId );
RiaLogging::debug( "Wellbore trajectory URL: " + url );
requestParquetDataByUrl( url, wellboreTrajectoryId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestWellLogParquetDataById( const QString& wellLogId )
{
QString url = constructWellLogDownloadUrl( m_server, wellLogId );
RiaLogging::debug( "Well log URL: " + url );
requestParquetDataByUrl( url, wellLogId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestParquetDataByUrl( const QString& url, const QString& id )
{
QString token = m_token;
requestParquetData( url, m_dataPartitionId, token, id );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<QByteArray, QString> RiaOsduConnector::requestWellboreTrajectoryParquetDataByIdBlocking( const QString& wellboreTrajectoryId )
2024-02-28 08:10:52 +01:00
{
QString url = constructWellboreTrajectoriesDownloadUrl( m_server, wellboreTrajectoryId );
RiaLogging::debug( "Wellbore trajectory URL: " + url );
return requestParquetDataByUrlBlocking( url, wellboreTrajectoryId );
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<QByteArray, QString> RiaOsduConnector::requestWellLogParquetDataByIdBlocking( const QString& wellLogId )
2024-02-28 08:10:52 +01:00
{
QString url = constructWellLogDownloadUrl( m_server, wellLogId );
RiaLogging::debug( "Well log URL: " + url );
return requestParquetDataByUrlBlocking( url, wellLogId );
2024-02-28 08:10:52 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<QByteArray, QString> RiaOsduConnector::requestParquetDataByUrlBlocking( const QString& url, const QString& id )
{
QString token = requestTokenBlocking();
QEventLoop loop;
connect( this, SIGNAL( parquetDownloadFinished( const QByteArray&, const QString&, const QString& ) ), &loop, SLOT( quit() ) );
requestParquetData( url, m_dataPartitionId, token, id );
loop.exec();
QMutexLocker lock( &m_mutex );
return { m_parquetData[id], m_parquetErrors[id] };
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::requestParquetData( const QString& url, const QString& dataPartitionId, const QString& token, const QString& id )
{
RiaLogging::info( "Requesting download of parquet from: " + url );
auto reply = makeDownloadRequest( url, dataPartitionId, token, RiaDefines::contentTypeParquet() );
connect( reply,
&QNetworkReply::finished,
[this, reply, url, id]()
{
if ( reply->error() == QNetworkReply::NoError )
{
QByteArray contents = reply->readAll();
RiaLogging::info( QString( "Download succeeded: %1 bytes." ).arg( contents.length() ) );
emit parquetDownloadFinished( contents, "", id );
}
else
{
2024-06-26 12:33:18 +02:00
QString errorMessage = "Request failed: " + url + " failed." + reply->errorString();
RiaLogging::error( errorMessage );
emit parquetDownloadFinished( QByteArray(), errorMessage, id );
}
} );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaOsduConnector::parquetDownloadComplete( const QByteArray& contents, const QString& errorMessage, const QString& id )
{
CAF_ASSERT( !id.isEmpty() );
QMutexLocker lock( &m_mutex );
m_parquetData[id] = contents;
m_parquetErrors[id] = errorMessage;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduConnector::requestTokenBlocking()
{
if ( m_token.isEmpty() )
{
QEventLoop loop;
connect( this, SIGNAL( tokenReady( const QString& ) ), &loop, SLOT( quit() ) );
requestToken();
loop.exec();
}
return m_token;
}