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

Add subscriptions functionality #194

Closed
wants to merge 14 commits into from
Closed

Add subscriptions functionality #194

wants to merge 14 commits into from

Conversation

Robin5605
Copy link
Contributor

Closes #193

@Robin5605 Robin5605 force-pushed the subscriptions branch 2 times, most recently from 050b646 to dbd93d9 Compare February 22, 2024 21:02
@Robin5605 Robin5605 closed this Feb 22, 2024
@Robin5605 Robin5605 reopened this Feb 22, 2024
@Robin5605 Robin5605 marked this pull request as ready for review February 23, 2024 02:50
@Robin5605 Robin5605 marked this pull request as draft February 23, 2024 03:10
@Robin5605 Robin5605 marked this pull request as ready for review February 24, 2024 02:11
if nn_name:
query = query.where(Scan.name == name)
if nn_version:
query = query.where(Scan.version == version)
if nn_since:
query = query.where(Scan.finished_at >= dt.datetime.fromtimestamp(since, tz=dt.timezone.utc))

data = session.scalars(query)
packages = session.scalars(query).unique().all()

log.info("Package information queried")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be debug or lower



class AddSubscription(Subscription):
"""Payload for when a user wants to subscribe to a malicious package."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily malicious.

Suggested change
"""Payload for when a user wants to subscribe to a malicious package."""
"""Payload for when a user wants to subscribe to a package."""



class RemoveSubscription(Subscription):
"""Payload for when a user wants to unsubscribe from a malicious package."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Payload for when a user wants to unsubscribe from a malicious package."""
"""Payload for when a user wants to unsubscribe from a package."""

except PackageNotFoundError:
continue

package = session.scalar(select(Package).where(Package.name == package_metadata.title))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only make 3 queries in this endpoint. 1 to deduplicate and 2 to insert the new packages and scans. We should collect all the Scans at once, then insert everything at the end, instead of querying for every package.

Comment on lines +272 to +277
package = session.scalar(select(Package).where(Package.name == name))
if package is None:
package = Package(name=name)

package.scans.append(scan)
session.add(package)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can minimize overhead here by inserting both of these in one query, rather than sending a query, running some logic, then sending another query.


person = session.scalar(query)
if not person:
person = Person(discord_id=payload.discord_id, email_address=payload.email_address)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this endpoint be the only way to create Person entries seems off. We need some way to manage these.

Comment on lines +73 to +83
person = session.scalar(select(Person).where(Person.id == payload.user_id))
if person is None:
raise HTTPException(404, detail="A user with that ID could not be found")

package = session.scalar(select(Package).where(Package.name == payload.package_name))
if package is None:
raise HTTPException(404, detail="A package with that name could not be found")

person.packages.remove(package)

session.commit()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we delete directly from the subscriptions table and save a query?

if person is None:
raise HTTPException(404, detail="A user with that ID could not be found")

subscribed_packages = [package.name for package in person.packages]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Either put this list comp inline with the GetPerson creation like in get_all_people_route or make the same variable in the other function.

@@ -13,7 +13,8 @@ def oldest_queued_package(db_session: Session):
return db_session.scalar(select(func.min(Scan.queued_at)).where(Scan.status == Status.QUEUED))


def test_min_queue_date_of_queued_rows(test_data: list[Scan], db_session: Session):
def test_min_queue_date_of_queued_rows(db_session: Session):
test_data = db_session.scalars(select(Scan)).all()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why get rid of the fixture to just grab the data on the first line for every test?

queued_at=datetime.now(),
queued_by="remmy",
)
person = Person(discord_id=123, email_address="[email protected]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this and other repeated data to a fixture

@Robin5605 Robin5605 requested a review from a team as a code owner April 5, 2024 14:43
@Robin5605 Robin5605 requested a review from a team as a code owner April 5, 2024 14:43
@shenanigansd shenanigansd marked this pull request as draft May 30, 2024 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add subscriptions functionality
2 participants