Commit 01850b2f authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Remove obsolete script lbfr...

parent 69206b8e
# -*- coding: utf-8 -*-
""" NAME
lbfr_people_author_moa
SYNOPSIS
check database consistency between people, author and moa events.
DESCRIPTION
In the track_lhcbfrance application, three events are defined: people
authors and M&O A. The script verify the consistency between them.
OPTIONS
-h, --help
Display the help and exit.
EXAMPLE
> cd ...track_events/scripts
> ./run -S test_lhcbfrance script lbfr_people_author_moa.py
> ./run -S track_lhcbfrance script lbfr_people_author_moa.py
AUTHOR
R. Le Gac -- Jan 2016
"""
from datetime import timedelta
AUTHORS = ['Chercheur',
'Emérite',
'Enseignant-chercheur',
'PhdStudent',
'PostDoc']
MOA = ['Chercheur',
'Emérite',
'Enseignant-chercheur',
'PostDoc']
def check_authors():
"""Check the events consistency for people belonging to the author list.
Author event starts 6 months after the people arrival and ends
one year after its departure.
"""
check_events(get_id(db.projects, project="LHCb"),
get_category_ids(AUTHORS),
"Author",
timedelta(days=182.),
timedelta(days=365))
def check_events(id_project,
people_categorie_ids,
event,
delay_start=timedelta(0.),
delay_end=timedelta(0.)):
"""check the consistency between "people event" and the one selected
in the arguments.
For people having an entry in the "people event" subset, the algorithm
checks that an "event" exits and that its start / end dates agree with
the "people event" ones.
The list of people categories is used to select a subset of
"people events", for example those related to authors, ...
Args:
id_project (int): identifier of the project.
people_categorie_ids (list): list of people categories to build a
subset of "people event".
event (str): the name of the event for which the consistency is checked.
delay_start (timedelta): the delay between the start date of the "people
event" and the "event"
delay_end (timedelta): the delay between the end date of the "people
event" and the "event".
"""
q_cat = db.history.id_people_categories.belongs(people_categorie_ids)
q_project = db.history.id_projects == id_project
id_evt_event = get_id(db.events, event=event)
q_evt_event = (db.history.id_events == id_evt_event) & (q_project)
id_evt_people = get_id(db.events, event="People")
q_evt_people = (db.history.id_events == id_evt_people)
q_evt_people &= (q_cat) & (q_project)
# scan the list of people
for person in db(db.people).select():
id_people = person.id
if id_people == UNDEF_ID:
continue
last_name = db.people[id_people].last_name
# look for "people event(s)" associates to the person
# determine start and end dates of people event
tpl = evt_people(q_evt_people, id_people)
if len(tpl) == 0:
continue
start_date, end_date, team_ids = tpl
# computed the expected dates for the event "event"
exp_start = (None if start_date is None else start_date + delay_start)
exp_end = (None if end_date is None else end_date + delay_end)
# check the event "event"
query = (q_evt_event) & (db.history.id_people == id_people)
myset = db(query)
nrows = myset.count()
# signal too many event
if nrows > 1:
print last_name, start_date, end_date, "→ to many %s event!\n" % event
print "Have a look and fix it by hand.\n"
continue
# the event is missing, add it
if nrows == 0:
print last_name, start_date, end_date, '→ no %s event' % event
if len(team_ids) != 1:
print "Team change during the period."
print "Have a look and fix it by hand.\n"
continue
data = dict(id_events=id_evt_event,
id_people=id_people,
id_projects=id_project,
id_teams=list(team_ids)[0],
start_date= exp_start,
end_date=exp_end)
fix_event(**data)
continue
# the event exits
row = myset.select(db.history.ALL).first()
evt_start, evt_end = row.start_date, row.end_date
# check the start date
# deal with special case
ok_start = evt_start is None and start_date is None
ok_start |= evt_start <= start_date
ok_start |= evt_start == exp_start
if not ok_start:
print last_name, "→ wrong %s start date" % event
print "The person start :", start_date
print "The event start :", evt_start
print "The expected start:", exp_start
if evt_start and exp_start:
print "Delta [days] :", (evt_start - exp_start).days
data = dict(id=row.id, start_date= exp_start)
fix_event(**data)
# check the end date
ok_end = evt_end is None and end_date is None
ok_end |= evt_end == exp_end
if not ok_end:
print last_name, "→ wrong %s end date" % event
print "The person quit :", end_date
print "The event end :", evt_end
print "The expected end:", exp_end
if evt_end and exp_end:
print "Delta [days] :", (evt_end - exp_end).days
data = dict(id=row.id, end_date= exp_end)
fix_event(**data)
def check_moa():
"""Check the events consistency for people belonging to the M&O A list.
"""
check_events(get_id(db.projects, project="LHCb"),
get_category_ids(MOA),
"M&O Cat A",
timedelta(days=0),
timedelta(days=0))
def evt_people(query_event_people, id_people):
"""For a given person determine the minimum start date and the
maximum end date of the people event. It also extract the list
of team ids.
Args:
query_event_people(gluon.dal.Query): the query to select all the
"people event" or a subset of them.
id_people (int): the identifier of the person in the people table.
Returns:
tuple: containing the start and the end dates. It is empty
when there is no people events associated to the person.
The last element is the list of id_teams.
"""
start_dates, end_dates, team_ids = set(), set(), set()
query = (query_event_people) & (db.history.id_people == id_people)
myset = db(query)
if myset.count() == 0:
return ()
for row in myset.select(db.history.ALL):
start_dates.add(row.start_date)
end_dates.add(row.end_date)
team_ids.add(row.id_teams)
start_date = (None if None in start_dates else min(start_dates))
end_date = (None if None in end_dates else max(end_dates))
return (start_date, end_date, team_ids)
def fix_event(**kwargs):
"""Fix the event.
Insert of update the event.
Keyword arguments:
id (int): id of the event
id_projects (int):
id_teams (int):
end_date (datetime or None):
start_date (datetime or None:
"""
rep = raw_input("Fix it [y/N]: ")
if rep.upper() != "Y":
print
return
# update
if "id" in kwargs:
id_rec = kwargs["id"]
del kwargs["id"]
db(db.history.id==id_rec).update(**kwargs)
# insert
else:
db.history.insert(**kwargs)
# commit
print "Database modified.\n"
db.commit()
def get_category_ids(categories):
"""Return a set of people_categories.id corresponding to the list
of categories.
Args
categories (list): list of people categories
Returns:
set: id corresponding to the list of categories.
"""
ids = set()
for category in categories:
# protection
if get_id(db.people_categories, category=category) is None:
print "Error: invalid category:", category
sys.exit(1)
query = db.people_categories.category == category
rows = db(query).select(db.people_categories.id)
dummy = [ids.add(row.id) for row in rows]
return ids
if __name__ == "__main__":
import re
import sys
from argparse import ArgumentParser
from plugin_dbui import get_id, UNDEF_ID
# command line options
parser = ArgumentParser()
args = parser.parse_args()
# protection
if request.application not in ("test_lhcbfrance", "track_lhcbfrance"):
print "Invalid application", request.application
sys.exit(1)
# check and fix authors / M&O A
check_authors()
check_moa()
# exit
sys.exit(0)
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment