• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Clinical-Genomics / arnold / 7102454002

05 Dec 2023 02:52PM UTC coverage: 0.0%. Remained the same
7102454002

push

github

web-flow
Update to pydantic v2 (#47) (patch)

Added
Move the models to pydantic v2

add Codeowners for automatic reviewer tagging

0 of 89 new or added lines in 5 files covered. (0.0%)

28 existing lines in 5 files now uncovered.

0 of 699 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/arnold/api/api_v1/endpoints/plots.py
1
import datetime as dt
×
2
from typing import Optional, List, Literal
×
3
from pydantic import BaseModel
×
4

5
from arnold.adapter import ArnoldAdapter
×
6

7
from fastapi import APIRouter, Depends, status
×
8
from fastapi.responses import JSONResponse
×
9
import logging
×
10
from arnold.settings import get_arnold_adapter
×
11

12
LOG = logging.getLogger(__name__)
×
13

14
router = APIRouter()
×
15

16

17
class ReceivedPlotDatapointsModel(BaseModel):
×
18
    tag: Optional[str]
×
19
    priority: Optional[str]
×
20
    total: int
×
21

22

23
class ReceivedPlotModel(BaseModel):
×
24
    date: str
×
25
    datapoints: List[ReceivedPlotDatapointsModel]
×
26

27

28
@router.get("/received", response_model=List[ReceivedPlotModel], response_model_exclude_none=True)
×
29
def received(
×
30
    adapter: ArnoldAdapter = Depends(get_arnold_adapter),
31
    date_min: Optional[dt.date] = dt.date.min,
32
    date_max: Optional[dt.date] = dt.date.max,
33
):
34
    samples = adapter.sample_collection.aggregate(
×
35
        [
36
            {
37
                "$match": {
38
                    "delivery_date": {"$exists": 1},
39
                    "received_date": {
40
                        "$gte": dt.datetime.fromordinal(date_min.toordinal()),
41
                        "$lte": dt.datetime.fromordinal(date_max.toordinal()),
42
                    },
43
                }
44
            },
45
            {
46
                "$project": {
47
                    "received_date": 1,
48
                    "sample_id": 1,
49
                    "tag": {"$substr": ["$application", 0, 3]},
50
                    "received_year": {"$year": "$received_date"},
51
                    "received_month": {"$month": "$received_date"},
52
                }
53
            },
54
            {
55
                "$group": {
56
                    "_id": {"tag": "$tag", "year": "$received_year", "month": "$received_month"},
57
                    "total": {"$sum": 1},
58
                }
59
            },
60
            {
61
                "$project": {
62
                    "year": "$_id.year",
63
                    "month": "$_id.month",
64
                    "date": {
65
                        "$concat": [
66
                            {"$substr": ["$_id.year", 0, -1]},
67
                            "/",
68
                            {"$substr": ["$_id.month", 0, -1]},
69
                        ]
70
                    },
71
                    "tag": "$_id.tag",
72
                    "total": "$total",
73
                    "_id": 0,
74
                }
75
            },
76
            {
77
                "$group": {
78
                    "_id": {"date": "$date", "year": "$year", "month": "$month"},
79
                    "datapoints": {"$push": "$$ROOT"},
80
                }
81
            },
82
            {
83
                "$project": {
84
                    "date": "$_id.date",
85
                    "year": "$_id.year",
86
                    "month": "$_id.month",
87
                    "datapoints": "$datapoints",
88
                    "_id": 0,
89
                }
90
            },
91
            {"$unset": ["datapoints.date", "datapoints.year", "datapoints.month"]},
92
            {"$sort": {"year": -1, "month": -1}},
93
            {"$unset": ["year", "month"]},
94
        ]
95
    )
96
    return list(samples)
×
97

98

99
@router.get("/turnaround", response_model=List[ReceivedPlotModel], response_model_exclude_none=True)
×
100
def turnaround(
×
101
    adapter: ArnoldAdapter = Depends(get_arnold_adapter),
102
    date_min: Optional[dt.date] = dt.date.min,
103
    date_max: Optional[dt.date] = dt.date.max,
104
    group: Optional[Literal["priority", "tag"]] = "tag",
105
):
106
    samples = adapter.sample_collection.aggregate(
×
107
        [
108
            {
109
                "$match": {
110
                    "delivery_date": {"$exists": 1},
111
                    "received_date": {
112
                        "$gte": dt.datetime.fromordinal(date_min.toordinal()),
113
                        "$lte": dt.datetime.fromordinal(date_max.toordinal()),
114
                    },
115
                }
116
            },
117
            {
118
                "$project": {
119
                    "received_date": 1,
120
                    "delivery_date": 1,
121
                    "sample_id": 1,
122
                    "tag": {"$substr": ["$application", 0, 3]},
123
                    "received_year": {"$year": "$received_date"},
124
                    "received_month": {"$month": "$received_date"},
125
                    "priority": "$priority",
126
                    "turnaround_time": {
127
                        "$divide": [{"$subtract": ["$delivery_date", "$received_date"]}, 86400000]
128
                    },
129
                }
130
            },
131
            {"$match": {"turnaround_time": {"$gt": 0}}},
132
            {
133
                "$group": {
134
                    "_id": {
135
                        f"{group}": f"${group}",
136
                        "year": "$received_year",
137
                        "month": "$received_month",
138
                    },
139
                    "total": {"$avg": "$turnaround_time"},
140
                }
141
            },
142
            {
143
                "$project": {
144
                    "year": "$_id.year",
145
                    "month": "$_id.month",
146
                    "date": {
147
                        "$concat": [
148
                            {"$substr": ["$_id.year", 0, -1]},
149
                            "/",
150
                            {"$substr": ["$_id.month", 0, -1]},
151
                        ]
152
                    },
153
                    f"{group}": f"$_id.{group}",
154
                    "total": {"$round": ["$total", 0]},
155
                    "_id": 0,
156
                }
157
            },
158
            {
159
                "$group": {
160
                    "_id": {"date": "$date", "year": "$year", "month": "$month"},
161
                    "datapoints": {"$push": "$$ROOT"},
162
                }
163
            },
164
            {
165
                "$project": {
166
                    "date": "$_id.date",
167
                    "year": "$_id.year",
168
                    "month": "$_id.month",
169
                    "datapoints": "$datapoints",
170
                    "_id": 0,
171
                }
172
            },
173
            {"$unset": ["datapoints.date", "datapoints.year", "datapoints.month"]},
174
            {"$sort": {"year": -1, "month": -1}},
175
            {"$unset": ["year", "month"]},
176
        ]
177
    )
178
    return list(samples)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc