15.2.163. camcops_server.cc_modules.cc_taskindex

camcops_server/cc_modules/cc_taskindex.py


Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.


Server-side task index.

Note in particular that if you, as a developer, change the is_complete() criteria for a task, you should cause the server index to be rebuilt (because it caches is_complete() information).

class camcops_server.cc_modules.cc_taskindex.PatientIdNumIndexEntry(**kwargs)[source]

Represents a server index entry for a camcops_server.cc_modules.cc_patientidnum.PatientIdNum.

  • Only current ID numbers are indexed.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

classmethod check_index(session: sqlalchemy.orm.session.Session, show_all_bad: bool = False) bool[source]

Checks the index.

Parameters
  • session – an SQLAlchemy Session

  • show_all_bad – show all bad entries? (If false, return upon the first)

Returns

is the index OK?

Return type

bool

classmethod index_idnum(idnum: camcops_server.cc_modules.cc_patientidnum.PatientIdNum, session: sqlalchemy.orm.session.Session) None[source]

Indexes an ID number and inserts the index into the database.

Parameters
classmethod make_from_idnum(idnum: camcops_server.cc_modules.cc_patientidnum.PatientIdNum) camcops_server.cc_modules.cc_taskindex.PatientIdNumIndexEntry[source]

Returns an ID index entry for the specified camcops_server.cc_modules.cc_patientidnum.PatientIdNum. The returned index requires inserting into a database session.

classmethod rebuild_idnum_index(session: sqlalchemy.orm.session.Session, indexed_at_utc: pendulum.datetime.DateTime) None[source]

Rebuilds the index entirely. Uses SQLAlchemy Core (not ORM) for speed.

Parameters
  • session – an SQLAlchemy Session

  • indexed_at_utc – current time in UTC

classmethod unindex_patient(patient: camcops_server.cc_modules.cc_patient.Patient, session: sqlalchemy.orm.session.Session) None[source]

Removes all ID number indexes from the database for a patient.

Parameters
classmethod update_idnum_index_for_upload(session: sqlalchemy.orm.session.Session, indexed_at_utc: pendulum.datetime.DateTime, tablechanges: camcops_server.cc_modules.cc_client_api_core.UploadTableChanges) None[source]

Updates the index for a device’s upload.

  • Deletes index entries for records that are on the way out.

  • Creates index entries for records that are on the way in.

  • Should be called after both the Patient and PatientIdNum tables are committed; see special ordering in camcops_server.cc_modules.client_api.commit_all().

Parameters
class camcops_server.cc_modules.cc_taskindex.TaskIndexEntry(**kwargs)[source]

Represents a server index entry for a camcops_server.cc_modules.cc_task.Task.

  • Only current tasks are indexed. This simplifies direct linking to patient PKs.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

any_patient_idnums_invalid(req: CamcopsRequest) bool[source]

Do we have a patient who has any invalid ID numbers?

Parameters

req – a camcops_server.cc_modules.cc_request.CamcopsRequest

classmethod check_index(session: sqlalchemy.orm.session.Session, show_all_bad: bool = False) bool[source]

Checks the index.

Parameters
  • session – an SQLAlchemy Session

  • show_all_bad – show all bad entries? (If false, return upon the first)

Returns

is the index OK?

Return type

bool

get_patient_idnum_objects() List[camcops_server.cc_modules.cc_patientidnum.PatientIdNum][source]

Gets all PatientIdNum objects for the patient.

classmethod index_task(task: camcops_server.cc_modules.cc_task.Task, session: sqlalchemy.orm.session.Session, indexed_at_utc: pendulum.datetime.DateTime) None[source]

Indexes a task and inserts the index into the database.

Parameters
property is_anonymous: bool

Is the task anonymous?

is_complete() bool[source]

Is the task complete?

is_live_on_tablet() bool[source]

Is the task live on the source device (e.g. tablet)?

classmethod make_from_task(task: camcops_server.cc_modules.cc_task.Task, indexed_at_utc: pendulum.datetime.DateTime) camcops_server.cc_modules.cc_taskindex.TaskIndexEntry[source]

Returns a task index entry for the specified camcops_server.cc_modules.cc_task.Task. The returned index requires inserting into a database session.

Parameters
property pk: int

Return’s the task’s server PK.

classmethod rebuild_entire_task_index(session: sqlalchemy.orm.session.Session, indexed_at_utc: pendulum.datetime.DateTime, skip_tasks_with_missing_tables: bool = False) None[source]

Rebuilds the entire index.

Parameters
  • session – an SQLAlchemy Session

  • indexed_at_utc – current time in UTC

  • skip_tasks_with_missing_tables – should we skip over tasks if their tables are not in the database? (This is so we can rebuild an index from a database upgrade, but not crash because newer tasks haven’t had their tables created yet.)

classmethod rebuild_index_for_task_type(session: sqlalchemy.orm.session.Session, taskclass: Type[camcops_server.cc_modules.cc_task.Task], indexed_at_utc: pendulum.datetime.DateTime, delete_first: bool = True) None[source]

Rebuilds the index for a particular task type.

Parameters
  • session – an SQLAlchemy Session

  • taskclass – a subclass of camcops_server.cc_modules.cc_task.Task

  • indexed_at_utc – current time in UTC

  • delete_first – delete old index entries first? Should always be True unless called as part of a master rebuild that deletes everything first.

property shortname: str

Returns the task’s shortname.

property tablename: str

Returns the base table name of the task.

property task: Optional[camcops_server.cc_modules.cc_task.Task]
Returns

the associated camcops_server.cc_modules.cc_task.Task, or None if none exists.

Raises

HTTPBadRequest

classmethod unindex_task(task: camcops_server.cc_modules.cc_task.Task, session: sqlalchemy.orm.session.Session) None[source]

Removes a task index from the database.

Parameters
classmethod update_task_index_for_upload(session: sqlalchemy.orm.session.Session, tablechanges: camcops_server.cc_modules.cc_client_api_core.UploadTableChanges, indexed_at_utc: pendulum.datetime.DateTime) None[source]

Updates the index for a device’s upload.

  • Deletes index entries for records that are on the way out.

  • Creates index entries for records that are on the way in.

  • Deletes/recreates index entries for records being preserved.

Parameters
property when_created: pendulum.datetime.DateTime

Returns the creation date/time as a Pendulum DateTime object.

camcops_server.cc_modules.cc_taskindex.check_indexes(session: sqlalchemy.orm.session.Session, show_all_bad: bool = False) bool[source]

Checks all server index tables.

Parameters
  • session – an SQLAlchemy Session

  • show_all_bad – show all bad entries? (If false, return upon the first)

Returns

are the indexes OK?

Return type

bool

camcops_server.cc_modules.cc_taskindex.reindex_everything(session: sqlalchemy.orm.session.Session, skip_tasks_with_missing_tables: bool = False) None[source]

Deletes from and rebuilds all server index tables.

Parameters
  • session – an SQLAlchemy Session

  • skip_tasks_with_missing_tables – should we skip over tasks if their tables are not in the database? (This is so we can rebuild an index from a database upgrade, but not crash because newer tasks haven’t had their tables created yet.)

camcops_server.cc_modules.cc_taskindex.task_factory_unfiltered(dbsession: sqlalchemy.orm.session.Session, basetable: str, serverpk: int) Optional[camcops_server.cc_modules.cc_task.Task][source]

Load a task from the database and return it. No permission filtering is performed. (Used by camcops_server.cc_modules.cc_taskindex.TaskIndexEntry.)

Parameters
  • dbsession – a sqlalchemy.orm.session.Session

  • basetable – name of the task’s base table

  • serverpk – server PK of the task

Returns

the task, or None if the PK doesn’t exist

Raises

HTTPBadRequest

camcops_server.cc_modules.cc_taskindex.update_indexes_and_push_exports(req: CamcopsRequest, batchdetails: camcops_server.cc_modules.cc_client_api_core.BatchDetails, tablechanges: camcops_server.cc_modules.cc_client_api_core.UploadTableChanges) None[source]

Update server indexes, if required.

Also triggers background jobs to export “new arrivals”, if required.

Parameters