Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
hotaru
Posts: 1021
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Wed Sep 20, 2017 7:31 pm UTC

Flumble wrote:Is there a linuxy way to monitor services and send an email whenever a services dies or comes back up (and preferably every hour while something's down)?
I'm currently using systemd timers to run a small script to log the status of things like the xkcd minecraft server and I could run a sendmail command when the status is negative, but it seems there ought to be a better way.
If there's no linuxy way, I'll look into graphite+grafana or <insert better suggestion>.

you could use OnFailure= in the the unit file with https://github.com/kylemanna/systemd-utils to send an email notification.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Sep 20, 2017 8:00 pm UTC

commodorejohn wrote:
Hey, those Homestar Runner cartoons aren't gonna play themselves.
Actually something more banal, extracting media from them. Granted there is swfexport to do that, though it makes Windows Defender very very angry, and also some of the files I'm exporting from seem to just... sometimes not follow the spec so... fun.

It was kinda interesting though. Why do I do this for fun.

Related, it's really annoying that BitMap.SetPixel is soooo much slower than using an unsafe block with UnlockBits. Like I get being slower due to some bounds checks, but why is it so much slower?

commodorejohn
Posts: 958
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Coding: Fleeting Thoughts

Postby commodorejohn » Wed Sep 20, 2017 8:09 pm UTC

Yeah, swfexport was already spotty back in the days when Flash MX was new, I can't imagine it copes well with newer versions.

This takes me back to the days of extracting random J-pop tracks from animutations because they never credited the original source and I had no idea what to Google...
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

User avatar
Flumble
Yes Man
Posts: 1945
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Thu Sep 21, 2017 10:51 pm UTC

hotaru wrote:you could use OnFailure= in the the unit file with https://github.com/kylemanna/systemd-utils to send an email notification.

I'm not running the services on that that device, just the monitors (which are mostly of the form "try to establish a TCP connection/HTTP session" to find out whether the (black box) program is running properly). I can let the monitor purposefully stop with an error code to trigger OnFailure, but that's as ugly as having a separate script to send emails.


My eye is caught by the influxdb TICK stack now, mostly because it comes with docker images and a simple logging plugin that runs a script periodically and takes care of the output by itself ...and you can configure it to send emails when something goes wrong.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Sat Sep 23, 2017 11:09 am UTC

Flumble wrote:I can let the monitor purposefully stop with an error code to trigger OnFailure, but that's as ugly as having a separate script to send emails.

What's ugly about that? Your watchdog reports failure via exit code, and systemd does the notification as configured. That seems like it's working as intended.

It would be cleaner to have the OnFailure on the server where the minecraft server is running, but apparently that's not an option?

Also, what exactly is causing the minecraft server to fail? Does the server process crash? Does the server process enter a deadlock?. Does the whole server crash and fail to reboot? Network connectivity issues? Something else entirely? Depending on the problem, I'd suggest different fixes.

Flumble wrote:My eye is caught by the influxdb TICK stack now, mostly because it comes with docker images and a simple logging plugin that runs a script periodically and takes care of the output by itself ...and you can configure it to send emails when something goes wrong.

That seems like overkill. If you seek reliability, then complexity is your enemy. Unless you're willing to ignore issues like updates, security fixes and general maintenance, then dumping a docker image somewhere is usually more trouble than it's worth. It's certainly more brittle than a sendmail script. I may be a pessimist, but I wouldn't be surprised if that monitoring solution has more downtime than the minecraft server you meant to monitor.


On my home network, I'm using collectd. It's still overkill for monitoring a single service, but it has plugins for many things I want to monitor (network bandwidth on my router, temperature of all CPUs, free disk space and SMART errors etc), supports custom plugins (e.g. I have a few raspberry pis with DHT22 temperature/humidity sensors) and it has threshold-based alerts, including things like "only notify if the server couldn't be reached 3 times in a row".
Distributed logging is supported, e.g. every computer runs a collectd instance, which reports the data to a central server where it gets written to some kind of database. Collectd is flexible about its storage backends. IIRC influxdb works, but I'm using rrdtool for its simplicity and built-in graphing features.

Configuration of the whole thing takes a while, but installation is trivial (most distributions have a package), updating is trivial (the configuration format is very stable; only once did I have to manually adjust something) and the overhead is low enough that I can just start an instance on each computer I need to monitor, including the low-powered raspberry pis.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Mon Oct 16, 2017 8:07 pm UTC

Calculating the number of years between two dates, particularly when the client wants it to be a decimal... based on # of months out of 12... is apparently a non-trivial problem.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Mon Oct 16, 2017 9:53 pm UTC

The bad news: dates are hard.
The good news: if you cheat, the client (probably) won't notice. Because the client doesn't understand dates either, they have no idea what they're requesting, and thus won't notice even if your results are a few millenia off.

Then again, even though your problem is underspecified, I don't think it'd be more than 2-5 lines with a standard date API?

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Oct 17, 2017 1:17 pm UTC

Well, I probably over-engineered given you're right they probably have no clue what they're asking for, but something like this seemed to work:

Code: Select all

if(larger.Year > smaller.Year)
{
    years = larger.Year - smaller.Year
    if(smaller.AddYears(years) > larger) years--;
    smaller = smaller.AddYears(years);
}

if(smaller.Month < larger.Month)
{
    months = larger.Month - smaller.Month;
    if(smaller.AddMonths(months) > larger) months--;
}
else if(smaller.Month > larger.Month)
{
    months = 12 - smaller.Month + larger.Month;
    if(smaller.AddMonths(months) > larger) months--;
}

total = years + months / 12;


.Net doesn't provide something ilke this out of box, primarily because a TimeSpan doesn't make sense to have a .Months or .Years property. A timespan is measured in ticks, and months/years have different amounts of ticks depending on when the span would start...

EDIT: I think I fixed it more, though it's even more verbose now. :P

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5497
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Oct 17, 2017 2:19 pm UTC

Basically, you wish .NET had a DateDiff function like Visual Basic and SQL Server.
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Oct 17, 2017 6:43 pm UTC

Hmmm, well there is interop so I probably could include the VB DLL... On the other hand that's pretty terrible so I'd rather not.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Tue Oct 17, 2017 8:15 pm UTC

Just connect to an SQL server and SELECT DATEDIFF($date1, $date2) :roll:

With a date library based on unix time, it's rather simple.

Code: Select all

function date_diff(date1, date2) {
  var diff_in_ms = Math.abs(date1.getTime() - date2.getTime());
  return diff_in_ms / 1000 / 60 / 60 / 24 / 365.25; // close enough.
}


But microsoft does not like unix time, and if you want the difference between the beginning of the months, try this:

Code: Select all

function date_diff_months(date1, date2) {
  var date1_in_months = date1.getYear() * 12 + date1.getMonth();
  var date2_in_months = date2.getYear() * 12 + date2.getMonth();
  return Math.abs(date1 - date2) / 12;
}

This works, no matter if your language returns getYear() as 2 or 4 digits or 3 digits since 1900, and no matter if getMonth() returns 0- or 1-based values.

User avatar
You, sir, name?
Posts: 6974
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Fri Oct 27, 2017 8:52 am UTC

Working with time is hard. Working with dates is even harder. There's leap days, leap seconds, sometimes the timezone changes based on the date, indexing is comically inconsistent on a lot of big APIs.

My favorite is stuff is financial rules like "find the third wednesday of the month that date X is in, unless it's a red day, then the following business day". That's... kinda hard. On a level where I sort of want to just create a static table of wednesdays per month from 1970 through 2040 or whatever and then use that instead of looking it up. You know I'm going to use it for my unit test anyway.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Fri Oct 27, 2017 2:04 pm UTC

You, sir, name? wrote:My favorite is stuff is financial rules like "find the third wednesday of the month that date X is in, unless it's a red day, then the following business day". That's... kinda hard.

Is it? You're only working with dates, thus no timezones, leap seconds or anything actually troublesome. And weeks are surprisingly regular - there has not been an instance where, say, a wednesday was skipped or where two wednesdays followed each other. Calculating whether a specific date is a wednesday is something that any crappy date class should be able to do for you.

The "for (day = 1; day < 30; day++)"-solution isn't beautiful, but works (and may thus be a good reference implementation for your test cases, so your actual implementation can be [18,17,16,15,21,20,19][new Date(d.getFullYear(), d.getMonth(), 1).getDay()] or something).

The hardest part is the function that returns all holidays, because that needs constant updates, and maybe you got to deal with the cases where the calculated date changes because a holiday appeared.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Sat Oct 28, 2017 3:45 pm UTC

Tub wrote:
And weeks are surprisingly regular - there has not been an instance where, say, a wednesday was skipped or where two wednesdays followed each other.

Russia in 1918 would like to have a word with you. (If I'm reading the calendar right, they had two thursdays in a row.)

(But I'm being a pedant.)
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Sat Oct 28, 2017 4:39 pm UTC

Sorry, I don't believe in dates before 1970.

Seriously, do you have a source for that? Google is surprisingly resistant against the search terms I'm using..

User avatar
Xenomortis
Not actually a special flower.
Posts: 1397
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Sat Oct 28, 2017 6:42 pm UTC

https://en.wikipedia.org/wiki/Adoption_ ... ern_Europe
The USSR switched from the Julian to the Gregorian calendar in 1918 - the 30th Jan 1918 (Wednesday) was followed by 14th Feb (Thursday).
Not quite "two sequential Thursdays", but a date-time complication nonetheless.
Image

User avatar
phlip
Restorer of Worlds
Posts: 7543
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Mon Oct 30, 2017 2:03 am UTC

Possible confounding factor here being that 30 Jan 1918 on the Gregorian calendar was a Thursday... but, 30 Jan 1918 on the Julian calendar was a Wednesday, so Russia had a Wednesday followed by a Thursday, nothing unusual there, just the actual date did weird things.

There are also cases where a place jumped the dateline... switching timezone from UTC-12 to UTC+12 (or vice-versa, I can't remember). They skipped a day, but on both the "days of the week" and "days of the month" sense, so the idea that a particular calendar date is a particular day of the week, and these are an entirely regular pattern, still holds up. As long as you're still working with the Gregorian calendar, your Wednesdays are going to have dates that are 7 days apart.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
ucim
Posts: 5568
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: Coding: Fleeting Thoughts

Postby ucim » Mon Oct 30, 2017 1:12 pm UTC

In any case, the issue is the same as travelers who cross a time zone. They have (say) 3:13 followed by 2:14. But it's 3:13 central time, followed by 2:14 mountain time, which is not incongruous at all. If you don't include the timezone (or the calendar system), you are not specifying time completely, and the ambiguity is simply stored in the unstated parts.

Tomorrow, "the thirty-first" is going to be followed by "the first", and few people will be confused by the omission of the month name.

Of course, for those following the One True Calendar, it will be 25 Voyage, 4AT, followed by 26 Voyage, 4AT. It's just a question of standards.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Tue Oct 31, 2017 4:56 am UTC

phlip wrote:
Possible confounding factor here being that 30 Jan 1918 on the Gregorian calendar was a Thursday... but, 30 Jan 1918 on the Julian calendar was a Wednesday, so Russia had a Wednesday followed by a Thursday, nothing unusual there, just the actual date did weird things.

Ah, indeed. I just did a cal lookup and counted backwards, and didn't realize the Julian day was different.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Sun Nov 19, 2017 10:51 pm UTC

Today's random thought: "I wish I had a javascript-to-php transpiler."

As horrible as that sounds, it would solve a real problem I have.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1397
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Mon Nov 20, 2017 12:42 pm UTC

No, I can see that one would need a translator if they wanted to convert one lunatic's mad ramblings into the dialect of a depressed drunk.
What I can't envisage is wanting to communicate with either of these people whilst sane and/or sober.
Image

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Nov 20, 2017 11:43 pm UTC

Tub wrote:
Today's random thought: "I wish I had a javascript-to-php transpiler."

As horrible as that sounds, it would solve a real problem I have.

AND I HAVE A SOLUTION FOR YOU

My friend's project, Cassis, is a subset of JS and PHP that works the same across both languages (plus a supporting library to get a bunch of functions working in both). You can write code in Cassis and deploy it both in browsers and servers. ^_^

It purposely takes on a more PHP-centric, functions-not-methods approach, as that maximizes the surface area of identical syntax.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Tue Nov 21, 2017 4:56 pm UTC

Xanthir wrote:My friend's project, Cassis, is a subset of JS and PHP that works the same across both languages (plus a supporting library to get a bunch of functions working in both). You can write code in Cassis and deploy it both in browsers and servers. ^_^

That's great, now all I need is a JavaScript to Cassis transpiler.

Honestly, that's not even a transpiler. Actual transpilers exist, and they can transpile a lot more code - just not the code I have. Solutions like Cassis are a dime a dozen, though most of them never bother going for a public release - you always end up implementing only the subset of the APIs that your project needs, which means that reusability is low. I've had similar code somewhere, but it's useless for my current problem - existing code that was actually written as JavaScript.

The basic premise is interesting though. Remembering that $ is a valid character in javascript variable names, you can observe that php and javascript appear lexically compatible:

Code: Select all

function isPrime($n) {
    for ($i=2;$i<$n;$i++) {
        if ($n % $i == 0)
            return false;
    }
    return true;
}

This is both valid JavaScript and PHP! We can use variables (though with restricted naming), loops, if/else, functions and simple arithmetic operations.

Recent PHP versions (>5.4) also offer the [] array literal syntax, which allows compatible syntax for arrays!

Code: Select all

function sumPrimes($array) {
    $sum = 0;
    for ($i=0;$i<count($array);$i++) {
        $e = $array[$i];
        if (isPrime($e))
            $sum += $e;
    }
    return $sum;
}

$sum = sumPrimes([2,4,6,9,5,8,13]);

Well, it's not entirely compatible. count($array) is not a javascript function. That's where Cassis enters the scene, re-implementing count() in javascript. Also, let's implement print().

Code: Select all

function print(n) {
    console.log(n);
}

function count(arr) {
    return arr.length;
}

Minor differences apply (carriage returns after print, count() behavior on non-arrays), but those could be fixed if one felt like it.
All together, this is our first useful example program!

Code: Select all

function isPrime($n) {
    for ($i=2;$i<$n;$i++) {
        if ($n % $i == 0)
            return false;
    }
    return true;
}

function sumPrimes($array) {
    $sum = 0;
    for ($i=0;$i<count($array);$i++) {
        $e = $array[$i];
        if (isPrime($e))
            $sum += $e;
    }
    return $sum;
}

print(
sumPrimes([2,4,6,9,5,8,13]));

Code: Select all

> cat lib.js code.phpjs | node ; echo '-------' ; cat lib.php code.phpjs | php
20
-------
20


This wasn't a lot of code, right? Very manageable, very readable, even tested and correct. Did you spot the problem?

gd1
Posts: 61
Joined: Wed Nov 14, 2012 5:42 am UTC

Re: Coding: Fleeting Thoughts

Postby gd1 » Thu Nov 23, 2017 11:40 pm UTC

So basically in C,
Array = duct tape
Malloc = wd40
?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests