|
| 1 | +use super::connection::{DbPool, get_connection_with_retry}; |
| 2 | +use crate::models::cache::{CacheStatsRecord, NewCacheStatsRecord, UpdateCacheStatsRecord}; |
| 3 | +use crate::schema::cache_stats; |
| 4 | +use chrono::Utc; |
| 5 | +use diesel::prelude::*; |
| 6 | + |
| 7 | +/// Cache statistics database operations |
| 8 | +pub struct CacheStatsOperations<'a> { |
| 9 | + pool: &'a DbPool, |
| 10 | +} |
| 11 | + |
| 12 | +impl<'a> CacheStatsOperations<'a> { |
| 13 | + pub fn new(pool: &'a DbPool) -> Self { |
| 14 | + Self { pool } |
| 15 | + } |
| 16 | + |
| 17 | + /// Gets the current cache stats record (there should only be one) |
| 18 | + pub fn get_cache_stats(&self) -> Result<Option<CacheStatsRecord>, diesel::result::Error> { |
| 19 | + let mut conn = get_connection_with_retry(self.pool).map_err(|e| { |
| 20 | + diesel::result::Error::DatabaseError( |
| 21 | + diesel::result::DatabaseErrorKind::UnableToSendCommand, |
| 22 | + Box::new(e.to_string()), |
| 23 | + ) |
| 24 | + })?; |
| 25 | + |
| 26 | + cache_stats::table |
| 27 | + .order(cache_stats::id.desc()) |
| 28 | + .first::<CacheStatsRecord>(&mut conn) |
| 29 | + .optional() |
| 30 | + } |
| 31 | + |
| 32 | + /// Updates the cache stats record |
| 33 | + pub fn update_cache_stats( |
| 34 | + &self, |
| 35 | + hit_count: u64, |
| 36 | + miss_count: u64, |
| 37 | + ) -> Result<CacheStatsRecord, diesel::result::Error> { |
| 38 | + let mut conn = get_connection_with_retry(self.pool).map_err(|e| { |
| 39 | + diesel::result::Error::DatabaseError( |
| 40 | + diesel::result::DatabaseErrorKind::UnableToSendCommand, |
| 41 | + Box::new(e.to_string()), |
| 42 | + ) |
| 43 | + })?; |
| 44 | + |
| 45 | + let now = Utc::now().naive_utc(); |
| 46 | + |
| 47 | + // Try to update existing record first |
| 48 | + let update_result = diesel::update(cache_stats::table) |
| 49 | + .set(UpdateCacheStatsRecord { |
| 50 | + hit_count: Some(hit_count as i64), |
| 51 | + miss_count: Some(miss_count as i64), |
| 52 | + updated_at: Some(now), |
| 53 | + }) |
| 54 | + .get_result::<CacheStatsRecord>(&mut conn); |
| 55 | + |
| 56 | + match update_result { |
| 57 | + Ok(record) => Ok(record), |
| 58 | + Err(diesel::result::Error::NotFound) => { |
| 59 | + // No record exists, create one |
| 60 | + let new_record = NewCacheStatsRecord { |
| 61 | + hit_count: hit_count as i64, |
| 62 | + miss_count: miss_count as i64, |
| 63 | + created_at: now, |
| 64 | + updated_at: now, |
| 65 | + }; |
| 66 | + |
| 67 | + diesel::insert_into(cache_stats::table) |
| 68 | + .values(&new_record) |
| 69 | + .get_result::<CacheStatsRecord>(&mut conn) |
| 70 | + } |
| 71 | + Err(e) => Err(e), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Increments hit count |
| 76 | + pub fn increment_hit_count(&self) -> Result<(), diesel::result::Error> { |
| 77 | + let mut conn = get_connection_with_retry(self.pool).map_err(|e| { |
| 78 | + diesel::result::Error::DatabaseError( |
| 79 | + diesel::result::DatabaseErrorKind::UnableToSendCommand, |
| 80 | + Box::new(e.to_string()), |
| 81 | + ) |
| 82 | + })?; |
| 83 | + |
| 84 | + let now = Utc::now().naive_utc(); |
| 85 | + |
| 86 | + // Try to increment existing record |
| 87 | + let update_result = diesel::update(cache_stats::table) |
| 88 | + .set(( |
| 89 | + cache_stats::hit_count.eq(cache_stats::hit_count + 1), |
| 90 | + cache_stats::updated_at.eq(now), |
| 91 | + )) |
| 92 | + .execute(&mut conn); |
| 93 | + |
| 94 | + match update_result { |
| 95 | + Ok(0) => { |
| 96 | + // No record exists, create one with hit_count = 1 |
| 97 | + let new_record = NewCacheStatsRecord { |
| 98 | + hit_count: 1, |
| 99 | + miss_count: 0, |
| 100 | + created_at: now, |
| 101 | + updated_at: now, |
| 102 | + }; |
| 103 | + |
| 104 | + diesel::insert_into(cache_stats::table) |
| 105 | + .values(&new_record) |
| 106 | + .execute(&mut conn)?; |
| 107 | + } |
| 108 | + Ok(_) => {} // Successfully updated |
| 109 | + Err(e) => return Err(e), |
| 110 | + } |
| 111 | + |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + |
| 115 | + /// Increments miss count |
| 116 | + pub fn increment_miss_count(&self) -> Result<(), diesel::result::Error> { |
| 117 | + let mut conn = get_connection_with_retry(self.pool).map_err(|e| { |
| 118 | + diesel::result::Error::DatabaseError( |
| 119 | + diesel::result::DatabaseErrorKind::UnableToSendCommand, |
| 120 | + Box::new(e.to_string()), |
| 121 | + ) |
| 122 | + })?; |
| 123 | + |
| 124 | + let now = Utc::now().naive_utc(); |
| 125 | + |
| 126 | + // Try to increment existing record |
| 127 | + let update_result = diesel::update(cache_stats::table) |
| 128 | + .set(( |
| 129 | + cache_stats::miss_count.eq(cache_stats::miss_count + 1), |
| 130 | + cache_stats::updated_at.eq(now), |
| 131 | + )) |
| 132 | + .execute(&mut conn); |
| 133 | + |
| 134 | + match update_result { |
| 135 | + Ok(0) => { |
| 136 | + // No record exists, create one with miss_count = 1 |
| 137 | + let new_record = NewCacheStatsRecord { |
| 138 | + hit_count: 0, |
| 139 | + miss_count: 1, |
| 140 | + created_at: now, |
| 141 | + updated_at: now, |
| 142 | + }; |
| 143 | + |
| 144 | + diesel::insert_into(cache_stats::table) |
| 145 | + .values(&new_record) |
| 146 | + .execute(&mut conn)?; |
| 147 | + } |
| 148 | + Ok(_) => {} // Successfully updated |
| 149 | + Err(e) => return Err(e), |
| 150 | + } |
| 151 | + |
| 152 | + Ok(()) |
| 153 | + } |
| 154 | +} |
0 commit comments