summaryrefslogtreecommitdiffstats
path: root/src/graph-data.vala
blob: 045c6ad50ba4a30b19fc8c9d953116e71e2d54fa (plain)
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
/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

public class stock {
	public class product {
		public struct subitem {
			public uint64 timestamp;
			public int amount;

			public string json {
				owned get {
					return "[%llu, %d]".printf(timestamp*1000, amount);
				}
			}
		}

		public uint64 id;
		public string name;
		subitem[] data;

		public product(uint64 id, string name) {
			this.id = id;
			this.name = name;
			this.data = null;
		}

		public void add(uint64 timestamp, int amount) {
			subitem newitem = {timestamp, amount};
			this.data += newitem;
		}

		public string json {
			owned get {
				var data_array = "[";
				if(data != null) {
					for(int i=0; i < data.length-1; i++)
						data_array += data[i].json + ", ";
					data_array += data[data.length-1].json;
				}
				data_array += "]";

				return "{label: \"%s\", data: %s}".printf(name, data_array);
			}
		}
	}

	Gee.HashMap<uint64?,product> data;

	public stock() {
		data = new Gee.HashMap<uint64?,product>();
	}

	public void add(product i) {
		data[i.id] = i;
	}

	public product get_product(uint64 id) {
		return data[id];
	}

	public string json {
		owned get {
			var result = "{";
			foreach(var entry in data.entries) {
				uint64? id = entry.key;
				string pdata = entry.value.json;
				result += @"\"product_$id\": $pdata, ";
			}
			result = result.substring(0, result.length-2);
			result += "}";
			return result;
		}
	}
}

public class profit_per_day {
	public struct subitem {
		public uint64 timestamp;
		public Price amount;

		public string json {
			owned get {
				return @"[$(timestamp*1000), $amount]";
			}
		}
	}

	private subitem[] profit_data;
	private subitem[] sales_data;

	public void add_profit(uint64 timestamp, int amount) {
		subitem newitem = {timestamp, amount};
		this.profit_data += newitem;
	}

	public void add_sales(uint64 timestamp, int amount) {
		subitem newitem = {timestamp, amount};
		this.sales_data += newitem;
	}

	public string json {
		owned get {
			var result = "{\"profit\": {label: \"Profit\", data: [";
			if(profit_data != null) {
				for(int i=0; i < profit_data.length-1; i++)
					result += profit_data[i].json + ", ";
				result += profit_data[profit_data.length-1].json;
			}
			result += "]},\"sales\": {label: \"Sales\", data:[";
			if(sales_data != null) {
				for(int i=0; i < sales_data.length-1; i++)
					result += sales_data[i].json + ", ";
				result += sales_data[sales_data.length-1].json;
			}
			result += "]}}";

			return result;
		}
	}
}

public class profit_per_weekday {
	public Price[] day = new Price[7];

	public profit_per_weekday() {
		for(int i=0; i<day.length; i++)
			day[i] = 0;
	}

	public string json {
		owned get {
			var result = "[";
			result += @"{ label: \"Monday\", data: $(day[0]) },";
			result += @"{ label: \"Tuesday\", data: $(day[1]) },";
			result += @"{ label: \"Wednesday\", data: $(day[2]) },";
			result += @"{ label: \"Thursday\", data: $(day[3]) },";
			result += @"{ label: \"Friday\", data: $(day[4]) },";
			result += @"{ label: \"Saturday\", data: $(day[5]) },";
			result += @"{ label: \"Sunday\", data: $(day[6]) }";
			result += "]";
			return result;
		}
	}
}

public class profit_per_product {
	Gee.HashMap<string,int> data;

	public profit_per_product() {
		data = new Gee.HashMap<string,int>();
	}

	public void add(string product, int amount) {
		if(data.has_key(product))
			data[product] = data[product] + amount;
		else
			data[product] = amount;
	}

	public string json {
		owned get {
			var result = "[";
			foreach(var e in data.entries) {
				result += @"{ label: \"$(e.key)\", data: $((Price) e.value) },";
			}
			if(result.length > 1) {
				result = result.substring(0, result.length-1);
			}
			result += "]";
			return result;
		}
	}
}