~alexharrington/xibo/upgrade-db-backup-switch

« back to all changes in this revision

Viewing changes to client/dotNET/StatLog.cs

  • Committer: Dan Garner
  • Date: 2009-08-02 17:26:04 UTC
  • mfrom: (55.3.7 1.0.0)
  • Revision ID: mail@dangarner.co.uk-20090802172604-8dgszq4st41u39qu
MergedĀ lp:~dangarner/xibo/xmds-logging-103

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Xibo - Digitial Signage - http://www.xibo.org.uk
 
3
 * Copyright (C) 2009 Daniel Garner
 
4
 *
 
5
 * This file is part of Xibo.
 
6
 *
 
7
 * Xibo is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU Affero General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * any later version. 
 
11
 *
 
12
 * Xibo is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU Affero General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Affero General Public License
 
18
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
using System;
 
21
using System.Collections.Generic;
 
22
using System.Collections.ObjectModel;
 
23
using System.Text;
 
24
using System.IO;
 
25
using System.Windows.Forms;
 
26
using System.Xml;
 
27
 
 
28
namespace XiboClient
 
29
{
 
30
    class StatLog
 
31
    {
 
32
        private Collection<Stat> _stats;
 
33
        private xmds.xmds _xmds;
 
34
        private String _lastSubmit;
 
35
        private HardwareKey _hardwareKey;
 
36
        private Boolean _xmdsProcessing;
 
37
 
 
38
        public StatLog()
 
39
        {
 
40
            _stats = new Collection<Stat>();
 
41
            _xmds = new xmds.xmds();
 
42
 
 
43
            // Register a listener for the XMDS stats
 
44
            _xmds.SubmitStatsCompleted += new XiboClient.xmds.SubmitStatsCompletedEventHandler(_xmds_SubmitStatsCompleted);
 
45
 
 
46
            // Get the key for this display
 
47
            _hardwareKey = new HardwareKey();
 
48
 
 
49
            _xmdsProcessing = false;
 
50
        }
 
51
 
 
52
        /// <summary>
 
53
        /// Record a complete Layout Event
 
54
        /// </summary>
 
55
        /// <param name="fromDT"></param>
 
56
        /// <param name="toDT"></param>
 
57
        /// <param name="scheduleID"></param>
 
58
        /// <param name="layoutID"></param>
 
59
        public void RecordLayout(String fromDT, String toDT, int scheduleID, int layoutID)
 
60
        {
 
61
            if (!Properties.Settings.Default.statsEnabled) return;
 
62
 
 
63
            Stat stat = new Stat();
 
64
 
 
65
            stat.type = StatType.Layout;
 
66
            stat.fromDate = fromDT;
 
67
            stat.toDate = toDT;
 
68
            stat.scheduleID = scheduleID;
 
69
            stat.layoutID = layoutID;
 
70
 
 
71
            _stats.Add(stat);
 
72
 
 
73
            return;
 
74
        }
 
75
 
 
76
        /// <summary>
 
77
        /// Record a complete Media Event
 
78
        /// </summary>
 
79
        /// <param name="fromDT"></param>
 
80
        /// <param name="toDT"></param>
 
81
        /// <param name="layoutID"></param>
 
82
        /// <param name="mediaID"></param>
 
83
        public void RecordMedia(String fromDT, String toDT, int layoutID, String mediaID)
 
84
        {
 
85
            if (!Properties.Settings.Default.statsEnabled) return;
 
86
 
 
87
            Stat stat = new Stat();
 
88
 
 
89
            stat.type = StatType.Media;
 
90
            stat.fromDate = fromDT;
 
91
            stat.toDate = toDT;
 
92
            stat.layoutID = layoutID;
 
93
            stat.mediaID = mediaID;
 
94
 
 
95
            _stats.Add(stat);
 
96
 
 
97
            return;
 
98
        }
 
99
 
 
100
        /// <summary>
 
101
        /// Record a complete Event
 
102
        /// </summary>
 
103
        /// <param name="fromDT"></param>
 
104
        /// <param name="toDT"></param>
 
105
        /// <param name="tag"></param>
 
106
        public void RecordEvent(String fromDT, String toDT, String tag)
 
107
        {
 
108
            if (!Properties.Settings.Default.statsEnabled) return;
 
109
 
 
110
            Stat stat = new Stat();
 
111
 
 
112
            stat.type = StatType.Event;
 
113
            stat.fromDate = fromDT;
 
114
            stat.toDate = toDT;
 
115
            stat.tag = tag;
 
116
 
 
117
            _stats.Add(stat);
 
118
 
 
119
            return;
 
120
        }
 
121
 
 
122
        /// <summary>
 
123
        /// RecordStat
 
124
        /// </summary>
 
125
        /// <param name="stat"></param>
 
126
        public void RecordStat(Stat stat)
 
127
        {
 
128
            System.Diagnostics.Debug.WriteLine(String.Format("Recording a Stat Record. Current Count = {0}", _stats.Count.ToString()), LogType.Audit.ToString());
 
129
 
 
130
            _stats.Add(stat);
 
131
 
 
132
            // At some point we will need to flush the stats
 
133
            if (_stats.Count >= Properties.Settings.Default.StatsFlushCount)
 
134
            {
 
135
                Flush();
 
136
            }
 
137
 
 
138
            return;
 
139
        }
 
140
 
 
141
        /// <summary>
 
142
        /// Flush the stats
 
143
        /// </summary>
 
144
        public void Flush()
 
145
        {
 
146
            System.Diagnostics.Debug.WriteLine(new LogMessage("Flush", String.Format("IN")), LogType.Audit.ToString());
 
147
 
 
148
            // Determine if there is anything to flush
 
149
            if (_stats.Count < 1 || _xmdsProcessing) return;
 
150
 
 
151
            int threshold = ((int)Properties.Settings.Default.collectInterval * 5);
 
152
 
 
153
            // Determine where we want to log.
 
154
            if (Properties.Settings.Default.XmdsLastConnection.AddSeconds(threshold) < DateTime.Now)
 
155
            {
 
156
                FlushToFile();
 
157
            }
 
158
            else
 
159
            {
 
160
                FlushToXmds();
 
161
            }
 
162
 
 
163
            System.Diagnostics.Debug.WriteLine(new LogMessage("Flush", String.Format("OUT")), LogType.Audit.ToString());
 
164
        }
 
165
 
 
166
        /// <summary>
 
167
        /// Send the Stat to a File
 
168
        /// </summary>
 
169
        private void FlushToFile()
 
170
        {
 
171
            System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToFile", String.Format("IN")), LogType.Audit.ToString());
 
172
 
 
173
            // There is something to flush - we want to parse the collection adding to the TextWriter each time.
 
174
            try
 
175
            {
 
176
                // Open the Text Writer
 
177
                StreamWriter tw = new StreamWriter(File.Open(Application.UserAppDataPath + "//" + Properties.Settings.Default.StatsLogFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
 
178
 
 
179
                try
 
180
                {
 
181
                    foreach (Stat stat in _stats)
 
182
                    {
 
183
                        tw.WriteLine(stat.ToString());
 
184
                    }
 
185
                }
 
186
                catch (Exception ex)
 
187
                {
 
188
                    System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToFile", String.Format("Error writing stats line to file with exception {0}", ex.Message)), LogType.Error.ToString());
 
189
                }
 
190
                finally
 
191
                {
 
192
                    // Close the tw.
 
193
                    tw.Close();
 
194
                    tw.Dispose();
 
195
                }
 
196
            }
 
197
            catch (Exception ex)
 
198
            {
 
199
                // Log this exception
 
200
                System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToFile", String.Format("Error writing stats to file with exception {0}", ex.Message)), LogType.Error.ToString());
 
201
            }
 
202
            finally
 
203
            {
 
204
                // Always clear the stats. If the file open failed for some reason then we dont want to try again
 
205
                _stats.Clear();
 
206
            }
 
207
 
 
208
            System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToFile", String.Format("OUT")), LogType.Audit.ToString());
 
209
        }
 
210
 
 
211
        /// <summary>
 
212
        /// Send the Stats to XMDS
 
213
        /// </summary>
 
214
        private void FlushToXmds()
 
215
        {
 
216
            System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToXmds", String.Format("IN")), LogType.Audit.ToString());
 
217
 
 
218
            String stats;
 
219
 
 
220
            stats = "<log>";
 
221
 
 
222
            // Load the Stats collection into a string
 
223
            try
 
224
            {
 
225
                foreach (Stat stat in _stats)
 
226
                {
 
227
                    stats += stat.ToString();
 
228
                }
 
229
            }
 
230
            catch (Exception ex)
 
231
            {
 
232
                System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToXmds", String.Format("Error converting stat to a string {0}", ex.Message)), LogType.Error.ToString());
 
233
            }
 
234
 
 
235
            stats += "</log>";
 
236
 
 
237
            // Store the stats as the last sent (so we have a record if it fails)
 
238
            _lastSubmit = stats;
 
239
 
 
240
            // Clear the stats collection
 
241
            _stats.Clear();
 
242
            
 
243
            // Submit the string to XMDS
 
244
            _xmdsProcessing = true;
 
245
 
 
246
            _xmds.SubmitStatsAsync(Properties.Settings.Default.Version, Properties.Settings.Default.ServerKey, _hardwareKey.Key, stats);
 
247
 
 
248
            // Log out
 
249
            System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToXmds", String.Format("OUT")), LogType.Audit.ToString());
 
250
        }
 
251
 
 
252
        /// <summary>
 
253
        /// Capture the XMDS call and see if it went well
 
254
        /// </summary>
 
255
        /// <param name="sender"></param>
 
256
        /// <param name="e"></param>
 
257
        void _xmds_SubmitStatsCompleted(object sender, XiboClient.xmds.SubmitStatsCompletedEventArgs e)
 
258
        {
 
259
            System.Diagnostics.Debug.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("IN")), LogType.Audit.ToString());
 
260
 
 
261
            _xmdsProcessing = false;
 
262
 
 
263
            // Test if we succeeded or not
 
264
            if (e.Error != null)
 
265
            {
 
266
                // We had an error, log it.
 
267
                System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Error during Submit to XMDS {0}", e.Error.Message)), LogType.Error.ToString());
 
268
 
 
269
                // Dump the stats to a file instead
 
270
                if (_lastSubmit != "")
 
271
                {
 
272
                    try
 
273
                    {
 
274
                        // Open the Text Writer
 
275
                        StreamWriter tw = new StreamWriter(File.Open(Application.UserAppDataPath + "//" + Properties.Settings.Default.StatsLogFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
 
276
 
 
277
                        try
 
278
                        {
 
279
                            tw.Write(_lastSubmit);
 
280
                        }
 
281
                        catch (Exception ex)
 
282
                        {
 
283
                            // Log this exception
 
284
                            System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Error writing stats to file with exception {0}", ex.Message)), LogType.Error.ToString());
 
285
                        }
 
286
                        finally
 
287
                        {
 
288
                            tw.Close();
 
289
                            tw.Dispose();
 
290
                        }
 
291
                    }
 
292
                    catch (Exception ex)
 
293
                    {
 
294
                        // Log this exception
 
295
                        System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Could not open the file with exception {0}", ex.Message)), LogType.Error.ToString());
 
296
                    }
 
297
                }
 
298
            }
 
299
 
 
300
            // Clear the last sumbit
 
301
            _lastSubmit = "";
 
302
 
 
303
            System.Diagnostics.Debug.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("OUT")), LogType.Audit.ToString());
 
304
        }
 
305
    }
 
306
 
 
307
    class Stat 
 
308
    {
 
309
        public StatType type;
 
310
        public String fromDate;
 
311
        public String toDate;
 
312
        public int layoutID;
 
313
        public int scheduleID;
 
314
        public String mediaID;
 
315
        public String tag;
 
316
 
 
317
        public override string ToString()
 
318
        {
 
319
            // Format the message into the expected XML sub nodes.
 
320
            // Just do this with a string builder rather than an XML builder.
 
321
            String theMessage;
 
322
 
 
323
            theMessage = String.Format("<stat type=\"{0}\" fromdt=\"{1}\" todt=\"{2}\" layoutid=\"{3}\" scheduleid=\"{4}\" mediaid=\"{5}\"></stat>", type, fromDate, toDate, layoutID.ToString(), scheduleID.ToString(), mediaID);
 
324
            
 
325
            return theMessage;
 
326
        }
 
327
    }
 
328
 
 
329
    public enum StatType { Layout, Media, Event };
 
330
}