Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listruns(): use databroker v2 API #332

Closed
prjemian opened this issue Jun 26, 2020 · 3 comments · Fixed by #344
Closed

listruns(): use databroker v2 API #332

prjemian opened this issue Jun 26, 2020 · 3 comments · Fixed by #344
Assignees
Milestone

Comments

@prjemian
Copy link
Contributor

prjemian commented Jun 26, 2020

The listruns() utility function uses the databroker v0 and v1 API. Time to upgrade that to the v2 API AND upgrade the search capabilities.

This continues the maintenance of #330.

@prjemian prjemian self-assigned this Jun 26, 2020
@prjemian
Copy link
Contributor Author

This means we need to learn how to manage multiple catalog names.

@prjemian
Copy link
Contributor Author

This short program demonstrates the lookup process:

program source
#!/usr/bin/env python

from apstools.utils import listruns
from databroker import catalog
from databroker.queries import TimeRange
import datetime
import pyRestTable
import time


def ts2dt(timestamp):
    return datetime.datetime.fromtimestamp(timestamp)


def lookup_catalog(*args, 
                   cat=None, 
                   since=None, 
                   until=None,
                   **query):
    """
    look for runs in the given catalog, limited by search terms

    The basic search::

        results.search({'num_points': {'$gt': 50}})
        results.search(plan_name={"$in": ["scan", "count"]})

    For more complex queries, see
    https://blueskyproject.io/databroker/v2/user/index.html#search-for-runs
    and additional mongo query terms at
    https://docs.mongodb.com/v3.2/reference/operator/query/#query-selectors
    """
    if cat is None:
        raise ValueError("must provide a catalog to search")
    elif len(cat) == 0:
        raise ValueError("empty catalog")

    since = since or "1980"     # before the APS
    until = until or "2200"     # after a long time from now

    cat = cat.search(TimeRange(since=since, until=until))
    if len(cat) == 0:
        raise ValueError(
            f"no runs start between dates {since} <= run < {until}")

    cat = cat.search(query)
    if len(cat) == 0:
        raise ValueError(f"no runs matching search terms {query}")

    return cat


def main():
    for cat_name in list(catalog):
        print(f"catalog '{cat_name}' : {len(catalog[cat_name])} run(s)")

    findings = lookup_catalog(
        cat=catalog["mongodb_config"], 
        # scan_id=1, 
        plan_name={"$in": ["scan", "count"]},
        until="2020-05",
        )
    print(f"found {len(findings)} run(s)")
    quantity = 20

    table = pyRestTable.Table()
    table.labels = "# run_id plan start_time end_time".split()
    for i, item in enumerate(findings.items()):
        if i >= quantity:
            break
        uid, run = item
        plan = run.metadata["start"]["plan_name"]
        t_start = ts2dt(run.metadata["start"]["time"])
        t_stop = ts2dt(run.metadata["stop"]["time"])
        table.addRow((i+1, uid[:7], plan, t_start, t_stop))
    print(table)


if __name__ == "__main__":
    t0 = time.time()
    main()
    print(f"completed in {time.time()-t0:.03f}s")
program output
(base) jemian@wow .../Documents/sandbox $ source activate bluesky_2020_5
(bluesky_2020_5) jemian@wow .../Documents/sandbox $ /home/beams/JEMIAN/.conda/envs/bluesky_2020_5/bin/python /home/beams1/JEMIAN/Documents/sandbox/exercise_databroker.py
catalog 'bluesky_jemian' : 6 run(s)
catalog 'mongodb_config' : 2212 run(s)
found 267 run(s)
== ======= ===== ========================== ==========================
#  run_id  plan  start_time                 end_time                  
== ======= ===== ========================== ==========================
1  ffea142 scan  2020-04-22 01:23:50.285154 2020-04-22 01:24:02.688853
2  a452fca scan  2020-02-07 17:25:06.569987 2020-02-07 17:25:21.943831
3  c5888d3 scan  2020-02-07 17:24:38.956347 2020-02-07 17:24:55.999934
4  efdabec count 2019-10-10 10:48:46.849207 2019-10-10 10:48:47.053579
5  f3d079b scan  2019-06-26 18:02:48.764402 2019-06-26 18:03:00.223207
6  d9a941b scan  2019-06-26 17:45:48.662637 2019-06-26 17:45:59.030007
7  db2b376 scan  2019-06-26 16:38:25.183099 2019-06-26 16:38:29.531819
8  92c9447 scan  2019-06-07 10:35:35.754503 2019-06-07 10:35:46.669580
9  3364fd7 count 2019-04-12 16:54:06.757963 2019-04-12 16:54:17.213930
10 c564196 count 2019-04-12 16:10:30.190482 2019-04-12 16:10:40.555634
11 d73ac08 count 2019-04-12 13:37:26.307637 2019-04-12 13:38:02.868660
12 f0575e5 count 2019-04-12 13:36:52.778201 2019-04-12 13:36:56.677720
13 0534c7f count 2019-04-12 13:36:39.693377 2019-04-12 13:36:43.628311
14 99af5cc count 2019-04-12 13:35:03.014027 2019-04-12 13:35:04.296118
15 7a6eb8b scan  2019-04-09 15:01:03.187160 2019-04-09 15:01:03.639877
16 dd65002 scan  2019-04-09 14:52:29.762735 2019-04-09 14:52:31.197622
17 31433fc scan  2019-04-09 14:50:35.656701 2019-04-09 14:50:37.068442
18 04dd51a scan  2019-04-09 14:49:52.272295 2019-04-09 14:49:53.622844
19 98ab97e scan  2019-04-09 14:44:54.504829 2019-04-09 14:44:55.829448
20 3ed4058 scan  2019-04-09 14:42:57.119880 2019-04-09 14:42:57.259392
== ======= ===== ========================== ==========================

completed in 2.242s

@prjemian prjemian added this to the 1.2.7 milestone Jun 26, 2020
@prjemian
Copy link
Contributor Author

The current listruns() was implicated in an issue of the XPCS instrument. See aps-8id-dys/ipython-8idiuser#190

@prjemian prjemian modified the milestones: 1.2.7, 1.3.0 Jun 30, 2020
prjemian added a commit that referenced this issue Jul 11, 2020
prjemian added a commit that referenced this issue Jul 12, 2020
prjemian added a commit that referenced this issue Jul 12, 2020
prjemian added a commit that referenced this issue Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant