-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
142 lines (126 loc) · 4.59 KB
/
Program.cs
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
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System;
using System.Linq;
using System.IO;
using System.Threading;
using CallDataReader.Models;
using System.Data.SqlClient;
using System.Data;
using Dapper;
namespace CallDataReader
{
class Program
{
static void Main()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
var config = builder.Build();
string call_db_cs = config.GetConnectionString("CallDB");
string tracking_db_cs = config.GetConnectionString("TrackingDB");
long previous_max_id = Get_Previous_Max_Id(tracking_db_cs);
DateTime end_time = DateTime.Today.AddHours(5).AddMinutes(-1);
if (DateTime.Now.Hour > 4) end_time = DateTime.Today.AddDays(1).AddHours(5).AddMinutes(-1);
int error_count = 0;
//Console.WriteLine($"Program is scheduled to end on: { end_time.ToString() }");
//new ErrorLog("Program running", end_time.ToString(), "", "", "", true);
// process steps
// main loop
while (DateTime.Now < end_time)
{
try
{
// I'm aware that DateTime.Now's accuracy is bad, but it's good enough for this.
// plus or minus 15 milliseconds is fine.
DateTime loopend = DateTime.Now.AddSeconds(5);
// read the calls that have been added since we last ran
var data = BaseData.Get(call_db_cs, previous_max_id);
if (data != null)
{
data = (from bd in data
orderby bd.call_origin_time ascending
select bd).ToList();
if (data.Count() > 0)
{
long new_max = (from d in data select d.call_id).Max();
previous_max_id = Math.Max(new_max, previous_max_id);
List<Caller> callers = new List<Caller>();
List<CallerAddress> addresses = new List<CallerAddress>();
List<CallerLocation> locations = new List<CallerLocation>();
HashSet<string> seen_sessions = new HashSet<string>();
// break them dowm into their components
foreach (BaseData d in data)
{
if (Caller.IsValidCaller(d))
{
if (!seen_sessions.Contains(d.session_id))
{
seen_sessions.Add(d.session_id);
callers.Add(new Caller(d));
if (CallerAddress.IsValidAddress(d))
{
addresses.Add(new CallerAddress(d));
}
}
if (CallerLocation.IsValidLocation(d))
{
locations.Add(new CallerLocation(d));
}
}
}
// save the components
Caller.Save(callers, tracking_db_cs);
CallerAddress.Save(addresses, tracking_db_cs);
CallerLocation.Save(locations, tracking_db_cs);
//Console.WriteLine($"Saved {callers.Count().ToString()} callers");
//new ErrorLog($"Saved {callers.Count().ToString()} callers", callers.Count().ToString(), "", "", "", true);
}
// wait for at most 5 seconds
var now = DateTime.Now;
int current = (int)loopend.Subtract(now).TotalMilliseconds;
if (current > 0)
{
Thread.Sleep(current);
}
error_count = 0;
}
else
{
Console.WriteLine("Data returned from the database is invalid, sleeping for 60 seconds.");
new ErrorLog("Data returned is invalid", "sleeping for 60 seconds", "", "", "", true);
Thread.Sleep(60000);
}
}
catch(Exception ex)
{
new ErrorLog(ex);
error_count++;
if(error_count > 10)
{
Thread.Sleep(60000); // let's wait a minute
error_count = 0;
}
}
}
}
private static long Get_Previous_Max_Id(string cs)
{
string query = "SELECT ISNULL(MAX(call_id), 7145348) FROM Tracking.dbo.[911_callers]";
try
{
using (IDbConnection db = new SqlConnection(cs))
{
return db.ExecuteScalar<long>(query);
}
}
catch (Exception ex)
{
new ErrorLog(ex, query);
return -1;
}
// this function will return the max value stored in the database once we have some data in there.
}
}
}