File: date-calc.html

package info (click to toggle)
condor 8.6.8~dfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 79,476 kB
  • sloc: cpp: 649,368; perl: 108,260; python: 22,654; ansic: 20,588; sh: 19,729; fortran: 3,340; yacc: 1,844; makefile: 1,213; xml: 1,003; sql: 767; java: 703; ruby: 512; awk: 505; lex: 248; f90: 64; csh: 24
file content (240 lines) | stat: -rw-r--r-- 7,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>test</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"></script>
<script src="dateext.js"></script>
</head>
<body>

<pre></pre>

<script>
"use strict";



/*
base_url - Mandatory. URL (relative or absolute) to the data files.  The
URL must contain the string "..", which indicates where ".oldest." or
".DATE." will be placed.

ready_func - Optional. If present, called when the object is ready. Will be
passed a copy of "this".  The delay is because the object needs to load the
".oldest." file before it can field queries.

fail_func - Optional. If present, called if the object is unable to
initialize.  Will be passed a copy of "this".  Will fail if the ".oldest."
file could not be loaded.
*/
function HTCondorViewDateFiles(base_url, ready_func, fail_func) {
	this.base_url = base_url;
	this.my_promise = jQuery.Deferred();

	if(ready_func) { this.my_promise.done(ready_func); }
	if(fail_func) { this.my_promise.fail(fail_func); }

	var oldest_url = HTCondorViewDateFiles.expand(base_url, "oldest");
	var that = this;
	$.ajax(oldest_url, {dataType:'json'}).then(
		function(data){that.oldest_loaded(data);},
		function(data){that.failed_load();}
		);
}

/*
Returns a jQuery.promise which triggers when the object is ready to
field queries, or upon failing to load the ".oldest." file (and thus
will never be ready to field queries)
*/
HTCondorViewDateFiles.prototype.promise = function() {
	return this.my_promise.promise();
};


HTCondorViewDateFiles.expand = function(base_url, content) {
	return base_url.replace("..", "."+content+".");
};


HTCondorViewDateFiles.prototype.oldest_loaded = function(oldest) {
	this.oldest = {};
	var keys = Object.keys(oldest);
	var key;
	var val;
	var dateval;
	for (var i = 0; i < keys.length; i++) {
		key = keys[i];
		val = oldest[key];
		dateval = Date.parseMore(val);
		this.oldest[key] = dateval;
		//console.log(val," -> ", dateval.toUTCString());
		if((!this.absolute_oldest) || this.absolute_oldest.getTime() > dateval.getTime()) {
			this.absolute_oldest = dateval;
		}
	}
	//console.log("Oldest available data", this.oldest);
	//console.log("Absolute oldest:", this.absolute_oldest);
	this.my_promise.resolve(this);
};

HTCondorViewDateFiles.prototype.failed_load = function() {
	this.my_promise.reject(this);
};

HTCondorViewDateFiles.truncate_to_day = function(dateobj) {
	// TODO: probably faster to set individual fields to 0.
	return Date.parseMore(dateobj.getUTCISOYearMonthDay());
}

HTCondorViewDateFiles.truncate_to_week = function(dateobj) {
	// TODO: There is probably a faster solution, but it may
	// be complex.
	var w = dateobj.getUTCISOWeekDate();
	var r = Date.parseMore(w);
	var tmp = r.getUTCISOWeekDate();
	//console.log("        ", dateobj,"->", w, "->", r, "(",tmp,")");
	return r;
}

HTCondorViewDateFiles.truncate_to_month = function(dateobj) {
	// TODO: probably faster to set individual fields to 0.
	return Date.parseMore(dateobj.getUTCISOYearMonth());
}

HTCondorViewDateFiles.prototype.get_files_aide = function(start, end, step, truncater, formatter) {
//console.log("    ", start, end, step, truncater, formatter);
//console.log("  start", start);
	var now = truncater(start);
//console.log("  trunc", now);
//console.log("    end", end, "+",step,"- 1");
	end.setTime(end.getTime()+step-1);
//console.log("      =", end);
	end = truncater(end);
//console.log("  trunc", end);

	var retfiles = [];
	var stamp;

	while(now.getTime() < end.getTime()) {
//console.log("    ", now, end);
		stamp = formatter(now);
		retfiles.push(stamp);
		now.setTime(now.getTime() + step);
//console.log("       ", now, end);
	}

	return retfiles;
}

HTCondorViewDateFiles.prototype.get_files = function(start,end) {
	start = Date.parseMore(start);
	end = Date.parseMore(end);

	if(start.getTime() < this.absolute_oldest.getTime()) {
		start = this.absolute_oldest;
	}
	if(start.getTime() > end.getTime()) { return []; }

	//console.log("considering", start, "to", end);

	var day = 1000*60*60*24;
	var week = day*7;
	var month = day*31;

	var delta = end.getTime() - start.getTime();

	var stamp;
	var retfiles = [];


	var now;

	// We choose the smallest interval that covers the time period
	// and is at least as long as the time period.
	// Goal is to load no more than 2 files unless there are no other
	// options. In addition, which granularity of files we load should
	// always be the same for a given duration; we don't want to jump
	// between daily and weekly files because we're loading 36 hours and
	// sometimes we'd need 2 daily and sometimes 3.

	if( delta <= day &&
		this.oldest.daily &&
		start.getTime() >= this.oldest.daily.getTime()) {
		retfiles = this.get_files_aide(start, end, day,
			HTCondorViewDateFiles.truncate_to_day,
			function(d) { return d.getUTCISOYearMonthDay(); }
			);
		console.log("  daily:", retfiles);
		return retfiles;
	}

	if( delta <= week &&
		this.oldest.weekly &&
		start.getTime() >= this.oldest.weekly.getTime()) {
		retfiles = this.get_files_aide(start, end, week,
			HTCondorViewDateFiles.truncate_to_week,
			function(d) { return d.getUTCISOWeekDate(); }
			);
		console.log("  weekly:", retfiles);
		return retfiles;
	}

	retfiles = this.get_files_aide(start, end, month,
		HTCondorViewDateFiles.truncate_to_month,
		function(d) { return d.getUTCISOYearMonth(); }
		);
	console.log("  monthly:", retfiles);
	return retfiles;
};




var df = new HTCondorViewDateFiles("data/submitter..json", ready_to_go);

function tryit(desc, df, s,e) {
	console.log(desc);
	var r = df.get_files(s,e);
	//console.log("    ",r);
	console.log("");
}

function ready_to_go(df) {
	tryit("daily, 1 (1 hour)", df, "2015-12-01T12:00:00Z", "2015-12-01T13:00:00Z");
	tryit("daily, 1 (1 day)", df, "2015-11-30T00:00:00Z", "2015-12-01T00:00:00Z");
	tryit("daily, 1 (1 day -1s)", df, "2015-11-30T00:00:00Z", "2015-11-30T23:59:59Z");
	tryit("daily, 2 (1 day)", df, "2015-11-30T03:00:00Z", "2015-12-01T03:00:00Z");
	tryit("daily, 2 (1 day -1s)", df, "2015-11-30T03:00:00Z", "2015-12-01T02:59:59Z");


	tryit("weekly, 1 (1 day +1s)", df, "2015-11-30T00:00:00Z", "2015-12-01T00:00:01Z");
	tryit("weekly, 1 (1 week -1s)", df, "2015-11-23T00:00:00Z", "2015-11-29T23:59:59Z");
	tryit("weekly, 1 (1 week)", df, "2015-11-23T00:00:00Z", "2015-11-30T00:00:00Z");
	tryit("weekly, 2 (1 week)", df, "2015-11-23T00:00:01Z", "2015-11-30T00:00:01Z");
	tryit("weekly, 2 (1 week shift)", df, "2015-11-24T00:00:00Z", "2015-12-01T00:00:00Z");
	tryit("weekly, 2 (3 days)", df, "2015-11-29T00:00:00Z", "2015-12-01T00:00:00Z");
	tryit("weekly, 2 (3 daysish)", df, "2015-11-29T23:59:59Z", "2015-12-01T00:00:00Z");

	tryit("monthly, 1 (2 weeks)", df, "2015-11-16T00:00:00", "2015-11-29T23:59:59Z");
	tryit("monthly, 1 (1 week and change)", df, "2015-11-23T00:00:00Z", "2015-11-30T00:00:01Z");
	tryit("monthly, 2 (1 week and change)", df, "2015-11-24T00:00:00Z", "2015-12-01T00:00:00Z");
	tryit("monthly, 2 (1 week and change)", df, "2015-11-24T00:00:00Z", "2015-12-01T00:00:01Z");
	tryit("monthly, 2 (1 week and change)", df, "2015-11-25T00:00:00Z", "2015-12-02T00:00:01Z");
	tryit("monthly, 2 (1 week and change)", df, "2015-11-26T00:00:00Z", "2015-12-03T00:00:01Z");
	tryit("monthly, 2 (3 weeks)", df, "2015-11-25T00:00:00Z", "2015-12-16T00:00:01Z");
	tryit("monthly, 2 (3 weeks)", df, "2015-10-25T00:00:00Z", "2015-11-16T00:00:01Z");
	tryit("monthly, 1 (1 month)", df, "2015-11-01T00:00:00Z", "2015-12-01T00:00:00Z");
	tryit("monthly, 2 (1 month)", df, "2015-11-01T00:00:01Z", "2015-12-01T00:00:01Z");
	tryit("monthly, 12 (12 months)", df, "2015-01-01T00:00:00Z", "2016-01-01T00:00:00Z");
	tryit("monthly, 13 (12 months)", df, "2015-01-01T00:00:01Z", "2016-01-01T00:00:01Z");
	tryit("monthly, 13 (12 months)", df, "2015-06-01T00:00:01Z", "2016-06-01T00:00:01Z");


}
</script>


</body>
</html>