1 from http.server import BaseHTTPRequestHandler, HTTPServer
12 font-family: monospace;
13 background-color: #e0e0ff;
16 font-family: monospace;
20 border: 1px solid black;
25 background-color: black;
31 background-color: white;
33 border-left: 1px solid black;
34 border-right: 1px solid black;
42 background-color: green;
45 font-family: monospace;
77 <tr><th /><th>earned</th><th>progress</th><th>surplus</th></tr >
80 <form action="/" method="POST">
82 <tr><th>hourly rate</th><th>worked today</th></tr>
83 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_1" value="%s"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_1" value="%s" step="5" /> minutes</td>
84 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_2" value="%s"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_2" value="%s" step="5" /> minutes</td>
85 <tr><td class="input_container"><input type="number" min="1" class="rate" name="workday_hourly_rate_3" value="%s"/>€</td><td><input type="number" min="0" class="minutes" name="workday_minutes_worked_3" value="%s" step="5" /> minutes</td>
87 <tr><th>yearly income goal</th><td><input type="number" class="year_goal" min="1" name="year_goal" value="%s" />€</td></tr>
88 <tr><th>monthly income goal</th><td class="countable">%.2f€</td></tr>
89 <tr><th>weekly income goal</th><td class="countable">%.2f€</td></tr>
90 <tr><th>workdays per month</th><td class="input_container"><input type="number" class="workdays" min="1" max="28" name="workdays_per_month" value="%s" /></td></tr>
91 <tr><th>workday income goal</th><td class="countable">%.2f€</td></tr>
92 <tr><th>workdays per week</th><td class="countable">%.2f€</td></tr>
94 <input type="submit" name="update" value="update inputs" />
95 <input type="submit" name="finish" value="finish day" />
102 "timestamp_month": 0,
107 "workday_hourly_rate_1": 10,
108 "workday_minutes_worked_1": 0,
109 "workday_hourly_rate_2": 25,
110 "workday_minutes_worked_2": 0,
111 "workday_hourly_rate_3": 50,
112 "workday_minutes_worked_3": 0,
114 "workdays_per_month": 16
117 lock_file = "db.lock"
119 if os.path.exists(db_file):
120 with open(db_file, "r") as f:
125 class MyServer(BaseHTTPRequestHandler):
128 from urllib.parse import parse_qs
129 length = int(self.headers['content-length'])
130 postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
132 db["workday_minutes_worked_1"] = int(postvars[b'workday_minutes_worked_1'][0].decode())
133 db["workday_minutes_worked_2"] = int(postvars[b'workday_minutes_worked_2'][0].decode())
134 db["workday_minutes_worked_3"] = int(postvars[b'workday_minutes_worked_3'][0].decode())
135 db["workday_hourly_rate_1"] = int(postvars[b'workday_hourly_rate_1'][0].decode())
136 db["workday_hourly_rate_2"] = int(postvars[b'workday_hourly_rate_2'][0].decode())
137 db["workday_hourly_rate_3"] = int(postvars[b'workday_hourly_rate_3'][0].decode())
138 db["year_goal"] = int(postvars[b'year_goal'][0].decode())
139 db["workdays_per_month"] = int(postvars[b'workdays_per_month'][0].decode())
140 if b'finish' in postvars.keys():
141 day_income = (db["workday_minutes_worked_1"] / 60.0) * db["workday_hourly_rate_1"]
142 day_income += (db["workday_minutes_worked_2"] / 60.0) * db["workday_hourly_rate_2"]
143 day_income += (db["workday_minutes_worked_3"] / 60.0) * db["workday_hourly_rate_3"]
144 db["year_income"] += day_income
145 db["month_income"] += day_income
146 db["week_income"] += day_income
147 db["workday_minutes_worked_1"] = 0
148 db["workday_minutes_worked_2"] = 0
149 db["workday_minutes_worked_3"] = 0
150 if not os.path.exists(lock_file):
151 with open(lock_file, "w+"): pass
152 with open(db_file, "w") as f:
155 self.send_response(302)
156 self.send_header('Location', '/')
159 self.send_response(400)
161 self.wfile.write(bytes("Sorry, lock file!", "utf-8"))
166 self.send_response(200)
167 self.send_header("Content-type", "text/html")
170 today = datetime.datetime.now()
171 if not os.path.exists(lock_file):
173 if today.year != db["timestamp_year"]:
174 db["timestamp_year"] = today.year
175 db["year_income"] = 0
177 if today.month != db["timestamp_month"]:
178 db["timestamp_month"] = today.month
179 db["month_income"] = 0
181 if today.isocalendar()[1] != db["timestamp_week"]:
182 db["timestamp_week"] = today.isocalendar()[1]
183 db["week_income"] = 0
186 print("Resetting timestamp")
187 with open(lock_file, "w+"): pass
188 with open(db_file, "w") as f:
192 self.send_response(400)
194 self.wfile.write(bytes("Sorry, lock file!", "utf-8"))
196 day_of_year = today.toordinal() - datetime.date(today.year, 1, 1).toordinal() + 1
197 year_length = 365 + calendar.isleap(today.year)
198 workday_goal = db["year_goal"] / 12 / db["workdays_per_month"]
199 workdays_per_week = (db["workdays_per_month"] * 12) / (year_length / 7)
200 month_goal = db["year_goal"] / 12
201 week_goal = db["year_goal"] / (year_length / 7)
202 def success_color(success):
209 def progressbar(title, earned, goal, time_progress=-1):
210 time_progress_indicator = ""
211 success_income = earned / goal
212 success_income_cut = min(success_income, 1.0)
213 success_income_bonus = max(success_income - 1.0, 0)
214 success = success_income + 0
215 diff_goal = earned - goal
216 if time_progress >= 0:
218 if time_progress > 0:
219 success = success_income / time_progress
220 time_progress_indicator = "<div class=\"time_progress\" style=\"margin-left: %spx\"></div>" % int(time_progress * 100)
221 return "<tr><th>%s</th>" \
222 "<td class=\"countable\">%.2f€</td>" \
223 "<td class=\"progressbar\">%s<div class=\"progress\" style=\"background-color: %s; width: %s\"></div></td>" \
224 "<td class=\"progressbar\"><div class=\"diff_goal\">%.2f€</div><div class=\"progress surplus\" style=\"width: %s\"></div></td></tr>" % (
225 title, earned, time_progress_indicator, success_color(success), int(success_income_cut * 100), diff_goal, int(success_income_bonus * 100))
226 day_income = (db["workday_minutes_worked_1"] / 60.0) * db["workday_hourly_rate_1"]
227 day_income += (db["workday_minutes_worked_2"] / 60.0) * db["workday_hourly_rate_2"]
228 day_income += (db["workday_minutes_worked_3"] / 60.0) * db["workday_hourly_rate_3"]
229 year_plus = db["year_income"] + day_income
230 month_plus = db["month_income"] + day_income
231 week_plus = db["week_income"] + day_income
232 progress_time_year = day_of_year / year_length
233 progress_time_month = today.day / calendar.monthrange(today.year, today.month)[1]
234 progress_time_week = today.weekday() / 7
235 year_line = progressbar("year", year_plus, db["year_goal"], progress_time_year)
236 month_line = progressbar("month", month_plus, month_goal, progress_time_month)
237 week_line = progressbar("week", week_plus, week_goal, progress_time_week)
238 day_line = progressbar("workday", day_income, workday_goal)
239 body = year_line + "\n" + month_line + "\n" + week_line + "\n" + day_line
240 page = header + body + footer % (
241 db["workday_hourly_rate_1"], db["workday_minutes_worked_1"],
242 db["workday_hourly_rate_2"], db["workday_minutes_worked_2"],
243 db["workday_hourly_rate_3"], db["workday_minutes_worked_3"],
247 db["workdays_per_month"],
251 self.wfile.write(bytes(page, "utf-8"))
253 if __name__ == "__main__":
254 webServer = HTTPServer((hostName, serverPort), MyServer)
255 print("Server started http://%s:%s" % (hostName, serverPort))
257 webServer.serve_forever()
258 except KeyboardInterrupt:
260 webServer.server_close()
261 print("Server stopped.")