INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Expression Help - Date Math  (Read 7224 times)

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Expression Help - Date Math
« on: April 17, 2014, 10:17:21 pm »

I want to make a Smartlist that shows all files (which meet other criteria I can easily already define) for whom:

[Date Imported] is NOT in the last N weeks.
Where N is a variable defined in a field called [Weeks to Keep].

I'm sure there's a way to do this with some date math trickery and subtracting from Now() or something, but I'm struggling a bit to figure it out. Hoping someone (MrC) can help me out.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #1 on: April 17, 2014, 11:56:26 pm »

You can do this directly in a smartlist if you hard code the comparison value and unit:

   [Date Imported]=>2w

In an search query expression, you can use:

   [=Math(above([Date Imported,0], Now() - (7 * [Weeks to Keep])))]=0

This tests that Date Imported value (in days) is above (greater than) Now - the number of days to go back.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #2 on: April 18, 2014, 09:16:35 am »

You can do this directly in a smartlist if you hard code the comparison value and unit:

   [Date Imported]=>2w

Right, of course, but I don't want a fixed timespan for every file like an animal.  ;)

Thanks.

I'll try it out later, but that looks good.  It was Math(above()) I didn't know about.  Also, you don't need to strip commas or anything weird from the FP date field?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #3 on: April 18, 2014, 11:33:26 am »

If your locale uses commas as the decimal separator, then you'll need to replace them such that Math() doesn't trip up.

   [=Math( above( replace([Date Imported,0],/,,.), Replace(Now() - (7 * [Weeks to Keep]), /,,.) ) )]=0

Other equivalent search query expressions (untested):

   [=Math( below( replace([Date Imported,0],/,,.), Replace(Now() - (7 * [Weeks to Keep]), /,,.) ) )]=1

   [=Compare( [Date Imported,0], <, Math( Replace(Now() - (7 * [Weeks to Keep]), /,,.) ) )]=1
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #4 on: April 18, 2014, 12:24:04 pm »

I see, that's only for people with odd locales.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #5 on: April 18, 2014, 01:31:18 pm »

For the record, not that it matters, but I was half-sleeping when I posted my original question and I'm actually planning to use [Date Modified] not [Date Imported].

What I'm up to is this...

I have a set of shows that I record that I don't want to keep long-term.  This includes things like:
* Kids shows (Dora, Caillou, Sesame Street)
* News Programs (if I didn't watch the BBC News from 6am before the one at 6p aired, I'm never going to watch it)
* Jeopardy (which I don't even record for myself, but for a buddy who then downloads them from me)
* The Daily Show and The Colbert Report (I keep them a little longer than the news programs, but once they're more than a week old, they can go)

So, up until now, I had SageTV set to auto-delete these for me.  Unfortunately, this is clunky for a bunch of reasons:
1. It leaves all of the sidecar files behind.
2. It breaks links in MC.
3. It screws up sometimes and keeps things it shouldn't have, while deleting newer things.
4. The only way you can specify this is to specify "Keep at least X episodes" (it isn't by time, it is by count).

So, that all stinks, but the enemy of the good is the easy, so I've left it alone.  MCFileRemover could remove the broken links for me, and could have been used to do the whole thing.  Since I just enhanced my MCFileRemover to handle sidecars properly (and my existing system doesn't), I thought it was worth eating my own dogfood.

So... I'm making a super-fancy "just tag them" method in MC.  Should be pretty nice!
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #6 on: April 19, 2014, 01:56:08 pm »

Great.  Thanks, MrC, that works perfectly.  Here's my final Smartlist:

Code: [Select]
[Media Type]=[Video] [Media Sub Type]=[TV Show] [Weeks to Keep]=>0 [Date Modified]=>6d [Date Imported]=>6d [=Math(above([Date Modified,0], Now() - (7 * [Weeks to Keep])))]=0 ~sort=[Date Modified]
The other stuff in the list is basically a fail-safe.  Don't delete anything where:
* [Weeks to Keep] is messed up (that way, since you can't use ranges in Allowed Values when defining the field, I don't have to worry about math errors from negative numbers and whatnot causing havoc in the Expression)
* [Date Modified] and [Date Imported] tests are to keep it from deleting anything that has been "touched" in the past week.  That way, if something is going haywire with Auto-Import or whatever, MCFileRemover won't eliminate all evidence immediately (I'll have at least 6 days to figure it out).
* Sorts by [Date Modified] so that the oldest stuff goes up top (and therefore gets removed first).

Next...

If you don't mind helping me further, I'd love to make a separate list that replicates how you do it in SageTV:
* Keep at Most N episodes of a particular [Series]

And, I add a second relational field like [Max Episodes to Keep] or something, and then...  Not sure how to tell how many episodes there are of a particular show.  If I was looking for a fixed number, say 6, then I could:

Code: [Select]
[Media Type]=[Video] [Media Sub Type]=[TV Show] [Genre]=[Kids],[News] ~sort=[Series],[Date Modified] ~limit=-1,6,[Series]
But can I make the 6 in the above expression a field somehow with variables or some such nonsense?  Or is there some better way to do it?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #7 on: April 19, 2014, 02:02:21 pm »

By the way, there's a mistake in the documentation (which I think you wrote) for the ~limit and ~num search modifiers.

It says:

Quote
First 10 distinct artists will be randomly selected from the list of all possible artists, and then 2 random files from each of those artists will be selected and returned.

and

Quote
Example: Return at most 20 randomly selected tracks:

    ~n=20

That's not right.  They aren't selected randomly, they are selected based on the sort order.  They're only random if you use ~sort=Random or ~mix before you limit.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #8 on: April 19, 2014, 02:28:37 pm »

By the way, there's a mistake in the documentation (which I think you wrote) for the ~limit and ~num search modifiers.

It says:

and

That's not right.  They aren't selected randomly, they are selected based on the sort order.  They're only random if you use ~sort=Random or ~mix before you limit.

This area is a little complicated, as the results returned are dependent on context.

If you create a Smartlist with only the modifier:

   ~limit=10,2,[Artist]

you will get random results returned.  However, if you precede it by a sort:

   ~sort=[Artist] ~limit=10,2,[Artist]

you'll get the same list each time.

So the results are random, unless you've defined a sort or some parent has a default sort defined (as in a parent view or container).  it is probably safest to say that a file list is Random, unless otherwise sorted.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #9 on: April 19, 2014, 02:33:17 pm »

So the results are random, unless you've defined a sort or some parent has a default sort defined (as in a parent view or container).  it is probably safest to say that a file list is Random, unless otherwise sorted.

Hah.  I see.  So, it implies a ~sort=Random unless you otherwise specify.  I'd just basically never make a smarlist without some explicitly defined sort order, I guess, even if that was explicitly random, so I'd never run into that.  Clever.

Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #10 on: April 19, 2014, 02:37:56 pm »

Next...

If you don't mind helping me further, I'd love to make a separate list that replicates how you do it in SageTV:
* Keep at Most N episodes of a particular [Series]

And, I add a second relational field like [Max Episodes to Keep] or something, and then...  Not sure how to tell how many episodes there are of a particular show.  If I was looking for a fixed number, say 6, then I could:

Code: [Select]
[Media Type]=[Video] [Media Sub Type]=[TV Show] [Genre]=[Kids],[News] ~sort=[Series],[Date Modified] ~limit=-1,6,[Series]
But can I make the 6 in the above expression a field somehow with variables or some such nonsense?  Or is there some better way to do it?

You won't be able to use the ~limit modifier, since like all the modifiers, field interpolation cannot be used for values.

I think the way I'd go about this is to use global variables to calculate the 1 to n index of each show.  The shows would need to be sorted, and then indexed on a per-show basis.  Your smartlist would then use a search query expression to test the show's index against [Max Episodes to Keep], as in:

    [=Compare(load(showindex_[name]_[series]), <=, [Max Episodes to Keep])]=1

Would that work?
Logged
The opinions I express represent my own folly.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #11 on: April 19, 2014, 02:42:39 pm »

Hah.  I see.  So, it implies a ~sort=Random unless you otherwise specify.  I'd just basically never make a smarlist without some explicitly defined sort order, I guess, even if that was explicitly random, so I'd never run into that.  Clever.

It boggled my little brain for a long time, until I went through the process of documenting it and worked out what was happening.  In fact, the first question I jotted down on the old Wiki page way back when was:

    (Q: when are random items returned?)

because so many examples stated random results.

Edit: I just checked to verify that I had documented this behavior on the wiki, and I had:

Quote
Keep in mind that the order of the files returned from a query is generally undefined or random. Sorting is done by the view, or specifically by other query constructs such as Modifiers.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #12 on: April 19, 2014, 10:11:59 pm »

Remind me how I save() the global variable again...?

Do I have to manually run a search separately from my smartlist or something?  There's a reason I've never used any of the global variables support, but I don't remember the details.  This instance could be different though, since I only want to do the "removal passes" at a specified date and time, rather than using the results in a View.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #13 on: April 19, 2014, 10:24:41 pm »

You'll do it in the rules for file display.  I explain the passes / stages here:

   http://yabb.jriver.com/interact/index.php?topic=77826.msg528415#msg528415

Do any accumulating there, and then you can used the saved values in the view (and sometimes elsewhere).
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #14 on: April 19, 2014, 10:32:48 pm »

I have a Smartlist, not a View... Does it need an associated View to load the variable, and does it only run when opened?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #15 on: April 19, 2014, 10:57:07 pm »

Since the Set Rules for File Display is just a search, can I do it from within a single Smartlist (and will it refresh even via COM is, I guess, the other question).

Meaning, can I filter:

[Max Episodes to Keep]=>0
~sort(<whatever sort order I should use>)
[save(the variable)]=1

and then load it with your thing?  If so, what do I save and what sort order?  I'm confused and I'm not sure I follow the logic of what it is doing...
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #16 on: April 19, 2014, 11:02:20 pm »

You can use any Set rules for file display... View or Smartlist.  You can use a column in the view to see/use the calculated values.  But there are some limitations with how you use the calculated values.  For example, you won't be able to tally, and then in set rules for file display, and the further filter based on that tally result.
Logged
The opinions I express represent my own folly.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #17 on: April 19, 2014, 11:12:47 pm »

If I'm understanding what you're trying to do (flag files for deletion), you'd sequence the smartlist rule as:

   any-non-GV-rules/modifiers, sort, save()

The load() would be in the context of the smartlist's file list column as an expression column.  This is where you'd do your calculation (i.e. like the Compare() mentioned above).

If you indicate the specific fields you want used (for the intra-group index numbers), I'll provide you with the smartlist fragment.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #18 on: April 19, 2014, 11:26:58 pm »

If I'm understanding what you're trying to do (flag files for deletion), you'd sequence the smartlist rule as:

   any-non-GV-rules/modifiers, sort, save()

The load() would be in the context of the smartlist's file list column as an expression column.  This is where you'd do your calculation (i.e. like the Compare() mentioned above).

If you indicate the specific fields you want used (for the intra-group index numbers), I'll provide you with the smartlist fragment.

Here's the thing, I don't think the load() will ever happen.... Here's what the remover does:
1. It walks the Playlists tree in COM looking for a Playlist with the given path and name, then it looks up that list's playlist ID (needed because MC's COM doesn't provide a native way to look them up by path\name).
2. It gets the Playlist object back from COM, and gets a FilesAutomation object (which is, essentially, an array of FileAutomation objects). This is when it runs the Smartlist's search.
3. It then builds a list of system FileInfo objects by getting the File.Filename property from each FileAutomation object returned when you iterate over the FilesAutomation list.
4. Then it operates on those.  It doesn't query any of the fields, and I don't think it can even see the columns in the view.  The smartlist itself is never displayed (MC can be hidden and running only in Media Server mode).

You can use any Set rules for file display... View or Smartlist.

But, conceivably, two smartlists working in unison could do it...  Right?

The issue is, if I understand what's happening here... I'd likely have to load (as in click on within MC) the first, variable-creating, smartlist somehow, before then "running" the second smartlist, right?  Or else it would get stale results.  Since the idea of this would be to run automatically in the background on a timer, I can't have that.

But, maybe that's not the end of the world.  The same thing would work if, for the variable-creation "pass", instead of making a smartlist, I just switched to an All Items view and typed the search into the search box like a crazy person.  If so, then I can run a search in MC via COM's Filter() command (and probably an equivalent in MCWS if I ever get my MCWS wrapper done).

Which means, I could write a utility that just runs a given search (a general-purpose variable loader, essentially) and then, when that's done, run my second smartlist that uses (and filters, I guess) the results?

Or do I misunderstand and none of that is necessary?  You can cascade smartlists if you use the [Playlist] is or is not rules, but I can't use that do make it auto-run the previous search, right?

PS. If any of this is possible, I'd want to craft it so it would fail-empty in the end.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #19 on: April 19, 2014, 11:42:40 pm »

Here's a real-world example of what I mean...

I made my [Max Episodes to Keep] field.  It is:
Data Type: Integer
Relational: Store one for each series
Edit Type: Standard


I then tagged a single episode of Dora the Explorer with [Max Episodes to Keep]=2.

Then, I made a smartlist called Testers\Recent Episodes List:
Code: [Select]
[Media Type]=[Video] [Media Sub Type]=[TV Show] [Max Episodes to Keep]=>0 ~sort=[Series],[Date Modified]-d ~limit=-1,2,[Series]
Then, I made a second smartlist called Testers\Max Episodes Remover:
Code: [Select]
[Media Type]=[Video] [Media Sub Type]=[TV Show] [Max Episodes to Keep]=>0 -playlistid==53238849 ~sort=[Date Modified]
PlaylistID 53238849 is the Recent Episodes List of course.  That second list is the one I want.  See the screenshot.

That works perfectly.  The first list shows only the two most recently recorded episodes of each series that has been tagged with any [Max Episodes to Keep]=>0.  The remover list shows all the episodes of any show series with a [Max Episodes to Keep], sorted with the oldest first, and excludes any files that appear in the Recent Episodes List.  Then, I'd call MCFileRemover against it, and it would remove/delete any files in the list.  But that has two problems:

1. The Recent Episodes List only keeps the two most recent episodes, it doesn't actually follow the per-series [Max Episodes to Keep] field.  So, I'd somehow need to have one of these for any potential value of [Max Episodes to Keep] or something, and that'd be awful.

2. Linking them based on playlistid= is scary, and seems to break sometimes for odd reasons (you can't move them around at all, that's for sure).  When they fail, they tend to include-all for that search term, and that could be disastrous.  So, I don't like the idea of linking playlists to then automatically in the background delete files.

So, that won't work at all.  I think I see what you mean with the variable sorting, but I don't quite get what it would look like.  Could you give me the explicit sorting you mean, and what column to add with the save() so I can look at it with my own eyes?

Sorry, I've never used the global variables seriously before.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #20 on: April 20, 2014, 01:31:47 am »

I think if you are relying entirely on smartlist processing, you'll be a little limited.

Even though you get the smartlist via COM, it would still calculate the variables used within a successful search query.  To get access to those variables, you'd need to create a custom user field, and set the expression to the load() expression necessary to produce the value you want.  The results should be valid once the smartlist runs (via COM should be fine), and (essentially) undefined before then.  Once you have your list of files, you'd have to ask MC for that field for each of your file objects so you can make decisions based on those values.

I'm not sure how well the cascading smartlist would work in this situation.  If I recall correctly, a smartlist included by another smartlist is essentially like combining all the rules together (and there's no UI to allow for this form of query, hence the need to use inclusion/exclusion lists).  So your smartlist above really ends up being a single (fictitious) smartlis (broken into readable lines)t:

   [Media Type]=[Video] [Media Sub Type]=[TV Show] [Max Episodes to Keep]=>0 AND

   NO_FILES_WITHIN([Media Type]=[Video] [Media Sub Type]=[TV Show] [Max Episodes to Keep]=>0 ~sort=[Series],[Date Modified]-d ~limit=-1,2,[Series])

   ~sort=[Date Modified]

So if I'm correct here, you'd end up violating the restriction mentioned above that can't both calculate the GV and filter on that same GV in the same filter rule sequence.

MCWS I believe gives you the ability to get an MPL playlist returned based on a given search query.  This could have those calculated GVs if a calculated expression field referenced them.  I don't know if you can query and get back the filekey and/or filename and properties the same way with COM.

Forget what I said in the previous post about being able to sort in the smartlist first and then calculate via search query expressions.  I forgot about MC always moving the sort to after the search query rules in the query editor.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #21 on: April 20, 2014, 09:18:01 am »

I think I (finally) get it now.  The variables are only valid for a single "run" of any given view (so, during MC's "execution" of a view calculation).  They are scoped to the method call that produces the view, and not like a (true, ugly) global variable like would be used by a junky vbscript or whatever.  Am I getting that?

So, for example, if you forget all about COM, and I just did it the way you (recently) suggested with a calculated field.  The field I'd be making would simply output 0 for any file that should be kept, and 1 for any file that should be deleted (say [Should Be Deleted]).

Well, a regular, simple true/false field in the Library, could normally be used as a filter in a smartlist (of course).  So, if that User Field worked like a regular one from the Library, you could just make a list that looks for [Should Be Deleted]=>0 and show only those results, via the Files to Show search.  But, if what I think I understand now is true, that still wouldn't work regardless of what happened in any user interaction before the current "view refresh".  If the field is calculated based on a save()/load() pair, and there is no save() statement within that view's definition, then you can't do it.  Even if I was running these manually (physically clicking on one, then the other, in MC's UI)...

1. My field would work when I click on the smartlist that runs the search including the save() statements.  If that view/list has a column that displays the [Should Be Deleted] field it would work right from within this view or smartlist.

2. When I switch away to another list or view that tries to use [Should Be Deleted], the load() variable will be undefined again, because the variable no longer exists in the context of that "view refresh".

You see, I've never really understood them before.  And I think that's because they've been called "global variables" which made me think they were something they're not.  You can use them to pass information from the search to each "result" essentially (so, a good use would be a calculated column), but you can't use them to pass information from one view to another.

I think if you are relying entirely on smartlist processing, you'll be a little limited.

Even though you get the smartlist via COM, it would still calculate the variables used within a successful search query.  To get access to those variables, you'd need to create a custom user field, and set the expression to the load() expression necessary to produce the value you want.  The results should be valid once the smartlist runs (via COM should be fine), and (essentially) undefined before then.  Once you have your list of files, you'd have to ask MC for that field for each of your file objects so you can make decisions based on those values.

Yes, I could do it that way, but I'd have to custom code something that iterates over the Smartlist and looks at the value of that field (like MCAutoQueue pulls the value of the [Needs Processing] field and uses it to decide which processor to use for the file).

My hope was to avoid that and to keep the remover utility "stupid" and all it does is delete or remove all of the files in a given playlist.

I could do all of that, but honestly, it would be easier just to custom code the whole thing then, and not use MC's global variables at all.  I'd just write a utility that figures out the [Should Be Deleted] value itself (which it can do pretty easily by doing a search or two an then LINQ-querying the resulting list), and then writes this value to a regular (persistent) Library Field for each file record.  Then I can make my File Removing smartlist because it is a real field, and so long as I run step 1 beforehand, it would have fresh results.

But, I don't care enough to do all of that, I don't think.

Basically, the 1 week expiration is good enough for all of my needs except for a few of my News programs.  I have Sage set to record every episode of BBC News that airs on BBC America and PBS, for example.  And, the NBC Nightly News and I think one "evening news" program on CNN or MSNBC or something too (I don't watch these very much so I don't even remember which I have set up).  For the BBC News "recording favorite" I have shows to keep set in Sage to 3 (because it airs a few times a day, so it keeps the last 36 hours or so worth of versions of the news program), and for all the other ones I have it set to 1 (it keeps the last one recorded, and no others).

That works fine as it is, of course, so I could just leave it alone.  But, I was hoping to replicate that part in MC, and then I could use MCFileRemover to do it (which avoids the orphaned sidecar file problem).

But... It is the path of least resistance to just use the time-based expiration system I already have.  I could, of course, just make the [Weeks to Keep] field a [Days to Keep] field instead, and put 1 or 2 in there.  I didn't want to do that because the typical values I have for [Weeks to Keep] (other than these couple news items) are 1-3 months.  I like to keep the numbers small, and entering 4, 6, or 9 weeks is easier than using 30, 45, 60, 90, etc days when manually tagging the Series.

The path of less resistance would be to just abandon the "Number to Keep" and change my [Weeks to Keep] system to [Days to Keep].  The path of least resistance, however, is to just keep using Weeks and use 1 for all of those News programs above described, or just turn their recording off entirely, since I almost never actually, you know, watch them.

And that's what I think I'll do.

It is too bad, though, and it would be nice to have a solution for this within MC's expression language.  I've hit this wall before, with my desire to make the "currently watching" view for Theater View.  You can calculate anything you want about the files themselves, but you can't (easily) calculate much about groupings of those files based on their tags (in an abstract and flexible way)...

If [Rating]=>2 then do X with the file <<< is easy to do.
If any one file in a group (a particular [Artist], for example) has [Rating]=>2 then do something with all members of the group <<< is hard to do.

I think a great goal for them with the expression language would be to solve that problem.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #22 on: April 20, 2014, 09:32:45 am »

One last thing...

What happens to the values of Relational Fields if all items of a particular [Series] are deleted?

So, say I have it set up like I have it, and I set Dora the Explorer to 1 week.  Then, say, Dora the Explorer doesn't air at all for a while, two weeks (I know, like that would ever happen, there are like 12 airings per day, even on Christmas).  Anyway, if this happened, then at some point during that two weeks, I'm going to remove all of the episodes of Dora the Explorer from my Library because they'll all be older than 1 week.

Then, the show starts up again.  Have I lost my [Weeks to Keep]=1 setting for that show when the first new one imports?  Does the Removed Items database save me, if not, or are those values just written to the Library persistently somehow?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #23 on: April 20, 2014, 01:04:44 pm »

I'll respond in parts...

...The variables are only valid for a single "run" of any given view (so, during MC's "execution" of a view calculation).  They are scoped to the method call that produces the view, and not like a (true, ugly) global variable like would be used by a junky vbscript or whatever.  Am I getting that?

...

2. When I switch away to another list or view that tries to use [Should Be Deleted], the load() variable will be undefined again, because the variable no longer exists in the context of that "view refresh".

The GVs retain their values until MC exits, or when changed by another save().  So it is sufficient to run a smartlist that calculates them.  And if a calculated user field does a load() of that GV, it can be used elsewhere in MC like any calculated field.  Even another smartlist, which does a search query on that field.

Here's an example of a smartlist that will mark for deletion any Album that contains the Artist ABBA:

Smartlist1 rules for file display (either of these will work, but differ in the resulting file list shown):

   [=1save(0, gv_to_delete_[album])]=1 [=1ifelse(isequal([Artist], ABBA), save(6, gv_to_delete_[album]))]=1
   [=1save(0, gv_to_delete_[album])]=1 [Artist]=[ABBA] [=1save(6, gv_to_delete_[album])]=1

Smartlist2's rules for file display to query for albums-specific GVs that have the value 6:

    [=compare(load(gv_to_delete_[album]), =, 6)]=1

Running Smartlist1 will set the GV.  Later on in MC, when you visit Smartlist2, the results will be only album's containing Artist=ABBA.

If you created a calculated user field, say [To Delete], and assign the expression:

   compare(load(gv_to_delete_[album]), =, 6)

then if you set rules for file display in, say, Smartlist3:

    [To Delete]=[1]

you'll get the same results as in Smartlist2.  Or, set the field to:

   load(gv_to_delete_[album])&datatype=[number]

and you can set rules for file display as:

   [To Delete]=>=6
Logged
The opinions I express represent my own folly.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #24 on: April 20, 2014, 01:18:20 pm »

One last thing...

What happens to the values of Relational Fields if all items of a particular [Series] are deleted?

So, say I have it set up like I have it, and I set Dora the Explorer to 1 week.  Then, say, Dora the Explorer doesn't air at all for a while, two weeks (I know, like that would ever happen, there are like 12 airings per day, even on Christmas).  Anyway, if this happened, then at some point during that two weeks, I'm going to remove all of the episodes of Dora the Explorer from my Library because they'll all be older than 1 week.

Then, the show starts up again.  Have I lost my [Weeks to Keep]=1 setting for that show when the first new one imports?  Does the Removed Items database save me, if not, or are those values just written to the Library persistently somehow?

There needs to be at least one related record with a relational value for other (new) related records to inherit that value.  Here's how you can think of it:

   1. a new record's relational field will use an existing value from that same field in some other existing record.
   2. a change to that relational field on any record will ripple to all other records.
   3. a change of the relational key value (e.g. Series) will cause the changed records to inherit the value from existing records (related by that new key name of course).

So, Rule 1 tells us your new Dora episodes have nothing to inherit since no Series-related values exist for that Series.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #25 on: April 20, 2014, 01:50:46 pm »

I'll respond in parts...

The GVs retain their values until MC exits, or when changed by another save().

So, that means I was right before, and not this morning.  Okay, then...

Let me try your examples (thanks, I needed a simple example) and I'll get back to you.

A couple questions though, if you don't mind:
  • []=1 in the Smartlist1 search: If my only goal is to load the variables, and I'm not going to ever use or look at the results of the search, then does it matter if it is equal to 0 or equal to 1?  What does that do?
  • Why did you use 6 and not 1 for the variable value?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #26 on: April 20, 2014, 02:14:41 pm »

Hmmm... It is hard when your example doesn't work.

I made Smartlist1 with this search (copypasta-d from your post above):
Code: [Select]
[=1save(0, gv_to_delete_[album])]=1 [=1ifelse(isequal([Artist], ABBA), save(6, gv_to_delete_[album]))]=1
(Which, by the way, what is up with the 1 in 1save()?  Is that the problem?  I tried removing it, but it was still broken.)

I added an expression column to the view, with:
Code: [Select]
=compare(load(gv_to_delete_[album]), =, 6)
See the first screenshot attached.  See how the ABBA tracks all have 1s, but so do other random tracks (that don't say ABBA in the title anywhere)?

I made Smartlist2 with this search (again copypasta):
Code: [Select]
[=compare(load(gv_to_delete_[album]), =, 6)]=1
And I get (see the second attached screenshot): 44214 files, all that have a result of 1 for the same expression column.  I don't have 44k ABBA tracks, thanks.

So, then I tried the second version of your smartlist search:
Code: [Select]
[=1save(0, gv_to_delete_[album])]=1 [Artist]=[ABBA] [=1save(6, gv_to_delete_[album])]=1
This results in a visible list in Smartlist1 of just my ABBA songs.  The Expression Column lists all 1s (third screenshot).

But when I then switch to Smartlist2, it still contains exactly the same 44214 files.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #27 on: April 20, 2014, 02:25:54 pm »

There needs to be at least one related record with a relational value for other (new) related records to inherit that value.  Here's how you can think of it:

   1. a new record's relational field will use an existing value from that same field in some other existing record.
   2. a change to that relational field on any record will ripple to all other records.
   3. a change of the relational key value (e.g. Series) will cause the changed records to inherit the value from existing records (related by that new key name of course).

So, Rule 1 tells us your new Dora episodes have nothing to inherit since no Series-related values exist for that Series.

Grrr... Then if they all get deleted, the Series will get dropped from the "queue for deletion" and lose its settings.  That won't happen to Dora, but it could happen to other shows that take occasional long breaks (The Daily Show, for example).

I suppose I can add a phantom asset that is protected and never gets deleted, but that seems like a crude hack.
Maybe I add a check to prevent it from deleting the most recent episode of any [Series] (via the method referenced above)...?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #28 on: April 20, 2014, 02:26:21 pm »

The examples all work - i tested each of them.

The variable name I chose included the [album] name, so it will be confounded when Album is not set.  I was trying to show that a variable can be "relational", in that, the GV would be set on a per-album basis (you want some relational value, otherwise you'll get one GV for all your files and it will contain a single value).  The var is:

   gv_to_delete_[album]

See how the GV name is constructed via interpolation into:

   gv_to_delete_ABBA
   gv_to_delete_Golden Decade
   ...

You don't have to create that relationship - you might instead want/need:

      gv_to_delete_[Series]
  
but you'll still have the same problem when Series is unset, as you'll end up with a GV named:

      gv_to_delete_

and it will contain the same value for all empty Series (or Albums as the example used).

The 1 in the Search query is required because MC tests search query expressions for an integer value output.  Save() by default outputs nothing, so there would be no value to test.  Compare:

   [=save(0, GV)]=1
   [=1save(0, GV)]=1

Since save(0, GV) outputs nothing, the result query will fail in the first example, and the second example evaluates to:

    [=1]=1

since save(0, GV) evaluates to empty.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #29 on: April 20, 2014, 02:29:13 pm »

The variable name I chose included the [album] name, so it will be confounded when Album is not set.

Oh, I see.  I didn't even notice (my daughter was waking up and I was rushed) that the [Album] tag was blank on all of them.  Since I didn't limit the Smartlist in any other way, that was searching against all of my files completely (for which there are a ton of other media types with no [Album] tag).
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #30 on: April 20, 2014, 02:31:06 pm »

The 1 in the Search query is required because MC tests search query expressions for an integer value output

Got it.

Why the 6?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #31 on: April 20, 2014, 02:32:16 pm »

Grrr... Then if they all get deleted, the Series will get dropped from the "queue for deletion" and lose its settings.  That won't happen to Dora, but it could happen to other shows that take occasional long breaks (The Daily Show, for example).

I suppose I can add a phantom asset that is protected and never gets deleted, but that seems like a crude hack.
Maybe I add a check to prevent it from deleting the most recent episode of any [Series] (via the method referenced above)...?

Yup.  I think the approach being used for this is problematic.  You're wanting to use a file record to store values, and yet retain those values after the file records have been deleted.

To do this, you'll need to retain 1 record marked so that it isn't in your view or returned smartlist, but is only deleted when there is at least one more record available.  That's a very expensive (disk-wise) means to store a config file, essentially.  So why not just create a config file record that is related to your shows that retains the metadata you want?
Logged
The opinions I express represent my own folly.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #32 on: April 20, 2014, 02:34:27 pm »

Why the 6?

Semi-randomly chosen, just so that it would not be confused with 0 or 1 output, since there's already that wierd 1 in the search expression query, and the 1 as the comparison value, and Compare returns a 0 or 1.  Too many 0's and 1's to see how it all works.  6 seemed good.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #33 on: April 20, 2014, 04:18:00 pm »

Yup.  I think the approach being used for this is problematic.  You're wanting to use a file record to store values, and yet retain those values after the file records have been deleted.

It is easily solved with a Tag On Import rule, since they all are created in the same directory anyway.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #34 on: April 20, 2014, 04:25:21 pm »

Semi-randomly chosen, just so that it would not be confused with 0 or 1 output, since there's already that wierd 1 in the search expression query, and the 1 as the comparison value, and Compare returns a 0 or 1.  Too many 0's and 1's to see how it all works.  6 seemed good.

That's kind what I thought, but wanted to make sure I wasn't missing some essential component.

The variable setting seems to apply to all files regardless of filters I add to Smartlist1 (I guess, hence, global in the name).  So, to solve the blank [Album] field issue (just as an experiment), I changed Smartlist1 to:
Code: [Select]
-[Album]=[] [=1save(0, gv_to_delete_[album])]=1 [=1ifelse(isequal([Artist], ABBA), save(6, gv_to_delete_[album]))]=1
This changes the results of that list, which no longer displays any files for which [Album] is blank.  But, despite that I did it before the save(), Smartlist2 still has all of the [Album] = blank files.

So, if I understand it right, the only good way to do it is to make it fail to 0 (your saves in the example fail to 1)?  Or explicitly test for things where this might be an issue all within the expression that does the save()?
Or, I suppose adding the filter to the subsequent playlist is good enough, but then it isn't really a reliable indicator (by itself)?
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression Help - Date Math
« Reply #35 on: April 20, 2014, 04:41:03 pm »

You're probably being tripped up by the Globalness.  Since you've already inadvertently set the variable:

  gv_to_delete_

it still retains its value, even though Smartlist1 is no longer calculating it (since -[Album]=[] pre-filters).

That's why I typically do:

  [=1save(0, gv_to_delete_[album])]=1

up front - to ensure clearing of all values first, and then more filtering later.

If you Quit MC and restart it, hit Smartlist1, then you'll find Smartlist2 starts working.

GVs are a gross hack in MC, in the way that they have to be set (to be useful) in the Filtering stage, as a side effect.
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #36 on: April 20, 2014, 05:08:47 pm »

Doh.  I'd thought I'd tested that, but it wasn't this time.

Okay, now I get it, I'm reasonably confident.  I'll re-read the stuff above, and I'm sure I'll have further questions.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expression Help - Date Math
« Reply #37 on: April 21, 2014, 10:59:12 am »

It is easily solved with a Tag On Import rule, since they all are created in the same directory anyway.

Actually, what I think I'd like to do once I get this all done is set it up so:

[Weeks to Keep] works as discussed above, only it adds this exception:
  • Never delete the last remaining file of any given series


So, essentially, [Weeks to Keep] would always retain the single most-recent file for any given [Series], which would prevent the [Series] from being removed, which means the relational [Weeks to Keep] field would always be retained.  I think that should be possible using what we've discussed already, but if you have any suggestions for how to do it more elegantly, I'm all ears.

Then, separately...

[Max Episodes to Keep] using the fancy thing we've been discussing.  From what I can see, this will work fine.  All I'll need to do is write a simple applet that sends a Search to MC to set the variable (replacing the behavior of Smarlist1 in the examples above).  I can have the script that runs MCFileRemover.exe simply call this separate utility right before it runs the actual removal.

That shouldn't be difficult at all, and would be a useful utility to have anyway.

That way, each time the Remover script runs, it'll

1. Call MCVariableSetter.exe and blast in the GV I need.
2. Call MCFileRemover.exe to process the Max Episodes Exceeded playlist.

I think that will work pretty brilliantly.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/
Pages: [1]   Go Up