Table Of Contents

Previous topic

blitz.data

Next topic

models

This Page

database

DatabaseClient

class blitz.data.database.DatabaseClient(verbose=False, path=':memory:')[source]

Provides database operations for the client using SqlAlchemy

add(item)[source]

Adds a single item to the database

Parameters:item – The item to add to the database
Returns:The item that was added
add_cache(time_logged, category_id, value)[source]

Quick helper to add a cache record to the database

Parameters:
  • time_logged – the timestamp the reading was logged
  • category_id – the ID of the category this reading should be added to
  • value – the value of the reading
Returns:

the Reading that was generated

add_many(items)[source]

Adds the given items to the database with the passed attributes

Parameters:items – A list of Model instances to be added
Returns:The list of items that was added (should now be populated with IDs)
add_reading(session_id, time_logged, category_id, value)[source]

Quick helper to add a reading record to the database

Parameters:
  • session_id – the ID of the session to create the reading for
  • time_logged – the timestamp the reading was logged
  • category_id – the ID of the category this reading should be added to
  • value – the value of the reading
Returns:

the Reading that was generated

all(model)[source]

Returns all the records for a given model type

Parameters:model – The model to return all records for
Returns:A list of all records for a given model
clear_cache()[source]

Clears all variables from the cache

Returns:the Reading that was generated
clear_errors()[source]

Removes all errors from the database

Returns:nothing
clear_session_data(session_id)[source]

Clears historic session data for the given session (ref) id

Parameters:session_id – the id of the session to clear data for
create_tables(force_drop=False)[source]

Uses the supplied engine and models to create the required table structure

Parameters:force_drop – forces the existing tables to be dropped and recreated if True (default False)
Returns:nothing
delete_session(session_id)[source]

Clears all the data for a given sessio and removes it from the session list. This process must be mirrored on the server otherwise the session will be added back into the list when a “Session List” query is carried out

Parameters:session_id – the id of the session to be deleted
find(model, query)[source]

Returns ALL items which match the given query

Parameters:
  • model – The model to query on
  • query – the dictionary of “field: value” pairs to filter on
Returns:

a list of all matching records

get(model, query)[source]

Gets a single item from the database (the first that matches the query dict)

Parameters:
  • model – the model to query
  • query – the dict of “field: value” pairs to filter on
Returns:

A single model matching the query string

get_by_id(model, model_id)[source]

Gets an object by ID

get_cache(since=0)[source]

Gets cached variables. If a “since” argument is applied, it only returns values that have been read since this time. If no since value is applied then it returns the most recent. All queries are limited to 50 values per variable

Parameters:since – a UNIX timestamp to retrieve values since
Returns:A list (variable) of lists (values)
get_cache_variables()[source]

Gets the variables associated with the cache

Returns:a list of Cache objects
get_config(key)[source]

Gets a config value from the database from the given key

Parameters:key – the config key to retrieve
Returns:the Config item matching the given key
get_or_create_category(key)[source]

Gets the id of a category, or if none is found, create it and return the ID of the created object

Parameters:key – the category name to get or create
Returns:the id of the Category that was retrieved or added
get_session_readings(session_id)[source]

Gets a list of readings for a particular session

Parameters:session_id – the ref_id of the session to get variables for.
Returns:a list of Reading objects for the session ID
get_session_variables(session_id)[source]

Gets the variables associated with a given session :param session_id: the ref_id of the session to get variables for. :returns: a list of Reading objects

handle_error(err_id)[source]

Removes a single error from the database

Returns:nothing
load_fixtures(testing=False)[source]

Loads fixtures from blitz.data.fixtures

Parameters:testing – if True all fixtures are added, otherwise just configuration
Returns:nothing
log_error(description, severity=1)[source]

Log an error to the database - this will be sent to the client

Parameters:
  • description – the description of the error
  • severity – the severity of the error (defaults to 1)
Returns:

the Notification that was added

set_config(key, value, do_update=True)[source]

Sets a config value in the database, adding or updating as required

Parameters:
  • key – the config key to set
  • value – the config value to set for the given key
Returns:

nothing

update_session_availability(session_id)[source]

Updates a session information after a download to determine if it is available for viewing.

Parameters:session_id – the ref_id of the session being checked
Returns:nothing
update_session_list(sessions_list)[source]

Session list comes in [session_id, start_timestamp, end_timstamp] format This replaces the existing session list

Parameters:sessions_list – a list of lists of session information [id, timeStarted, timeStopped, numberOfReadings]
Returns:nothing

DatabaseServer

class blitz.data.database.DatabaseServer[source]

The redis database server - retains several documents:

  • session the id of the current session
  • session_N_start the timestamp when the logging session N began
  • session_N_end the timestamp when logging session N ended
  • sessions a list of session in the database
  • session_N a queue of raw session data for session_id N
available_sessions()[source]

Gets all the available session from the database as a list

Returns:a list of available sessions or an empty list if there are none
build_client_session_list()[source]

Builds a list of session information in the format:

[
    [ID, start timestamp, end timestamp, number of readings]
    ...
]
Returns:the list of sessions
delete_session(session_id)[source]

Deletes a session and all associated data from the database. The session number will not be reused

Parma session_id:
 the session ID to delete
Returns:nothing
flush()[source]

Cleans out the database

Warning

USE WITH CAUTION - this will irrevocably destroy all logged session data

Returns:nothing
get_all_from_session(session_id)[source]

Gets all messages logged during the given session ID

Parameters:session_id – the ID of the session to return information for
Returns:the readings from the session
get_latest_from_session(session_id)[source]

Gets the most recent logged variable from the database and returns it as a raw message string.

Parameters:session_id – The id of the session to return the top variable from
Returns:A string containing the last raw serial message received from a board in this session
get_ten_from_session()[source]

Gets the last ten readings from the logging session

Returns:the list of raw serial messages
queue(message)[source]

Queues a new message against the current session. If no session is being run then it logs a warning and does nothing

Parameters:message – the message to push onto the session data
start_session()[source]

Starts a new session, creating the required session_N variables

Returns:the ID of the newly created session
stop_session()[source]

Stops the current session

Returns:nothing