188 def generate(map_blobs=False, class_name=None):
190 Base = declarative_base()
191 schema = {
"schema" :
"CMS_CONDITIONS"}
192 fk_schema_prefix = (
"%s." % schema[
"schema"])
if schema
else ""
195 __table_args__ = schema
196 __tablename__ =
'GLOBAL_TAG'
198 headers = [
"name",
"validity",
"description",
"release",
"insertion_time",
"snapshot_time",
"scenario",
"workflow",
"type"]
200 name = Column(String(100), unique=
True, nullable=
False, primary_key=
True)
201 validity = Column(Integer, nullable=
False)
202 description = Column(String(4000), nullable=
False)
203 release = Column(String(100), nullable=
False)
204 insertion_time = Column(DateTime, nullable=
False)
205 snapshot_time = Column(DateTime, nullable=
False)
206 scenario = Column(String(100))
207 workflow = Column(String(100))
208 type = Column(String(1))
209 tag_map = relationship(
'GlobalTagMap', backref=
'global_tag')
211 def __init__(self, dictionary={}, convert_timestamps=True):
213 for key
in dictionary:
215 if convert_timestamps:
218 self.__dict__[key] = dictionary[key]
219 except KeyError
as k:
223 return '<GlobalTag %r>' % self.name
225 def as_dicts(self, convert_timestamps=False):
227 Returns dictionary form of Global Tag object.
231 'validity': self.validity,
232 'description': self.description,
233 'release': self.release,
234 'insertion_time':
to_timestamp(self.insertion_time)
if convert_timestamps
else self.insertion_time,
235 'snapshot_time':
to_timestamp(self.snapshot_time)
if convert_timestamps
else self.snapshot_time,
236 'scenario': self.scenario,
237 'workflow': self.workflow,
243 return [self.name, self.release,
to_timestamp(self.insertion_time),
to_timestamp(self.snapshot_time), self.description]
245 def all(self, **kwargs):
247 Returns `amount` Global Tags ordered by Global Tag name.
249 query = self.session.query(GlobalTag)
251 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
252 query_result = query.order_by(GlobalTag.name).
limit(amount).
all()
256 def tags(self, **kwargs):
258 Returns `amount` *Global Tag Maps* belonging to this Global Tag.
260 kwargs[
"global_tag_name"] = self.name
261 all_tags = self.session.query(GlobalTagMap.global_tag_name, GlobalTagMap.record, GlobalTagMap.label, GlobalTagMap.tag_name)
263 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
264 all_tags = all_tags.order_by(GlobalTagMap.tag_name).
limit(amount).
all()
265 column_names = [
"global_tag_name",
"record",
"label",
"tag_name"]
266 all_tags = [dict(list(
zip(column_names, list(map(to_timestamp, row)))))
for row
in all_tags]
270 def iovs(self, **kwargs):
272 Returns `amount` IOVs belonging to all Tags held in this Global Tag.
273 For large Global Tags (which is most of them), VERY slow.
274 Highly recommended to instead used `tags().get_members("tag_name").data()` to get a `list` of tag names,
275 and then get IOVs from each Tag name.
277 At some point, this method may replace the method currently used.
283 tag_names = self.tags().get_members(
"tag_name").
data()
284 iovs_all_tags = self.session.query(IOV).
filter(IOV.tag_name.in_(tag_names))
286 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
287 iovs_all_tags = iovs_all_tags.limit(amount).subquery()
290 iovs_gt_tags = self.session.query(GlobalTagMap.tag_name, iovs_all_tags.c.since,\
291 iovs_all_tags.c.payload_hash, iovs_all_tags.c.insertion_time)\
292 .
filter(GlobalTagMap.global_tag_name == self.name)\
293 .
join(iovs_all_tags, GlobalTagMap.tag_name == iovs_all_tags.c.tag_name)
295 iovs_gt_tags = iovs_gt_tags.order_by(iovs_all_tags.c.since).
all()
297 column_names = [
"tag_name",
"since",
"payload_hash",
"insertion_time"]
298 all_iovs = [dict(list(
zip(column_names, row)))
for row
in iovs_gt_tags]
303 def __sub__(self, other):
305 Allows Global Tag objects to be used with the "-" arithmetic operator to find their difference.
306 Note: gt1 - gt2 = gt1.diff(gt2) ( = gt2 - gt1 = gt2.diff(gt1))
308 return self.diff(other)
312 Returns the json_list of differences in the form of tuples:
314 (record, label, tag name of gt1 (self), tag name of gt2 (gt))
317 record_label_to_tag_name1 = dict([((gt_map.record, gt_map.label), gt_map.tag_name)
for gt_map
in self.tags().
data()])
318 record_label_to_tag_name2 = dict([((gt_map.record, gt_map.label), gt_map.tag_name)
for gt_map
in gt.tags().
data()])
320 record_label_pairs = sorted(set(record_label_to_tag_name1) | set(record_label_to_tag_name2))
323 tags_pairs_with_differences = []
325 for record_label
in record_label_pairs:
326 tag_name1 = record_label_to_tag_name1.get(record_label)
327 tag_name2 = record_label_to_tag_name2.get(record_label)
329 if tag_name1 ==
None or tag_name2 ==
None or tag_name1 != tag_name2:
331 "Record" : record_label[0],
332 "Label" : record_label[1],
333 (
"%s Tag" % self.name) : tag_name1,
334 (
"%s Tag" % gt.name) : tag_name2
339 class GlobalTagMap(
Base):
340 __table_args__ = schema
341 __tablename__ =
'GLOBAL_TAG_MAP'
343 headers = [
"global_tag_name",
"record",
"label",
"tag_name"]
345 global_tag_name = Column(String(100), ForeignKey(fk_schema_prefix +
'GLOBAL_TAG.name'), primary_key=
True, nullable=
False)
346 record = Column(String(100), ForeignKey(fk_schema_prefix +
'RECORDS.record'), primary_key=
True, nullable=
False)
347 label = Column(String(100), primary_key=
True, nullable=
False)
348 tag_name = Column(String(100), ForeignKey(fk_schema_prefix +
'TAG.name'), nullable=
False)
350 def __init__(self, dictionary={}, convert_timestamps=True):
352 for key
in dictionary:
354 if convert_timestamps:
357 self.__dict__[key] = dictionary[key]
358 except KeyError
as k:
362 return '<GlobalTagMap %r>' % self.global_tag_name
364 def as_dicts(self, convert_timestamps=False):
366 Returns dictionary form of this Global Tag Map.
369 "global_tag_name" :
str(self.global_tag_name),
370 "record" :
str(self.record),
371 "label" :
str(self.label),
372 "tag_name" :
str(self.tag_name)
377 class GlobalTagMapRequest(
Base):
378 __table_args__ = schema
379 __tablename__ =
'GLOBAL_TAG_MAP_REQUEST'
381 queue = Column(String(100), primary_key=
True, nullable=
False)
382 tag = Column(String(100), ForeignKey(fk_schema_prefix +
'TAG.name'), primary_key=
True, nullable=
False)
383 record = Column(String(100), ForeignKey(fk_schema_prefix +
'RECORDS.record'), primary_key=
True, nullable=
False)
384 label = Column(String(100), primary_key=
True, nullable=
False)
385 status = Column(String(1), nullable=
False)
386 description = Column(String(4000), nullable=
False)
387 submitter_id = Column(Integer, nullable=
False)
388 time_submitted = Column(DateTime, nullable=
False)
389 last_edited = Column(DateTime, nullable=
False)
391 def __init__(self, dictionary={}, convert_timestamps=True):
393 for key
in dictionary:
395 if convert_timestamps:
398 self.__dict__[key] = dictionary[key]
399 except KeyError
as k:
402 headers = [
"queue",
"tag",
"record",
"label",
"status",
"description",
"submitter_id",
"time_submitted",
"last_edited"]
406 Returns dictionary form of this Global Tag Map Request.
409 "queue" : self.queue,
411 "record" : self.record,
412 "label" : self.label,
413 "status" : self.status,
414 "description" : self.description,
415 "submitter_id" : self.submitter_id,
416 "time_submitted" : self.time_submitted,
417 "last_edited" : self.last_edited
421 return '<GlobalTagMapRequest %r>' % self.queue
427 __table_args__ = schema
428 __tablename__ =
'IOV'
430 headers = [
"tag_name",
"since",
"payload_hash",
"insertion_time"]
432 tag_name = Column(String(4000), ForeignKey(fk_schema_prefix +
'TAG.name'), primary_key=
True, nullable=
False)
433 since = Column(Integer, primary_key=
True, nullable=
False)
434 payload_hash = Column(String(40), ForeignKey(fk_schema_prefix +
'PAYLOAD.hash'), nullable=
False)
435 insertion_time = Column(DateTime, primary_key=
True, nullable=
False)
437 def __init__(self, dictionary={}, convert_timestamps=True):
439 for key
in dictionary:
441 if convert_timestamps:
444 self.__dict__[key] = dictionary[key]
445 except KeyError
as k:
448 def as_dicts(self, convert_timestamps=False):
450 Returns dictionary form of this IOV.
453 "tag_name" : self.tag_name,
454 "since" : self.since,
455 "payload_hash" : self.payload_hash,
456 "insertion_time" :
to_timestamp(self.insertion_time)
if convert_timestamps
else self.insertion_time
460 return '<IOV %r>' % self.tag_name
463 return [self.since,
to_timestamp(self.insertion_time), self.payload_hash]
465 def all(self, **kwargs):
467 Returns `amount` IOVs ordered by since.
469 query = self.session.query(IOV)
471 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
472 query_result = query.order_by(IOV.tag_name).order_by(IOV.since).
limit(amount).
all()
477 __table_args__ = schema
478 __tablename__ =
'PAYLOAD'
480 headers = [
"hash",
"object_type",
"version",
"insertion_time"]
482 hash = Column(String(40), primary_key=
True, nullable=
False)
483 object_type = Column(String(4000), nullable=
False)
484 version = Column(String(4000), nullable=
False)
485 insertion_time = Column(DateTime, nullable=
False)
487 data = Column(Binary, nullable=
False)
488 streamer_info = Column(Binary, nullable=
False)
489 blobs_mapped = map_blobs
491 def __init__(self, dictionary={}, convert_timestamps=True):
493 for key
in dictionary:
495 if convert_timestamps:
498 self.__dict__[key] = dictionary[key]
499 except KeyError
as k:
503 def as_dicts(self, convert_timestamps=False):
505 Returns dictionary form of this Payload's metadata (not the actual Payload).
509 "object_type" : self.object_type,
510 "version" : self.version,
511 "insertion_time" :
to_timestamp(self.insertion_time)
if convert_timestamps
else self.insertion_time,
513 "streamer_info" : self.streamer_info
516 def as_dicts(self, convert_timestamps=False):
518 Returns dictionary form of this Payload's metadata (not the actual Payload).
522 "object_type" : self.object_type,
523 "version" : self.version,
524 "insertion_time" :
to_timestamp(self.insertion_time)
if convert_timestamps
else self.insertion_time
528 return '<Payload %r>' % self.hash
531 return [self.hash, self.object_type, self.version,
to_timestamp(self.insertion_time)]
533 def parent_tags(self, **kwargs):
535 Returns `amount` parent Tags ordered by Tag name.
541 kwargs[
"payload_hash"] = self.hash
542 query = self.session.query(IOV.tag_name)
544 query_result = query.all()
545 tag_names = [entry[0]
for entry
in query_result]
546 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
547 tags = self.session.query(Tag).
filter(Tag.name.in_(tag_names)).order_by(Tag.name).
limit(amount).
all()
550 def all(self, **kwargs):
552 Returns `amount` Payloads ordered by Payload hash.
554 query = self.session.query(Payload)
556 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
557 query_result = query.order_by(Payload.hash).
limit(amount).
all()
562 __table_args__ = schema
563 __tablename__ =
'RECORDS'
565 headers = [
"record",
"object",
"type"]
567 record = Column(String(100), primary_key=
True, nullable=
False)
568 object = Column(String(200), nullable=
False)
569 type = Column(String(20), nullable=
False)
573 Returns dictionary form of this Record.
576 "record" : self.record,
577 "object" : self.object,
582 return '<Record %r>' % self.record
585 return [self.record, self.object]
587 def all(self, **kwargs):
589 Returns `amount` Records ordered by Record record.
591 query = self.session.query(Record)
593 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
594 query_result = query.order_by(Record.record).
limit(amount).
all()
599 __table_args__ = schema
600 __tablename__ =
'TAG'
602 headers = [
"name",
"time_type",
"object_type",
"synchronization",
"end_of_validity",\
603 "description",
"last_validated_time",
"insertion_time",
"modification_time",
"protection_code"]
605 name = Column(String(4000), primary_key=
True, nullable=
False)
606 time_type = Column(String(4000), nullable=
False)
607 object_type = Column(String(4000), nullable=
False)
608 synchronization = Column(String(4000), nullable=
False)
609 end_of_validity = Column(Integer, nullable=
False)
610 description = Column(String(4000), nullable=
False)
611 last_validated_time = Column(BigInteger, nullable=
False)
612 insertion_time = Column(DateTime, nullable=
False)
613 modification_time = Column(DateTime, nullable=
False)
614 protection_code = Column(Integer, nullable=
False)
619 iovs_list = relationship(
'IOV', backref=
'tag')
621 def __init__(self, dictionary={}, convert_timestamps=True):
623 for key
in dictionary:
625 if convert_timestamps:
628 self.__dict__[key] = dictionary[key]
629 except KeyError
as k:
632 def as_dicts(self, convert_timestamps=False):
634 Returns dictionary form of this Tag.
638 "time_type" : self.time_type,
639 "object_type" : self.object_type,
640 "synchronization" : self.synchronization,
641 "end_of_validity" : self.end_of_validity,
642 "description" : self.description,
643 "last_validated_time" : self.last_validated_time,
644 "insertion_time" :
to_timestamp(self.insertion_time)
if convert_timestamps
else self.insertion_time,
645 "modification_time" :
to_timestamp(self.modification_time)
if convert_timestamps
else self.modification_time,
646 "record" : self.record,
651 return '<Tag %r>' % self.name
654 return [self.name, self.time_type, self.object_type, self.synchronization,
to_timestamp(self.insertion_time), self.description]
656 def parent_global_tags(self, **kwargs):
658 Returns `amount` Global Tags that contain this Tag.
663 kwargs[
"tag_name"] = self.name
664 query = self.session.query(GlobalTagMap.global_tag_name)
666 query_result = query.all()
667 if len(query_result) != 0:
668 global_tag_names = [entry[0]
for entry
in query_result]
669 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
670 global_tags = self.session.query(GlobalTag).
filter(GlobalTag.name.in_(global_tag_names)).order_by(GlobalTag.name).
limit(amount).
all()
675 def all(self, **kwargs):
677 Returns `amount` Tags ordered by Tag name.
679 query = self.session.query(Tag)
681 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
682 query_result = query.order_by(Tag.name).
limit(amount).
all()
685 def iovs(self, **kwargs):
687 Returns `amount` IOVs that belong to this Tag ordered by IOV since.
690 iovs_query = self.session.query(IOV).
filter(IOV.tag_name == self.name)
692 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
693 iovs = iovs_query.order_by(IOV.since).
limit(amount).
all()
696 def latest_iov(self):
698 Returns the single highest since held by this Tag.
699 Insertion times do not matter - if there are two IOVs at since > all others, both have the highest since.
701 iov = self.session.query(IOV).
filter(IOV.tag_name == self.name).order_by(IOV.since.desc()).
first()
704 def __sub__(self, other):
706 Allows the arithmetic operator "-" to be applied to find the difference between two tags.
707 Note: diff() is symmetric, hence tag1 - tag2 = tag2 - tag1.
709 return self.diff(other)
711 def diff(self, tag, short=False):
713 Returns the `diff` of the first Tag, and the Tag given.
714 Summary of algorithm:
716 Compute the ordered set of iov sinces from both tags, and construct a list of triples, (since, tag1 hash, tag2 hash).
717 Set previous_payload1 and previous_payload2 to be the first hash values from each tag for the first since in the merged list.
718 Note: depending on where each Tag's IOVs start, 1 or both of these values can be None.
719 Set the first_since_in_equality_range = -1, which holds the since at which the last hashes were equal in the Tags.
720 For each triple (since, hash1, hash2),
722 If the first_since_in_equality_range = None,
723 We are at the first since in the merged list, so set first_since... = since
724 Note: this is so set the previous... values for the second row, since the first row will never result in a print because
725 a row is only printed when past iovs have been processed.
727 If either hash1 or hash2 is None, set it to the previous hash found
728 Note: if a Tag defines a hash for one since and then not another for n rows, the last defined hash will be carried through because of this.
730 If the previous found hashes were equal, that means we have equality on the range [first_since_in_equality_range, since)
731 Note: we CANNOT conclude anything about the hashes corresponding to sinces >= since
732 because we have no looked forward, but we do know about the previous hashes.
735 The region of equality has ended, and so we have that [first_since_in_equality_range, since) is equal for both Tags
736 Hence, print that for this range we have equal hashes denoted by "=" in each hash column.
740 The previous hashes were not equal, BUT we must check that ths hashes on this row are not identical...
741 If the hashes on this row are the same as the hashes above (hash1 == previous_payload1 and hash2 == previous_payload2),
742 then we have not found the end of a region of equality!
743 If the hashes have changed, print a row.
746 if tag.__class__.__name__ !=
"Tag":
747 raise TypeError(
"Tag given must be a CondDBFW Tag object.")
750 iovs1 = dict([(iov.since, iov.payload_hash)
for iov
in self.iovs().
data()])
751 iovs2 = dict([(iov.since, iov.payload_hash)
for iov
in tag.iovs().
data()])
753 iovs = [(x, iovs1.get(x), iovs2.get(x))
for x
in sorted(set(iovs1) | set(iovs2))]
754 iovs.append((
"Infinity", 1, 2))
757 previous_hash1 =
None
758 previous_hash2 =
None
759 first_since_in_equality_range =
None
760 previous_equal =
False
762 for since, hash1, hash2
in iovs:
764 if first_since_in_equality_range ==
None:
769 first_since_in_equality_range = since
770 previous_hash1 = hash1
771 previous_hash2 = hash2
772 previous_equal = hash1 == hash2
778 hash1 = previous_hash1
780 hash2 = previous_hash2
785 table.append({
"since" :
"[%s, %s)" % (first_since_in_equality_range, since), self.name :
"=", tag.name :
"="})
787 first_since_in_equality_range = since
793 if not(hash1 == previous_hash1
and hash2 == previous_hash2):
794 table.append({
"since" :
"[%s, %s)" % (first_since_in_equality_range, since), self.name : previous_hash1, tag.name : previous_hash2})
795 first_since_in_equality_range = since
797 previous_hash1 = hash1
798 previous_hash2 = hash2
799 previous_equal = hash1 == hash2
804 def merge_into(self, tag, range_object):
806 Given another connection, apply the 'merge' algorithm to merge the IOVs from this Tag
807 into the IOVs of the other Tag.
809 tag : CondDBFW Tag object that the IOVs from this Tag should be merged into.
811 range_object : CondDBFW.data_sources.Range object to describe the subset of IOVs that should be copied
812 from the database this Tag belongs to.
814 Script originally written by Joshua Dawes,
815 and adapted by Giacomo Govi, Gianluca Cerminara and Giovanni Franzoni.
819 merged_tag_name = oracle_tag.name +
"_merged"
822 since_range = range_object
828 if sqlite_tag ==
None:
829 raise TypeError(
"Tag to be merged cannot be None.")
831 sqlite_iovs = sqlite_tag.iovs().
data()
832 sqlite_tag.iovs().as_table()
834 new_tag = self.connection.models[
"tag"](sqlite_tag.as_dicts(convert_timestamps=
False), convert_timestamps=
False)
835 new_tag.name = merged_tag_name
837 imported_iovs = oracle_tag.iovs(since=since_range).
data()
839 for i
in range(0, len(imported_iovs)):
840 imported_iovs[i].source =
"oracle"
842 sqlite_iovs_sinces=[]
843 for i
in range(0, len(sqlite_iovs)):
844 sqlite_iovs[i].source =
"sqlite"
845 sqlite_iovs_sinces.append(sqlite_iovs[i].since)
848 print(sqlite_iovs_sinces)
850 new_iovs_list = imported_iovs + sqlite_iovs
851 new_iovs_list = sorted(new_iovs_list, key=
lambda iov : iov.since)
853 for (n, iov)
in enumerate(new_iovs_list):
855 if iov.source ==
"oracle":
856 if new_iovs_list[n].since
in sqlite_iovs_sinces:
859 iov.source =
"tobedeleted"
863 for i
in reversed(list(
range(0,n))):
864 if new_iovs_list[i].source ==
"sqlite":
865 print(
"change %s to %s at since %d" % (iov.payload_hash, new_iovs_list[i].payload_hash, iov.since))
866 iov.payload_hash = new_iovs_list[i].payload_hash
870 new_iov_list_copied = []
872 for iov
in new_iovs_list:
874 if iov.source !=
"tobedeleted":
875 new_iov_list_copied.append(iov)
877 new_iov_list_copied = sorted(new_iov_list_copied, key=
lambda iov : iov.since)
879 now = datetime.datetime.utcnow()
882 for iov
in new_iov_list_copied:
883 new_iovs.append( self.connection.models[
"iov"](iov.as_dicts(convert_timestamps=
False), convert_timestamps=
False) )
885 iov.insertion_time = now
886 iov.tag_name = merged_tag_name
888 new_tag.iovs_list = new_iovs
894 class TagAuthorization(
Base):
895 __table_args__ = schema
896 __tablename__ =
'TAG_AUTHORIZATION'
898 headers = [
"tag_name",
"access_type",
"credential",
"credential_type"]
900 tag_name = Column(String(100), ForeignKey(fk_schema_prefix +
'TAG.name'), primary_key=
True, nullable=
False)
901 access_type = Column(Integer, nullable=
False)
902 credential = Column(String(100), primary_key=
True, nullable=
False)
903 credential_type = Column(Integer, nullable=
False)
907 Returns dictionary form of this Tag Authorization.
910 "tag_name" : self.tag_name,
911 "access_type" : self.access_type,
912 "credential" : self.credential,
913 "credential_type" : self.credential_type
917 return '<TagAuthorization %s %s %s %s>' % (self.tag_name, self.access_type, self.credential, self.credential_type)
920 return [self.tag_name, self.access_type, self.credential, self.credential_type]
922 def all(self, **kwargs):
924 Returns `amount` Records ordered by Record record.
926 query = self.session.query(TagAuthorization)
928 amount = kwargs[
"amount"]
if "amount" in list(kwargs.keys())
else None
929 query_result = query.order_by(TagAuthorization.tag).
limit(amount).
all()
932 classes = {
"globaltag" : GlobalTag,
"iov" : IOV,
"globaltagmap" : GlobalTagMap,\
933 "payload" : Payload,
"tag" : Tag,
"TagAuthorization": TagAuthorization,
"Base" : Base}
935 if class_name ==
None:
return classes[class_name]
const uint16_t range(const Frame &aFrame)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
static std::string join(char **cmd)
char data[epos_bytes_allocation]