Skip to content

Instantly share code, notes, and snippets.

@rinx
Last active August 29, 2015 14:05
Show Gist options
  • Save rinx/a0464550f5e66505b56c to your computer and use it in GitHub Desktop.
Save rinx/a0464550f5e66505b56c to your computer and use it in GitHub Desktop.
lab_lifelog viewer
2014/07/10 10:00:00 11:00:00
2014/07/11 10:00:00 12:00:00
2014/07/12 10:00:00 13:00:00
2014/07/13 10:00:00 14:00:00
2014/07/14 10:00:00 15:00:00
2014/07/15 10:00:00 16:00:00
2014/07/16 10:00:00 17:00:00
2014/07/17 10:00:00 18:00:00
2014/07/18 10:00:00 19:00:00
2014/07/19 10:00:00 20:00:00
2014/07/20 10:00:00 21:00:00
2014/07/21 10:00:00 22:00:00
2014/08/29 13:59:15 18:59:18
2014/08/30 00:00:00 13:00:00
2014/08/31 16:00:40 21:00:00
#!/bin/sh
SCRIPTDIR=$(cd $(dirname $0); pwd)
timecard="${SCRIPTDIR}/timecard"
role=$1
today=`date "+%Y/%m/%d"`
nowtime=`date "+%H:%M:%S"`
if [ ! -f $timecard ]; then
touch $timecard
fi
if [ "$role" = "checkin" ]; then
grep $timecard -e $today > /dev/null
if [ $? = 1 ]; then
echo "$today $nowtime" >> $timecard
echo "$today $nowtime"
echo "checkin successful!!"
else
echo "sorry, you're already checked in..."
fi
elif [ "$role" = "checkout" ]; then
grep $timecard -e $today > /dev/null
if [ $? = 0 ]; then
tmp=`grep $timecard -e $today`
tmptoday=`date "+%Y\/%m\/%d"`
chrcnt=`grep $timecard -e $today | sed -e 's@[^:]@@g' | wc -c`
if [ "$chrcnt" = "3" ]; then
sed -e "s/^${tmptoday}.*$//g" $timecard | sed '/^$/d' > ${timecard}_tmp
rm $timecard
mv ${timecard}_tmp $timecard
echo "$tmp $nowtime" >> $timecard
echo "$tmp $nowtime"
echo "checkout successful!!"
else
echo "sorry, you're already checked out..."
fi
else
echo "sorry, you're not checked in today..."
fi
elif [ "$role" = "view" ]; then
echo "Please open http://0.0.0.0:8000/view.html"
python -m SimpleHTTPServer
else
echo "invalid arguments" 1>&2
exit 1
fi
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
.day {
fill: #eee;
stroke: #fff;
stroke-width: 3px;
}
.month {
fill: none;
stroke: #fff;
stroke-width: 5px;
}
.RdYlGn .q0-11{fill:rgb(255,255,229)}
.RdYlGn .q1-11{fill:rgb(247,252,185)}
.RdYlGn .q2-11{fill:rgb(217,240,163)}
.RdYlGn .q3-11{fill:rgb(173,221,142)}
.RdYlGn .q4-11{fill:rgb(120,198,121)}
.RdYlGn .q5-11{fill:rgb(65,171,93)}
.RdYlGn .q6-11{fill:rgb(35,132,67)}
.RdYlGn .q7-11{fill:rgb(0,104,55)}
.RdYlGn .q8-11{fill:rgb(0,69,41)}
.RdYlGn .q9-11{fill:rgb(0,69,41)}
.RdYlGn .q10-11{fill:rgb(0,69,41)}
</style>
<body>
<h1>lab_life</h1>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 136,
cellSize = 17; // cell size
var targetYear = 2014;
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
hour = d3.time.format("%H"),
percent = d3.format(".1%"),
format = d3.time.format("%Y/%m/%d");
var color = d3.scale.quantize()
.domain([0, 1.0])
.range(d3.range(11).map(function(d) { return "q" + d + "-11"; }));
var svg = d3.select("body").selectAll("svg")
.data(d3.range(targetYear, targetYear + 1))
.enter().append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")");
svg.append("text")
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
var rect = svg.selectAll(".day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("rect")
.attr("class", "day")
.attr("width", cellSize)
.attr("height", cellSize)
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", function(d) { return day(d) * cellSize; })
.datum(format);
rect.append("title")
.text(function(d) { return d; });
svg.selectAll(".month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("path")
.attr("class", "month")
.attr("d", monthPath);
var calcHours = function(d) {
var endTime = Date.parse('2000/01/01 ' + String(d).split(' ')[2]),
startTime = Date.parse('2000/01/01 ' + String(d).split(' ')[1]);
return (endTime - startTime) / 3600000 / 24;
}
d3.text("timecard", "text/plane", function(error, txt) {
var data = d3.nest()
.key(function(d) { return d.split(' ')[0]; })
.rollup(function(d) { return d; })
.map(txt.split('\n'));
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(calcHours(data[d])); })
.select("title")
.text(function(d) { return d + "\n" + String(data[d]).split(' ')[1] + "->" + String(data[d]).split(' ')[2]; });
});
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize
+ "H" + w0 * cellSize + "V" + 7 * cellSize
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize
+ "H" + (w1 + 1) * cellSize + "V" + 0
+ "H" + (w0 + 1) * cellSize + "Z";
}
d3.select(self.frameElement).style("height", "2910px");
</script>
@rinx
Copy link
Author

rinx commented Aug 31, 2014

[Usage]

$ sh timecard.sh checkin
$ sh timecard.sh checkout
$ sh timecard.sh view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment