Skip to content

Commit

Permalink
Add additional whisper utilities for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
toelen committed Aug 26, 2011
1 parent a552ed9 commit b16e1f0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
Expand All @@ -19,5 +24,6 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri Aug 26 15:26:44 CEST 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
8 changes: 4 additions & 4 deletions src/main/java/org/github/whisper4j/Whisper.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,8 @@ public static Point[] unpackPoints(byte[] series, int currentInterval,
double value = buf.getDouble((i * pointsize) + 4);

if (timestamp == currentInterval) {
System.out.println(i + " " + currentInterval + " --- -" + i
+ " - " + timestamp + " , " + value);
//System.out.println(i + " " + currentInterval + " --- -" + i
// + " - " + timestamp + " , " + value);
// This is a timestamp
Point p = new Point();
p.timestamp = timestamp;
Expand All @@ -719,8 +719,8 @@ public static Point[] unpackPoints(byte[] series, int currentInterval,
// + " - " + timestamp + " , " + value);

} else {
System.out.println(i + " " + currentInterval + " ??? -" + i
+ " - " + timestamp + " , " + value);
//System.out.println(i + " " + currentInterval + " ??? -" + i
// + " - " + timestamp + " , " + value);
Point p = new Point();
p.timestamp = 0;
p.value = 0;
Expand Down
33 changes: 24 additions & 9 deletions src/test/java/org/github/whisper4j/test/TestCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@
import org.junit.Test;

public class TestCreate {
@Before
public void setup() {
File f = new File("./temp.wsp");
if (f.exists()) {
f.delete();
}
}

@Test
public void test_12h_2y() throws Exception {

Util.delete("./temp.wsp");
// maxRetention: 63072000
// fileSize: 17548
// aggregationMethod: average
Expand Down Expand Up @@ -68,5 +60,28 @@ public void test_12h_2y() throws Exception {
Assert.assertEquals("size", 17520, info.size);
Assert.assertEquals("retention", 63072000, info.retention);
}

@Test
public void testUpdate() throws Exception {
String testFile = TestReadHeader.getWhistperFile(getClass(), "15m_8.wsp");

Util.delete(testFile);
Util.create(testFile, "15m:8");

int oneMinuteFromNow = Whisper.time() + 60;
long value = 12345;
Util.update(testFile, oneMinuteFromNow+":"+value);

Whisper jisper = new Whisper();

TimeInfo timeInfo = jisper.fetch(testFile, oneMinuteFromNow - 120, oneMinuteFromNow + 120);
Assert.assertNotNull(timeInfo);

for(Point p : timeInfo.points){
if(p != null && p.timestamp != 0){
System.out.println(p);
}
}
}

}
58 changes: 33 additions & 25 deletions src/test/java/org/github/whisper4j/test/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,43 @@ public static void delete(String path) {
}
}

/**
* Calls whisper-create.py with the given arguments
*
* @param path
* @param timePerPoint_timeToStore
*/
public synchronized static void create(String path,
String timePerPoint_timeToStore) {
path = path.replace('\\', '/');
String[] options = new String[] {
"./src/test/python/whisper-create.py", path,
timePerPoint_timeToStore };
try {

// File f = new File(path);
// PythonInterpreter python = new PythonInterpreter();
//
// python.exec("import sys");
// python.exec("import os");
// // python.exec("os.chdir('src/test/python/')");
// python.exec("from whisper.py import whisper");
// // python.exec("import whisper.py");
// // python.execfile( "./src/test/python/whisper.py");
// // python.e("sys.argv = ['', 'my', 'args', 'here']");
// python.execfile(f.getAbsolutePath());
// // python.exec(options[0]+" "+options[1]+" "+options[2]);
// python.cleanup();
// System.out.println(options);
// jython.run(options);

// PySystemState.initialize();
jython.main(options);

// System.gc();
// jython.shutdownInterpreter();
PythonInterpreter python = new PythonInterpreter();
python.exec("import sys");
python.exec("sys.path.append(\"./src/test/python/\")");
python.exec("sys.argv = ['whisper-create.py', '" + path + "', '"
+ timePerPoint_timeToStore + "']");
python.execfile("./src/test/python/whisper-create.py");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Calls whisper-create.py with the given arguments
*
* @param path
* @param timePerPoint_timeToStore
*/
public synchronized static void update(String path,
String timePerPoint_timeToStore) {
path = path.replace('\\', '/');
try {
PythonInterpreter python = new PythonInterpreter();
python.exec("import sys");
python.exec("sys.path.append(\"./src/test/python/\")");
python.exec("sys.argv = ['whisper-update.py', '" + path + "', '"
+ timePerPoint_timeToStore + "']");
python.execfile("./src/test/python/whisper-update.py");
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
58 changes: 58 additions & 0 deletions src/test/python/whisper-fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python

import sys, time
import whisper
from optparse import OptionParser

now = int( time.time() )
yesterday = now - (60 * 60 * 24)

option_parser = OptionParser(usage='''%prog [options] path''')
option_parser.add_option('--from', default=yesterday, type='int', dest='_from',
help=("Unix epoch time of the beginning of "
"your requested interval (default: 24 hours ago)"))
option_parser.add_option('--until', default=now, type='int',
help="Unix epoch time of the end of your requested interval (default: now)")
option_parser.add_option('--json', default=False, action='store_true',
help="Output results in JSON form")
option_parser.add_option('--pretty', default=False, action='store_true',
help="Show human-readable timestamps instead of unix times")

(options, args) = option_parser.parse_args()

if len(args) != 1:
option_parser.print_usage()
sys.exit(1)

path = args[0]

from_time = int( options._from )
until_time = int( options.until )

(timeInfo, values) = whisper.fetch(path, 1313585674, 1313672074)
#(timeInfo, values) = whisper.fetch(path, from_time, until_time)

(start,end,step) = timeInfo

if options.json:
values_json = str(values).replace('None','null')
print '''{
"start" : %d,
"end" : %d,
"step" : %d,
"values" : %s
}''' % (start,end,step,values_json)
sys.exit(0)

t = start
for value in values:
if options.pretty:
timestr = time.ctime(t)
else:
timestr = str(t)
if value is None:
valuestr = "None"
else:
valuestr = "%f" % value
print "%s\t%s" % (timestr,valuestr)
t += step
41 changes: 41 additions & 0 deletions src/test/python/whisper-info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python

import sys, os
import whisper
from optparse import OptionParser

option_parser = OptionParser(usage='''%prog path [field]''')
(options, args) = option_parser.parse_args()

if len(args) < 1:
option_parser.print_usage()
sys.exit(1)

path = args[0]
if len(args) > 1:
field = args[1]
else:
field = None

info = whisper.info(path)
info['fileSize'] = os.stat(path).st_size

if field:
if field not in info:
print 'Unknown field "%s". Valid fields are %s' % (field, ','.join(info))
sys.exit(1)

print info[field]
sys.exit(0)


archives = info.pop('archives')
for key,value in info.items():
print '%s: %s' % (key,value)
print

for i,archive in enumerate(archives):
print 'Archive %d' % i
for key,value in archive.items():
print '%s: %s' % (key,value)
print
29 changes: 29 additions & 0 deletions src/test/python/whisper-update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python

import sys, time
import whisper
from optparse import OptionParser

now = int( time.time() )

option_parser = OptionParser(
usage='''%prog [options] path timestamp:value [timestamp:value]*''')

(options, args) = option_parser.parse_args()

if len(args) < 2:
option_parser.print_usage()
sys.exit(1)

path = args[0]
datapoint_strings = args[1:]
datapoint_strings = [point.replace('N:', '%d:' % now)
for point in datapoint_strings]
datapoints = [tuple(point.split(':')) for point in datapoint_strings]

if len(datapoints) == 1:
timestamp,value = datapoints[0]
whisper.update(path, value, timestamp)
else:
print datapoints
whisper.update_many(path, datapoints)

0 comments on commit b16e1f0

Please sign in to comment.