 Hello, and welcome to this video by me Chris of films by Chris. That's Chris of the K link in the description and If you've been following my channel for a while, you will know that I do like to do hardware projects In fact, I have a second channel that's geared towards hardware and I love the ESP 8266 chips. These are little tiny Chips that have Wi-Fi in them. They're like a buck fifty to three bucks depending on what model you get and think of it as like Like like like well originally they were being used as Wi-Fi modules for arduinos And they'll someone realize that you can actually program and do a lot of stuff with them without the need for the arduinos so for a few bucks you can have a Wi-Fi enabled arduino type board and I use them all of my house for different things and one of the things I use them for is I have water pumps on the side of my house You know for my water system and I've already had one pump go bad because it was running constantly by accident so I hooked up two of them one to each pump and Every time they turn on they hooked my Wi-Fi and they log that they've turned on and if they run more than a certain number of minutes Originally I did five minutes I think I've knocked that down to two minutes because they shouldn't be running consistently longer than that it starts sending me alerts to my phone Well And I set it up so I can log into my server because it's all logged on my server and I can do I can just type in Pump and it's gonna give me Outputs here of every time the pump in the ground run which the pump in the ground should not be running very often as you can see it runs Maybe Six to eight times a day and that's to fill up the tank on the side of my house But then there's a pump in that tank which is called pump to here, which as you can see runs a lot more In fact, it is only 10 a.m. It's already run a lot in fact I think it's running more than it should be which is nice that I have this data here to look at that because there is a Bladder tank that when I turn on the water in my house the bladder tank should run for a little bit and pressurize the house before that pump turns on But I feel like the pumps turning on almost instantly when I flushed my toilet or turn on my sink And I really should looking at because the reason I had one pump go bad in the ground is because I had a bladder tank Go bad and it caused that pump to constantly be running and It cost almost a thousand dollars Actually, sometimes I got a good deal from what I've been told that I caught the cost me under a thousand dollars to get that last one replaced The pumps themselves are six to eight hundred bucks plus labor to have somebody go down there and replace it So I really should be looking more into this, but I wrote a little script. That's will it's a very inefficient script We're gonna look at it But it's called pump and it's for the second pump although I can easily modify it for the first pump but Pump to graph and I'm gonna run that as you'll see what takes a while to run But it's gonna give me like a graph for each day the usage for that So I can see if a day is at a whack or not and there are some days in here that you'll see are a little out of whack And I'll explain that in a moment But here we have this nice little chart for each day going back in history. I have the date The number of times it ran and a little bar graph So I can look for ones that are super long or super short and try to figure out why Or if I can see over time it's running more and more and more there might be a problem and again I this script is very inefficient. I looped through a file multiple times to get these numbers and Could definitely you should be able to run through it and count up the numbers going through the file once I've done that in the past but it was just quicker for me and it's It took what 10 15 seconds for that run. It's not something I'm running often But I thought we just look at that so I'm gonna go into this This file here and we're just gonna have a quick overlook on how I'm doing this So first thing in here. Well, first thing is that we have it's a bash script So we have our shebang line And then copyright information this code is up online. We have The a variable for our temp file then we have our main file Which as you can see at the bottom of the file here is our first function that's run It says welcome generating data and then it's gonna get the dates and what it's gonna do here It's gonna look at the file where I currently have all the logs for pump to save Then it's gonna grep for 20. That's kind of I just threw that in there in case there's any error lines I didn't want that to mess up my files here So it's looking for lines to have to zero in it since all of them should have dates of 2000 something and then hopefully I must have put that in there because I must have had a Jumbled line in there somewhere that was screwing stuff up So that's just to verify that at least there's a 20 in that line Hopefully grabbing lines only have dates in them then we're gonna cut based on the Pipe character and grabbing that first character So if I was against run pump or in this case pump to you can see what I have I have a time date stamp here for each time it's run and then I have the printed out date and time just Make it easier and again. This isn't a database. I'm just dumping everything into a plain text file So what we're doing here the next part of this is grabbing everything on this side of the line So let's grab in those date stamps So let me go ahead and go back into our script here So it's grabbing that time stamp and then converting it and getting just the date in a format of year dash month Dash day and dumping that into our temp file, which we've already established up here is in our temp directory now After that the rest of it is gonna happen in the main function I probably should have put it in a another function, but here we're going to say we're gonna cat out that temp file We're gonna sort unique it which is going to give us One line for every date that's in the log again Not the most efficient way to do this and then we're going to go through and look at each of those dates one at a time We're going to get a count by looping through the file. We're going to so we're gonna take the first date Let's say it's January 1st 2017 it's gonna go and basically we're going to again. I don't know this this I'm catting and grepping which is a bad practice. So really a better way to do this would be This get rid of that so that that's a little more efficient So we're going to be looking for a Line with the date so line is the date that we just grabbed so one date It's going to grab all the lines in our file that have that date and it's going to give us a count Then it's going to print the date and The count so if it ran 50 times a day is gonna say January 1st 2017 50 times and then it's going to and it's gonna give it three Digits in that number so even it's 50 It's gonna be zero five zero if it ran hundred times would be one zero zero if it was one time over to say zero zero one And that just allows our output to be properly four minutes So it's not staggered and what I mean by that If I ran that art this script again You're gonna see here What I'm talking about Again ten or fifteen seconds for it to generate all that data because it's looping through that file multiple times and again, so We have the date and the count and the reason if we didn't have this zero here This would be staggered in which would throw off our graph and we want everything up to this point to be the same length Ups in that semi-colon so that that's what that's for we're giving it that three digit because hopefully Hopefully my pump isn't running over a thousand times a day. That would be bad So looking through that and then we're gonna take that count and we're going to put an equal sign for every Number in that account and the dash end here is saying don't put a new line. It's gonna say put an equal sign No new line loop through it will do that if it's 50 times If it's one time go through one time and then after all that we say echo and a blank line Just to give us a new line and after it's all done in exit So it's a very simple script a very inefficient script and I'm not recommending to do it this way again. I had a a Years ago my buddy James who you've seen in videos in the past. It's been a while and we were talking about efficiency of scripting and Like writing stuff in bash scripts versus C. He's big into C. I'm big in the bash I knew a very little C. I know just enough to get by with C and I we wrote a script we're like, okay Let's just loop through something and count something so we decided to grab we found a copy of the Old Testament New King James Bible online and we wanted to count how many times each word Happens in there and put them in order and originally I did it like this and it took a little over 10 minutes I think for it to run doing something similar to this Which he was very surprised it ran that fast doing it this way And then I rewrote it again to where it counts it as it goes through and it only took like a minute and a half So so it could definitely be improved, but 10 to 15 seconds to something I'm gonna look at every couple of months just to keep an eye on stuff This is good enough. Anyway, I'll try to remember put a link in the description to this script online And of course, you know, just so you can look over it. That's it I just wanted to I wrote the script and I thought that I would share it with you And that's it, you know, there's better ways to make graphs and charts in fact There's I saw a github page that actually will create colored graphs in the shell You know different types of graphs, but this is how I did it with my basic knowledge in a few minutes So I do thank you for watching. I hope you enjoy this video I hope that you visit my website films by Chris comm that's Chris the K link in the description Also leaked to my patreon page patreon.com forward slash male X1000 if you want to support me Can I support me through PayPal on my website in the support section? I do thank you for watching liking sharing subscribing commenting all that stuff Thank you for watching again, and I hope that you have a great day