private void IncrementReturningDailyCounter(TransitStatsRequest request) { if (!request.IncrementNewUser && !request.IncrementReturningUser) { return; } DateTime now = DateTime.UtcNow; DateTime ts = new DateTime(now.Year, now.Month, now.Day); CounterReturningDaily cntr = (CounterReturningDaily)Session.CreateCriteria(typeof(CounterReturningDaily)) .Add(Expression.Eq("Timestamp", ts)) .UniqueResult(); if (cntr == null) { cntr = new CounterReturningDaily(); cntr.Timestamp = ts; cntr.ReturningTotal = 0; cntr.NewTotal = 0; } if (request.IncrementReturningUser) { cntr.ReturningTotal++; } if (request.IncrementNewUser) { cntr.NewTotal++; } Session.Save(cntr); }
private void IncrementRawCounter(TransitStatsRequest request) { string uri = request.RequestUri; if (uri.Length > 255) { uri = uri.Substring(0, 255); } // increment the raw uri counter Counter counter_raw = (Counter)Session.CreateCriteria(typeof(Counter)) .Add(Expression.Eq("Uri", uri)).UniqueResult(); if (counter_raw == null) { counter_raw = new Counter(); counter_raw.Uri = uri; counter_raw.Created = DateTime.UtcNow; counter_raw.Total = 0; } counter_raw.Total++; counter_raw.Modified = DateTime.UtcNow; try { Session.Save(counter_raw); } catch { Session.Evict(counter_raw); } }
private void UpdateRefererQuery(TransitStatsRequest request) { if (string.IsNullOrEmpty(request.RefererQuery)) { return; } RefererQuery query = (RefererQuery)Session.CreateCriteria(typeof(RefererQuery)) .Add(Expression.Eq("Keywords", request.RefererQuery)) .UniqueResult(); if (query == null) { query = new RefererQuery(); query.Created = DateTime.UtcNow; query.Total = 0; query.Keywords = request.RefererQuery; } query.Updated = DateTime.UtcNow; query.Total++; try { Session.Save(query); } catch { Session.Evict(query); } }
public void Track(TransitStatsRequest request) { ITransaction trans = Session.BeginTransaction(); try { // global counters IncrementHourlyCounter(); IncrementDailyCounter(); IncrementWeeklyCounter(); IncrementMonthlyCounter(); IncrementYearlyCounter(); // per-user counter IncrementUniqueMonthlyCounter(request); IncrementReturningDailyCounter(request); // per-uri page counter IncrementRawCounter(request); // Referer hosts UpdateRefererHost(request); // Referer query UpdateRefererQuery(request); trans.Commit(); } catch { trans.Rollback(); throw; } }
private void IncrementUniqueMonthlyCounter(TransitStatsRequest request) { if (!request.IncrementUniqueMonthlyUser) { return; } DateTime now = DateTime.UtcNow; DateTime ts = new DateTime(now.Year, now.Month, 1); CounterUniqueMonthly cntr = (CounterUniqueMonthly)Session.CreateCriteria(typeof(CounterUniqueMonthly)) .Add(Expression.Eq("Timestamp", ts)) .UniqueResult(); if (cntr == null) { cntr = new CounterUniqueMonthly(); cntr.Timestamp = ts; cntr.Total = 0; } cntr.Total++; Session.Save(cntr); }
private void UpdateRefererHost(TransitStatsRequest request) { if (string.IsNullOrEmpty(request.RefererUri)) { return; } // don't track navigation between pages Uri requesturi = new Uri(request.RequestUri); Uri refereruri = new Uri(request.RefererUri); if (refereruri.Host == requesturi.Host) { return; } RefererHost host = (RefererHost)Session.CreateCriteria(typeof(RefererHost)) .Add(Expression.Eq("Host", refereruri.Host)) .UniqueResult(); if (host == null) { // find whether this is a dup host RefererHostDup duphost = (RefererHostDup) Session.CreateSQLQuery( "SELECT {R.*} FROM RefererHostDup {R}" + " WHERE '" + Renderer.SqlEncode(refereruri.Host) + "' LIKE Host") .AddEntity("R", typeof(RefererHostDup)) .SetMaxResults(1) .UniqueResult(); if (duphost != null) { host = duphost.RefererHost; } } if (host == null) { host = new RefererHost(); host.Created = DateTime.UtcNow; host.Host = refereruri.Host; host.Total = 0; } host.Updated = DateTime.UtcNow; // // hack: try our best to insert the host record // when an exception happens this session can't be flushed any more // if (!string.IsNullOrEmpty(request.RefererUri) && (request.RefererUri.Length < 255)) { host.LastRefererUri = request.RefererUri; } if (!string.IsNullOrEmpty(request.RequestUri) && (request.RequestUri.Length < 255)) { host.LastRequestUri = request.RequestUri; } host.Total++; try { Session.Save(host); } catch { Session.Evict(host); } }