def tsadd(policyId, userId, imageDigest, tag, final_action, inobj, session=None):
    if not session:
        session = db.Session

    latest_result = session.query(PolicyEval).filter_by(policyId=policyId, userId=userId, imageDigest=imageDigest, tag=tag).order_by(desc(PolicyEval.created_at)).first()
    new_result = PolicyEval(userId=userId, imageDigest=imageDigest, tag=tag, policyId=policyId, final_action=final_action, created_at=int(time.time()))
    new_result.update(inobj)

    if latest_result:
        rc = latest_result.content_compare(new_result)
        if rc:
            # same - update old object
            latest_result.update({'created_at':int(time.time())})
        else:
            # different, make new object
            session.add(new_result)
    else:
        # brand new object
        session.add(new_result)

#    try:
#        session.commit()
#    except Exception as err:
#        raise err
#    finally:
#        session.rollback()

    return(True)
def add_all_for_digest(records, session):
    """
    Assumes these are all valid records.

    :param records: list of dicts from PolicyEval json dumps
    :param session:
    :return:
    """

    recs = []
    for r in records:
        rec = PolicyEval()
        rec.update(r)
        recs.append(session.add(rec))

    return recs
def add(
    policyId, userId, imageDigest, tag, final_action, created_at, inobj, session=None
):
    if not session:
        session = db.Session

    our_result = (
        session.query(PolicyEval)
        .filter_by(
            policyId=policyId,
            userId=userId,
            imageDigest=imageDigest,
            tag=tag,
            final_action=final_action,
            created_at=created_at,
        )
        .first()
    )
    if not our_result:
        new_service = PolicyEval(
            userId=userId,
            imageDigest=imageDigest,
            tag=tag,
            policyId=policyId,
            final_action=final_action,
            created_at=created_at,
        )
        new_service.update(inobj)

        session.add(new_service)
    else:
        our_result.update(inobj)

    #    try:
    #        session.commit()
    #    except Exception as err:
    #        raise err
    #    finally:
    #        session.rollback()

    return True