🔀 Merge Download Count Feature

This commit is contained in:
JMARyA 2025-01-26 11:16:43 +01:00
commit 249f8d873c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 635 additions and 3 deletions

View file

@ -4,6 +4,8 @@ use std::{
path::{Path, PathBuf},
};
use based::get_pg;
use sqlx::FromRow;
use tar::Archive;
use super::{Repository, arch::Architecture};
@ -17,14 +19,14 @@ pub struct Package {
pub arch: Architecture,
/// Name of the package
pub name: String,
pub rel: u64,
pub rel: i32,
/// Version of the package
pub version: Option<String>,
}
impl Package {
/// Create a new package
pub fn new(repo: &str, arch: Architecture, pkg_name: &str, version: &str, rel: u64) -> Self {
pub fn new(repo: &str, arch: Architecture, pkg_name: &str, version: &str, rel: i32) -> Self {
let pkg = Package {
repo: repo.to_string(),
arch,
@ -37,7 +39,7 @@ impl Package {
pkg
}
pub fn version(ver: &str) -> (String, u64) {
pub fn version(ver: &str) -> (String, i32) {
let mut splitted = ver.split('-').collect::<Vec<_>>();
let rel = splitted.pop().unwrap();
let ver = splitted.join("-");
@ -504,3 +506,42 @@ pub fn list_tar_file(tar: &PathBuf) -> Option<Vec<String>> {
Some(paths)
}
#[derive(Debug, Clone, FromRow)]
pub struct PackageMeta {
pub repo: String,
pub name: String,
pub arch: String,
pub version: String,
pub rel: String,
pub download_count: i32,
}
pub trait PackageMetaInfo {
fn download_amount(&self) -> impl std::future::Future<Output = i32>;
fn increase_download_count(&self) -> impl std::future::Future<Output = ()>;
}
impl PackageMetaInfo for Package {
async fn download_amount(&self) -> i32 {
let res: Option<(i32,)> = sqlx::query_as("SELECT download_count FROM package_meta WHERE repo = $1 AND name = $2 AND version = $3 AND arch = $4 AND rel = $5")
.bind(&self.repo)
.bind(&self.name)
.bind(self.version.as_ref().unwrap())
.bind(&self.arch.to_string())
.bind(&self.rel)
.fetch_optional(get_pg!()).await.unwrap();
res.map(|x| x.0).unwrap_or(0)
}
async fn increase_download_count(&self) {
sqlx::query("INSERT INTO package_meta (repo, name, arch, version, rel) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (repo, name, arch, version, rel) DO UPDATE SET download_count = package_meta.download_count + 1")
.bind(&self.repo)
.bind(&self.name)
.bind(&self.arch.to_string())
.bind(&self.version.as_ref().map(|x| x.to_string()).unwrap_or_default())
.bind(&self.rel)
.execute(get_pg!()).await.unwrap();
}
}