Example #1
0
def heartbeat():
    username = request.authorization.username
    extractor = Extractor.query.filter_by(username=username).first()

    # set the extractor last contact datetime to now
    now = datetime.now()
    extractor.last_contact = now
    extractor.save()

    # get the month and year to tell the extractor to start from
    next_month = extractor.next_month if extractor.next_month else now.month
    next_year = extractor.next_year if extractor.next_year else now.year

    #
    # build and send a Slack notification about this ping
    slack_body_lines = []
    extractor_department = extractor.first_department()
    if extractor_department:
        slack_body_lines.append('For: {}'.format(extractor_department.name))
    else:
        slack_body_lines.append('Username: {}'.format(username))

    slack_date_line = 'No extraction start date in reply.'
    slack_date_line = 'Replied with extraction start date: {}/{}'.format(next_month, next_year)

    slack_body_lines.append(slack_date_line)
    send_slack_message('Comport Pinged by Extractor!', slack_body_lines)

    #
    # remove records for this department from the incidents_updated table
    IncidentsUpdated.delete_records(department_id=extractor_department.id)

    # respond to the extractor
    heartbeat_response = json.dumps({"received": request.json, "nextMonth": next_month, "nextYear": next_year})
    return heartbeat_response
Example #2
0
def heartbeat():
    username = request.authorization.username
    extractor = Extractor.query.filter_by(username=username).first()

    # set the extractor last contact datetime to now
    now = datetime.now()
    extractor.last_contact = now
    extractor.save()

    # get the month and year to tell the extractor to start from
    next_month = extractor.next_month if extractor.next_month else now.month
    next_year = extractor.next_year if extractor.next_year else now.year

    #
    # build and send a Slack notification about this ping
    slack_body_lines = []
    extractor_department = extractor.first_department()
    if extractor_department:
        slack_body_lines.append('For: {}'.format(extractor_department.name))
    else:
        slack_body_lines.append('Username: {}'.format(username))

    slack_date_line = 'No extraction start date in reply.'
    slack_date_line = 'Replied with extraction start date: {}/{}'.format(next_month, next_year)

    slack_body_lines.append(slack_date_line)
    send_slack_message('Comport Pinged by Extractor!', slack_body_lines)

    #
    # remove records for this department from the incidents_updated table
    IncidentsUpdated.delete_records(department_id=extractor_department.id)

    # respond to the extractor
    heartbeat_response = json.dumps({"received": request.json, "nextMonth": next_month, "nextYear": next_year})
    return heartbeat_response
    def test_delete_records(self):
        """ Delete incidents_updated records using the delete_records method.
        """
        # create a couple of departments
        department_bpd = Department.create(name="B Police Department", short_name="BPD")
        department_impd = Department.create(name="IM Police Department", short_name="IMPD")

        # create some 'incidents updated' records
        uof_type = "uof"
        ois_type = "ois"
        ids_and_types = [
            ("123abc", uof_type, department_bpd.id),
            ("234bcd", uof_type, department_bpd.id),
            ("345cde", ois_type, department_bpd.id),
            ("456def", uof_type, department_impd.id),
            ("567efg", uof_type, department_impd.id),
            ("678fgh", ois_type, department_impd.id),
        ]
        for opaque_id, use_type, department_id in ids_and_types:
            IncidentsUpdated.create(department_id=department_id, opaque_id=opaque_id, incident_type=use_type)

        # verify that the records were saved as expected
        assert len(IncidentsUpdated.query.all()) == len(ids_and_types)
        assert len(IncidentsUpdated.query.filter_by(department_id=department_bpd.id).all()) == 3
        assert len(IncidentsUpdated.query.filter_by(department_id=department_impd.id).all()) == 3
        assert len(IncidentsUpdated.query.filter_by(incident_type=uof_type).all()) == 4
        assert len(IncidentsUpdated.query.filter_by(incident_type=ois_type).all()) == 2

        # delete the impd records
        num_deleted = IncidentsUpdated.delete_records(department_id=department_impd.id)
        assert num_deleted == len(ids_and_types) / 2

        # verify the results
        assert len(IncidentsUpdated.query.all()) == len(ids_and_types) - num_deleted
        assert len(IncidentsUpdated.query.filter_by(department_id=department_bpd.id).all()) == 3
        assert len(IncidentsUpdated.query.filter_by(department_id=department_impd.id).all()) == 0
        assert len(IncidentsUpdated.query.filter_by(incident_type=uof_type).all()) == 2
        assert len(IncidentsUpdated.query.filter_by(incident_type=ois_type).all()) == 1

        # delete the bpd uof records
        num_deleted = IncidentsUpdated.delete_records(department_id=department_bpd.id, incident_type=uof_type)
        assert num_deleted == 2

        # verify the results
        assert len(IncidentsUpdated.query.all()) == 1
        assert len(IncidentsUpdated.query.filter_by(department_id=department_bpd.id).all()) == 1
        assert len(IncidentsUpdated.query.filter_by(incident_type=uof_type).all()) == 0
        assert len(IncidentsUpdated.query.filter_by(incident_type=ois_type).all()) == 1
Example #4
0
    def test_delete_records(self):
        ''' Delete incidents_updated records using the delete_records method.
        '''
        # create a couple of departments
        department_bpd = Department.create(name="B Police Department",
                                           short_name="BPD")
        department_impd = Department.create(name="IM Police Department",
                                            short_name="IMPD")

        # create some 'incidents updated' records
        uof_type = "uof"
        ois_type = "ois"
        ids_and_types = [("123abc", uof_type, department_bpd.id),
                         ("234bcd", uof_type, department_bpd.id),
                         ("345cde", ois_type, department_bpd.id),
                         ("456def", uof_type, department_impd.id),
                         ("567efg", uof_type, department_impd.id),
                         ("678fgh", ois_type, department_impd.id)]
        for opaque_id, use_type, department_id in ids_and_types:
            IncidentsUpdated.create(department_id=department_id,
                                    opaque_id=opaque_id,
                                    incident_type=use_type)

        # verify that the records were saved as expected
        assert len(IncidentsUpdated.query.all()) == len(ids_and_types)
        assert len(
            IncidentsUpdated.query.filter_by(
                department_id=department_bpd.id).all()) == 3
        assert len(
            IncidentsUpdated.query.filter_by(
                department_id=department_impd.id).all()) == 3
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=uof_type).all()) == 4
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=ois_type).all()) == 2

        # delete the impd records
        num_deleted = IncidentsUpdated.delete_records(
            department_id=department_impd.id)
        assert num_deleted == len(ids_and_types) / 2

        # verify the results
        assert len(
            IncidentsUpdated.query.all()) == len(ids_and_types) - num_deleted
        assert len(
            IncidentsUpdated.query.filter_by(
                department_id=department_bpd.id).all()) == 3
        assert len(
            IncidentsUpdated.query.filter_by(
                department_id=department_impd.id).all()) == 0
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=uof_type).all()) == 2
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=ois_type).all()) == 1

        # delete the bpd uof records
        num_deleted = IncidentsUpdated.delete_records(
            department_id=department_bpd.id, incident_type=uof_type)
        assert num_deleted == 2

        # verify the results
        assert len(IncidentsUpdated.query.all()) == 1
        assert len(
            IncidentsUpdated.query.filter_by(
                department_id=department_bpd.id).all()) == 1
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=uof_type).all()) == 0
        assert len(
            IncidentsUpdated.query.filter_by(
                incident_type=ois_type).all()) == 1