问:
Append date from another table without joining
I have two tables in oracle that has data for different years.

Table 1: This has data for years 2016 - 2018. This is a static table ie wont be refreshed again.
Table 2: This table has data for years >= 2019. This table is being refreshed 3 times a week and has a column REFRESH_DATE that contains the last refreshed date.

Now I have to do a union of table 1 and table 2 but since table 1 is static how do I take table 2's refresh date to table 1?

For example 1) Current Scenario:

```<code>with tABLE_1 as

( select  '99WC2008' as pol_id, to_date( '1/23/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model from dual Union all
select  '96WC2003' as pol_id,  to_date('1/22/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model from dual Union all
select  '90WC2003' as pol_id,  to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model from dual Union all
select  '10XYZ23' as pol_id,  to_date('1/12/2017','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model from dual Union all
select  '09XYZ23' as pol_id,  to_date('1/11/2018','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model from dual)
,
TABLE_2 AS

( select  'XWG5564' as pol_id, to_date( '1/23/2019','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  'TTFG556' as pol_id,  to_date('1/22/2020','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  'TTH676Y' as pol_id,  to_date('1/21/2021','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
Select  'DFE5756' as pol_id,  to_date('2/01/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  '567HNG6' as pol_id,  to_date('2/08/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual)

SELECT * FROM
(
select pol_id,  Eff_dt, industry, model  from TABLE_1
UNION ALL
select pol_id,  Eff_dt, industry, model  from TABLE_2
)
ORDER BY EFF_DT DESC

2) Required_output: Always taking the Refresh date from Table_2 and display it in Table_1 as well.

    with tABLE_1 as

    ( select  '99WC2008' as pol_id, to_date( '1/23/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model from dual Union all
    select  '96WC2003' as pol_id,  to_date('1/22/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model from dual Union all
    select  '90WC2003' as pol_id,  to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model from dual Union all
    select  '10XYZ23' as pol_id,  to_date('1/12/2017','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model from dual Union all
    select  '09XYZ23' as pol_id,  to_date('1/11/2018','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model from dual)
    ,
    TABLE_2 AS

    ( select  'XWG5564' as pol_id, to_date( '1/23/2019','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  'TTFG556' as pol_id,  to_date('1/22/2020','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  'TTH676Y' as pol_id,  to_date('1/21/2021','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    Select  'DFE5756' as pol_id,  to_date('2/01/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  '567HNG6' as pol_id,  to_date('2/08/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual)

    SELECT * FROM
    (
    select pol_id,  Eff_dt, industry, model ,  to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from TABLE_1
    UNION ALL
    select pol_id,  Eff_dt, industry, model ,REFRESH_DATE  from TABLE_2
    )
    ORDER BY EFF_DT DESC
</code>```

Is it possible to get data from another table without joining? In the above Union step i am hardcoding the date for table_1, the requirement is to automatically pull that date from table_2.

答:
Another option is to get a null refresh date from table 1 branch of the union, as a placeholder; and then in the outer query, use an analytic max():

```<code>max(refresh_date) over ()
</code>```

... to get the highest value across that combined data set - i.e., the date from the table 2 rows.

```<code>SELECT pol_id, eff_dt, industry, model,
  max(refresh_date) over () as refresh_date
FROM
(
  select pol_id,  Eff_dt, industry, model, null as refresh_date from TABLE_1
  UNION ALL
  select pol_id,  Eff_dt, industry, model, refresh_date from TABLE_2
)
</code>```

db<>fiddle

You probably won't gain any real advantage from not hitting table 2 a second time, since you're already retrieving all the data from it; and any gain could be outweighed by the cost of the analytic call. You might still find one approach is marginally faster than the other - I suspect you'd have to look quite hard to notice though.

Body:
I have two tables in oracle that has data for different years.

Table 1: This has data for years 2016 - 2018. This is a static table ie wont be refreshed again.
Table 2: This table has data for years >= 2019. This table is being refreshed 3 times a week and has a column REFRESH_DATE that contains the last refreshed date.

Now I have to do a union of table 1 and table 2 but since table 1 is static how do I take table 2's refresh date to table 1?

For example 1) Current Scenario:

with tABLE_1 as

( select  '99WC2008' as pol_id, to_date( '1/23/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model from dual Union all
select  '96WC2003' as pol_id,  to_date('1/22/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model from dual Union all
select  '90WC2003' as pol_id,  to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model from dual Union all
select  '10XYZ23' as pol_id,  to_date('1/12/2017','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model from dual Union all
select  '09XYZ23' as pol_id,  to_date('1/11/2018','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model from dual)
,
TABLE_2 AS

( select  'XWG5564' as pol_id, to_date( '1/23/2019','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  'TTFG556' as pol_id,  to_date('1/22/2020','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  'TTH676Y' as pol_id,  to_date('1/21/2021','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
Select  'DFE5756' as pol_id,  to_date('2/01/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
select  '567HNG6' as pol_id,  to_date('2/08/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual)

SELECT * FROM
(
select pol_id,  Eff_dt, industry, model  from TABLE_1
UNION ALL
select pol_id,  Eff_dt, industry, model  from TABLE_2
)
ORDER BY EFF_DT DESC

2) Required_output: Always taking the Refresh date from Table_2 and display it in Table_1 as well.

    with tABLE_1 as

    ( select  '99WC2008' as pol_id, to_date( '1/23/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model from dual Union all
    select  '96WC2003' as pol_id,  to_date('1/22/2016','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model from dual Union all
    select  '90WC2003' as pol_id,  to_date('1/21/2017','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model from dual Union all
    select  '10XYZ23' as pol_id,  to_date('1/12/2017','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model from dual Union all
    select  '09XYZ23' as pol_id,  to_date('1/11/2018','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model from dual)
    ,
    TABLE_2 AS

    ( select  'XWG5564' as pol_id, to_date( '1/23/2019','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '-1' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  'TTFG556' as pol_id,  to_date('1/22/2020','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.34' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  'TTH676Y' as pol_id,  to_date('1/21/2021','mm/dd/yyyy') as Eff_dt , 'RETAIL' as industry , '0.55' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    Select  'DFE5756' as pol_id,  to_date('2/01/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual Union all
    select  '567HNG6' as pol_id,  to_date('2/08/2022','mm/dd/yyyy') as Eff_dt , 'TECH' as industry , '0.3' as Model, to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from dual)

    SELECT * FROM
    (
    select pol_id,  Eff_dt, industry, model ,  to_date( '2/08/2022','mm/dd/yyyy') as REFRESH_DATE from TABLE_1
    UNION ALL
    select pol_id,  Eff_dt, industry, model ,REFRESH_DATE  from TABLE_2
    )
    ORDER BY EFF_DT DESC

Is it possible to get data from another table without joining? In the above Union step i am hardcoding the date for table_1, the requirement is to automatically pull that date from table_2.

Body:
Another option is to get a null refresh date from table 1 branch of the union, as a placeholder; and then in the outer query, use an analytic max():

max(refresh_date) over ()

... to get the highest value across that combined data set - i.e., the date from the table 2 rows.

SELECT pol_id, eff_dt, industry, model,
  max(refresh_date) over () as refresh_date
FROM
(
  select pol_id,  Eff_dt, industry, model, null as refresh_date from TABLE_1
  UNION ALL
  select pol_id,  Eff_dt, industry, model, refresh_date from TABLE_2
)

db<>fiddle

You probably won't gain any real advantage from not hitting table 2 a second time, since you're already retrieving all the data from it; and any gain could be outweighed by the cost of the analytic call. You might still find one approach is marginally faster than the other - I suspect you'd have to look quite hard to notice though.


来源: stackexchange

问:
What is the simplest way to add Facebook Connect to a PHP web site?

Facebook Connect

I have spent a few weeks putting together a basic web site which uses Facebook connect as authentication.

I have studied 'theRunAround', the provided example application. However the code is convoluted and uses a large number of files and classes, most of which have a lot of functionality that I have no need for. I have also had a good read of the documentation.

My code works almost all the time. However, occasionally I receive strange, unpredictable errors.

Required Features

Check if user is already logged in to Facebook, if so, retrieve the fb_uid, name, profile pic

Return a list of friends which have also connected to this application

Are there any tutorials, better than the ones provided by Facebook regarding such simple functionality?

答:
I have recently added a friend notification feature to Cogenuity using the facebook connect technology.

First, you need to create the facebook object in your php code. I am assuming that you have already done the facebook application registration goodness. I found a lot of the parts to facebook connect not to work but what did work was this.

```<code>$user = $facebook->require_login();
</code>```

I found that the FQL parts worked pretty good.

```<code>$query = "select uid1 from friend where uid2 = {$user}";
$results = $facebook->api_client->fql_query($query);
</code>```

I used this FQL query which may serve your needs.

```<code>$query = "SELECT name, pic_square, status, about_me FROM user WHERE uid = {$uid}";
$results = $facebook->api_client->fql_query($query);
</code>```

As does the notification_send method.

```<code>$facebook->api_client->notifications_send($notifyTarget, $intro, 'user_to_user');
</code>```

I hope this helps.

OwnerDisplayName: Jon

ContentLicense: CC BY-SA 2.5

Body:
I have recently added a friend notification feature to Cogenuity using the facebook connect technology.

First, you need to create the facebook object in your php code. I am assuming that you have already done the facebook application registration goodness. I found a lot of the parts to facebook connect not to work but what did work was this.

$user = $facebook->require_login();

I found that the FQL parts worked pretty good.

$query = "select uid1 from friend where uid2 = {$user}";
$results = $facebook->api_client->fql_query($query);

I used this FQL query which may serve your needs.

$query = "SELECT name, pic_square, status, about_me FROM user WHERE uid = {$uid}";
$results = $facebook->api_client->fql_query($query);

As does the notification_send method.

$facebook->api_client->notifications_send($notifyTarget, $intro, 'user_to_user');

I hope this helps.

OwnerDisplayName: Glenn


问:
How to force a GPRS connection instead of 3G or Wifi?
I am writing an application where I need to have specifically a GPRS connection, even if wifi or 3G (or better) is available. I have figured out how to disable wifi (by using WifiManager), however, I can't seem to find out how I can programmatically disable 3G and only use a GPRS connection, with equal effect as changing this manually in the device settings.

Is there a way to do this with code?

UPDATE: What I am looking for does not necessarily have to be in the public Android APIs. It might as well be in one of the undocumented APIs, or as a last resort, device-specific APIs, I just need something that works. However, I'd rather not have to root a device to use these functions.

答: as far as i know changing the type of the mobile network is not possible out from an app.</p>

ContentLicense: CC BY-SA 3.0


问:
How to add 5 stars rating as default?
I'm trying to make 5 stars the default option in the Woocommerce rating system, so when a user is going to leave a review he doesn't have to manually select the five stars option.

I've tried the solution outlined here, but it didn't work:

https://pastebin.com/f0at8QaF

```<code>add_filter('site-reviews/config/forms/review-form', function ($fields) {
if (isset($fields['rating'])) {
    $fields['rating']['value'] = 5;
}
return $fields;
</code>```

});

I'm using a theme I purchased that changed a few things, so maybe that's the problem; but any idea how to do this?

答:
One simple idea is to use JQuery to select 5-stars reviews. You need to write your jquery script and call it in your functions.php file. Check below how this works for me.

1)This is an example of how your jQuery script (in wp-content/themes/your-theme/your-script.js) might look:
*you may need to locate and change the classes in your theme that refer to stars

```<code>(function($) {
$(document).ready(function() {
$('.stars').addClass('selected');
$('.star-5').addClass('active');
})
})(jQuery);
</code>```

2)And this is an example of how you call your script at your theme's functions.php file.

```<code>function add_my_script() {
    wp_enqueue_script(
        'fullreciew', // name your script so that you can attach other scripts and de-register, etc.
        get_template_directory_uri() . '/jquery_test.js', // this is the location of your script file
        array('jquery')
    );
}
add_action( 'wp_head', 'add_my_script' );
</code>```

Tested at https://themeforest.net/item/flatsome-multipurpose-responsive-woocommerce-theme/5484319 and it works

ContentLicense: CC BY-SA 4.0

Title: How to add 5 stars rating as default?

Body:
I'm trying to make 5 stars the default option in the Woocommerce rating system, so when a user is going to leave a review he doesn't have to manually select the five stars option.

I've tried the solution outlined here, but it didn't work:

https://pastebin.com/f0at8QaF

add_filter('site-reviews/config/forms/review-form', function ($fields) {
if (isset($fields['rating'])) {
    $fields['rating']['value'] = 5;
}
return $fields;

});

I'm using a theme I purchased that changed a few things, so maybe that's the problem; but any idea how to do this?

Body:
One simple idea is to use JQuery to select 5-stars reviews. You need to write your jquery script and call it in your functions.php file. Check below how this works for me.

1)This is an example of how your jQuery script (in wp-content/themes/your-theme/your-script.js) might look:
*you may need to locate and change the classes in your theme that refer to stars

(function($) {
$(document).ready(function() {
$('.stars').addClass('selected');
$('.star-5').addClass('active');
})
})(jQuery);

2)And this is an example of how you call your script at your theme's functions.php file.

function add_my_script() {
    wp_enqueue_script(
        'fullreciew', // name your script so that you can attach other scripts and de-register, etc.
        get_template_directory_uri() . '/jquery_test.js', // this is the location of your script file
        array('jquery')
    );
}
add_action( 'wp_head', 'add_my_script' );

Tested at https://themeforest.net/item/flatsome-multipurpose-responsive-woocommerce-theme/5484319 and it works


问:
Unittesting Azure System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json'
I have a library that retrieves secrets from Azure. I can use it without a problem from a console app, but when I use it from unittest, I get an error:

Test method UnitTests.UnitTest1.TestCredentials threw exception:
System.IO.FileLoadException: Could not load file or assembly
'System.Text.Json, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
at Common.Credentials.GetDbConnStr(String databaseName)    at UnitTests.UnitTest1.TestCredentials()

```<code>using System;
using System.Configuration;
using System.Text.Json;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

namespace Common
{
    public class Credentials
    {
        public static string GetDbConnStr(string databaseName)
        {
            string keyVaultUrl = "https://cat.vault.azure.net/";
            Console.WriteLine($"Retrieving credentials for {databaseName}");
            var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
            var res = client.GetSecret("conn");
            return res.Value.Value.ToString() + $"Database={databaseName}";
        }
    }
}
</code>```

and here is the test:

```<code>using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Data.SqlClient;
using Common;

namespace UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestCredentials()
        {
            string conn_str = Credentials.GetDbConnStr("test");
        }
    }
}
</code>```

Note that I can see System.Text.Json.dll in the bin/Debug folder of the test project.

答:
Thanks to Anand Sowmithiran who posted the comment

Check your versions of Function runtime, .NET, etc. - refer this Github issue

I upgraded System.Text.Json to version 6.0.2 as suggested in the issue and the problem went away.

Body:
I have a library that retrieves secrets from Azure. I can use it without a problem from a console app, but when I use it from unittest, I get an error:

Test method UnitTests.UnitTest1.TestCredentials threw exception:
System.IO.FileLoadException: Could not load file or assembly
'System.Text.Json, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
at Common.Credentials.GetDbConnStr(String databaseName)    at UnitTests.UnitTest1.TestCredentials()

using System;
using System.Configuration;
using System.Text.Json;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

namespace Common
{
    public class Credentials
    {
        public static string GetDbConnStr(string databaseName)
        {
            string keyVaultUrl = "https://cat.vault.azure.net/";
            Console.WriteLine($"Retrieving credentials for {databaseName}");
            var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
            var res = client.GetSecret("conn");
            return res.Value.Value.ToString() + $"Database={databaseName}";
        }
    }
}

and here is the test:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Data.SqlClient;
using Common;

namespace UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestCredentials()
        {
            string conn_str = Credentials.GetDbConnStr("test");
        }
    }
}

Note that I can see System.Text.Json.dll in the bin/Debug folder of the test project.


问:
Is it possible to connect Azure to google drive?
I am writing a system in ASP.NET CORE and uploading it to AZURE.

I'm looking for a free place to store files that customers upload.

Azure storage costs money so I thought of connecting it to Google drive
Is it possible? And can you explain to me how?

答:
You could defiantly store your files in Google drive.  Use the google drive api, do it with a service account as it sounds like you will only be uploading to an account you control.

```<code>GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                             .CreateScoped(scopes);
                    }

                    // Create the  Analytics service.
                    return new DriveService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Drive Service account Authentication Sample",
                    });
</code>```

Uploading files to Google Drive API with a service account

as for the hosting in azure part.  I recommend using the json key file as opposed to the p12 certificate file.  I have had issues with that in azure before.

Upload from memory

As the files your users are uploading may just be a memory stream rather then files stored on your hard drive this is also possible using a MemoryStream

```<code>var uploadString = "Test";
var fileName = "ploadFileString.txt";
// Upload file Metadata
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
    {
     Name = fileName,
     Parents = new List<string>() { "1R_QjyKyvET838G6loFSRu27C-3ASMJJa" }  // folder to upload the file to
     };

var fsSource = new MemoryStream(Encoding.UTF8.GetBytes(uploadString ?? ""));

string uploadedFileId;

// Create a new file, with metadata and stream.
var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
request.Fields = "*";
var results = await request.UploadAsync(CancellationToken.None);

if (results.Status == UploadStatus.Failed)
   {
   Console.WriteLine($"Error uploading file: {results.Exception.Message}");
   }
// the file id of the new file we created
uploadedFileId = request.ResponseBody?.Id;
</code>```

How to upload to Google Drive API from memory with C#

Body:
You could defiantly store your files in Google drive.  Use the google drive api, do it with a service account as it sounds like you will only be uploading to an account you control.

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                             .CreateScoped(scopes);
                    }

                    // Create the  Analytics service.
                    return new DriveService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Drive Service account Authentication Sample",
                    });

Uploading files to Google Drive API with a service account

as for the hosting in azure part.  I recommend using the json key file as opposed to the p12 certificate file.  I have had issues with that in azure before.

Upload from memory

As the files your users are uploading may just be a memory stream rather then files stored on your hard drive this is also possible using a MemoryStream

var uploadString = "Test";
var fileName = "ploadFileString.txt";
// Upload file Metadata
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
    {
     Name = fileName,
     Parents = new List<string>() { "1R_QjyKyvET838G6loFSRu27C-3ASMJJa" }  // folder to upload the file to
     };

var fsSource = new MemoryStream(Encoding.UTF8.GetBytes(uploadString ?? ""));

string uploadedFileId;

// Create a new file, with metadata and stream.
var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
request.Fields = "*";
var results = await request.UploadAsync(CancellationToken.None);

if (results.Status == UploadStatus.Failed)
   {
   Console.WriteLine($"Error uploading file: {results.Exception.Message}");
   }
// the file id of the new file we created
uploadedFileId = request.ResponseBody?.Id;

How to upload to Google Drive API from memory with C#


问:
ReCaptcha gives valid as a response even when one character is skipped
I am using http://code.google.com/apis/recaptcha/docs/verify.html, Reptcha gives valid as a response even one character is skipped.</p>

答:
http://www.google.com/recaptcha/learnmore

Each new word that cannot be read correctly by OCR is given to a user
  in conjunction with another word for which the answer is already
  known. The user is then asked to read both words. If they solve the
  one for which the answer is known, the system assumes their answer is
  correct for the new one. The system then gives the new image to a
  number of other people to determine, with higher confidence, whether
  the original answer was correct.


问:
How can I get build a query based on array of IDs using EF
I'm trying to build a query selecting all records containing IDs which are stored in the list using that code:

```<code>var assistsIds = _context.Assistances.Where(c => c.IdUser == user.IdUser)
                        .Select(x => x.Owner.IdOwner).ToList();
</code>```

Then I'm going through all the list elements to get a query:

```<code>var query = _context.Accounts.Where(_ => _.IsDeleted != 1);

                foreach(var assist in assistsIds)
                {
                    query = query.Where(_ => _.IdOwner == assist);
                }
</code>```

The result is that I'm getting something like this:

```<code>SELECT * FROM Accounts WHERE IdOwner = 1 AND IdOwner = 2 ...etc
</code>```

Instead of:

```<code>SELECT * FROM Accounts WHERE IdOwner = 1 OR IdOwner = 2 ... etc
</code>```

Is there a way to apply OR operator, or maybe there is some other way to achieve that?

答:
You could use Contains:

```<code>var query = _context.Accounts
                .Where(_ => _.IsDeleted != 1 && assistsIds.Contains(_.IdOwner));
</code>```

This will return all records which match an Id in the assistsIds list.

Body:
I'm trying to build a query selecting all records containing IDs which are stored in the list using that code:

var assistsIds = _context.Assistances.Where(c => c.IdUser == user.IdUser)
                        .Select(x => x.Owner.IdOwner).ToList();

Then I'm going through all the list elements to get a query:

var query = _context.Accounts.Where(_ => _.IsDeleted != 1);

                foreach(var assist in assistsIds)
                {
                    query = query.Where(_ => _.IdOwner == assist);
                }

The result is that I'm getting something like this:

SELECT * FROM Accounts WHERE IdOwner = 1 AND IdOwner = 2 ...etc

Instead of:

SELECT * FROM Accounts WHERE IdOwner = 1 OR IdOwner = 2 ... etc

Is there a way to apply OR operator, or maybe there is some other way to achieve that?

Body:
You could use Contains:

var query = _context.Accounts
                .Where(_ => _.IsDeleted != 1 && assistsIds.Contains(_.IdOwner));

This will return all records which match an Id in the assistsIds list.


问:
How to unit test a method with a `using` statement?
How can I write a unit test for a method that has a using statement?

For example let assume that I have a method Foo.

```<code>public bool Foo()
{
    using (IMyDisposableClass client = new MyDisposableClass())
    {
        return client.SomeOtherMethod();
    }
}
</code>```

How can I test something like the code above?

Sometimes I choose not to use using statement and Dispose() an object manually.  I hope that someone will show me a trick I can use.

答:
You could also change the method signature to allow passing in a mock for unit testing. This would provide an alternative to using a factory, which would also need to be unit tested. DI into the method as opposed to the class constructor may be preferable here.</p>
```<code>public bool Foo(IMyDisposableClass mock = null)
{
    using (IMyDisposableClass client = mock ?? new MyDisposableClass())
    {
        return client.SomeOtherMethod();
    }
}
</code>```

Body:
How can I write a unit test for a method that has a using statement?

For example let assume that I have a method Foo.

public bool Foo()
{
    using (IMyDisposableClass client = new MyDisposableClass())
    {
        return client.SomeOtherMethod();
    }
}

How can I test something like the code above?

Sometimes I choose not to use using statement and Dispose() an object manually.  I hope that someone will show me a trick I can use.

Body:
You could also change the method signature to allow passing in a mock for unit testing. This would provide an alternative to using a factory, which would also need to be unit tested. DI into the method as opposed to the class constructor may be preferable here.

public bool Foo(IMyDisposableClass mock = null)
{
    using (IMyDisposableClass client = mock ?? new MyDisposableClass())
    {
        return client.SomeOtherMethod();
    }
}


问:
Pandas calculate new column based on condition
I have average watch time duration in secs, video length in secs and the day of the week for a list of shows.

```<code>    Week Day    Movie Name  Time Spent(seconds)     Duration sec    Week Number

0   Friday       Movie A    130.0                   1740.000000        4
1   Friday       Movie B    14665.0                 36278.000000       4
2   Friday       Movie C    26083.0                 166802.000002      4
3   Monday       Movie B    13515.0                 29290.000000       0
4   Monday       Movie C    21756.0                 136829.999999      0
5   Saturday     Movie A    94.0                    1680.000000        5
6   Saturday     Movie B    31373.0                 64092.000000       5
7   Saturday     Movie E    615.0                   2640.000000        5
8   Saturday     Movie D    175.0                   11940.000000       5
9   Saturday     Movie C    4671.0                  29340.000000       5
10  Sunday       Movie A    9238.0                  52797.000000       6
11  Sunday       Movie B    12016.0                 31332.000000       6
12  Sunday       Movie E    528.0                   2520.000000        6
13  Thursday     Movie A    212.0                   1860.000000        3
14  Thursday     Movie B    39018.0                 91197.000000       3
15  Thursday     Movie E    26402.0                 348991.000000      3
16  Thursday     Movie C    374.0                   4860.000000        3
17  Tuesday      Movie B    40456.0                 97607.000000       1
18  Tuesday      Movie C    7181.0                  29700.000000       1
19  Wednesday    Movie B    13243.0                 32093.000000       2
20  Wednesday    Movie D    1385.0                  34476.000000       2
21  Wednesday    Movie C    21256.0                 137097.999999      2
</code>```

I want to calculate watch percentage of each show by weekday. I can easily conceptualize this in excel

```<code>SUM(average watch time)/SUM(video length for the weekday)
</code>```

Edit: This is how I'm doing it as of now

```<code>result = []
for index, row in dff.iterrows():
    value = row["Time Spent(seconds)"]/dff.where(dff["Week Day"] == row["Week Day"])["Duration sec"].sum()
    result.append(value)
dff["Watch Time %"] = result
</code>```

Summation of Weekday can be cached but notice the condition dff["Week Day"] == row["Week Day"].

How can I get weekday and the sum of video length for the weekday based on the current row that pandas is working on? Is there an efficient way of doing it other than iterrows?

答:
Assuming you have a dataframe that looks something like the following:

```<code>mdf =
    Movie   Day Length  View_Time
0   A        2  2.6      1.8
1   A        3  2.6      2.6
2   B        1  5.8      2.9
3   A        4  2.6      2.6
4   B        6  5.8      4.2
5   A        0  2.6      0.5
6   B        3  5.8      2.0
</code>```

You can compute the length per day by each movie as follows:

```<code> mdf.groupby(['Movie', 'Day']).Length.sum().to_frame()

Which produces a dataframe of the form:
              Length
Movie   Day
A        0    2.6
         2    2.6
         3    2.6
         4    2.6
B        1    5.8
         3    5.8
         6    5.8
</code>```

Body:
I have average watch time duration in secs, video length in secs and the day of the week for a list of shows.

    Week Day    Movie Name  Time Spent(seconds)     Duration sec    Week Number

0   Friday       Movie A    130.0                   1740.000000        4
1   Friday       Movie B    14665.0                 36278.000000       4
2   Friday       Movie C    26083.0                 166802.000002      4
3   Monday       Movie B    13515.0                 29290.000000       0
4   Monday       Movie C    21756.0                 136829.999999      0
5   Saturday     Movie A    94.0                    1680.000000        5
6   Saturday     Movie B    31373.0                 64092.000000       5
7   Saturday     Movie E    615.0                   2640.000000        5
8   Saturday     Movie D    175.0                   11940.000000       5
9   Saturday     Movie C    4671.0                  29340.000000       5
10  Sunday       Movie A    9238.0                  52797.000000       6
11  Sunday       Movie B    12016.0                 31332.000000       6
12  Sunday       Movie E    528.0                   2520.000000        6
13  Thursday     Movie A    212.0                   1860.000000        3
14  Thursday     Movie B    39018.0                 91197.000000       3
15  Thursday     Movie E    26402.0                 348991.000000      3
16  Thursday     Movie C    374.0                   4860.000000        3
17  Tuesday      Movie B    40456.0                 97607.000000       1
18  Tuesday      Movie C    7181.0                  29700.000000       1
19  Wednesday    Movie B    13243.0                 32093.000000       2
20  Wednesday    Movie D    1385.0                  34476.000000       2
21  Wednesday    Movie C    21256.0                 137097.999999      2

I want to calculate watch percentage of each show by weekday. I can easily conceptualize this in excel

SUM(average watch time)/SUM(video length for the weekday)

Edit: This is how I'm doing it as of now

result = []
for index, row in dff.iterrows():
    value = row["Time Spent(seconds)"]/dff.where(dff["Week Day"] == row["Week Day"])["Duration sec"].sum()
    result.append(value)
dff["Watch Time %"] = result

Summation of Weekday can be cached but notice the condition dff["Week Day"] == row["Week Day"].

How can I get weekday and the sum of video length for the weekday based on the current row that pandas is working on? Is there an efficient way of doing it other than iterrows?

Body:
Assuming you have a dataframe that looks something like the following:

mdf =
    Movie   Day Length  View_Time
0   A        2  2.6      1.8
1   A        3  2.6      2.6
2   B        1  5.8      2.9
3   A        4  2.6      2.6
4   B        6  5.8      4.2
5   A        0  2.6      0.5
6   B        3  5.8      2.0

You can compute the length per day by each movie as follows:

 mdf.groupby(['Movie', 'Day']).Length.sum().to_frame()

Which produces a dataframe of the form:
              Length
Movie   Day
A        0    2.6
         2    2.6
         3    2.6
         4    2.6
B        1    5.8
         3    5.8
         6    5.8


问:
How can I make an UltraTextEditor keep my line breaks in edit mode?
I have a winform with an Infragistics UltraTextEditor control. I can type in enter while editing the text. It saves just fine.

However, if I go back into edit mode, the control collapses the text into the same line.

If I leave the control the text resumes its normal format with the line breaks.

How can I make it keep my line breaks in edit mode?

答:
One possible reason for this is when UltraTextEditor enters in edit mode it is rendering MS TextBox and this is where the editing happens. The important thing here is MS TextBox is able to show new lines only when they are represented with \r\n, while UltraTextEditor will show new lines even if they are represented only with \n. This will end with displaying the new lines when not in edit mode, editor handle this, not displaying new lines when in edit mode, TextBox cannot handle this.

If you are manipulating the Value of the editor and changing the new lines from \r\n to \n this is why you are facing this problem. Note manipulating the value does not exactly means you go and change the new lines. This could happen if you, for example, save the value somewhere, e.g. in XML file, and then load it back to the editor.

If you do not find where the new lines got broken you can try to fix the new lines in editor's EnterEditMode event.


问:
Express.js - GraphQL API hot reload not work
I try to write a mockAPI with a hot reload. This means I want to restart/update the server data after I'm editing the schema or the returned data. Everything works like expected. The only thing I'm missing is the reload. Actually, the server is restarting but the data is still the same (I was testing it with Postman).

I was assuming that I have to restart the server after I'm editing the schema or the returned data. I noticed that a callback function already exists with graphqlHTTP to listen to the schema and the rootValue, but this doesn't work either.

Note: I don't want to kill the Node process!

index.js

```<code>/** require file system */
const fs = require('fs');
const md5 = require('md5');
const path = require('path');
const { readFileSync } = require('fs');
/** Start MockAPI */
const http = require('http');
const expressModule = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

module.exports = class mockAPI {
  constructor(options = { host: '127.0.0.1', port: 3000, namespace: '' }) {
    /** start express */
    this.modules();

    this.directories = {
      data: './data',
      schema: './schema'
    };

    this.ignore = ['.DS_Store', 'Thumbs.db'];

    this.host = options.host;
    this.port = options.port;
    this.namespace = options.namespace;

    this.registerRoutes({
      data: this.getRoutes(this.directories.data),
      schema: this.getRoutes(this.directories.schema)
    });

    /** start watching (true) */
    this.start(true);
  }

  modules() {
    /** start express */
    this.express = expressModule();
    this.server = http.createServer(this.express);
  }

  start(watch) {
    /** start server */
    return new Promise((resolve, reject) => {
      this.server
        .listen(this.port, this.host, () => {
          const restart = !watch ? 're' : '';

          console.info(
            '\x1b[1m\x1b[32m%s\x1b[0m\x1b[0m',
            `\n> MockAPI server ${restart}open on ... http://${this.server.address().address}:${
              this.server.address().port
            }\n`
          );
          /** start watching API files */
          if (watch) this.watch(this.directories);
        })
        .on('error', err => {
          if (err) reject(err);
        });

      resolve();
    });
  }

  stop(restart) {
    /** stop server */
    return new Promise((resolve, reject) => {
      this.server.close(err => {
        if (err) reject(err);

        const color = restart ? '33' : '31';
        const message = restart ? 'restart' : 'closed';

        console.info(
          `\x1b[1m\x1b[${color}m%s\x1b[0m\x1b[0m`,
          `> MockAPI server ${message} on ... http://${this.host}:${this.port}`
        );

        resolve();
      });
    });
  }

  watch(directories) {
    this.md5Previous = null;
    this.fsWait = false;

    Object.keys(directories).map(key => {
      const dir = directories[key];

      fs.watch(path.resolve(__dirname, dir), { recursive: true }, (event, filename) => {
        if (event === 'change' && filename) {
          /** if people exec multiple times save */
          if (this.fsWait) return false;

          const md5Current = md5(fs.readFileSync(path.resolve(__dirname, `${dir}/${filename}`)));
          /** compare file hashes */
          if (md5Current === this.md5Previous) return false;

          /** restart server */
          this.fsWait = true;
          setTimeout(async () => {
            this.fsWait = false;
            this.md5Previous = md5Current;

            console.info('\x1b[33m%s\x1b[0m', `- ${filename} changed`);
            /**
             * any solution here?
             * in this scope
             */
            await this.server.removeAllListeners('upgrade');
            await this.server.removeAllListeners('request');

            await this.stop(true);

            await this.modules();
            await this.registerRoutes({
              data: this.getRoutes(this.directories.data),
              schema: this.getRoutes(this.directories.schema)
            });

            this.start(false);
            /**
             * any solution here?
             * in this scope
             */
          }, 1000);
        }
      });
    });

    return true;
  }

  getRoutes(dir) {
    const absPath = path.resolve(__dirname, dir);
    let routes = [];

    const readDir = path => {
      let data = [];

      fs.readdirSync(path, { withFileTypes: true }).forEach(file => {
        if (this.ignore.includes(file.name)) return false;

        if (file.isDirectory()) return readDir(`${path}/${file.name}`);

        const route = path === absPath ? '' : path.replace(absPath, '');
        const name = file.name.replace(/\.js|\.graphql/, '');
        const endpoint = name.replace(/\s+/g, '-');

        data.push({
          name: name,
          dir: route,
          file: file.name,
          route: `${route}/${endpoint}`
        });
      });

      routes = [...routes, ...data];
    };
    readDir(absPath);
    /** sort array by route */
    return routes.sort((a, b) => a.route.localeCompare(b.route));
  }

  registerRoutes(routes) {
    if (routes.schema.length !== routes.data.length) {
      console.error(
        '\x1b[31m%s\x1b[0m',
        `- schema.length(${routes.schema.length}) !== data.length(${routes.data.length}).\n  Each schema must match a data file in the same file structure with the same file name.`
      );
      process.exit;
    }

    routes.data.forEach(async (vdata, key) => {
      if (vdata.route === routes.schema[key].route) {
        const typeDefs = await readFileSync(
          `./mock-api/schema${routes.schema[key].dir}/${routes.schema[key].file}`
        ).toString('utf-8');

        let schema = await buildSchema(typeDefs);
        let data = await require(`./data${vdata.dir}/${vdata.file}`);

        this.express.use(
          this.namespace + vdata.route,
          graphqlHTTP({
            schema: schema,
            rootValue: data,
            graphiql: true
          })
        );
      }
    });
  }
};
</code>```

Would be great if someone can help me out.

If you want to reproduce the process, just create a directory /data and /schema in the same root directory as this code/snippet/file and call any yarn or npm command, to load this module (index.js). Inside of the /data directory place a JS file to describe the resolvers and a GraphQL file in the /schema directory to describe the schema. Each file must have the same name to match the route, which is registered by the file structure.

So if you place /data/test/data.js in /data you have to write the schema in /schema/test/data.graphql and it is accessible with http://127.0.0.1/test/data.

Here are examples to quickly reproduce.

data.js

```<code>/**
 * Returns GraphQL data
 * @returns {object} data
 */
module.exports = {
  hello: () => {
    return 'Hello world!';
  }
};

</code>```

data.graphql

```<code>type Query {
  hello: String
}
</code>```

To clarify what I want is, I need to flush, clear, delete, or any other method to delete the stored data.

答:
The solution was to create a child process with spawn.

index.js

```<code>/** file system */
const { spawn } = require('child_process');
/** require environment variables */
let env = require('../config/env');
/**
 * Returns Mock API server as child process
 * @param {boolean} initial process
 * @returns {object} process data
 * --------------------------------
 */
const server = initial => {
  /** merge environment variables */
  env = Object.assign(process.env, env, { INITIAL: initial });

  /** start child process for the Mock API */
  let child = spawn('node', ['mock-api/MockAPI.js'], {
    env: env
  });

  /** child info/logs */
  child.stdout.on('data', data => {
    try {
      /** check if data is object */
      const obj = JSON.parse(data.toString());
      /** handle object data */
      if ({}.hasOwnProperty.call(obj, 'restart')) {
        child.kill();
      }
    } catch (error) {
      console.log(data.toString());
    }
  });

  /** child errors */
  child.stderr.on('data', data => {
    console.error(
      '\x1b[1m\x1b[31m%s\x1b[0m\x1b[0m',
      `> Mock API server crashed on ... http://${env.MOCK_API_HOST}:${env.MOCK_API_PORT}\n`,
      data.toString()
    );
  });

  child.on('close', () => {
    /** Wait for process to exit, then run again */
    setTimeout(() => server(false), 500);
  });

  /** kill child process if parent process gets killed */
  process.on('exit', () => child.kill('SIGINT'));

  return child;
};

module.exports = (() => server(true))();
</code>```

MockAPI.js

```<code>/** file system */
const fs = require('fs');
const md5 = require('md5');
const path = require('path');
const { readFileSync } = require('fs');
/** server data */
const http = require('http');
const expressModule = require('express');
const express = expressModule();
const server = http.createServer(express);
/** graphql */
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
/** define globals */
const data = './data';
const ignore = ['.DS_Store', 'Thumbs.db'];
const env = process.env;
const host = env.MOCK_API_HOST || '127.0.0.1';
const port = env.MOCK_API_PORT || 3000;
const namespace = env.MOCK_API_NAMESPACE || '';
/**
 * get routes data from ./data directory
 *
 * NOTE:
 * each graphql schema must
 * match a JS data file in
 * the same directory
 *
 * @param {string} directory
 * @returns {Array} routes
 */
const getRoutes = dir => {
  const root = path.resolve(__dirname, dir);
  const routes = [];

  const data = path =>
    fs.readdirSync(path, { withFileTypes: true }).forEach(file => {
      if (ignore.includes(file.name) || file.name.endsWith('.graphql')) return false;

      if (file.isDirectory()) return data(`${path}/${file.name}`);

      const name = file.name.replace(/\.js/, '');
      const route = path === root ? '' : path.replace(root, '');
      const endpoint = name.replace(/\s+/g, '-');

      routes.push({
        name: name,
        data: file.name,
        directory: route,
        graphql: `${name}.graphql`,
        route: `${route}/${endpoint}`
      });
    });
  data(root);

  return routes;
};
/**
 * register all routes
 *
 * NOTE:
 * each graphql schema must
 * match a JS data file in
 * the same directory
 *
 * @param {Array} routes
 */
const registerRoutes = routes => {
  routes.forEach(async route => {
    const graphql = path.resolve(__dirname, `./data${route.directory}/${route.graphql}`);
    await fs.stat(graphql, (error, stats) => {
      error, stats;
    });

    const data = path.resolve(__dirname, `./data${route.directory}/${route.data}`);
    await fs.stat(data, (error, stats) => {
      error, stats;
    });

    const typeDefs = await buildSchema(readFileSync(graphql).toString('utf-8'));
    const rootValue = await require(data);

    express.use(
      namespace + route.route,
      graphqlHTTP({
        schema: typeDefs,
        rootValue: rootValue,
        graphiql: true
      })
    );
  });
};
/**
 * start watching data files
 *
 * stop current process if files
 * were edited and restart the server
 *
 * @param {String} directory
 */
const watch = directory => {
  let md5Previous = null;
  let fsWait = false;

  fs.watch(path.resolve(__dirname, directory), { recursive: true }, (event, filename) => {
    if ((event === 'change' || event === 'rename') && filename) {
      /** if people exec multiple times save */
      if (fsWait) return false;

      const md5Current = md5(fs.readFileSync(path.resolve(__dirname, `${directory}/${filename}`)));
      /** compare file hashes */
      if (md5Current === md5Previous) return false;

      /** restart server */
      fsWait = true;
      setTimeout(async () => {
        fsWait = false;
        md5Previous = md5Current;

        console.info(
          '\x1b[33m%s\x1b[0m',
          `- ${filename} changed\n> Mock API server restarts on ... http://${host}:${port}`
        );

        /** restart child process */
        console.log(JSON.stringify({ restart: true }));
      }, 2000);
    }
  });
};
/**
 * initialize server
 */
const init = () => {
  registerRoutes(getRoutes(data));

  server
    .listen(port, host, () => {
      const open = env.INITIAL === 'true' ? 'open' : 'reopened';

      console.info(
        '\x1b[1m\x1b[32m%s\x1b[0m\x1b[0m',
        `> Mock API server ${open} on ... http://${server.address().address}:${
          server.address().port
        }`
      );

      /** start watching data files */
      watch(data);
    })
    .on('error', error => {
      if (error) console.log('[ERROR] Mock API: ', error);
    });
};

init();
</code>```

Body:
I try to write a mockAPI with a hot reload. This means I want to restart/update the server data after I'm editing the schema or the returned data. Everything works like expected. The only thing I'm missing is the reload. Actually, the server is restarting but the data is still the same (I was testing it with Postman).

I was assuming that I have to restart the server after I'm editing the schema or the returned data. I noticed that a callback function already exists with graphqlHTTP to listen to the schema and the rootValue, but this doesn't work either.

Note: I don't want to kill the Node process!

index.js

/** require file system */
const fs = require('fs');
const md5 = require('md5');
const path = require('path');
const { readFileSync } = require('fs');
/** Start MockAPI */
const http = require('http');
const expressModule = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

module.exports = class mockAPI {
  constructor(options = { host: '127.0.0.1', port: 3000, namespace: '' }) {
    /** start express */
    this.modules();

    this.directories = {
      data: './data',
      schema: './schema'
    };

    this.ignore = ['.DS_Store', 'Thumbs.db'];

    this.host = options.host;
    this.port = options.port;
    this.namespace = options.namespace;

    this.registerRoutes({
      data: this.getRoutes(this.directories.data),
      schema: this.getRoutes(this.directories.schema)
    });

    /** start watching (true) */
    this.start(true);
  }

  modules() {
    /** start express */
    this.express = expressModule();
    this.server = http.createServer(this.express);
  }

  start(watch) {
    /** start server */
    return new Promise((resolve, reject) => {
      this.server
        .listen(this.port, this.host, () => {
          const restart = !watch ? 're' : '';

          console.info(
            '\x1b[1m\x1b[32m%s\x1b[0m\x1b[0m',
            `\n> MockAPI server ${restart}open on ... http://${this.server.address().address}:${
              this.server.address().port
            }\n`
          );
          /** start watching API files */
          if (watch) this.watch(this.directories);
        })
        .on('error', err => {
          if (err) reject(err);
        });

      resolve();
    });
  }

  stop(restart) {
    /** stop server */
    return new Promise((resolve, reject) => {
      this.server.close(err => {
        if (err) reject(err);

        const color = restart ? '33' : '31';
        const message = restart ? 'restart' : 'closed';

        console.info(
          `\x1b[1m\x1b[${color}m%s\x1b[0m\x1b[0m`,
          `> MockAPI server ${message} on ... http://${this.host}:${this.port}`
        );

        resolve();
      });
    });
  }

  watch(directories) {
    this.md5Previous = null;
    this.fsWait = false;

    Object.keys(directories).map(key => {
      const dir = directories[key];

      fs.watch(path.resolve(__dirname, dir), { recursive: true }, (event, filename) => {
        if (event === 'change' && filename) {
          /** if people exec multiple times save */
          if (this.fsWait) return false;

          const md5Current = md5(fs.readFileSync(path.resolve(__dirname, `${dir}/${filename}`)));
          /** compare file hashes */
          if (md5Current === this.md5Previous) return false;

          /** restart server */
          this.fsWait = true;
          setTimeout(async () => {
            this.fsWait = false;
            this.md5Previous = md5Current;

            console.info('\x1b[33m%s\x1b[0m', `- ${filename} changed`);
            /**
             * any solution here?
             * in this scope
             */
            await this.server.removeAllListeners('upgrade');
            await this.server.removeAllListeners('request');

            await this.stop(true);

            await this.modules();
            await this.registerRoutes({
              data: this.getRoutes(this.directories.data),
              schema: this.getRoutes(this.directories.schema)
            });

            this.start(false);
            /**
             * any solution here?
             * in this scope
             */
          }, 1000);
        }
      });
    });

    return true;
  }

  getRoutes(dir) {
    const absPath = path.resolve(__dirname, dir);
    let routes = [];

    const readDir = path => {
      let data = [];

      fs.readdirSync(path, { withFileTypes: true }).forEach(file => {
        if (this.ignore.includes(file.name)) return false;

        if (file.isDirectory()) return readDir(`${path}/${file.name}`);

        const route = path === absPath ? '' : path.replace(absPath, '');
        const name = file.name.replace(/\.js|\.graphql/, '');
        const endpoint = name.replace(/\s+/g, '-');

        data.push({
          name: name,
          dir: route,
          file: file.name,
          route: `${route}/${endpoint}`
        });
      });

      routes = [...routes, ...data];
    };
    readDir(absPath);
    /** sort array by route */
    return routes.sort((a, b) => a.route.localeCompare(b.route));
  }

  registerRoutes(routes) {
    if (routes.schema.length !== routes.data.length) {
      console.error(
        '\x1b[31m%s\x1b[0m',
        `- schema.length(${routes.schema.length}) !== data.length(${routes.data.length}).\n  Each schema must match a data file in the same file structure with the same file name.`
      );
      process.exit;
    }

    routes.data.forEach(async (vdata, key) => {
      if (vdata.route === routes.schema[key].route) {
        const typeDefs = await readFileSync(
          `./mock-api/schema${routes.schema[key].dir}/${routes.schema[key].file}`
        ).toString('utf-8');

        let schema = await buildSchema(typeDefs);
        let data = await require(`./data${vdata.dir}/${vdata.file}`);

        this.express.use(
          this.namespace + vdata.route,
          graphqlHTTP({
            schema: schema,
            rootValue: data,
            graphiql: true
          })
        );
      }
    });
  }
};

Would be great if someone can help me out.

If you want to reproduce the process, just create a directory /data and /schema in the same root directory as this code/snippet/file and call any yarn or npm command, to load this module (index.js). Inside of the /data directory place a JS file to describe the resolvers and a GraphQL file in the /schema directory to describe the schema. Each file must have the same name to match the route, which is registered by the file structure.

So if you place /data/test/data.js in /data you have to write the schema in /schema/test/data.graphql and it is accessible with http://127.0.0.1/test/data.

Here are examples to quickly reproduce.

data.js

/**
 * Returns GraphQL data
 * @returns {object} data
 */
module.exports = {
  hello: () => {
    return 'Hello world!';
  }
};

data.graphql

type Query {
  hello: String
}

To clarify what I want is, I need to flush, clear, delete, or any other method to delete the stored data.

Body:
The solution was to create a child process with spawn.

index.js

/** file system */
const { spawn } = require('child_process');
/** require environment variables */
let env = require('../config/env');
/**
 * Returns Mock API server as child process
 * @param {boolean} initial process
 * @returns {object} process data
 * --------------------------------
 */
const server = initial => {
  /** merge environment variables */
  env = Object.assign(process.env, env, { INITIAL: initial });

  /** start child process for the Mock API */
  let child = spawn('node', ['mock-api/MockAPI.js'], {
    env: env
  });

  /** child info/logs */
  child.stdout.on('data', data => {
    try {
      /** check if data is object */
      const obj = JSON.parse(data.toString());
      /** handle object data */
      if ({}.hasOwnProperty.call(obj, 'restart')) {
        child.kill();
      }
    } catch (error) {
      console.log(data.toString());
    }
  });

  /** child errors */
  child.stderr.on('data', data => {
    console.error(
      '\x1b[1m\x1b[31m%s\x1b[0m\x1b[0m',
      `> Mock API server crashed on ... http://${env.MOCK_API_HOST}:${env.MOCK_API_PORT}\n`,
      data.toString()
    );
  });

  child.on('close', () => {
    /** Wait for process to exit, then run again */
    setTimeout(() => server(false), 500);
  });

  /** kill child process if parent process gets killed */
  process.on('exit', () => child.kill('SIGINT'));

  return child;
};

module.exports = (() => server(true))();

MockAPI.js

/** file system */
const fs = require('fs');
const md5 = require('md5');
const path = require('path');
const { readFileSync } = require('fs');
/** server data */
const http = require('http');
const expressModule = require('express');
const express = expressModule();
const server = http.createServer(express);
/** graphql */
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
/** define globals */
const data = './data';
const ignore = ['.DS_Store', 'Thumbs.db'];
const env = process.env;
const host = env.MOCK_API_HOST || '127.0.0.1';
const port = env.MOCK_API_PORT || 3000;
const namespace = env.MOCK_API_NAMESPACE || '';
/**
 * get routes data from ./data directory
 *
 * NOTE:
 * each graphql schema must
 * match a JS data file in
 * the same directory
 *
 * @param {string} directory
 * @returns {Array} routes
 */
const getRoutes = dir => {
  const root = path.resolve(__dirname, dir);
  const routes = [];

  const data = path =>
    fs.readdirSync(path, { withFileTypes: true }).forEach(file => {
      if (ignore.includes(file.name) || file.name.endsWith('.graphql')) return false;

      if (file.isDirectory()) return data(`${path}/${file.name}`);

      const name = file.name.replace(/\.js/, '');
      const route = path === root ? '' : path.replace(root, '');
      const endpoint = name.replace(/\s+/g, '-');

      routes.push({
        name: name,
        data: file.name,
        directory: route,
        graphql: `${name}.graphql`,
        route: `${route}/${endpoint}`
      });
    });
  data(root);

  return routes;
};
/**
 * register all routes
 *
 * NOTE:
 * each graphql schema must
 * match a JS data file in
 * the same directory
 *
 * @param {Array} routes
 */
const registerRoutes = routes => {
  routes.forEach(async route => {
    const graphql = path.resolve(__dirname, `./data${route.directory}/${route.graphql}`);
    await fs.stat(graphql, (error, stats) => {
      error, stats;
    });

    const data = path.resolve(__dirname, `./data${route.directory}/${route.data}`);
    await fs.stat(data, (error, stats) => {
      error, stats;
    });

    const typeDefs = await buildSchema(readFileSync(graphql).toString('utf-8'));
    const rootValue = await require(data);

    express.use(
      namespace + route.route,
      graphqlHTTP({
        schema: typeDefs,
        rootValue: rootValue,
        graphiql: true
      })
    );
  });
};
/**
 * start watching data files
 *
 * stop current process if files
 * were edited and restart the server
 *
 * @param {String} directory
 */
const watch = directory => {
  let md5Previous = null;
  let fsWait = false;

  fs.watch(path.resolve(__dirname, directory), { recursive: true }, (event, filename) => {
    if ((event === 'change' || event === 'rename') && filename) {
      /** if people exec multiple times save */
      if (fsWait) return false;

      const md5Current = md5(fs.readFileSync(path.resolve(__dirname, `${directory}/${filename}`)));
      /** compare file hashes */
      if (md5Current === md5Previous) return false;

      /** restart server */
      fsWait = true;
      setTimeout(async () => {
        fsWait = false;
        md5Previous = md5Current;

        console.info(
          '\x1b[33m%s\x1b[0m',
          `- ${filename} changed\n> Mock API server restarts on ... http://${host}:${port}`
        );

        /** restart child process */
        console.log(JSON.stringify({ restart: true }));
      }, 2000);
    }
  });
};
/**
 * initialize server
 */
const init = () => {
  registerRoutes(getRoutes(data));

  server
    .listen(port, host, () => {
      const open = env.INITIAL === 'true' ? 'open' : 'reopened';

      console.info(
        '\x1b[1m\x1b[32m%s\x1b[0m\x1b[0m',
        `> Mock API server ${open} on ... http://${server.address().address}:${
          server.address().port
        }`
      );

      /** start watching data files */
      watch(data);
    })
    .on('error', error => {
      if (error) console.log('[ERROR] Mock API: ', error);
    });
};

init();


问:
How to use jq (or python) to remove a particular key(?) in some JSON?
Here is the input json. It is a partial JSON dump of a puppet class extracted from Red Hat Satellite.

```<code>{
  "Id": 9999,
  "Match": "fqdn=server.fqdn.com",
  "Value": {
    "user1": {
      "order": 90,
      "nofile": {
        "comment": "I want to lose this comment",
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "comment": "I want to lose this also",
        "soft": "32768",
        "hard": "32768"
      }
      },
    "user2": {
      "order": 90,
      "nofile": {
        "comment": "I want to lose this comment",
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "comment": "I want to lose this also",
        "soft": "32768",
        "hard": "32768"
      }
    }
  }
}

</code>```

I want to delete all the "comment"s. I can't find an example that does this, or an explanation of something that does this in general. Or I am too stupid to understand what I have read so far.

So my output would be

```<code>{
  "Id": 9999,
  "Match": "fqdn=server.fqdn.com",
  "Value": {
    "user1": {
      "order": 90,
      "nofile": {
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "soft": "32768",
        "hard": "32768"
      }
      },
    "user2": {
      "order": 90,
      "nofile": {
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "soft": "32768",
        "hard": "32768"
      }
    }
  }
}

</code>```

Any takers?

答:
```<code>del( .. | objects | .comment )
</code>```

Demo on jqplay

In Python, it would look something like this:

def remove_comments(node):
   if isinstance(node, dict):
      if 'comment' in node:
         del node['comment']
      for child in node.values():
         remove_comments(child)
   elif isinstance(node, list):
      for child in node:
         remove_comments(child)
```

Body:
Here is the input json. It is a partial JSON dump of a puppet class extracted from Red Hat Satellite.

{
  "Id": 9999,
  "Match": "fqdn=server.fqdn.com",
  "Value": {
    "user1": {
      "order": 90,
      "nofile": {
        "comment": "I want to lose this comment",
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "comment": "I want to lose this also",
        "soft": "32768",
        "hard": "32768"
      }
      },
    "user2": {
      "order": 90,
      "nofile": {
        "comment": "I want to lose this comment",
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "comment": "I want to lose this also",
        "soft": "32768",
        "hard": "32768"
      }
    }
  }
}

I want to delete all the "comment"s. I can't find an example that does this, or an explanation of something that does this in general. Or I am too stupid to understand what I have read so far.

So my output would be

{
  "Id": 9999,
  "Match": "fqdn=server.fqdn.com",
  "Value": {
    "user1": {
      "order": 90,
      "nofile": {
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "soft": "32768",
        "hard": "32768"
      }
      },
    "user2": {
      "order": 90,
      "nofile": {
        "soft": "65535",
        "hard": "65535"
      },
      "nproc": {
        "soft": "32768",
        "hard": "32768"
      }
    }
  }
}

Any takers?

Body:
del( .. | objects | .comment )

Demo on jqplay

In Python, it would look something like this:

def remove_comments(node):
   if isinstance(node, dict):
      if 'comment' in node:
         del node['comment']
      for child in node.values():
         remove_comments(child)
   elif isinstance(node, list):
      for child in node:
         remove_comments(child)


问:
load event not firing in jsonstore
team,

I'm having a jsonstore to load my list. I'm loading store using inline data without any proxy config. I wanted to call load event of my store. Kindly let me know how to do it.

Below is my store code:

Store = new Ext.data.JsonStore({
    model  : 'SettingModel',

```<code>getGroupString:function(){
        return 'Set Duration';
        },
autoLoad:false,
data: [
    {name: '5 mins',   value: '5'},
    {name: '15 mins',   value: '15'},
    {name: '25 mins',   value: '25'},
    {name: '45 mins',   value: '45'}
],
listeners : {
    'load': function(){
        Ext.Msg.alert('App',"load");
    }
}});
</code>```

Store.load();

with the above code i get the following error message : "Uncaught Error: You are using a ServerProxy but have not supplied it with a url"

if i remove the "Store.load();" line, im not getting error and list is populating with data. But the load event is not fired.

Kindly help me how to make load event to fire in this scenario.

Thanks in Advance

答:
pablodcar's answer should really do for you. You may instantiate your store with an empty data array and then call the loadData() method with the data array you want to load. This method can be called from any where if you initiate your store with a storeId and then look-it up using Ext.data.StoreManager.lookup(storeId).

To be notified whenever the store data is changed, you may listen to the datachanged event.

Title: load event not firing in jsonstore

Body:
team,

I'm having a jsonstore to load my list. I'm loading store using inline data without any proxy config. I wanted to call load event of my store. Kindly let me know how to do it.

Below is my store code:

Store = new Ext.data.JsonStore({
    model  : 'SettingModel',

getGroupString:function(){
        return 'Set Duration';
        },
autoLoad:false,
data: [
    {name: '5 mins',   value: '5'},
    {name: '15 mins',   value: '15'},
    {name: '25 mins',   value: '25'},
    {name: '45 mins',   value: '45'}
],
listeners : {
    'load': function(){
        Ext.Msg.alert('App',"load");
    }
}});

Store.load();

with the above code i get the following error message : "Uncaught Error: You are using a ServerProxy but have not supplied it with a url"

if i remove the "Store.load();" line, im not getting error and list is populating with data. But the load event is not fired.

Kindly help me how to make load event to fire in this scenario.

Thanks in Advance


问:
My raw primitives cannot be displayed in Raymarching
I encountered a very strange problem while learning Raymarching.
My equation cannot be displayed well.
It can be displayed well in matlab. But it can't be shown on Shadertoy at all.

My equation：

```<code>f(x,y,z) = (x^2+y^2+z^2)^2+2*y*(x^2+y^2+z^2)+2*(x^2+z^2);
</code>```

code in matlab:

```<code>f =@(x,y,z) (x.^2+y.^2+z.^2)^2+2*y*(x.^2+y.^2+z.^2)+2*(x.^2+z.^2);
fimplicit3(f)
</code>```

matlab displays a normal picture

code in shadertoy：

```<code>float sdRound(vec3 p)
{
   float lengthXYZ = (p.x * p.x+p.y * p.y+p.z * p.z);
   return lengthXYZ * lengthXYZ+2.0 * p.y * lengthXYZ+2.0 * (p.x * p.x+p.z * p.z);
}
</code>```

I learned, practiced, and modified the iq code. (https://www.shadertoy.com/view/Xds3zN).

But it cannot be displayed normally.
Shadertoy displays abnormal pictures
Shadertoy displays abnormal pictures

I don't know where the problem occurred. Please help me. Has troubled me for a long time.

Forgive my poor English. I use Google Translate.

答:
Your problem seems to lay in the very understanding of what raymarching is.

Your formula mathematically describes a surface (that is why it is displayed nicely in matlab), but this is not how raymarching works.

Defining an object for raymarching means defining a distance function for your object (which is different from a function mathematically describing its surface). These functions are identical for zero-returned values (which means coordinates are on the surface), but they are different for other values.

To create something similar to your object, I would suggest to explore the sdRoundCone method from iq's Raymarching primitives, as it seems the closest to what you want to achieve.

Body:
I encountered a very strange problem while learning Raymarching.
My equation cannot be displayed well.
It can be displayed well in matlab. But it can't be shown on Shadertoy at all.

My equation：

f(x,y,z) = (x^2+y^2+z^2)^2+2*y*(x^2+y^2+z^2)+2*(x^2+z^2);

code in matlab:

f =@(x,y,z) (x.^2+y.^2+z.^2)^2+2*y*(x.^2+y.^2+z.^2)+2*(x.^2+z.^2);
fimplicit3(f)

matlab displays a normal picture

code in shadertoy：

float sdRound(vec3 p)
{
   float lengthXYZ = (p.x * p.x+p.y * p.y+p.z * p.z);
   return lengthXYZ * lengthXYZ+2.0 * p.y * lengthXYZ+2.0 * (p.x * p.x+p.z * p.z);
}

I learned, practiced, and modified the iq code. (https://www.shadertoy.com/view/Xds3zN).

But it cannot be displayed normally.
Shadertoy displays abnormal pictures
Shadertoy displays abnormal pictures

I don't know where the problem occurred. Please help me. Has troubled me for a long time.

Forgive my poor English. I use Google Translate.


问:
How to pass arguments to Laravel factories?
I have a users table and a one-to-zero/one relation with a businesses table (users.user_id => businesses.user_id). On my users table I have a discriminator which tells me if the user is of type business and therefore I need to have details on the businesses table as well.

I want to create my Users with my factory which currently is working and then only create business details where the discriminator points to a business account.

I have three options in my mind:

Create from users factory and then using '->each()' do some checks on the discriminator and create a new business user using a the factory. However I cannot pass to the business factory the user_id that the user was assigned.

First create the users. Then in my Business seeder, retrieve all Users that match a 'business' discriminator. Then for all of these users run a factory that creates the business details. But again, I would have to link somehow the user_id of the already create user with the business factory user_id.

In my business factory, create a new User and retrieve the id, thus making the link between users.user_id and business.user_id. However I am using a random generator for user.user_type so even if I have the businesses table filled it might be for users that have the discriminator as 'personal'.

Is there another way? Can I pass arguments from my Seeder to the factory?

答:
Send attribute,

```<code>factory(App\User::class)->create(['businessId' => $businessId]);
</code>```

Retrieve it,

```<code>$factory->define(App\User::class, function (Faker $faker, $businessInfo) {
    //$businessInfo['businessId']
});
</code>```

Body:
Send attribute,

factory(App\User::class)->create(['businessId' => $businessId]);

Retrieve it,

$factory->define(App\User::class, function (Faker $faker, $businessInfo) {
    //$businessInfo['businessId']
});


问:
jenkins pipeline fails without some explanatory exception
I have a pipeline where among other parameters like machine name, client it is supposed to get latest ami from aws for that branch for example and then put it the clien.json which terraform would use to create the machine but als I want to enable the user to be able to provide a value for the parameter and when there is no value for that parameter to have the value picked from latest ami in develop for example:

#!/usr/bin/env groovy

pipeline {
agent { label 'new' }

```<code>parameters {

    string(name: 'AMI_ID', defaultValue: '', description: '[Mandatory]')

}

stages {

    stage('Retrieve latest AMI.') {
        when {
            expression { ${AMI_ID} == '' }
        }
        steps {
            script {
               AMI_ID = sh(script: "aws ec2 describe-images --region region1 --owners 123456 --filters \"Name=tag:type,Values=develop\" --query 'sort_by(Images, &CreationDate)[-1].ImageId'  | jq -r '.'", returnStdout: true).trim()
                echo "AMI retrieved: " + $ { AMI_ID }

            }
        }
    }
    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {

                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}
</code>```

}

答:
Found the root cause was that I was passing a shell script variable to groovy where I say:

```<code>clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })
</code>```

Instead I should have passed the AMI_ID to a groovy variable above and say like:

clientJson.put("client_ec2_eda_ami_id", currentAmi)

Peace of the code with the aws query dimmed:

```<code>    stage('Retrieve latest AMI.') {
        }
        steps {
            script {
                currentAmi = params.AMI_ID
                if (currentAmi.isEmpty())
                    currentAmi = sh(script: "aws ec2 query blah blah")
                        echo  "Ami retrieved is: ${currentAmi}"
            }
        }
    }

    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {
                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", currentAmi)

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}
}
</code>```

Body:
I have a pipeline where among other parameters like machine name, client it is supposed to get latest ami from aws for that branch for example and then put it the clien.json which terraform would use to create the machine but als I want to enable the user to be able to provide a value for the parameter and when there is no value for that parameter to have the value picked from latest ami in develop for example:

#!/usr/bin/env groovy

pipeline {
agent { label 'new' }

parameters {

    string(name: 'AMI_ID', defaultValue: '', description: '[Mandatory]')

}

stages {

    stage('Retrieve latest AMI.') {
        when {
            expression { ${AMI_ID} == '' }
        }
        steps {
            script {
               AMI_ID = sh(script: "aws ec2 describe-images --region region1 --owners 123456 --filters \"Name=tag:type,Values=develop\" --query 'sort_by(Images, &CreationDate)[-1].ImageId'  | jq -r '.'", returnStdout: true).trim()
                echo "AMI retrieved: " + $ { AMI_ID }

            }
        }
    }
    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {

                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}

}

Body:
Found the root cause was that I was passing a shell script variable to groovy where I say:

clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })

Instead I should have passed the AMI_ID to a groovy variable above and say like:

clientJson.put("client_ec2_eda_ami_id", currentAmi)

Peace of the code with the aws query dimmed:

    stage('Retrieve latest AMI.') {
        }
        steps {
            script {
                currentAmi = params.AMI_ID
                if (currentAmi.isEmpty())
                    currentAmi = sh(script: "aws ec2 query blah blah")
                        echo  "Ami retrieved is: ${currentAmi}"
            }
        }
    }

    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {
                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", currentAmi)

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}
}


问:
Q: Firebase Extension Limit Child Nodes
I am running a Firebase app with 160k DAU, an online turn based game which uses RTDB to send updates between players. My database load peaks at around 60% at the busiest hour.

A long time problem for me has been how to "clean up" the match node after the game is over. Current structure:

root/games/$gameId

gameId is provided by the host in the matchmaking process. After that both players subscribe to this node and send updates.

What I could do is to have one of the 2 clients delete the node after the game, but that would be another (unreliable) write to increase the database load. My main goal is to keep "dead game nodes" to a minimum and thus the storage cost (Blaze plan). I only recently found out about the extension, but I am curious what my "cost" would be.

Q1: Would every trigger of this extension count as a "write" to my database? Seeing as only 1000 writes per second is allowed for one instance, this would further contribute to this count (it would trigger many times per second).

Q2: In the youtube information video it says it "works best" with auto-generated IDs, but is it needed? https://www.youtube.com/watch?v=i_u_6RknUro

I know that the extension is in fact just a cloud function but I would like to know how it works behind the scenes before I dare to use it :)

Thankful for any help :)

答:
All Firebase Extensions are open-source, so if you want to learn how the Limit Child Nodes extension works, you can check the source code link from its installation page.

The code from there:

```<code>export const rtdblimit = functions.handler.database.ref.onCreate(
  async (snapshot): Promise<void> => {
    logs.start();

    try {
      const parentRef = snapshot.ref.parent;
      const parentSnapshot = await parentRef.once("value");

      logs.childCount(parentRef.path, parentSnapshot.numChildren());

      if (parentSnapshot.numChildren() > config.maxCount) {
        let childCount = 0;
        const updates = {};
        parentSnapshot.forEach((child) => {
          if (++childCount <= parentSnapshot.numChildren() - config.maxCount) {
            updates[child.key] = null;
          }
        });

        logs.pathTruncating(parentRef.path, config.maxCount);
        await parentRef.update(updates);
        logs.pathTruncated(parentRef.path, config.maxCount);
      } else {
        logs.pathSkipped(parentRef.path);
      }

      logs.complete();
    } catch (err) {
      logs.error(err);
    }
  }
);
</code>```

From that it looks like this extension:

gets triggered when a new child node is created in the list.

then reads the entire parent node to determine how many children there are.

then deletes any number of children that are over its maximum with a single write operation.

This matches with my recollection of the code, since I wrote the first version of it. :)

To your questions:

Q1: Would every trigger of this extension count as a "write" to my database?

The triggering of the Cloud Functions happens when you write a child node to the database. It's the write that counts as a write, the trigger itself happens out-of-band. If you read the rest of the code I linked, you'll see that it deletes all child nodes over the maximum as a single write.

Q2: In the youtube information video it says it "works best" with auto-generated IDs, but is it needed?

The nodes are read without an ordering condition (const parentSnapshot = await parentRef.once("value")) and then it deletes nodes from the start of that list. So it assumes the nodes to delete are at the start of the list, when this is ordered by their keys. As long as that applies to your data too, it doesn't matter where the keys come from.

Title: Q: Firebase Extension Limit Child Nodes

Body:
All Firebase Extensions are open-source, so if you want to learn how the Limit Child Nodes extension works, you can check the source code link from its installation page.

The code from there:

export const rtdblimit = functions.handler.database.ref.onCreate(
  async (snapshot): Promise<void> => {
    logs.start();

    try {
      const parentRef = snapshot.ref.parent;
      const parentSnapshot = await parentRef.once("value");

      logs.childCount(parentRef.path, parentSnapshot.numChildren());

      if (parentSnapshot.numChildren() > config.maxCount) {
        let childCount = 0;
        const updates = {};
        parentSnapshot.forEach((child) => {
          if (++childCount <= parentSnapshot.numChildren() - config.maxCount) {
            updates[child.key] = null;
          }
        });

        logs.pathTruncating(parentRef.path, config.maxCount);
        await parentRef.update(updates);
        logs.pathTruncated(parentRef.path, config.maxCount);
      } else {
        logs.pathSkipped(parentRef.path);
      }

      logs.complete();
    } catch (err) {
      logs.error(err);
    }
  }
);

From that it looks like this extension:

gets triggered when a new child node is created in the list.

then reads the entire parent node to determine how many children there are.

then deletes any number of children that are over its maximum with a single write operation.

This matches with my recollection of the code, since I wrote the first version of it. :)

To your questions:

Q1: Would every trigger of this extension count as a "write" to my database?

The triggering of the Cloud Functions happens when you write a child node to the database. It's the write that counts as a write, the trigger itself happens out-of-band. If you read the rest of the code I linked, you'll see that it deletes all child nodes over the maximum as a single write.

Q2: In the youtube information video it says it "works best" with auto-generated IDs, but is it needed?

The nodes are read without an ordering condition (const parentSnapshot = await parentRef.once("value")) and then it deletes nodes from the start of that list. So it assumes the nodes to delete are at the start of the list, when this is ordered by their keys. As long as that applies to your data too, it doesn't matter where the keys come from.


问:
making reference to file two by two in a bash command
I have this list of file that I have to analyse by pair (the a_1 with a_2, b_1 with b_2 and so on)

```<code>a_1.fq
a_2.fq
b_1.fq
b_2.fq
c_1.fq
...
</code>```

I want to set a for loop to make reference to these pairs of file in a command, bellow
this is just an example of what I want to do (with a false syntax) :

```<code>$ for File1 File2 in *1.fq *2.fq; do STAR --readFilein File1 File2 ; done
</code>```

Thank you a lot for your help

答:
You can try something like that:</p>
```<code>for letter in {a..z}
do
 # Your logic
  echo "Working on $letter"
  cat $letter\_1.fq
  cat $letter\_2.fq
done
</code>```

Body:
I have this list of file that I have to analyse by pair (the a_1 with a_2, b_1 with b_2 and so on)

a_1.fq
a_2.fq
b_1.fq
b_2.fq
c_1.fq
...

I want to set a for loop to make reference to these pairs of file in a command, bellow
this is just an example of what I want to do (with a false syntax) :

$ for File1 File2 in *1.fq *2.fq; do STAR --readFilein File1 File2 ; done

Thank you a lot for your help

Body:
You can try something like that:

for letter in {a..z}
do
 # Your logic
  echo "Working on $letter"
  cat $letter\_1.fq
  cat $letter\_2.fq
done


问:
How to invoke a REST API when an object arrives on S3?
S3 can be configured to invoke lambda when an object arrives in it.

Is it possible to invoke a REST API (endpoint of a microservice running in EKS) when an object arrives in S3?

答: You can set up an SNS topic as a target for the event from S3. In the SNS topic, you can add an HTTP/s subscriber, which can be your API endpoint.</p>


问:
replace values by different conditions in a dataframe
I have a dataframe like this:

```<code>df_test = pd.DataFrame({'ID1':['A','B','C','BA','BA','AB','>','>','>','>'],
                       'ID2':['','','','','','','mh','mh','nn','nn']})
df_test
</code>```

I want to obtain a dataframe like this based on the column 'ID1'(1. if len(ID1)>2: then ID1=ID1[-1](for example 'BA', 'AB' will be replaced with 'A', 'B', respectively); 2. if ID1='>': then ID1=ID2(for example: '>' will be replaced with 'mh','nn',respectively)):

```<code>df_result = pd.DataFrame({'ID1':['A','B','C','A','A','B','mh','mh','nn','nn']})
df_result
</code>```

答:
Let us do mask

```<code>df_test.ID1.mask(df_test.ID1.eq('>'),df_test.ID2,inplace=True)
df_test
Out[217]:
  ID1 ID2
0   A
1   B
2   C
3  BA
4  BA
5  AB
6  mh  mh
7  mh  mh
8  nn  nn
9  nn  nn
</code>```

Body:
I have a dataframe like this:

df_test = pd.DataFrame({'ID1':['A','B','C','BA','BA','AB','>','>','>','>'],
                       'ID2':['','','','','','','mh','mh','nn','nn']})
df_test

I want to obtain a dataframe like this based on the column 'ID1'(1. if len(ID1)>2: then ID1=ID1[-1](for example 'BA', 'AB' will be replaced with 'A', 'B', respectively); 2. if ID1='>': then ID1=ID2(for example: '>' will be replaced with 'mh','nn',respectively)):

df_result = pd.DataFrame({'ID1':['A','B','C','A','A','B','mh','mh','nn','nn']})
df_result

Body:
Let us do mask

df_test.ID1.mask(df_test.ID1.eq('>'),df_test.ID2,inplace=True)
df_test
Out[217]:
  ID1 ID2
0   A
1   B
2   C
3  BA
4  BA
5  AB
6  mh  mh
7  mh  mh
8  nn  nn
9  nn  nn


问:
Entity Framework with NOLOCK
How can I use the NOLOCK function on Entity Framework? Is XML the only way to do this?

答:
Necro-ing this thread because it is the first that comes up in a google search. There have been changes to how EF structures its queries. This regex will also cover joins.

as an aside, transaction level read uncommitted works (examples are above with transactionscope), however it will still block updates. Using the table level nolock will allow for updates to continue. Be very aware and do some research on the effects of using dirty reads before doing this

```<code>(?<tableAlias>((FROM)|(JOIN))\s\[([^\s]+)\]\sAS\s\[([^\s]+)\](?!\sWITH\s\(NOLOCK\)))
</code>```

you can test it with any regex online tester (like regex101.com) with this sample

```<code>FROM [table1] AS [t]
INNER JOIN [table2] AS [t2] ON ([t].[ID] = [t2].[ID])
INNER JOIN [table3] AS [t3] WITH (NOLOCK) ON ([t].[ID] = [t3].[ID])
</code>```

I also cleaned up the example but left out the bool flag for trigger. feel free to add if you want

```<code>public class NoLockInterceptor : DbCommandInterceptor
    {

        public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
        {

            var finishedresult = base.ReaderExecuting(command.NoLockCommand(), eventData, result);
            return finishedresult;
        }

        public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
        {

            var finishedresult = base.ReaderExecutingAsync(command.NoLockCommand(), eventData, result, cancellationToken);
            return finishedresult;
        }

        public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result)
        {
            var finishedresult = base.ScalarExecuting(command.NoLockCommand(), eventData, result);
            return finishedresult;
        }

        public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default)
        {
            var finishedresult = base.ScalarExecutingAsync(command.NoLockCommand(), eventData, result, cancellationToken);
            return finishedresult;
        }

    }

    public static class DbCommandExtensions
    {
        private static Regex _tableAliasRegex = new Regex(@"(?<tableAlias>((FROM)|(JOIN))\s\[([^\s]+)\]\sAS\s\[([^\s]+)\](?!\sWITH\s\(NOLOCK\)))",
                RegexOptions.Multiline | RegexOptions.IgnoreCase);

        public static DbCommand NoLockCommand(this DbCommand command)        {

            string updatedCommandText = _tableAliasRegex.Replace(command.CommandText, "${tableAlias} WITH (NOLOCK)");

            command.CommandText = updatedCommandText;
            return command;
        }
    }
</code>```

Title: Entity Framework with NOLOCK

Body:
Necro-ing this thread because it is the first that comes up in a google search. There have been changes to how EF structures its queries. This regex will also cover joins.

as an aside, transaction level read uncommitted works (examples are above with transactionscope), however it will still block updates. Using the table level nolock will allow for updates to continue. Be very aware and do some research on the effects of using dirty reads before doing this

(?<tableAlias>((FROM)|(JOIN))\s\[([^\s]+)\]\sAS\s\[([^\s]+)\](?!\sWITH\s\(NOLOCK\)))

you can test it with any regex online tester (like regex101.com) with this sample

FROM [table1] AS [t]
INNER JOIN [table2] AS [t2] ON ([t].[ID] = [t2].[ID])
INNER JOIN [table3] AS [t3] WITH (NOLOCK) ON ([t].[ID] = [t3].[ID])

I also cleaned up the example but left out the bool flag for trigger. feel free to add if you want

public class NoLockInterceptor : DbCommandInterceptor
    {

        public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
        {

            var finishedresult = base.ReaderExecuting(command.NoLockCommand(), eventData, result);
            return finishedresult;
        }

        public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
        {

            var finishedresult = base.ReaderExecutingAsync(command.NoLockCommand(), eventData, result, cancellationToken);
            return finishedresult;
        }

        public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result)
        {
            var finishedresult = base.ScalarExecuting(command.NoLockCommand(), eventData, result);
            return finishedresult;
        }

        public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default)
        {
            var finishedresult = base.ScalarExecutingAsync(command.NoLockCommand(), eventData, result, cancellationToken);
            return finishedresult;
        }

    }

    public static class DbCommandExtensions
    {
        private static Regex _tableAliasRegex = new Regex(@"(?<tableAlias>((FROM)|(JOIN))\s\[([^\s]+)\]\sAS\s\[([^\s]+)\](?!\sWITH\s\(NOLOCK\)))",
                RegexOptions.Multiline | RegexOptions.IgnoreCase);

        public static DbCommand NoLockCommand(this DbCommand command)        {

            string updatedCommandText = _tableAliasRegex.Replace(command.CommandText, "${tableAlias} WITH (NOLOCK)");

            command.CommandText = updatedCommandText;
            return command;
        }
    }