<commit_before>__author__ = 'arkilic'

from mongoengine import Document, DynamicDocument, DynamicEmbeddedDocument
from mongoengine import (DateTimeField, StringField, DictField, IntField, FloatField, ListField,
                         ReferenceField, EmbeddedDocumentField, DENY, MapField)
from getpass import getuser


class BeamlineConfig(DynamicDocument):
    """
    Attributes
    ----------
    config_params: dict
        Custom configuration parameters for a given run. Avoid using '.' in field names.
        If you're interested in doing so, let me know @arkilic
        This has a one-to-many relationship with BeginRunEvent documents
    """
    config_params = DictField(required=False, unique=False)
    meta = {'indexes': ['-_id']}


class BeginRunEvent(DynamicDocument):
    """ Provide a head for a sequence of events. Entry point for
    an experiment's run. BeamlineConfig is NOT required to create a BeginRunEvent
    The only prereq is an EventDescriptor that identifies the nature of event that is
    starting and

    Attributes
    ----------
    uid: str
        Globally unique id for this run
    time : timestamp
        The date/time as found at the client side when an begin_run_event is
        created.
    beamline_id : str
        Beamline String identifier. Not unique, just an indicator of
        beamline code for multiple beamline systems
    project : str, optional
        Name of project that this run is part of
    owner : str
        Specifies the unix user credentials of the user creating the entry
    group : str
        Unix group to associate this data with
    scan_id : int
         scan identifier visible to the user and data analysis
    beamline_config: bson.ObjectId
        Foreign key to beamline config corresponding to a given run
    sample : dict
        Information about the sample, may be a UID to another collection
    """
    uid = StringField(required=True, unique=True)
    time = FloatField(required=True)
    project = StringField(required=False)
    beamline_id = StringField(max_length=20, unique=False, required=True)
    scan_id = IntField(required=True)
    beamline_config = ReferenceField(BeamlineConfig, reverse_delete_rule=DENY,
                                     required=False,
                                     db_field='beamline_config_id')
    owner = StringField(default=getuser(), required=True, unique=False)
    group = StringField(required=False, unique=False, default=None)
    sample = DictField(required=False)  # lightweight sample placeholder.
    meta = {'indexes': ['-_id', '-owner', '-time', '-scan_id', '-uid']}


class EndRunEvent(DynamicDocument):
    """Indicates the end of a series of events

    Attributes
    ----------
    begin_run_event : bson.ObjectId
        Foreign key to corresponding BeginRunEvent
    exit_status : {'success', 'fail', 'abort'}
        provides information regarding the run success.
    time : float
        The date/time as found at the client side when an event is
        created.
    reason : str, optional
        Long-form description of why the run ended.
        20 char max.
    """
    time = FloatField(required=True)
    begin_run_event = ReferenceField(BeginRunEvent, reverse_delete_rule=DENY,
                                     required=True, db_field='begin_run_id')

    exit_status = StringField(max_length=10, required=False, default='success',
                              choices=('success', 'abort', 'fail'))
    reason = StringField(required=False, max_length=20)
    meta = {'indexes': ['-_id', '-time', '-exit_status', '-begin_run_event']}
    uid = StringField(required=True, unique=True)


class DataKey(DynamicEmbeddedDocument):
    """Describes the objects in the data property of Event documents.
    Be aware that this is DynamicEmbeddedDoc Append fields rather than
    custom dict field

    Attributes
    ----------<commit_msg>MNT : removed length limit on reason in EndRunEvent<commit_after>__author__ = 'arkilic'

from mongoengine import Document, DynamicDocument, DynamicEmbeddedDocument
from mongoengine import (DateTimeField, StringField, DictField, IntField, FloatField, ListField,
                         ReferenceField, EmbeddedDocumentField, DENY, MapField)
from getpass import getuser


class BeamlineConfig(DynamicDocument):
    """
    Attributes
    ----------
    config_params: dict
        Custom configuration parameters for a given run. Avoid using '.' in field names.
        If you're interested in doing so, let me know @arkilic
        This has a one-to-many relationship with BeginRunEvent documents
    """
    config_params = DictField(required=False, unique=False)
    meta = {'indexes': ['-_id']}


class BeginRunEvent(DynamicDocument):
    """ Provide a head for a sequence of events. Entry point for
    an experiment's run. BeamlineConfig is NOT required to create a BeginRunEvent
    The only prereq is an EventDescriptor that identifies the nature of event that is
    starting and

    Attributes
    ----------
    uid: str
        Globally unique id for this run
    time : timestamp
        The date/time as found at the client side when an begin_run_event is
        created.
    beamline_id : str
        Beamline String identifier. Not unique, just an indicator of
        beamline code for multiple beamline systems
    project : str, optional
        Name of project that this run is part of
    owner : str
        Specifies the unix user credentials of the user creating the entry
    group : str
        Unix group to associate this data with
    scan_id : int
         scan identifier visible to the user and data analysis
    beamline_config: bson.ObjectId
        Foreign key to beamline config corresponding to a given run
    sample : dict
        Information about the sample, may be a UID to another collection
    """
    uid = StringField(required=True, unique=True)
    time = FloatField(required=True)
    project = StringField(required=False)
    beamline_id = StringField(max_length=20, unique=False, required=True)
    scan_id = IntField(required=True)
    beamline_config = ReferenceField(BeamlineConfig, reverse_delete_rule=DENY,
                                     required=False,
                                     db_field='beamline_config_id')
    owner = StringField(default=getuser(), required=True, unique=False)
    group = StringField(required=False, unique=False, default=None)
    sample = DictField(required=False)  # lightweight sample placeholder.
    meta = {'indexes': ['-_id', '-owner', '-time', '-scan_id', '-uid']}


class EndRunEvent(DynamicDocument):
    """Indicates the end of a series of events

    Attributes
    ----------
    begin_run_event : bson.ObjectId
        Foreign key to corresponding BeginRunEvent
    exit_status : {'success', 'fail', 'abort'}
        provides information regarding the run success.
    time : float
        The date/time as found at the client side when an event is
        created.
    reason : str, optional
        Long-form description of why the run ended.
        20 char max.
    """
    time = FloatField(required=True)
    begin_run_event = ReferenceField(BeginRunEvent, reverse_delete_rule=DENY,
                                     required=True, db_field='begin_run_id')

    exit_status = StringField(max_length=10, required=False, default='success',
                              choices=('success', 'abort', 'fail'))
    reason = StringField(required=False)
    meta = {'indexes': ['-_id', '-time', '-exit_status', '-begin_run_event']}
    uid = StringField(required=True, unique=True)


class DataKey(DynamicEmbeddedDocument):
    """Describes the objects in the data property of Event documents.
    Be aware that this is DynamicEmbeddedDoc Append fields rather than
    custom dict field

    Attributes
    ----------
{'fraction_non_alphanumeric': 0.08202576859293277, 'fraction_numerical': 0.0017859420844495472, 'mean_word_length': 2.820662768031189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487269', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2011, 'n_words': 732}