Fix stringsource parsing to that apostrophes don't count as quotes.
[schedulator.git] / logsource.cs
blob5d59a7645bc8f996f3815afb5a748e0f9d3d1090
1 using System;
2 using System.IO;
3 using System.Web;
4 using System.Collections;
5 using Wv;
6 using Wv.Schedulator;
8 namespace Wv.Schedulator
10 public class LogSource : Source
12 string logstr;
14 public LogSource(Schedulator s, string name, string logstr)
15 : base(s, name)
17 this.logstr = logstr;
20 public static Source create(Schedulator s, string name,
21 string prefix, string suffix)
23 return new LogSource(s, name, suffix);
26 static string clean_filename(string s)
28 string s2 = "";
29 foreach (char c in s)
31 if (Char.IsLetterOrDigit(c))
32 s2 += c;
33 else
34 s2 += "_";
36 return s2;
39 static string get_file_from_id(string id)
41 try
43 StreamReader r = File.OpenText(
44 String.Format("schedules/{0}.sched.log",
45 clean_filename(id)));
46 return r.ReadToEnd();
48 catch (IOException)
50 // nothing
52 return "";
55 public static Source create_from_file_id(Schedulator s, string name,
56 string prefix, string suffix)
58 string id = wv.isempty(suffix) ? s.name : suffix;
59 return new LogSource(s, name, get_file_from_id(id));
62 class Values
64 public TimeSpan
65 origest = TimeSpan.Zero,
66 currest = TimeSpan.Zero;
67 public DateTime date;
70 public override void cleanup_tasks()
72 Hashtable changes = new Hashtable();
74 string[] lines = logstr.Split("\n".ToCharArray());
75 foreach (string line in lines)
77 string[] words = line.Split(" ".ToCharArray());
78 if (words.Length < 2) continue;
79 string key = HttpUtility.UrlDecode(words[0]);
80 string value = HttpUtility.UrlDecode(words[1]);
81 DateTime date;
82 if (words.Length >= 3)
83 date = DateTime.Parse(words[2]);
84 else
85 date = DateTime.Parse("1999-01-01");
87 Values v;
88 if (changes[key] != null)
89 v = (Values)changes[key];
90 else
92 v = new Values();
93 changes.Add(key, v);
95 if (wv.isempty(v.origest) && !wv.isempty(v.currest))
96 v.origest = v.currest;
97 v.currest = StringSource.parse_estimate(0, value);
98 if (wv.isempty(v.origest))
99 v.origest = v.currest;
100 v.date = date;
103 foreach (string key in changes.Keys)
105 string[] words = key.Split("_".ToCharArray(), 3);
106 if (words.Length < 3) continue; // invalid
108 Task t = s.tasks.FindById(words[1] + ":" + words[2]);
109 if (t == null)
110 continue; // no longer exists, ignore
112 Values v = (Values)changes[key];
114 switch (words[0])
116 case "currest":
117 t.currest = v.currest;
118 t.origest = v.origest;
119 break;
120 case "elapsed":
121 t.elapsed = v.currest;
122 break;
125 t.done = (!wv.isempty(t.currest) && t.currest == t.elapsed);
126 if (t.done) t.donedate = v.date;