Exemplo n.º 1
0
        public static MultiUserSample Import(Database db, string user_id, string metric_name, string stamp, object val)
        {
            var sample = new MultiUserSample ();
            sample.UserId = db.GetUser (user_id).Id;

            // TODO collapse various DAP and DAAP library stats?
            sample.MetricId = db.GetMetric (metric_name).Id;

            DateTime stamp_dt;
            if (!DateTimeUtil.TryParseInvariant (stamp, out stamp_dt)) {
                Hyena.Log.Error ("Invalid stamp: ", stamp);
                return null;
            }

            sample.Stamp = stamp_dt;

            string value_str = val as string;
            if (value_str != null) {
                if (DateTimeUtil.TryParseInvariant (val as string, out value_dt)) {
                    // We want numeric dates to compare with
                    sample.Value = DateTimeUtil.ToTimeT (value_dt).ToString ();
                } else if (value_str.Contains (":") && TimeSpan.TryParse (val as string, out value_span)) {
                    sample.Value = value_span.TotalMilliseconds.ToString ();
                }
            }

            if (sample.Value == null) {
                sample.SetValue (val);
            }

            return sample;
        }
Exemplo n.º 2
0
        public static MultiUserSample Import(Database db, string user_id, string metric_name, string stamp, object val)
        {
            var sample = new MultiUserSample();

            sample.UserId = db.GetUser(user_id).Id;

            // TODO collapse various DAP and DAAP library stats?
            sample.MetricId = db.GetMetric(metric_name).Id;

            DateTime stamp_dt;

            if (!DateTimeUtil.TryParseInvariant(stamp, out stamp_dt))
            {
                Hyena.Log.Error("Invalid stamp: ", stamp);
                return(null);
            }

            sample.Stamp = stamp_dt;

            string value_str = val as string;

            if (value_str != null)
            {
                if (DateTimeUtil.TryParseInvariant(val as string, out value_dt))
                {
                    // We want numeric dates to compare with
                    sample.Value = DateTimeUtil.ToTimeT(value_dt).ToString();
                }
                else if (value_str.Contains(":") && TimeSpan.TryParse(val as string, out value_span))
                {
                    sample.Value = value_span.TotalMilliseconds.ToString();
                }
            }

            if (sample.Value == null)
            {
                sample.SetValue(val);
            }

            return(sample);
        }
Exemplo n.º 3
0
        public void Import()
        {
            var db = this;
            var sample_provider = SampleProvider;

            var  last_config        = LastReportNumber;
            long last_report_number = Int64.Parse(last_config.Value);

            var max_report_size = 150 * 1024;

            db.BeginTransaction();
            foreach (var file in System.IO.Directory.GetFiles("data"))
            {
                var match = report_number_regex.Match(file);
                if (!match.Success)
                {
                    continue;
                }

                long num = Int64.Parse(match.Groups[1].Captures[0].Value);
                if (num <= last_report_number)
                {
                    continue;
                }

                var file_size = new System.IO.FileInfo(file).Length;
                if (file_size > max_report_size)
                {
                    Log.InformationFormat("Skipping {0} because too large ({1:N0} KB compressed)", file, file_size / 1024.0);
                    continue;
                }

                last_report_number = num;
                Log.DebugFormat("Importing {0}", file);

                try {
                    JsonObject o = null;
                    using (var stream = System.IO.File.OpenRead(file)) {
                        using (var gzip_stream = new GZipInputStream(stream)) {
                            using (var txt_stream = new System.IO.StreamReader(gzip_stream)) {
                                o = new Deserializer(txt_stream.ReadToEnd()).Deserialize() as JsonObject;
                            }
                        }
                    }

                    if (o == null)
                    {
                        throw new Exception("Unable to parse JSON; empty file, maybe?");
                    }

                    string user_id        = (string)o["ID"];
                    int    format_version = (int)o["FormatVersion"];
                    if (format_version != MetricsCollection.FormatVersion)
                    {
                        Log.WarningFormat("Ignoring user report with old FormatVersion: {0}", format_version);
                        continue;
                    }

                    var metrics = o["Metrics"] as JsonObject;
                    foreach (string metric_name in metrics.Keys)
                    {
                        // Skip these; they are a ton of data, and really more for debug purposes
                        if (metric_name == "Banshee/LongSqliteCommand")
                        {
                            continue;
                        }

                        var samples = metrics[metric_name] as JsonArray;

                        string name = metric_name;
                        if (name.StartsWith(collapse_source_metric))
                        {
                            string [] pieces       = name.Split('/');
                            var       reduced_name = pieces[2].Substring(8, pieces[2].IndexOfAny(collapse_source_chars, 8) - 8);
                            name = String.Format("{0}{1}/{2}", collapse_source_metric, reduced_name, pieces[pieces.Length - 1]);
                        }

                        foreach (JsonArray sample in samples)
                        {
                            sample_provider.Save(MultiUserSample.Import(db, user_id, name, (string)sample[0], (object)sample[1]));
                        }
                    }
                    db.CommitTransaction();
                } catch (Exception e) {
                    Log.Exception(String.Format("Failed to read {0}", file), e);
                    db.RollbackTransaction();
                }

                last_config.Value = last_report_number.ToString();
                Config.Save(last_config);

                db.BeginTransaction();
            }
            db.CommitTransaction();

            last_config.Value = last_report_number.ToString();
            Config.Save(last_config);

            Log.InformationFormat("Done importing - last report # = {0}", last_report_number);
        }