Skip to content

Commit

Permalink
YDA-5698: add modified date option GL report
Browse files Browse the repository at this point in the history
Add option to show last modified date of research
and vault collections to the group lifecycle report.
  • Loading branch information
stsnel committed May 24, 2024
1 parent a43f1fe commit c1e3243
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Group lifecycle report: add options to show size and last modified date of research
and vault groups
- Fix prompt for password if no .irodsA file is available

## 2024-05-01 v1.1.2
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,23 @@ optional arguments:
### yreport\_grouplifecycle
```
usage: yreport_grouplifecycle [-h] [-q] [-s] [-y {1.7,1.8,1.9,1.10}]
usage: yreport_grouplifecycle [-h] [-q] [-s] [-m] [-y {1.7,1.8,1.9,1.10}]

Generates a list of research groups, along with their creation date,
expiration date (if available), lists of group managers, regular members, and
readonly members. The report also shows whether each research compartment
contains data, as well as whether its vault compartment contains data.
contains data, as well as whether its vault compartment contains data. The
report can optionally include size and last modified date of both the research
and vault collection.

optional arguments:
-h, --help show this help message and exit
-q, --quasi-xml Enable Quasi-XML parser in order to be able to parse
characters not supported by regular XML parser
-s, --size Include size of research collection and vault
collection in output
-m, --modified Include last modified date research collection and
vault collection in output
-y {1.7,1.8,1.9,1.10}, --yoda-version {1.7,1.8,1.9,1.10}
Override Yoda version on the server
```
Expand Down
42 changes: 41 additions & 1 deletion yclienttools/common_queries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from itertools import chain
import os
from typing import Dict
from typing import Dict, Union

from irods.column import Like
from irods.models import Collection, DataObject, Resource, User, UserGroup
Expand Down Expand Up @@ -97,6 +98,45 @@ def get_collection_size(session: iRODSSession,
return result


def get_collection_contents_last_modified(session: iRODSSession, collection_name: str) -> Union[None, datetime.datetime]:
"""Returns datetime of last modification of collection or its contents
:param session: iRODS session
:param collection_name: collection name
:returns: datetime of last modification of the collection or its contents
(data objects or subcollections, recursively), or None if no last
modified datetime could be determined
"""
last_timestamp = None

dataobjects_root = (session.query(DataObject.modify_time)
.filter(Collection.name == collection_name)
.get_results())
dataobjects_sub = (session.query(DataObject.modify_time)
.filter(Like(Collection.name, collection_name + "/%"))
.get_results())
collection_root = (session.query(Collection.modify_time)
.filter(Collection.name == collection_name)
.get_results())
collections_sub = (session.query(Collection.modify_time)
.filter(Like(Collection.name, collection_name + "/%"))
.get_results())

all_collection_data = chain(collection_root, collections_sub)
all_dataobject_data = chain(dataobjects_root, dataobjects_sub)

for collection in all_collection_data:
if last_timestamp is None or collection[Collection.modify_time] > last_timestamp:
last_timestamp = collection[Collection.modify_time]

for dataobject in all_dataobject_data:
if last_timestamp is None or dataobject[DataObject.modify_time] > last_timestamp:
last_timestamp = dataobject[DataObject.modify_time]

return last_timestamp


def get_revision_collection_name(session, collection_name):
'''Returns the revision collection name of a collection if it exists, otherwise None. '''
expected_prefix = "/{}/home/".format(session.zone)
Expand Down
24 changes: 22 additions & 2 deletions yclienttools/reportgrouplifecycle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'''Generates a list of research groups, along with their creation date, expiration date (if available),
lists of group managers, regular members, and readonly members. The report also shows whether each
research compartment contains data, as well as whether its vault compartment contains data.'''
research compartment contains data, as well as whether its vault compartment contains data.
The report can optionally include size and last modified date of both the research and
vault collection.
'''

import argparse
import csv
Expand All @@ -15,7 +19,7 @@
from irods.models import Collection, DataObject, User
from irods.session import iRODSSession
from yclienttools import common_args, common_config, session as s
from yclienttools.common_queries import collection_exists, get_collection_size
from yclienttools.common_queries import collection_exists, get_collection_contents_last_modified, get_collection_size
from yclienttools.options import GroupByOption


Expand All @@ -41,6 +45,8 @@ def _get_args() -> argparse.Namespace:
help='Enable Quasi-XML parser in order to be able to parse characters not supported by regular XML parser')
parser.add_argument("-s", "--size", default=False, action='store_true',
help='Include size of research collection and vault collection in output')
parser.add_argument("-m", "--modified", default=False, action='store_true',
help='Include last modified date research collection and vault collection in output')
common_args.add_default_args(parser)
return parser.parse_args()

Expand Down Expand Up @@ -182,6 +188,10 @@ def _get_columns(args: argparse.Namespace) -> List[str]:
else:
extra_cols = []

if args.modified:
extra_cols.append("Research last modified")
extra_cols.append("Vault last modified")

result = base_cols
result.extend(extra_cols)
return result
Expand All @@ -201,6 +211,10 @@ def _size_to_str(value: Union[int, None]) -> str:
return humanize.naturalsize(value, binary=True)


def _timestamp_to_date_str(value: Union[datetime.datetime, None]) -> str:
return "N/A" if value is None else value.strftime("%Y-%m-%d")


def report_groups_lifecycle(args: argparse.Namespace, session: iRODSSession):
output = csv.writer(sys.stdout, delimiter=',')
output.writerow(_get_columns(args))
Expand Down Expand Up @@ -234,4 +248,10 @@ def _has_data_to_string(value):
rowdata.append(_size_to_str(_get_research_size(session, group)))
rowdata.append(_size_to_str(_get_vault_size(session, group)))

if args.modified:
rowdata.append(_timestamp_to_date_str(
get_collection_contents_last_modified(session, _get_research_group_collection(session, group))))
rowdata.append(_timestamp_to_date_str(
get_collection_contents_last_modified(session, _get_vault_group_collection(session, group))))

output.writerow(rowdata)

0 comments on commit c1e3243

Please sign in to comment.