Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Coscine
backend
apis
Project
Commits
196125cd
Commit
196125cd
authored
Mar 26, 2021
by
Marcel Nellesen
Browse files
Update: Extending the Analytics Log
parent
82966f15
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/Project/Controllers/ProjectController.cs
View file @
196125cd
...
...
@@ -38,7 +38,7 @@ namespace Coscine.Api.Project.Controllers
private
readonly
ResourceTypeModel
_resourceTypeModel
;
private
readonly
ResourceModel
_resourceModel
;
private
readonly
CoscineLogger
_coscineLogger
;
private
readonly
AnalyticsLogObject
_analyticsLogObject
;
private
readonly
VisibilityModel
_visibilityModel
;
private
readonly
int
_maxAvailable
=
100
;
/// <summary>
...
...
@@ -57,7 +57,7 @@ namespace Coscine.Api.Project.Controllers
_resourceModel
=
new
ResourceModel
();
_projectQuotaModel
=
new
ProjectQuotaModel
();
_coscineLogger
=
new
CoscineLogger
(
logger
);
_
analyticsLogObject
=
new
AnalyticsLogObject
();
_
visibilityModel
=
new
VisibilityModel
();
}
/// <summary>
...
...
@@ -72,11 +72,6 @@ namespace Coscine.Api.Project.Controllers
.
Select
((
project
)
=>
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
project
))
.
OrderBy
(
element
=>
element
.
DisplayName
);
if
(
Request
.
Query
!=
null
&&
Request
.
Query
[
"noanalyticslog"
]
!=
"true"
)
{
LogAnalytics
(
"List Projects"
,
result
);
}
return
Ok
(
result
);
}
...
...
@@ -88,16 +83,16 @@ namespace Coscine.Api.Project.Controllers
public
ActionResult
<
IEnumerable
<
ProjectObject
>>
GetTopLevelProjects
()
{
var
user
=
_authenticator
.
GetUser
();
var
result
=
_projectModel
.
GetTopLevelWithAccess
(
user
,
UserRoles
.
Member
,
UserRoles
.
Owner
).
ToList
()
var
projects
=
_projectModel
.
GetTopLevelWithAccess
(
user
,
UserRoles
.
Member
,
UserRoles
.
Owner
).
ToList
()
.
Select
((
project
)
=>
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
project
))
.
OrderBy
(
element
=>
element
.
DisplayName
);
if
(
Request
.
Query
!=
null
&&
Request
.
Query
[
"noanalyticslog"
]
!=
"true"
)
{
LogAnalytics
(
"
View
Home
"
,
result
);
LogAnalyticsViewHome
(
projects
.
Select
(
x
=>
x
.
Id
.
ToString
()).
ToList
()
);
}
return
Ok
(
result
);
return
Ok
(
projects
);
}
/// <summary>
...
...
@@ -114,7 +109,7 @@ namespace Coscine.Api.Project.Controllers
{
SubProjectModel
subProjectModel
=
new
SubProjectModel
();
var
subProjectRel
=
subProjectModel
.
GetAllWhere
((
subProject
)
=>
subProject
.
SubProjectId
==
project
.
Id
&&
project
.
Deleted
==
false
);
var
parentProjectRelation
=
subProjectRel
.
FirstOrDefault
();
if
(
parentProjectRelation
!=
null
&&
_projectModel
.
HasAccess
(
user
,
parentProjectRelation
.
ProjectId
,
UserRoles
.
Member
,
UserRoles
.
Owner
))
{
...
...
@@ -153,7 +148,8 @@ namespace Coscine.Api.Project.Controllers
}).
OrderBy
(
element
=>
element
.
DisplayName
);
if
(
Request
.
Query
!=
null
&&
Request
.
Query
[
"noanalyticslog"
]
!=
"true"
)
{
LogAnalytics
(
"View Project"
,
null
,
resources
,
id
,
user
);
// intentionally log as view project to help identify the related user action
var
projectObject
=
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
_projectModel
.
GetById
(
project
.
Id
));
LogAnalyticsViewProject
(
project
,
resources
.
ToList
(),
projectObject
.
Disciplines
,
projectObject
.
Organizations
,
user
);
}
return
Json
(
resources
);
}
...
...
@@ -190,26 +186,23 @@ namespace Coscine.Api.Project.Controllers
return
Unauthorized
(
"The user is not authorized to perform a get on the selected project!"
);
}
var
projectQuotas
=
_projectQuotaModel
.
GetAllWhere
((
projectQuota
)
=>
projectQuota
.
ProjectId
==
projectGuid
&&
projectQuota
.
ResourceType
.
Enabled
==
true
);
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
x
.
Enabled
.
HasValue
&&
x
.
Enabled
.
Value
);
return
Json
(
resourceTypes
.
Select
(
x
=>
{
var
projectQuota
=
_projectQuotaModel
.
GetWhere
((
y
)
=>
y
.
ProjectId
==
projectGuid
&&
y
.
ResourceTypeId
==
x
.
Id
);
return
new
ProjectQuotaReturnObject
{
Id
=
x
.
Id
,
Name
=
x
.
DisplayName
,
Used
=
CalculateUsed
(
x
,
projectGuid
),
Allocated
=
projectQuota
==
null
?
0
:
projectQuota
.
Quota
};
}));
return
Json
(
resourceTypes
.
Select
(
x
=>
CreateProjectQuotaReturnObject
(
x
,
projectGuid
)));
}
private
ProjectQuotaReturnObject
CreateProjectQuotaReturnObject
(
ResourceType
x
,
Guid
projectGuid
)
{
var
projectQuota
=
_projectQuotaModel
.
GetWhere
((
y
)
=>
y
.
ProjectId
==
projectGuid
&&
y
.
ResourceTypeId
==
x
.
Id
);
return
new
ProjectQuotaReturnObject
{
Id
=
x
.
Id
,
Name
=
x
.
DisplayName
,
Used
=
CalculateUsed
(
x
,
projectGuid
),
Allocated
=
projectQuota
==
null
?
0
:
projectQuota
.
Quota
};
}
private
int
CalculateUsed
(
ResourceType
resourceType
,
Guid
projectId
)
...
...
@@ -271,12 +264,13 @@ namespace Coscine.Api.Project.Controllers
x
.
ProjectId
==
projectGuid
&&
x
.
ResourceTypeId
==
resourceTypeGuid
);
var
projectQuotaReturnObject
=
new
ProjectQuotaReturnObject
{
Id
=
resourceTypeGuid
,
Name
=
resourceType
.
DisplayName
,
Used
=
CalculateUsed
(
resourceType
,
projectGuid
),
Allocated
=
projectQuota
.
Quota
};
var
projectQuotaReturnObject
=
new
ProjectQuotaReturnObject
{
Id
=
resourceTypeGuid
,
Name
=
resourceType
.
DisplayName
,
Used
=
CalculateUsed
(
resourceType
,
projectGuid
),
Allocated
=
projectQuota
.
Quota
};
return
Json
(
projectQuotaReturnObject
);
}
...
...
@@ -332,7 +326,7 @@ namespace Coscine.Api.Project.Controllers
/// <param name="updateProjectQuotaObject">Object containing the update values.</param>
/// <returns>NoContent (204).</returns>
[
HttpPost
(
"[controller]/{projectId}/quota/{resourceTypeId}"
)]
public
IActionResult
UpdateQuota
(
string
projectId
,
string
resourceTypeId
,
[
FromBody
]
UpdateProjectQuotaObject
updateProjectQuotaObject
)
public
IActionResult
UpdateQuota
(
string
projectId
,
string
resourceTypeId
,
[
FromBody
]
UpdateProjectQuotaObject
updateProjectQuotaObject
)
{
var
user
=
_authenticator
.
GetUser
();
...
...
@@ -373,12 +367,12 @@ namespace Coscine.Api.Project.Controllers
var
projectQuotaForCurrent
=
_projectQuotaModel
.
GetWhere
(
x
=>
x
.
ProjectId
==
projectGuid
&&
x
.
ResourceTypeId
==
resourceTypeGuid
);
var
used
=
CalculateUsed
(
resourceType
,
projectGuid
);
if
(
used
>
updateProjectQuotaObject
.
Allocated
)
if
(
used
>
updateProjectQuotaObject
.
Allocated
)
{
return
BadRequest
(
$"Cannot set quota (
{
updateProjectQuotaObject
.
Allocated
}
) below the used value (
{
used
}
)."
);
}
if
(
updateProjectQuotaObject
.
Allocated
>
_maxAvailable
)
if
(
updateProjectQuotaObject
.
Allocated
>
_maxAvailable
)
{
return
BadRequest
(
$"Cannot set quota to
{
updateProjectQuotaObject
.
Allocated
}
. It would exceed the limit of
{
_maxAvailable
}
"
);
}
...
...
@@ -402,7 +396,7 @@ namespace Coscine.Api.Project.Controllers
var
project
=
_projectModel
.
GetById
(
Guid
.
Parse
(
id
));
if
(
_projectModel
.
HasAccess
(
user
,
project
,
UserRoles
.
Owner
))
{
LogAnalytics
(
"
Edit
Project
"
,
null
,
null
,
id
,
user
);
LogAnalyticsEditProject
(
project
,
_projectModel
.
GetMetadataCompleteness
(
projectObject
),
projectObject
.
Disciplines
,
projectObject
.
Organizations
,
user
);
return
Ok
(
_projectModel
.
UpdateByObject
(
project
,
projectObject
));
}
else
...
...
@@ -423,7 +417,8 @@ namespace Coscine.Api.Project.Controllers
var
project
=
_projectModel
.
GetById
(
Guid
.
Parse
(
id
));
if
(
_projectModel
.
HasAccess
(
user
,
project
,
UserRoles
.
Owner
))
{
LogAnalytics
(
"Delete Project"
,
null
,
null
,
id
,
user
);
var
projectObject
=
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
_projectModel
.
GetById
(
project
.
Id
));
LogAnalyticsDeleteProject
(
project
,
projectObject
.
Disciplines
,
projectObject
.
Organizations
,
user
);
DeleteProject
(
project
);
return
Json
(
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
project
));
}
...
...
@@ -498,7 +493,7 @@ namespace Coscine.Api.Project.Controllers
var
projectInstituteModel
=
new
ProjectInstituteModel
();
foreach
(
var
projectInstitute
in
projectInstituteModel
.
GetAllWhere
((
projectInstitute
)
=>
projectInstitute
.
ProjectId
==
project
.
Id
))
{
projectInstituteModel
.
Delete
(
projectInstitute
);
projectInstituteModel
.
Delete
(
projectInstitute
);
}
foreach
(
var
projectQuota
in
_projectQuotaModel
.
GetAllWhere
((
Quota
)
=>
Quota
.
ProjectId
==
project
.
Id
))
...
...
@@ -557,7 +552,7 @@ namespace Coscine.Api.Project.Controllers
ProjectOwner
=
user
});
LogAnalytics
(
"
Add
Project
"
,
null
,
null
,
project
.
Id
.
ToString
()
,
user
);
LogAnalyticsAddProject
(
project
,
_projectModel
.
GetMetadataCompleteness
(
projectObject
),
projectObject
.
Disciplines
,
projectObject
.
Organizations
,
user
);
return
Json
(
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
project
));
}
...
...
@@ -570,67 +565,111 @@ namespace Coscine.Api.Project.Controllers
private
bool
IsRWTHMember
(
User
user
)
{
var
externalIds
=
new
ExternalIdModel
().
GetAllWhere
((
externalId
)
=>
externalId
.
UserId
==
user
.
Id
);
if
(
externalIds
.
Count
()
==
0
)
if
(!
externalIds
.
Any
()
)
{
return
false
;
}
var
externalIdList
=
new
List
<
string
>();
foreach
(
var
externalId
in
externalIds
)
{
externalIdList
.
Add
(
externalId
.
ExternalId1
);
}
return
new
RdfStoreConnector
(
Program
.
Configuration
.
GetStringAndWait
(
"coscine/local/virtuoso/additional/url"
)).
GetTriples
(
new
Uri
(
"https://ror.org/04xfq0f34"
),
null
,
null
,
1
,
externalIdList
).
Count
()
!=
0
;
return
new
RdfStoreConnector
(
Program
.
Configuration
.
GetStringAndWait
(
"coscine/local/virtuoso/additional/url"
)).
GetTriples
(
new
Uri
(
"https://ror.org/04xfq0f34"
),
null
,
null
,
1
,
externalIdList
).
Any
()
;
}
/// <summary>
/// LogAnalytics
/// </summary>
/// <param name="operation">Operation</param>
/// <param name="projects">Projects</param>
/// <param name="resources">Resources</param>
/// <param name="projectId">Id of the project</param>
/// <param name="user">User object</param>
private
void
LogAnalytics
(
string
operation
,
IEnumerable
<
ProjectObject
>
projects
=
null
,
IEnumerable
<
ResourceObject
>
resources
=
null
,
string
projectId
=
null
,
User
user
=
null
)
private
void
LogAnalyticsViewHome
(
List
<
string
>
projectIds
)
{
if
(
CoscineLoggerConfiguration
.
IsLogLevelActivated
(
LogType
.
Analytics
))
{
_analyticsLogObject
.
Type
=
"Action"
;
_analyticsLogObject
.
Operation
=
operation
;
_coscineLogger
.
AnalyticsLog
(
new
AnalyticsLogObject
{
Type
=
"Action"
,
Operation
=
"View Home"
,
ProjectList
=
projectIds
});
}
private
void
LogAnalyticsViewProject
(
Database
.
DataModel
.
Project
project
,
List
<
ResourceObject
>
resources
,
IEnumerable
<
DisciplineObject
>
disciplines
,
IEnumerable
<
OrganizationObject
>
organizations
,
User
user
)
{
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
x
.
Enabled
.
HasValue
&&
x
.
Enabled
.
Value
);
var
objects
=
resourceTypes
.
Select
(
x
=>
CreateProjectQuotaReturnObject
(
x
,
project
.
Id
));
if
(
projects
!=
null
)
_coscineLogger
.
AnalyticsLog
(
new
AnalyticsLogObject
{
List
<
string
>
projectList
=
new
List
<
string
>();
foreach
(
var
entry
in
projects
)
{
projectList
.
Add
(
entry
.
Id
.
ToString
());
}
_analyticsLogObject
.
ProjectList
=
projectList
;
}
if
(
resources
!=
null
)
Type
=
"Action"
,
Operation
=
"View Project"
,
RoleId
=
_projectRoleModel
.
GetGetUserRoleForProject
(
project
.
Id
,
user
.
Id
).
ToString
(),
ProjectId
=
project
.
Id
.
ToString
(),
QuotaSize
=
objects
.
Select
(
x
=>
$"
{
x
.
Name
}
:
{
x
.
Used
}
/
{
x
.
Allocated
}
"
).
ToList
(),
Disciplines
=
disciplines
.
Select
(
x
=>
x
.
DisplayNameEn
).
ToList
(),
Organizations
=
organizations
.
Select
(
x
=>
x
.
DisplayName
).
ToList
(),
Visibility
=
project
.
VisibilityId
.
HasValue
?
_visibilityModel
.
GetById
(
project
.
VisibilityId
.
Value
)?.
DisplayName
:
null
,
ResourceList
=
resources
.
Select
(
x
=>
x
.
Id
.
ToString
()).
ToList
(),
});
}
private
void
LogAnalyticsEditProject
(
Database
.
DataModel
.
Project
project
,
string
metadataCompletness
,
IEnumerable
<
DisciplineObject
>
disciplines
,
IEnumerable
<
OrganizationObject
>
organizations
,
User
user
)
{
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
x
.
Enabled
.
HasValue
&&
x
.
Enabled
.
Value
);
var
objects
=
resourceTypes
.
Select
(
x
=>
CreateProjectQuotaReturnObject
(
x
,
project
.
Id
));
_coscineLogger
.
AnalyticsLog
(
new
AnalyticsLogObject
{
List
<
string
>
shownResources
=
new
List
<
string
>();
foreach
(
var
entry
in
resources
)
{
shownResources
.
Add
(
entry
.
Id
.
ToString
());
}
_analyticsLogObject
.
ResourceList
=
shownResources
;
}
if
(
projectId
!=
null
)
Type
=
"Action"
,
Operation
=
"Edit Project"
,
RoleId
=
_projectRoleModel
.
GetGetUserRoleForProject
(
project
.
Id
,
user
.
Id
).
ToString
(),
ProjectId
=
project
.
Id
.
ToString
(),
QuotaSize
=
objects
.
Select
(
x
=>
$"
{
x
.
Name
}
:
{
x
.
Used
}
/
{
x
.
Allocated
}
"
).
ToList
(),
MetadataCompleteness
=
metadataCompletness
,
Disciplines
=
disciplines
.
Select
(
x
=>
x
.
DisplayNameEn
).
ToList
(),
Organizations
=
organizations
.
Select
(
x
=>
x
.
DisplayName
).
ToList
(),
Visibility
=
project
.
VisibilityId
.
HasValue
?
_visibilityModel
.
GetById
(
project
.
VisibilityId
.
Value
)?.
DisplayName
:
null
,
});
}
private
void
LogAnalyticsAddProject
(
Database
.
DataModel
.
Project
project
,
string
metadataCompletness
,
IEnumerable
<
DisciplineObject
>
disciplines
,
IEnumerable
<
OrganizationObject
>
organizations
,
User
user
)
{
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
x
.
Enabled
.
HasValue
&&
x
.
Enabled
.
Value
);
var
objects
=
resourceTypes
.
Select
(
x
=>
CreateProjectQuotaReturnObject
(
x
,
project
.
Id
));
_coscineLogger
.
AnalyticsLog
(
new
AnalyticsLogObject
{
_analyticsLogObject
.
ProjectId
=
projectId
;
if
(
user
!=
null
)
{
_analyticsLogObject
.
RoleId
=
_projectRoleModel
.
GetGetUserRoleForProject
(
new
Guid
(
_analyticsLogObject
.
ProjectId
),
user
.
Id
).
ToString
();
}
}
_coscineLogger
.
AnalyticsLog
(
_analyticsLogObject
);
}
Type
=
"Action"
,
Operation
=
"Add Project"
,
RoleId
=
_projectRoleModel
.
GetGetUserRoleForProject
(
project
.
Id
,
user
.
Id
).
ToString
(),
ProjectId
=
project
.
Id
.
ToString
(),
QuotaSize
=
objects
.
Select
(
x
=>
$"
{
x
.
Name
}
:
{
x
.
Used
}
/
{
x
.
Allocated
}
"
).
ToList
(),
MetadataCompleteness
=
metadataCompletness
,
Disciplines
=
disciplines
.
Select
(
x
=>
x
.
DisplayNameEn
).
ToList
(),
Organizations
=
organizations
.
Select
(
x
=>
x
.
DisplayName
).
ToList
(),
Visibility
=
project
.
VisibilityId
.
HasValue
?
_visibilityModel
.
GetById
(
project
.
VisibilityId
.
Value
)?.
DisplayName
:
null
,
});
}
private
void
LogAnalyticsDeleteProject
(
Database
.
DataModel
.
Project
project
,
IEnumerable
<
DisciplineObject
>
disciplines
,
IEnumerable
<
OrganizationObject
>
organizations
,
User
user
)
{
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
x
.
Enabled
.
HasValue
&&
x
.
Enabled
.
Value
);
var
objects
=
resourceTypes
.
Select
(
x
=>
CreateProjectQuotaReturnObject
(
x
,
project
.
Id
));
_coscineLogger
.
AnalyticsLog
(
new
AnalyticsLogObject
{
Type
=
"Action"
,
Operation
=
"Delete Project"
,
RoleId
=
_projectRoleModel
.
GetGetUserRoleForProject
(
project
.
Id
,
user
.
Id
).
ToString
(),
ProjectId
=
project
.
Id
.
ToString
(),
QuotaSize
=
objects
.
Select
(
x
=>
$"
{
x
.
Name
}
:
{
x
.
Used
}
/
{
x
.
Allocated
}
"
).
ToList
(),
Disciplines
=
disciplines
.
Select
(
x
=>
x
.
DisplayNameEn
).
ToList
(),
Organizations
=
organizations
.
Select
(
x
=>
x
.
DisplayName
).
ToList
(),
Visibility
=
project
.
VisibilityId
.
HasValue
?
_visibilityModel
.
GetById
(
project
.
VisibilityId
.
Value
)?.
DisplayName
:
null
,
});
}
}
}
src/Project/Project.csproj
View file @
196125cd
...
...
@@ -19,6 +19,8 @@
<ItemGroup>
<PackageReference Include="Coscine.Action" Version="2.*-*" />
<PackageReference Include="Coscine.ApiCommons" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.*-*" />
<PackageReference Include="Coscine.Logging" Version="2.*-*" />
<PackageReference Include="Coscine.Metadata" Version="2.*-*" />
<PackageReference Include="Coscine.ResourceLoader" Version="2.*-*" />
</ItemGroup>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment