5 // Gabriel Burt <gabriel.burt@gmail.com>
7 // Copyright (c) 2010 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 using Hyena
.Data
.Sqlite
;
36 public class MultiUserSample
: Sample
, Hyena
.Data
.ICacheableItem
38 [DatabaseColumn (Index
= "SampleUserIdIndex")]
41 [DatabaseColumn (Index
= "SampleMetricIdIndex")]
45 public object CacheEntryId { get; set; }
46 public long CacheModelId { get; set; }
48 public MultiUserSample ()
52 static DateTime value_dt
;
53 static TimeSpan value_span
;
54 public static MultiUserSample
Import (Database db
, string user_id
, string metric_name
, string stamp
, object val
)
56 var sample
= new MultiUserSample ();
57 sample
.UserId
= db
.GetUser (user_id
).Id
;
59 // TODO collapse various DAP and DAAP library stats?
60 sample
.MetricId
= db
.GetMetric (metric_name
).Id
;
63 if (!DateTimeUtil
.TryParseInvariant (stamp
, out stamp_dt
)) {
64 Hyena
.Log
.Error ("Invalid stamp: ", stamp
);
68 sample
.Stamp
= stamp_dt
;
70 string value_str
= val
as string;
71 if (value_str
!= null) {
72 if (DateTimeUtil
.TryParseInvariant (val
as string, out value_dt
)) {
73 // We want numeric dates to compare with
74 sample
.Value
= DateTimeUtil
.ToTimeT (value_dt
).ToString ();
75 } else if (value_str
.Contains (":") && TimeSpan
.TryParse (val
as string, out value_span
)) {
76 sample
.Value
= value_span
.TotalMilliseconds
.ToString ();
80 if (sample
.Value
== null) {
81 sample
.SetValue (val
);