This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
forked from etsy/opsweekly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_oncall_weekly.php
77 lines (63 loc) · 3.51 KB
/
add_oncall_weekly.php
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
<?php
include_once 'phplib/base.php';
if (!db::connect()) {
echo "Database connection failed, cannot continue. ";
} else {
$username = getUsername();
$range_start = db::escape($_POST['oncall']['range_start']);
$range_end = db::escape($_POST['oncall']['range_end']);
logline("Started adding a new oncall update for {$username} with range_start: {$range_start} and range_end: {$range_end}...");
if (count($_POST['oncall']['notifications']) > 0) {
// See if this user is enrolled in sleep tracking
$profile_data = checkForUserProfile($username);
if ($profile_data && $profile_data['sleeptracking_provider'] != "none" && $profile_data['sleeptracking_provider'] != "") {
$sleep_provider = $profile_data['sleeptracking_provider'];
logline("Sleeptracking enabled: {$sleep_provider}");
$sleep = true;
// Get the user settings into an array by decoding the JSON we put in the database for them
$sleeptracking_settings = json_decode($profile_data['sleeptracking_settings'], 1);
// Get the provider settings from the config array
$sleepprovider_settings = $sleep_providers[$sleep_provider];
// Load the sleeptracking provider's PHP code
include_once($sleepprovider_settings['lib']);
} else {
logline("No need to do sleep tracking because {$username} has no provider chosen");
}
foreach($_POST['oncall']['notifications'] as $id => $n) {
$sleep_state = -1;
$mtts = -1;
$sleep_level = -1;
$confidence = -1;
$timestamp = db::escape($n['time']);
$hostname = db::escape($n['hostname']);
$output = db::escape($n['output']);
$service = db::escape($n['service']);
$state = db::escape($n['state']);
$tag = db::escape($n['tag']);
$notes = db::escape(htmlentities($n['notes'], ENT_QUOTES));
$alert_id = generateOnCallAlertID($timestamp, $hostname, $service);
if ($sleep) {
// Run the sleep tracking provider for this alert
$sleep_info = getSleepDetailAtTimestamp($timestamp, $sleeptracking_settings[$sleep_provider], $sleepprovider_settings);
if ($sleep_info) {
$sleep_state = $sleep_info['sleep_state'];
$mtts = $sleep_info['mtts'];
$sleep_level = $sleep_info['sleep_level'];
$confidence = $sleep_info['confidence'];
}
}
$query = "INSERT INTO oncall_weekly (alert_id, range_start, range_end, timestamp, hostname, service, state, contact, output, tag, sleep_state, mtts, sleep_level, sleep_confidence, notes) VALUES
('$alert_id', '$range_start', '$range_end', '$timestamp', '$hostname', '$service', '$state', '$username', '$output', '$tag', '$sleep_state', '$mtts', '$sleep_level', '$confidence','$notes')";
logline("Processing on call line with data: $query");
if (!db::query($query)) {
echo "Database update failed, error: " . db::error();
logline("Database update failed, error: " . db::error());
}
}
logline("Everything worked great, redirecting the user with success");
Header('Location: add.php?oncall_succ=hellyeah');
} else {
logline("We didn't find any notifications to process, redirect user back to add page");
Header('Location: add.php');
}
}