summary refs log tree commit diff
path: root/ascii-town-heatmap.rs
blob: 23b80da1ef88d8369a72c87e7b247ecbe724586d (plain) (blame)
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
// cargo-deps: hsl, image, csv = "1.0.0-beta.4", serde, serde_derive
extern crate hsl;
extern crate image;
extern crate csv;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::collections::BTreeSet;
use std::io;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Tile {
    tile_x: usize,
    tile_y: usize,
    create_time: u64,
    modify_count: u64,
    modify_time: u64,
    access_count: u64,
    access_time: u64
}

// Torus size.
const W: u32 = 512;

// Tile width/height in pixels.
const TW: u32 = 4;
const TH: u32 = 1;

fn main() {
    // Read the `torus.csv` into a 2D array of `(access, modify)`.
    // Also track the values we see in `BTreeSet` (for ordering).
    let mut tiles = [[(0, 0); W as usize]; W as usize];
    let mut sa = BTreeSet::new();
    let mut sm = BTreeSet::new();
    let mut rdr = csv::Reader::from_reader(io::stdin());
    for tile in rdr.deserialize().map(Result::<Tile, _>::unwrap) {
        let a = tile.access_count;
        let m = tile.modify_count;

        // HACK(eddyb) ignore 0,0 for analysis - too busy.
        if (tile.tile_x, tile.tile_y) != (0, 0) {
            sa.insert(a);
            sm.insert(m);
        }

        tiles[tile.tile_y][tile.tile_x] = (a, m);
    }

    // Extract the maximum values.
    // TODO(eddyb) chunk values for better representation.
    let ma = sa.iter().next_back().cloned().unwrap_or(0) as f64;
    let mm = sm.iter().next_back().cloned().unwrap_or(0) as f64;

    // Compose the heatmap image in-memory from the 2D array.
    let mut heatmap = image::ImageBuffer::new(W * TW, W * TH);
    let red = hsl::HSL::from_rgb(&[255, 0, 0]).h;
    // let green = hsl::HSL::from_rgb(&[0, 255, 0]).h;
    // let blue = hsl::HSL::from_rgb(&[0, 0, 255]).h;
    for y in 0..W {
        for x in 0..W {
            let (a, m) = tiles[y as usize][x as usize];
            let a = (a as f64 / ma).min(1.0).powf(0.1);
            let m = (m as f64 / mm).min(1.0).powf(0.1);

            // access => luminosity, modify => hue (green -> red)
            // let h = green * (1.0 - m) + red * m;
            // let s = 1.0;
            // let l = a.max(m) * 0.5;

            // access => luminosity, modify => saturation (grey -> red)
            let h = red;
            let s = m;
            let l = a * 0.5;

            let (r, g, b) = hsl::HSL { h, s, l }.to_rgb();
            let rgb = image::Rgb([r, g, b]);

            for dy in 0..TH {
                let y = ((y * 2 + W + 1) * TH / 2 + dy) % (W * TH);
                for dx in 0..TW {
                    let x = ((x * 2 + W + 1) * TW / 2 + dx) % (W * TW);
                    heatmap.put_pixel(x, y, rgb);
                }
            }
        }
    }

    // Save the heatmap image.
    heatmap.save("heatmap.png").unwrap();
}