MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: Summary Stats
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So I've upgraded our match statement, have real function calls, and I put just the empty functions for each one of those,
0:08
'cause how much fun is it to watch me write async def, the name of the function, over and over and over. So what we're gonna do now that we have
0:15
these empty functions in place is we're gonna start filling them out to work with the database. And let's start with summary.
0:21
That one's pretty straightforward. For summary, we need to get three pieces of information from the database, the package count, the user count.
0:29
Those are both pretty straightforward and the release count, which is a little bit extra work, but just a bit.
0:35
Now, I could go over here and I could start writing a wait package, dot, dot, dot, you know, do direct Beanie queries here. How reusable is that?
0:46
How much, how testable is that? How good of an idea is that? It is not a good idea. So instead, I'm gonna introduce this category of functions
0:56
or modules called services, not services like FastAPI, but just they provide services to other parts of the app.
1:03
They're not quite database level things, just slightly higher than that, okay? So I'm gonna create a sub package,
1:11
just a folder with a dunder net file called services. And in here, we're going to add, let's start with package service. And in the package service,
1:25
we're going to want to have a function, an async function, like almost all of them will be 'cause they're talking to the database.
1:32
This will just be package count or count packages. Take your pick. Or since it's in the package service, do we just call it count? I'll go with that.
1:44
We're gonna need multiple types of count. I'm going with package count. All right, and this is gonna return an integer
1:49
so we can use our type information here. Now, in order to do a query, we're gonna need access to our model package.
1:58
So we're gonna import that from models.package, package like that at the top, perfect. And then for count, it turns out that is super easy.
2:08
We can just say return this.count. Notice the CLS, that means it's a type and it applies to the whole type, not an instance, like save, for example,
2:18
would be an object or instance level one. Now the problem here is we're talking to the database. It has to be asynchronous.
2:26
So we await that async call. And there we go. Let's do something similar with a user service. So I can copy and paste that. Let's say user service.
2:37
And this is gonna be .user, import user. And this will be a user count. And guess what? That's user. Done. Done, done. Excellent. Okay. There we go.
2:53
Now what we want to do is just call this. So we'll say package service like that, let PyCharm import it at the top. And we just say package count.
3:02
Now again, this is async, right? Returns a coroutine, means you have to await it. And then the user count, we're going to await user service,
3:12
dot user count. I'll just run it again, see that it works. Yes, look at that. We got our summary stats working.
3:21
So 4,892 packages and we have 4,295 users. Excellent, what about releases? Remember, the releases looks a little bit different
3:32
because the releases are embedded in here. We kept count of that kind of information in a separate place so that we don't have
3:43
to have a real slow query here, right? And we put that into this release analytics section And on there is a total releases count.
3:52
So let's go back to the package service. It seems like it's kind of should be in charge of packages and releases. So we'll say release count.
4:01
But what we do here is different. We need to get that one record from the database. So we'll say, let's call it analytics or whatever.
4:09
So release analytics. We're just gonna do instead of a count, we're going to do a find one and get the one that's back if it doesn't exist. So if not
4:23
analytics or if analytics is none, we could print error, no analytics, right? Something
4:37
like this. Now, of course, this is also asynchronous, right? If you look at this, this is a co routine
4:45
again, so we will have to await it. But if it does exist, instead of package.count, we
4:51
go to analytics, and it has total releases like that. Although we don't await that. Great.
5:00
Okay, so let's try it one more time. See if we have our summary working. The most important One thing we're missing is calling that function.
5:10
AwaitPackageService.releasesCount. There we have it. The same number we just saw over in Studio 3T, we have 231,000 releases across 4,892 packages.
5:27
Awesome. Our summary stats are done.