Skip to content

Commit 1b82fd6

Browse files
adding creation and modification timestamps to prep_templates (#3066)
* adding creation and modification timestamps to prep_templates * Apply suggestions from code review [skip ci] Co-authored-by: Yoshiki Vázquez Baeza <yoshiki@ucsd.edu> Co-authored-by: Yoshiki Vázquez Baeza <yoshiki@ucsd.edu>
1 parent e6e5d80 commit 1b82fd6

File tree

7 files changed

+218
-106
lines changed

7 files changed

+218
-106
lines changed

qiita_db/environment_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
431431
# for the test Study (1) so a lot of the tests actually expect this.
432432
# Now, trying to regenerate directly in the populate_test_db might
433433
# require too many dev hours so the easiest is just do it here
434-
# UPDATE 02/27/19: moving to 74.sql as we added the file sizes
435-
if test and sql_patch_filename == '74.sql':
434+
# UPDATE 01/25/2021: moving to 81.sql as we added timestamps to
435+
# prep info files
436+
if test and sql_patch_filename == '81.sql':
436437
qdb.study.Study(1).sample_template.generate_files()

qiita_db/metadata_template/prep_template.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# -----------------------------------------------------------------------------
88
from itertools import chain
99
from os.path import join
10-
from time import strftime
1110
from copy import deepcopy
1211
from skbio.util import find_duplicates
1312

@@ -507,11 +506,16 @@ def generate_files(self, samples=None, columns=None):
507506
with qdb.sql_connection.TRN:
508507
# figuring out the filepath of the prep template
509508
_id, fp = qdb.util.get_mountpoint('templates')[0]
509+
# update timestamp in the DB first
510+
qdb.sql_connection.TRN.add(
511+
"""UPDATE qiita.prep_template
512+
SET modification_timestamp = CURRENT_TIMESTAMP
513+
WHERE prep_template_id = %s""", [self._id])
514+
ctime = self.modification_timestamp
510515
fp = join(fp, '%d_prep_%d_%s.txt' % (self.study_id, self._id,
511-
strftime("%Y%m%d-%H%M%S")))
516+
ctime.strftime("%Y%m%d-%H%M%S")))
512517
# storing the template
513518
self.to_file(fp)
514-
515519
# adding the fp to the object
516520
fp_id = qdb.util.convert_to_id("prep_template", "filepath_type")
517521
self.add_filepath(fp, fp_id=fp_id)
@@ -666,3 +670,35 @@ def to_dataframe(self, add_ebi_accessions=False):
666670
lambda sid: accessions[sid])
667671

668672
return df
673+
674+
@property
675+
def creation_timestamp(self):
676+
"""The creation timestamp of the prep information
677+
678+
Returns
679+
-------
680+
datetime.datetime
681+
The creation timestamp of the prep information
682+
"""
683+
with qdb.sql_connection.TRN:
684+
sql = """SELECT creation_timestamp
685+
FROM qiita.prep_template
686+
WHERE prep_template_id = %s"""
687+
qdb.sql_connection.TRN.add(sql, [self.id])
688+
return qdb.sql_connection.TRN.execute_fetchlast()
689+
690+
@property
691+
def modification_timestamp(self):
692+
"""The modification timestamp of the prep information
693+
694+
Returns
695+
-------
696+
datetime.datetime
697+
The modification timestamp of the prep information
698+
"""
699+
with qdb.sql_connection.TRN:
700+
sql = """SELECT modification_timestamp
701+
FROM qiita.prep_template
702+
WHERE prep_template_id = %s"""
703+
qdb.sql_connection.TRN.add(sql, [self.id])
704+
return qdb.sql_connection.TRN.execute_fetchlast()

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from os.path import join, exists
1212
from collections import Iterable
1313
from copy import deepcopy
14+
from datetime import datetime
1415

1516
import numpy.testing as npt
1617
import pandas as pd
@@ -978,7 +979,14 @@ def test_validate_restrictions(self):
978979
self.assertFalse(success)
979980

980981
metadata['target_gene'] = '16S rRNA'
982+
# as we are testing the update functionality of a prep info file, we
983+
# can also test that the timestamps are working correctly
984+
current_ct = pt.creation_timestamp
985+
current_mt = pt.modification_timestamp
986+
self.assertTrue(current_ct < current_mt)
981987
pt.update(metadata)
988+
self.assertEqual(current_ct, pt.creation_timestamp)
989+
self.assertTrue(current_mt < pt.modification_timestamp)
982990
success, message = pt.validate_restrictions()
983991
success, message = pt.validate_restrictions()
984992
self.assertEqual(message, '')
@@ -994,6 +1002,12 @@ def test_create(self):
9941002
self.metadata, self.test_study, self.data_type,
9951003
name='New Prep For Test')
9961004
self._common_creation_checks(pt, fp_count, "New Prep For Test")
1005+
# checking that the creation and modification timestamps are within
1006+
# 2 seconds of current time
1007+
dsecs = (datetime.now() - pt.modification_timestamp).total_seconds()
1008+
self.assertTrue(dsecs < 2)
1009+
dsecs = (datetime.now() - pt.creation_timestamp).total_seconds()
1010+
self.assertTrue(dsecs < 2)
9971011
# cleaning
9981012
qdb.metadata_template.prep_template.PrepTemplate.delete(pt.id)
9991013

qiita_db/support_files/patches/81.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Jan 25, 2021
2+
-- Add creation_timestamp and modification_timestamp for qiita.prep_template
3+
4+
ALTER TABLE qiita.prep_template ADD creation_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
5+
ALTER TABLE qiita.prep_template ADD modification_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from os.path import basename
2+
3+
from qiita_db.sql_connection import TRN
4+
from qiita_db.study import Study
5+
6+
7+
for study in Study.iter():
8+
for pt in study.prep_templates():
9+
filepaths = pt.get_filepaths()
10+
if filepaths:
11+
# filepaths are returned in order so we can take the
12+
# oldest and newest; then we get the filename and parse the
13+
# creation time. Note that the filename comes in one of these
14+
# formats: 1_prep_1_qiime_19700101-000000.txt or
15+
# 1_prep_1_19700101-000000.txt
16+
oldest = basename(filepaths[-1][1])[-19:-4].replace('-', ' ')
17+
newest = basename(filepaths[0][1])[-19:-4].replace('-', ' ')
18+
19+
with TRN:
20+
sql = """UPDATE qiita.prep_template
21+
SET creation_timestamp = %s,
22+
modification_timestamp = %s
23+
WHERE prep_template_id = %s"""
24+
TRN.add(sql, [oldest, newest, pt.id])
25+
TRN.execute()

qiita_db/support_files/qiita-db.dbs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,12 @@
939939
<column name="deprecated" type="boolean" jt="-7" >
940940
<defo><![CDATA[FALSE]]></defo>
941941
</column>
942+
<column name="creation_timestamp" type="timestamp" jt="-9" >
943+
<defo><![CDATA[FALSE]]></defo>
944+
</column>
945+
<column name="modification_timestamp" type="timestamp" jt="-11" >
946+
<defo><![CDATA[FALSE]]></defo>
947+
</column>
942948
<index name="pk_prep_template" unique="PRIMARY_KEY" >
943949
<column name="prep_template_id" />
944950
</index>
@@ -1889,4 +1895,4 @@ ALTER TABLE oauth_software ADD CONSTRAINT fk_oauth_software FOREIGN KEY ( client
18891895
]]></string>
18901896
</script>
18911897
</layout>
1892-
</project>
1898+
</project>

0 commit comments

Comments
 (0)